nodes.mx raw

   1  package main
   2  
   3  
   4  type Node interface {
   5  	Pos() Pos
   6  	SetPos(Pos)
   7  	aNode()
   8  }
   9  
  10  type node struct {
  11  	pos Pos
  12  }
  13  
  14  func (n *node) Pos() Pos       { return n.pos }
  15  func (n *node) SetPos(pos Pos) { n.pos = pos }
  16  func (*node) aNode()           {}
  17  
  18  type File struct {
  19  	Pragma    Pragma
  20  	PkgName   *Name
  21  	DeclList  []Decl
  22  	EOF       Pos
  23  	GoVersion string
  24  	node
  25  }
  26  
  27  type (
  28  	Decl interface {
  29  		Node
  30  		aDecl()
  31  	}
  32  
  33  	ImportDecl struct {
  34  		Group        *Group
  35  		Pragma       Pragma
  36  		LocalPkgName *Name
  37  		Path         *BasicLit
  38  		decl
  39  	}
  40  
  41  	ConstDecl struct {
  42  		Group    *Group
  43  		Pragma   Pragma
  44  		NameList []*Name
  45  		Type     Expr
  46  		Values   Expr
  47  		decl
  48  	}
  49  
  50  	TypeDecl struct {
  51  		Group      *Group
  52  		Pragma     Pragma
  53  		Name       *Name
  54  		TParamList []*Field
  55  		Alias      bool
  56  		Type       Expr
  57  		decl
  58  	}
  59  
  60  	VarDecl struct {
  61  		Group    *Group
  62  		Pragma   Pragma
  63  		NameList []*Name
  64  		Type     Expr
  65  		Values   Expr
  66  		decl
  67  	}
  68  
  69  	FuncDecl struct {
  70  		Pragma     Pragma
  71  		Recv       *Field
  72  		Name       *Name
  73  		TParamList []*Field
  74  		Type       *FuncType
  75  		Body       *BlockStmt
  76  		decl
  77  	}
  78  )
  79  
  80  type decl struct{ node }
  81  
  82  func (*decl) aDecl() {}
  83  
  84  type Group struct {
  85  	_ int
  86  }
  87  
  88  func NewName(pos Pos, value string) *Name {
  89  	n := &Name{}
  90  	n.pos = pos
  91  	n.Value = value
  92  	return n
  93  }
  94  
  95  type (
  96  	Expr interface {
  97  		Node
  98  		typeInfo
  99  		aExpr()
 100  	}
 101  
 102  	BadExpr struct {
 103  		expr
 104  	}
 105  
 106  	Name struct {
 107  		Value string
 108  		expr
 109  	}
 110  
 111  	BasicLit struct {
 112  		Value string
 113  		Kind  LitKind
 114  		Bad   bool
 115  		expr
 116  	}
 117  
 118  	CompositeLit struct {
 119  		Type     Expr
 120  		ElemList []Expr
 121  		NKeys    int
 122  		Rbrace   Pos
 123  		expr
 124  	}
 125  
 126  	KeyValueExpr struct {
 127  		Key, Value Expr
 128  		expr
 129  	}
 130  
 131  	FuncLit struct {
 132  		Type *FuncType
 133  		Body *BlockStmt
 134  		expr
 135  	}
 136  
 137  	ParenExpr struct {
 138  		X Expr
 139  		expr
 140  	}
 141  
 142  	SelectorExpr struct {
 143  		X   Expr
 144  		Sel *Name
 145  		expr
 146  	}
 147  
 148  	IndexExpr struct {
 149  		X     Expr
 150  		Index Expr
 151  		expr
 152  	}
 153  
 154  	SliceExpr struct {
 155  		X     Expr
 156  		Index [3]Expr
 157  		Full  bool
 158  		expr
 159  	}
 160  
 161  	AssertExpr struct {
 162  		X    Expr
 163  		Type Expr
 164  		expr
 165  	}
 166  
 167  	TypeSwitchGuard struct {
 168  		Lhs *Name
 169  		X   Expr
 170  		expr
 171  	}
 172  
 173  	Operation struct {
 174  		Op   Operator
 175  		X, Y Expr
 176  		expr
 177  	}
 178  
 179  	CallExpr struct {
 180  		Fun     Expr
 181  		ArgList []Expr
 182  		HasDots bool
 183  		expr
 184  	}
 185  
 186  	ListExpr struct {
 187  		ElemList []Expr
 188  		expr
 189  	}
 190  
 191  	ArrayType struct {
 192  		Len  Expr
 193  		Elem Expr
 194  		expr
 195  	}
 196  
 197  	SliceType struct {
 198  		Elem Expr
 199  		expr
 200  	}
 201  
 202  	DotsType struct {
 203  		Elem Expr
 204  		expr
 205  	}
 206  
 207  	StructType struct {
 208  		FieldList []*Field
 209  		TagList   []*BasicLit
 210  		expr
 211  	}
 212  
 213  	Field struct {
 214  		Name *Name
 215  		Type Expr
 216  		node
 217  	}
 218  
 219  	InterfaceType struct {
 220  		MethodList []*Field
 221  		expr
 222  	}
 223  
 224  	FuncType struct {
 225  		ParamList  []*Field
 226  		ResultList []*Field
 227  		expr
 228  	}
 229  
 230  	MapType struct {
 231  		Key, Value Expr
 232  		expr
 233  	}
 234  
 235  	ChanType struct {
 236  		Dir  ChanDir
 237  		Elem Expr
 238  		expr
 239  	}
 240  )
 241  
 242  type Type interface {
 243  	Underlying() Type
 244  	String() string
 245  }
 246  
 247  type typeInfo interface {
 248  	SetTypeInfo(TypeAndValue)
 249  	GetTypeInfo() TypeAndValue
 250  }
 251  
 252  type TypeAndValue struct {
 253  	Type  Type
 254  	Value ConstVal
 255  	exprFlags
 256  }
 257  
 258  type exprFlags uint16
 259  
 260  func (f exprFlags) IsVoid() bool          { return f&1 != 0 }
 261  func (f exprFlags) IsType() bool          { return f&2 != 0 }
 262  func (f exprFlags) IsBuiltin() bool       { return f&4 != 0 }
 263  func (f exprFlags) IsValue() bool         { return f&8 != 0 }
 264  func (f exprFlags) IsNil() bool           { return f&16 != 0 }
 265  func (f exprFlags) Addressable() bool     { return f&32 != 0 }
 266  func (f exprFlags) Assignable() bool      { return f&64 != 0 }
 267  func (f exprFlags) HasOk() bool           { return f&128 != 0 }
 268  func (f exprFlags) IsRuntimeHelper() bool { return f&256 != 0 }
 269  
 270  func (f *exprFlags) SetIsVoid()          { *f |= 1 }
 271  func (f *exprFlags) SetIsType()          { *f |= 2 }
 272  func (f *exprFlags) SetIsBuiltin()       { *f |= 4 }
 273  func (f *exprFlags) SetIsValue()         { *f |= 8 }
 274  func (f *exprFlags) SetIsNil()           { *f |= 16 }
 275  func (f *exprFlags) SetAddressable()     { *f |= 32 }
 276  func (f *exprFlags) SetAssignable()      { *f |= 64 }
 277  func (f *exprFlags) SetHasOk()           { *f |= 128 }
 278  func (f *exprFlags) SetIsRuntimeHelper() { *f |= 256 }
 279  
 280  type typeAndValue struct {
 281  	tv TypeAndValue
 282  }
 283  
 284  func (x *typeAndValue) SetTypeInfo(tv TypeAndValue) {
 285  	x.tv = tv
 286  }
 287  func (x *typeAndValue) GetTypeInfo() TypeAndValue {
 288  	return x.tv
 289  }
 290  
 291  type expr struct {
 292  	node
 293  	typeAndValue
 294  }
 295  
 296  func (*expr) aExpr() {}
 297  
 298  type ChanDir uint
 299  
 300  const (
 301  	_ ChanDir = iota
 302  	SendOnly
 303  	RecvOnly
 304  )
 305  
 306  type (
 307  	Stmt interface {
 308  		Node
 309  		aStmt()
 310  	}
 311  
 312  	SimpleStmt interface {
 313  		Stmt
 314  		aSimpleStmt()
 315  	}
 316  
 317  	EmptyStmt struct {
 318  		simpleStmt
 319  	}
 320  
 321  	LabeledStmt struct {
 322  		Label *Name
 323  		Stmt  Stmt
 324  		stmt
 325  	}
 326  
 327  	BlockStmt struct {
 328  		List   []Stmt
 329  		Rbrace Pos
 330  		stmt
 331  	}
 332  
 333  	ExprStmt struct {
 334  		X Expr
 335  		simpleStmt
 336  	}
 337  
 338  	SendStmt struct {
 339  		Chan, Value Expr
 340  		simpleStmt
 341  	}
 342  
 343  	DeclStmt struct {
 344  		DeclList []Decl
 345  		stmt
 346  	}
 347  
 348  	AssignStmt struct {
 349  		Op       Operator
 350  		Lhs, Rhs Expr
 351  		simpleStmt
 352  	}
 353  
 354  	BranchStmt struct {
 355  		Tok    Token
 356  		Label  *Name
 357  		Target Stmt
 358  		stmt
 359  	}
 360  
 361  	CallStmt struct {
 362  		Tok     Token
 363  		Call    Expr
 364  		DeferAt Expr
 365  		stmt
 366  	}
 367  
 368  	ReturnStmt struct {
 369  		Results Expr
 370  		stmt
 371  	}
 372  
 373  	IfStmt struct {
 374  		Init SimpleStmt
 375  		Cond Expr
 376  		Then *BlockStmt
 377  		Else Stmt
 378  		stmt
 379  	}
 380  
 381  	ForStmt struct {
 382  		Init SimpleStmt
 383  		Cond Expr
 384  		Post SimpleStmt
 385  		Body *BlockStmt
 386  		stmt
 387  	}
 388  
 389  	SwitchStmt struct {
 390  		Init   SimpleStmt
 391  		Tag    Expr
 392  		Body   []*CaseClause
 393  		Rbrace Pos
 394  		stmt
 395  	}
 396  
 397  	SelectStmt struct {
 398  		Body   []*CommClause
 399  		Rbrace Pos
 400  		stmt
 401  	}
 402  )
 403  
 404  type (
 405  	RangeClause struct {
 406  		Lhs Expr
 407  		Def bool
 408  		X   Expr
 409  		simpleStmt
 410  	}
 411  
 412  	CaseClause struct {
 413  		Cases Expr
 414  		Body  []Stmt
 415  		Colon Pos
 416  		node
 417  	}
 418  
 419  	CommClause struct {
 420  		Comm  SimpleStmt
 421  		Body  []Stmt
 422  		Colon Pos
 423  		node
 424  	}
 425  )
 426  
 427  type stmt struct{ node }
 428  
 429  func (stmt) aStmt() {}
 430  
 431  type simpleStmt struct {
 432  	stmt
 433  }
 434  
 435  func (simpleStmt) aSimpleStmt() {}
 436  
 437  type CommentKind uint
 438  
 439  const (
 440  	Above CommentKind = iota
 441  	Below
 442  	Left
 443  	Right
 444  )
 445  
 446  type Comment struct {
 447  	Kind CommentKind
 448  	Text string
 449  	Next *Comment
 450  }
 451