cc

package
v0.0.0-...-d5a01e8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2014 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package cc implements parsing, type checking, and printing of C programs.

Index

Constants

This section is empty.

Variables

View Source
var (
	CharType      = newType(Char)
	UcharType     = newType(Uchar)
	ShortType     = newType(Short)
	UshortType    = newType(Ushort)
	IntType       = newType(Int)
	UintType      = newType(Uint)
	LongType      = newType(Long)
	UlongType     = newType(Ulong)
	LonglongType  = newType(Longlong)
	UlonglongType = newType(Ulonglong)
	FloatType     = newType(Float)
	DoubleType    = newType(Double)
	VoidType      = newType(Void)
	BoolType      = &Type{Kind: TypedefType, Name: "bool", Base: IntType}
)

Functions

func AddInclude

func AddInclude(dir string)

func Postorder

func Postorder(x Syntax, f func(Syntax))

Preorder calls f for each piece of syntax of x in a postorder traversal.

func Preorder

func Preorder(x Syntax, f func(Syntax))

Preorder calls f for each piece of syntax of x in a preorder traversal.

func Walk

func Walk(x Syntax, before, after func(Syntax))

Walk traverses the syntax x, calling before and after on entry to and exit from each Syntax encountered during the traversal. In case of cross-linked input, the traversal never visits a given Syntax more than once.

Types

type Comment

type Comment struct {
	Span
	Text   string
	Suffix bool
}

type Comments

type Comments struct {
	Before []Comment // whole-line comments before this syntax
	Suffix []Comment // end-of-line comments after this syntax

	// For top-level syntax elements only, After lists whole-line
	// comments following the syntax.
	After []Comment
}

Comments collects the comments associated with a syntax element.

type Decl

type Decl struct {
	SyntaxInfo
	Name    string
	Type    *Type
	Storage Storage
	Init    *Init
	Body    *Stmt

	XOuter    *Decl
	CurFn     *Decl
	OuterType *Type
	GoPackage string
}

func (*Decl) String

func (d *Decl) String() string

type Expr

type Expr struct {
	SyntaxInfo
	Op    ExprOp   // operator
	Left  *Expr    // left (or only) operand
	Right *Expr    // right operand
	List  []*Expr  // operand list, for Comma, Cond, Call
	Text  string   // name or literal, for Name, Number, Goto, Arrow, Dot
	Texts []string // list of literals, for String
	Type  *Type    // type operand, for SizeofType, Offsetof, Cast, CastInit, VaArg
	Init  *Init    // initializer, for CastInit
	Block []*Stmt  // for c2go

	// derived information
	XDecl *Decl
	XType *Type // expression type, derived
}

An Expr is a parsed C expression.

func ParseExpr

func ParseExpr(str string) (*Expr, error)

func (*Expr) String

func (x *Expr) String() string

type ExprOp

type ExprOp int
const (
	Add        ExprOp // Left + Right
	AddEq             // Left += Right
	Addr              // &Left
	And               // Left & Right
	AndAnd            // Left && Right
	AndEq             // Left &= Right
	Arrow             // Left->Text
	Call              // Left(List)
	Cast              // (Type)Left
	CastInit          // (Type){Init}
	Comma             // x, y, z; List = {x, y, z}
	Cond              // x ? y : z; List = {x, y, z}
	Div               // Left / Right
	DivEq             // Left /= Right
	Dot               // Left.Name
	Eq                // Left = Right
	EqEq              // Left == Right
	Gt                // Left > Right
	GtEq              // Left >= Right
	Index             // Left[Right]
	Indir             // *Left
	Lsh               // Left << Right
	LshEq             // Left <<= Right
	Lt                // Left < Right
	LtEq              // Left <= Right
	Minus             // -Left
	Mod               // Left % Right
	ModEq             // Left %= Right
	Mul               // Left * Right
	MulEq             // Left *= Right
	Name              // Text (function, variable, or enum name)
	Not               // !Left
	NotEq             // Left != Right
	Number            // Text (numeric or chraracter constant)
	Offsetof          // offsetof(Type, Left)
	Or                // Left | Right
	OrEq              // Left |= Right
	OrOr              // Left || Right
	Paren             // (Left)
	Plus              //  +Left
	PostDec           // Left--
	PostInc           // Left++
	PreDec            // --Left
	PreInc            // ++Left
	Rsh               // Left >> Right
	RshEq             // Left >>= Right
	SizeofExpr        // sizeof(Left)
	SizeofType        // sizeof(Type)
	String            // Text (quoted string literal)
	Sub               // Left - Right
	SubEq             // Left -= Right
	Twid              // ~Left
	VaArg             // va_arg(Left, Type)
	Xor               // Left ^ Right
	XorEq             // Left ^= Right
)

func (ExprOp) String

func (op ExprOp) String() string
type Header struct {
	// contains filtered or unexported fields
}

type Init

type Init struct {
	SyntaxInfo
	Prefix []*Prefix // list of prefixes
	Expr   *Expr     // Expr
	Braced []*Init   // {Braced}

	XType *Type // derived type
}

Init is an initializer expression.

type Label

type Label struct {
	SyntaxInfo
	Op   LabelOp
	Expr *Expr
	Name string
}

type LabelOp

type LabelOp int
const (
	Case LabelOp
	Default
	LabelName
)

type Pos

type Pos struct {
	File string
	Line int
	Byte int
}

type Prefix

type Prefix struct {
	Span  Span
	Dot   string // .Dot =
	XDecl *Decl  // for .Dot
	Index *Expr  // [Index] =
}

Prefix is an initializer prefix.

type Printer

type Printer struct {
	// contains filtered or unexported fields
}

func (*Printer) Bytes

func (p *Printer) Bytes() []byte

func (*Printer) EndHTML

func (p *Printer) EndHTML()

func (*Printer) Print

func (p *Printer) Print(args ...interface{})

func (*Printer) StartHTML

func (p *Printer) StartHTML()

func (*Printer) String

func (p *Printer) String() string

type Prog

type Prog struct {
	SyntaxInfo
	Decls []*Decl
}

func Read

func Read(name string, r io.Reader) (*Prog, error)

func ReadMany

func ReadMany(names []string, readers []io.Reader) (*Prog, error)

type Scope

type Scope struct {
	Decl map[string]*Decl
	Tag  map[string]*Type
	Next *Scope
}

type Span

type Span struct {
	Start Pos
	End   Pos
}

func (Span) String

func (l Span) String() string

type Stmt

type Stmt struct {
	SyntaxInfo
	Op     StmtOp
	Pre    *Expr
	Expr   *Expr
	Post   *Expr
	Decl   *Decl
	Body   *Stmt
	Else   *Stmt
	Block  []*Stmt
	Labels []*Label
	Text   string
	Type   *Type
}

type StmtOp

type StmtOp int
const (
	StmtDecl StmtOp
	StmtExpr
	Empty
	Block
	ARGBEGIN
	Break
	Continue
	Do
	For
	If
	Goto
	Return
	Switch
	While
)

type Storage

type Storage int
const (
	Auto Storage = 1 << iota
	Static
	Extern
	Typedef
	Register
	Inline
)

func (Storage) String

func (c Storage) String() string

type Syntax

type Syntax interface {
	// GetSpan returns the start and end position of the syntax,
	// excluding leading or trailing comments.
	// The use of a Get prefix is non-standard but avoids a conflict
	// with the field named Span in most implementations.
	GetSpan() Span

	// GetComments returns the comments attached to the syntax.
	// This method would normally be named 'Comments' but that
	// would interfere with embedding a type of the same name.
	// The use of a Get prefix is non-standard but avoids a conflict
	// with the field named Comments in most implementations.
	GetComments() *Comments
}

A Syntax represents any syntax element.

type SyntaxInfo

type SyntaxInfo struct {
	Span     Span // location of syntax in input
	Comments Comments
}

SyntaxInfo contains metadata about a piece of syntax.

func (*SyntaxInfo) GetComments

func (s *SyntaxInfo) GetComments() *Comments

func (*SyntaxInfo) GetSpan

func (s *SyntaxInfo) GetSpan() Span

type Type

type Type struct {
	SyntaxInfo
	Kind     TypeKind
	Qual     TypeQual
	Base     *Type
	Tag      string
	Decls    []*Decl
	Width    *Expr
	Name     string
	TypeDecl *Decl
}

func (*Type) Def

func (t *Type) Def() *Type

func (*Type) Is

func (t *Type) Is(k TypeKind) bool

func (*Type) IsPtrVoid

func (t *Type) IsPtrVoid() bool

func (*Type) String

func (t *Type) String() string

type TypeKind

type TypeKind int
const (
	Void TypeKind
	Char
	Uchar
	Short
	Ushort
	Int
	Uint
	Long
	Ulong
	Longlong
	Ulonglong
	Float
	Double
	Enum
	Ptr
	Struct
	Union
	Array
	Func
	TypedefType
)

func (TypeKind) String

func (k TypeKind) String() string

type TypeQual

type TypeQual int
const (
	Const TypeQual = 1 << iota
	Volatile
)

func (TypeQual) String

func (q TypeQual) String() string

type TypedName

type TypedName struct {
	Type *Type
	Name string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL