2021-01-18 13:20:06 +01:00
|
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2019-12-22 02:34:37 +01:00
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
module ast
|
|
|
|
|
|
2020-04-19 00:07:57 +02:00
|
|
|
|
import v.token
|
2020-05-10 11:26:57 +02:00
|
|
|
|
import v.errors
|
2021-03-17 01:43:17 +01:00
|
|
|
|
import v.pref
|
2019-12-22 02:34:37 +01:00
|
|
|
|
|
2020-11-25 12:09:40 +01:00
|
|
|
|
pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl
|
2020-03-07 17:37:55 +01:00
|
|
|
|
|
2020-12-29 16:14:08 +01:00
|
|
|
|
pub type Expr = AnonFn | ArrayDecompose | ArrayInit | AsCast | Assoc | AtExpr | BoolLiteral |
|
2021-01-18 06:02:29 +01:00
|
|
|
|
CTempVar | CallExpr | CastExpr | ChanInit | CharLiteral | Comment | ComptimeCall |
|
2021-03-31 10:13:15 +02:00
|
|
|
|
ComptimeSelector | ConcatExpr | DumpExpr | EmptyExpr | EnumVal | FloatLiteral | GoExpr |
|
|
|
|
|
Ident | IfExpr | IfGuardExpr | IndexExpr | InfixExpr | IntegerLiteral | Likely | LockExpr |
|
2021-03-30 09:33:29 +02:00
|
|
|
|
MapInit | MatchExpr | NodeError | None | OffsetOf | OrExpr | ParExpr | PostfixExpr |
|
|
|
|
|
PrefixExpr | RangeExpr | SelectExpr | SelectorExpr | SizeOf | SqlExpr | StringInterLiteral |
|
2021-04-02 00:57:09 +02:00
|
|
|
|
StringLiteral | StructInit | TypeNode | TypeOf | UnsafeExpr
|
2020-05-18 18:08:28 +02:00
|
|
|
|
|
2021-03-17 01:43:17 +01:00
|
|
|
|
pub type Stmt = AsmStmt | AssertStmt | AssignStmt | Block | BranchStmt | CompFor | ConstDecl |
|
2021-03-31 10:13:15 +02:00
|
|
|
|
DeferStmt | EmptyStmt | EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt | ForStmt |
|
|
|
|
|
GlobalDecl | GoStmt | GotoLabel | GotoStmt | HashStmt | Import | InterfaceDecl | Module |
|
|
|
|
|
NodeError | Return | SqlStmt | StructDecl | TypeDecl
|
2020-04-04 05:14:40 +02:00
|
|
|
|
|
2020-11-04 12:34:12 +01:00
|
|
|
|
// NB: when you add a new Expr or Stmt type with a .pos field, remember to update
|
|
|
|
|
// the .position() token.Position methods too.
|
2021-03-17 01:43:17 +01:00
|
|
|
|
pub type ScopeObject = AsmRegister | ConstField | GlobalField | Var
|
2020-04-04 05:14:40 +02:00
|
|
|
|
|
2021-04-02 00:57:09 +02:00
|
|
|
|
// TODO: replace Param
|
|
|
|
|
pub type Node = CallArg | ConstField | EmptyNode | EnumField | Expr | File | GlobalField |
|
|
|
|
|
IfBranch | MatchBranch | NodeError | Param | ScopeObject | SelectBranch | Stmt | StructField |
|
|
|
|
|
StructInitField
|
2021-01-09 05:36:38 +01:00
|
|
|
|
|
2021-04-02 00:57:09 +02:00
|
|
|
|
pub struct TypeNode {
|
2020-03-02 10:53:38 +01:00
|
|
|
|
pub:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-04-18 00:19:33 +02:00
|
|
|
|
pos token.Position
|
2020-03-02 10:53:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-31 11:28:42 +02:00
|
|
|
|
pub struct EmptyExpr {
|
|
|
|
|
x int
|
|
|
|
|
}
|
2021-03-31 10:13:15 +02:00
|
|
|
|
|
|
|
|
|
pub fn empty_expr() Expr {
|
|
|
|
|
return EmptyExpr{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct EmptyStmt {
|
|
|
|
|
pub:
|
|
|
|
|
pos token.Position
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn empty_stmt() Stmt {
|
|
|
|
|
return EmptyStmt{}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 00:57:09 +02:00
|
|
|
|
pub struct EmptyNode {
|
|
|
|
|
x int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn empty_node() Node {
|
|
|
|
|
return EmptyNode{}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-02 13:06:44 +02:00
|
|
|
|
// `{stmts}` or `unsafe {stmts}`
|
2020-03-24 15:44:17 +01:00
|
|
|
|
pub struct Block {
|
|
|
|
|
pub:
|
2020-08-04 20:10:22 +02:00
|
|
|
|
stmts []Stmt
|
2020-08-02 13:06:44 +02:00
|
|
|
|
is_unsafe bool
|
2020-09-30 16:06:22 +02:00
|
|
|
|
pos token.Position
|
2020-03-24 15:44:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-06 16:13:12 +01:00
|
|
|
|
// | IncDecStmt k
|
2019-12-28 14:11:05 +01:00
|
|
|
|
// Stand-alone expression in a statement list.
|
|
|
|
|
pub struct ExprStmt {
|
|
|
|
|
pub:
|
2020-07-17 19:13:22 +02:00
|
|
|
|
expr Expr
|
|
|
|
|
pos token.Position
|
|
|
|
|
comments []Comment
|
|
|
|
|
is_expr bool
|
2020-05-21 22:35:43 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2019-12-28 14:11:05 +01:00
|
|
|
|
}
|
2019-12-22 02:34:37 +01:00
|
|
|
|
|
2019-12-26 11:21:41 +01:00
|
|
|
|
pub struct IntegerLiteral {
|
2019-12-26 03:40:18 +01:00
|
|
|
|
pub:
|
2020-03-17 02:49:15 +01:00
|
|
|
|
val string
|
2020-04-10 00:09:34 +02:00
|
|
|
|
pos token.Position
|
2019-12-26 03:40:18 +01:00
|
|
|
|
}
|
2019-12-24 18:54:43 +01:00
|
|
|
|
|
2019-12-27 10:03:29 +01:00
|
|
|
|
pub struct FloatLiteral {
|
|
|
|
|
pub:
|
|
|
|
|
val string
|
2020-04-20 14:49:26 +02:00
|
|
|
|
pos token.Position
|
2019-12-27 10:03:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 18:54:43 +01:00
|
|
|
|
pub struct StringLiteral {
|
|
|
|
|
pub:
|
2020-05-19 17:12:47 +02:00
|
|
|
|
val string
|
|
|
|
|
is_raw bool
|
2021-04-02 00:57:09 +02:00
|
|
|
|
language Language
|
2020-05-19 17:12:47 +02:00
|
|
|
|
pos token.Position
|
2019-12-24 18:54:43 +01:00
|
|
|
|
}
|
2019-12-22 02:34:37 +01:00
|
|
|
|
|
2020-03-21 07:01:06 +01:00
|
|
|
|
// 'name: $name'
|
|
|
|
|
pub struct StringInterLiteral {
|
|
|
|
|
pub:
|
|
|
|
|
vals []string
|
|
|
|
|
exprs []Expr
|
2020-06-16 10:41:51 +02:00
|
|
|
|
fwidths []int
|
|
|
|
|
precisions []int
|
|
|
|
|
pluss []bool
|
|
|
|
|
fills []bool
|
|
|
|
|
fmt_poss []token.Position
|
2020-04-10 10:59:07 +02:00
|
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
expr_types []Type
|
2020-06-16 10:41:51 +02:00
|
|
|
|
fmts []byte
|
|
|
|
|
need_fmts []bool // an explicit non-default fmt required, e.g. `x`
|
2020-03-21 07:01:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 09:54:15 +01:00
|
|
|
|
pub struct CharLiteral {
|
|
|
|
|
pub:
|
|
|
|
|
val string
|
2020-04-20 14:49:26 +02:00
|
|
|
|
pos token.Position
|
2020-02-04 09:54:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-29 08:51:55 +01:00
|
|
|
|
pub struct BoolLiteral {
|
|
|
|
|
pub:
|
|
|
|
|
val bool
|
2020-04-20 14:49:26 +02:00
|
|
|
|
pos token.Position
|
2019-12-29 08:51:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-02 20:09:15 +01:00
|
|
|
|
// `foo.bar`
|
|
|
|
|
pub struct SelectorExpr {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
pos token.Position
|
|
|
|
|
field_name string
|
|
|
|
|
is_mut bool // is used for the case `if mut ident.selector is MyType {`, it indicates if the root ident is mutable
|
|
|
|
|
mut_pos token.Position
|
|
|
|
|
next_token token.Kind
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
expr Expr // expr.field_name
|
|
|
|
|
expr_type Type // type of `Foo` in `Foo.bar`
|
|
|
|
|
typ Type // type of the entire thing (`Foo.bar`)
|
|
|
|
|
name_type Type // T in `T.name` or typeof in `typeof(expr).name`
|
2020-12-23 19:12:49 +01:00
|
|
|
|
scope &Scope
|
2021-04-02 00:57:09 +02:00
|
|
|
|
from_embed_type Type // holds the type of the embed that the method is called from
|
2020-01-02 20:09:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 00:59:39 +01:00
|
|
|
|
// root_ident returns the origin ident where the selector started.
|
|
|
|
|
pub fn (e &SelectorExpr) root_ident() Ident {
|
|
|
|
|
mut root := e.expr
|
|
|
|
|
for root is SelectorExpr {
|
2021-01-07 21:35:32 +01:00
|
|
|
|
// TODO: remove this line
|
2020-12-10 00:59:39 +01:00
|
|
|
|
selector_expr := root as SelectorExpr
|
|
|
|
|
root = selector_expr.expr
|
|
|
|
|
}
|
|
|
|
|
return root as Ident
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-28 11:02:06 +01:00
|
|
|
|
// module declaration
|
2019-12-28 09:15:32 +01:00
|
|
|
|
pub struct Module {
|
|
|
|
|
pub:
|
2021-01-20 06:04:59 +01:00
|
|
|
|
name string // encoding.base64
|
|
|
|
|
short_name string // base64
|
2021-04-02 00:57:09 +02:00
|
|
|
|
attrs []Attr
|
2020-05-16 16:12:23 +02:00
|
|
|
|
pos token.Position
|
2021-01-09 05:36:38 +01:00
|
|
|
|
name_pos token.Position // `name` in import name
|
2020-04-10 22:32:52 +02:00
|
|
|
|
is_skipped bool // module main can be skipped in single file programs
|
2019-12-28 09:15:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 02:08:10 +02:00
|
|
|
|
pub struct StructField {
|
|
|
|
|
pub:
|
2020-04-12 12:35:54 +02:00
|
|
|
|
pos token.Position
|
2020-09-11 14:37:14 +02:00
|
|
|
|
type_pos token.Position
|
2020-06-23 18:01:56 +02:00
|
|
|
|
comments []Comment
|
2020-04-12 12:35:54 +02:00
|
|
|
|
default_expr Expr
|
|
|
|
|
has_default_expr bool
|
2021-04-02 00:57:09 +02:00
|
|
|
|
attrs []Attr
|
|
|
|
|
is_pub bool
|
|
|
|
|
default_val string
|
|
|
|
|
is_mut bool
|
|
|
|
|
is_global bool
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
default_expr_typ Type
|
|
|
|
|
name string
|
|
|
|
|
typ Type
|
2020-04-05 02:08:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 00:57:09 +02:00
|
|
|
|
/*
|
2019-12-30 06:16:59 +01:00
|
|
|
|
pub struct Field {
|
|
|
|
|
pub:
|
|
|
|
|
name string
|
2020-04-09 15:33:46 +02:00
|
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-02-03 07:02:54 +01:00
|
|
|
|
}
|
2021-04-02 00:57:09 +02:00
|
|
|
|
*/
|
2020-02-03 07:02:54 +01:00
|
|
|
|
|
2020-12-28 12:52:04 +01:00
|
|
|
|
// const field in const declaration group
|
2020-04-04 05:14:40 +02:00
|
|
|
|
pub struct ConstField {
|
2020-02-03 07:02:54 +01:00
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
mod string
|
|
|
|
|
name string
|
|
|
|
|
expr Expr // the value expr of field; everything after `=`
|
|
|
|
|
is_pub bool
|
|
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type // the type of the const field, it can be any type in V
|
|
|
|
|
comments []Comment // comments before current const field
|
2020-04-04 05:14:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:52:04 +01:00
|
|
|
|
// const declaration
|
2020-04-04 05:14:40 +02:00
|
|
|
|
pub struct ConstDecl {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
is_pub bool
|
|
|
|
|
pos token.Position
|
2020-04-27 22:53:26 +02:00
|
|
|
|
pub mut:
|
2020-12-28 12:52:04 +01:00
|
|
|
|
fields []ConstField // all the const fields in the `const (...)` block
|
2021-01-03 21:10:25 +01:00
|
|
|
|
end_comments []Comment // comments that after last const field
|
2020-12-30 02:15:44 +01:00
|
|
|
|
is_block bool // const() block
|
2019-12-30 06:16:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct StructDecl {
|
|
|
|
|
pub:
|
2021-01-17 05:39:44 +01:00
|
|
|
|
pos token.Position
|
|
|
|
|
name string
|
2021-04-02 00:57:09 +02:00
|
|
|
|
gen_types []Type
|
2021-01-17 05:39:44 +01:00
|
|
|
|
is_pub bool
|
|
|
|
|
// _pos fields for vfmt
|
2020-06-23 18:01:56 +02:00
|
|
|
|
mut_pos int // mut:
|
|
|
|
|
pub_pos int // pub:
|
|
|
|
|
pub_mut_pos int // pub mut:
|
2021-02-05 16:46:43 +01:00
|
|
|
|
global_pos int // __global:
|
2021-01-17 05:39:44 +01:00
|
|
|
|
module_pos int // module:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
language Language
|
2020-06-23 18:01:56 +02:00
|
|
|
|
is_union bool
|
2021-04-02 00:57:09 +02:00
|
|
|
|
attrs []Attr
|
2020-06-23 18:01:56 +02:00
|
|
|
|
end_comments []Comment
|
2020-12-23 19:12:49 +01:00
|
|
|
|
embeds []Embed
|
2020-10-30 07:09:26 +01:00
|
|
|
|
pub mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
fields []StructField
|
2020-10-30 07:09:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 19:12:49 +01:00
|
|
|
|
pub struct Embed {
|
|
|
|
|
pub:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-12-23 19:12:49 +01:00
|
|
|
|
pos token.Position
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 07:09:26 +01:00
|
|
|
|
pub struct StructEmbedding {
|
|
|
|
|
pub:
|
|
|
|
|
name string
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-10-30 07:09:26 +01:00
|
|
|
|
pos token.Position
|
2019-12-30 06:16:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-31 19:59:38 +02:00
|
|
|
|
pub struct InterfaceDecl {
|
2020-04-05 12:31:39 +02:00
|
|
|
|
pub:
|
2020-10-15 22:12:59 +02:00
|
|
|
|
name string
|
|
|
|
|
field_names []string
|
|
|
|
|
is_pub bool
|
|
|
|
|
methods []FnDecl
|
2021-03-04 14:30:30 +01:00
|
|
|
|
mut_pos int // mut:
|
2021-01-23 07:57:17 +01:00
|
|
|
|
fields []StructField
|
2020-10-15 22:12:59 +02:00
|
|
|
|
pos token.Position
|
2020-10-14 21:48:49 +02:00
|
|
|
|
pre_comments []Comment
|
2020-03-31 19:59:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 02:38:39 +02:00
|
|
|
|
pub struct StructInitField {
|
|
|
|
|
pub:
|
|
|
|
|
expr Expr
|
2020-04-19 00:07:57 +02:00
|
|
|
|
pos token.Position
|
2021-03-30 09:43:17 +02:00
|
|
|
|
name_pos token.Position
|
2020-10-14 21:48:49 +02:00
|
|
|
|
comments []Comment
|
2020-12-09 10:11:22 +01:00
|
|
|
|
next_comments []Comment
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2020-04-27 22:53:26 +02:00
|
|
|
|
name string
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
|
|
|
|
expected_type Type
|
2021-04-02 16:26:37 +02:00
|
|
|
|
parent_type Type
|
2020-04-17 02:38:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 19:12:49 +01:00
|
|
|
|
pub struct StructInitEmbed {
|
|
|
|
|
pub:
|
|
|
|
|
expr Expr
|
|
|
|
|
pos token.Position
|
|
|
|
|
comments []Comment
|
|
|
|
|
next_comments []Comment
|
|
|
|
|
pub mut:
|
|
|
|
|
name string
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
|
|
|
|
expected_type Type
|
2020-12-23 19:12:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-30 06:16:59 +01:00
|
|
|
|
pub struct StructInit {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
pos token.Position
|
2021-03-30 09:43:17 +02:00
|
|
|
|
name_pos token.Position
|
2021-01-12 04:38:43 +01:00
|
|
|
|
is_short bool
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-02-02 14:42:00 +01:00
|
|
|
|
unresolved bool
|
2021-01-04 20:01:35 +01:00
|
|
|
|
pre_comments []Comment
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2021-01-04 20:01:35 +01:00
|
|
|
|
update_expr Expr
|
2021-04-02 00:57:09 +02:00
|
|
|
|
update_expr_type Type
|
2021-01-04 20:01:35 +01:00
|
|
|
|
update_expr_comments []Comment
|
|
|
|
|
has_update_expr bool
|
|
|
|
|
fields []StructInitField
|
|
|
|
|
embeds []StructInitEmbed
|
2019-12-30 06:16:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-28 09:15:32 +01:00
|
|
|
|
// import statement
|
|
|
|
|
pub struct Import {
|
|
|
|
|
pub:
|
2021-01-09 05:36:38 +01:00
|
|
|
|
mod string // the module name of the import
|
|
|
|
|
alias string // the `x` in `import xxx as x`
|
|
|
|
|
pos token.Position
|
|
|
|
|
mod_pos token.Position
|
|
|
|
|
alias_pos token.Position
|
2021-03-31 12:17:06 +02:00
|
|
|
|
syms_pos token.Position
|
2020-07-18 21:34:38 +02:00
|
|
|
|
pub mut:
|
2021-02-08 18:48:48 +01:00
|
|
|
|
syms []ImportSymbol // the list of symbols in `import {symbol1, symbol2}`
|
|
|
|
|
comments []Comment
|
|
|
|
|
next_comments []Comment
|
2020-07-18 21:34:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:52:04 +01:00
|
|
|
|
// import symbol,for import {symbol} syntax
|
2020-07-18 21:34:38 +02:00
|
|
|
|
pub struct ImportSymbol {
|
|
|
|
|
pub:
|
2020-08-04 20:10:22 +02:00
|
|
|
|
pos token.Position
|
|
|
|
|
name string
|
2019-12-28 09:15:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:52:04 +01:00
|
|
|
|
// anonymous function
|
2020-04-17 21:59:19 +02:00
|
|
|
|
pub struct AnonFn {
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-02-25 12:16:35 +01:00
|
|
|
|
decl FnDecl
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type // the type of anonymous fn. Both .typ and .decl.name are auto generated
|
|
|
|
|
has_gen bool // has been generated
|
2020-04-17 21:59:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:52:04 +01:00
|
|
|
|
// function or method declaration
|
2019-12-27 13:57:49 +01:00
|
|
|
|
pub struct FnDecl {
|
|
|
|
|
pub:
|
2020-12-10 10:51:22 +01:00
|
|
|
|
name string
|
|
|
|
|
mod string
|
2021-04-02 00:57:09 +02:00
|
|
|
|
params []Param
|
2020-12-10 10:51:22 +01:00
|
|
|
|
is_deprecated bool
|
|
|
|
|
is_pub bool
|
|
|
|
|
is_variadic bool
|
|
|
|
|
is_anon bool
|
2021-04-02 00:57:09 +02:00
|
|
|
|
is_manualfree bool // true, when [manualfree] is used on a fn
|
|
|
|
|
is_main bool // true for `fn main()`
|
|
|
|
|
is_test bool // true for `fn test_abcde`
|
|
|
|
|
is_conditional bool // true for `[if abc] fn abc(){}`
|
|
|
|
|
receiver StructField // TODO this is not a struct field
|
2020-12-31 12:41:30 +01:00
|
|
|
|
receiver_pos token.Position // `(u User)` in `fn (u User) name()` position
|
2020-12-10 10:51:22 +01:00
|
|
|
|
is_method bool
|
2020-12-31 12:41:30 +01:00
|
|
|
|
method_type_pos token.Position // `User` in ` fn (u User)` position
|
2020-12-10 10:51:22 +01:00
|
|
|
|
method_idx int
|
|
|
|
|
rec_mut bool // is receiver mutable
|
2021-04-02 00:57:09 +02:00
|
|
|
|
rec_share ShareType
|
|
|
|
|
language Language
|
2020-12-10 10:51:22 +01:00
|
|
|
|
no_body bool // just a definition `fn C.malloc()`
|
|
|
|
|
is_builtin bool // this function is defined in builtin/strconv
|
2020-12-31 12:41:30 +01:00
|
|
|
|
pos token.Position // function declaration position
|
|
|
|
|
body_pos token.Position // function bodys position
|
2020-12-10 10:51:22 +01:00
|
|
|
|
file string
|
2021-01-22 13:49:56 +01:00
|
|
|
|
generic_params []GenericParam
|
2020-12-10 10:51:22 +01:00
|
|
|
|
is_direct_arr bool // direct array access
|
2021-04-02 00:57:09 +02:00
|
|
|
|
attrs []Attr
|
2021-02-05 08:05:13 +01:00
|
|
|
|
skip_gen bool // this function doesn't need to be generated (for example [if foo])
|
2020-04-27 22:53:26 +02:00
|
|
|
|
pub mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
stmts []Stmt
|
2021-02-06 17:40:39 +01:00
|
|
|
|
defer_stmts []DeferStmt
|
2021-04-02 00:57:09 +02:00
|
|
|
|
return_type Type
|
2021-02-03 10:40:21 +01:00
|
|
|
|
has_return bool
|
2021-01-12 04:38:43 +01:00
|
|
|
|
comments []Comment // comments *after* the header, but *before* `{`; used for InterfaceDecl
|
|
|
|
|
next_comments []Comment // coments that are one line after the decl; used for InterfaceDecl
|
|
|
|
|
source_file &File = 0
|
|
|
|
|
scope &Scope
|
2021-02-03 15:20:10 +01:00
|
|
|
|
label_names []string
|
2019-12-29 07:24:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 13:49:56 +01:00
|
|
|
|
pub struct GenericParam {
|
|
|
|
|
pub:
|
|
|
|
|
name string
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 01:09:00 +02:00
|
|
|
|
// break, continue
|
2020-02-04 08:29:50 +01:00
|
|
|
|
pub struct BranchStmt {
|
|
|
|
|
pub:
|
2020-12-06 08:02:36 +01:00
|
|
|
|
kind token.Kind
|
|
|
|
|
label string
|
|
|
|
|
pos token.Position
|
2020-02-04 08:29:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:52:04 +01:00
|
|
|
|
// function or method call expr
|
2019-12-29 07:24:17 +01:00
|
|
|
|
pub struct CallExpr {
|
|
|
|
|
pub:
|
2021-03-30 09:43:17 +02:00
|
|
|
|
pos token.Position
|
|
|
|
|
name_pos token.Position
|
|
|
|
|
mod string
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2020-11-14 12:59:03 +01:00
|
|
|
|
name string // left.name()
|
2020-04-27 22:53:26 +02:00
|
|
|
|
is_method bool
|
2020-07-13 20:04:16 +02:00
|
|
|
|
is_field bool // temp hack, remove ASAP when re-impl CallExpr / Selector (joe)
|
2020-04-09 15:33:46 +02:00
|
|
|
|
args []CallArg
|
2021-04-02 00:57:09 +02:00
|
|
|
|
expected_arg_types []Type
|
|
|
|
|
language Language
|
2020-04-09 15:33:46 +02:00
|
|
|
|
or_block OrExpr
|
2021-04-02 00:57:09 +02:00
|
|
|
|
left Expr // `user` in `user.register()`
|
|
|
|
|
left_type Type // type of `user`
|
|
|
|
|
receiver_type Type // User
|
|
|
|
|
return_type Type
|
2020-04-25 15:57:11 +02:00
|
|
|
|
should_be_skipped bool
|
2021-04-02 00:57:09 +02:00
|
|
|
|
generic_types []Type
|
2020-11-15 10:13:35 +01:00
|
|
|
|
generic_list_pos token.Position
|
2020-10-22 03:51:25 +02:00
|
|
|
|
free_receiver bool // true if the receiver expression needs to be freed
|
2020-12-12 09:01:12 +01:00
|
|
|
|
scope &Scope
|
2021-04-02 00:57:09 +02:00
|
|
|
|
from_embed_type Type // holds the type of the embed that the method is called from
|
2021-01-09 22:47:33 +01:00
|
|
|
|
comments []Comment
|
2020-03-14 11:11:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-05 12:00:35 +02:00
|
|
|
|
/*
|
|
|
|
|
pub struct AutofreeArgVar {
|
|
|
|
|
name string
|
|
|
|
|
idx int
|
|
|
|
|
}
|
|
|
|
|
*/
|
2020-12-28 12:52:04 +01:00
|
|
|
|
// function call argument: `f(callarg)`
|
2020-03-14 11:11:43 +01:00
|
|
|
|
pub struct CallArg {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
is_mut bool
|
2021-04-02 00:57:09 +02:00
|
|
|
|
share ShareType
|
2021-01-12 04:38:43 +01:00
|
|
|
|
expr Expr
|
|
|
|
|
comments []Comment
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-11-09 14:24:46 +01:00
|
|
|
|
is_tmp_autofree bool // this tells cgen that a tmp variable has to be used for the arg expression in order to free it after the call
|
2020-10-30 19:26:08 +01:00
|
|
|
|
pos token.Position
|
2020-09-05 12:00:35 +02:00
|
|
|
|
// tmp_name string // for autofree
|
2019-12-27 13:57:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:52:04 +01:00
|
|
|
|
// function return statement
|
2019-12-27 13:57:49 +01:00
|
|
|
|
pub struct Return {
|
|
|
|
|
pub:
|
2020-07-17 19:13:22 +02:00
|
|
|
|
pos token.Position
|
|
|
|
|
exprs []Expr
|
|
|
|
|
comments []Comment
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
types []Type
|
2019-12-27 13:57:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 02:34:37 +01:00
|
|
|
|
/*
|
|
|
|
|
pub enum Expr {
|
2020-01-22 21:34:38 +01:00
|
|
|
|
Binary(InfixExpr)
|
2019-12-22 02:34:37 +01:00
|
|
|
|
If(IfExpr)
|
|
|
|
|
Integer(IntegerExpr)
|
|
|
|
|
}
|
|
|
|
|
*/
|
2019-12-24 18:54:43 +01:00
|
|
|
|
/*
|
2019-12-22 02:34:37 +01:00
|
|
|
|
pub struct Stmt {
|
|
|
|
|
pos int
|
|
|
|
|
//end int
|
|
|
|
|
}
|
2019-12-24 18:54:43 +01:00
|
|
|
|
*/
|
2020-03-10 12:01:37 +01:00
|
|
|
|
pub struct Var {
|
2019-12-24 18:54:43 +01:00
|
|
|
|
pub:
|
2020-10-29 01:09:38 +01:00
|
|
|
|
name string
|
|
|
|
|
expr Expr
|
2021-04-02 00:57:09 +02:00
|
|
|
|
share ShareType
|
2020-10-29 01:09:38 +01:00
|
|
|
|
is_mut bool
|
|
|
|
|
is_autofree_tmp bool
|
|
|
|
|
is_arg bool // fn args should not be autofreed
|
2021-02-12 01:03:11 +01:00
|
|
|
|
is_auto_deref bool
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
|
|
|
|
orig_type Type // original sumtype type; 0 if it's not a sumtype
|
|
|
|
|
sum_type_casts []Type // nested sum types require nested smart casting, for that a list of types is needed
|
2021-01-21 21:31:25 +01:00
|
|
|
|
// TODO: move this to a real docs site later
|
|
|
|
|
// 10 <- original type (orig_type)
|
|
|
|
|
// [11, 12, 13] <- cast order (sum_type_casts)
|
|
|
|
|
// 12 <- the current casted type (typ)
|
|
|
|
|
pos token.Position
|
|
|
|
|
is_used bool
|
|
|
|
|
is_changed bool // to detect mutable vars that are never changed
|
2020-12-06 08:38:21 +01:00
|
|
|
|
//
|
2020-12-05 20:52:50 +01:00
|
|
|
|
// (for setting the position after the or block for autofree)
|
2021-01-12 04:38:43 +01:00
|
|
|
|
is_or bool // `x := foo() or { ... }`
|
|
|
|
|
is_tmp bool // for tmp for loop vars, so that autofree can skip them
|
2019-12-24 18:54:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 09:18:15 +01:00
|
|
|
|
// used for smartcasting only
|
|
|
|
|
// struct fields change type in scopes
|
|
|
|
|
pub struct ScopeStructField {
|
|
|
|
|
pub:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
struct_type Type // type of struct
|
2020-11-23 16:16:13 +01:00
|
|
|
|
name string
|
|
|
|
|
pos token.Position
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
|
|
|
|
sum_type_casts []Type // nested sum types require nested smart casting, for that a list of types is needed
|
|
|
|
|
orig_type Type // original sumtype type; 0 if it's not a sumtype
|
2021-01-21 21:31:25 +01:00
|
|
|
|
// TODO: move this to a real docs site later
|
|
|
|
|
// 10 <- original type (orig_type)
|
|
|
|
|
// [11, 12, 13] <- cast order (sum_type_casts)
|
|
|
|
|
// 12 <- the current casted type (typ)
|
2020-11-11 09:18:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-03 07:03:44 +02:00
|
|
|
|
pub struct GlobalField {
|
2020-02-03 07:02:54 +01:00
|
|
|
|
pub:
|
2020-04-09 15:33:46 +02:00
|
|
|
|
name string
|
|
|
|
|
expr Expr
|
2020-04-07 18:51:39 +02:00
|
|
|
|
has_expr bool
|
2020-05-16 16:12:23 +02:00
|
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-10-03 07:03:44 +02:00
|
|
|
|
comments []Comment
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct GlobalDecl {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
pos token.Position
|
2020-10-03 07:03:44 +02:00
|
|
|
|
pub mut:
|
|
|
|
|
fields []GlobalField
|
|
|
|
|
end_comments []Comment
|
2020-02-03 07:02:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-14 15:20:11 +01:00
|
|
|
|
pub struct EmbeddedFile {
|
|
|
|
|
pub:
|
|
|
|
|
rpath string // used in the source code, as an ID/key to the embed
|
|
|
|
|
apath string // absolute path during compilation to the resource
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 00:57:09 +02:00
|
|
|
|
// Each V source file is represented by one File structure.
|
|
|
|
|
// When the V compiler runs, the parser will fill an []File.
|
2020-12-28 12:52:04 +01:00
|
|
|
|
// That array is then passed to V's checker.
|
2019-12-30 12:10:46 +01:00
|
|
|
|
pub struct File {
|
2019-12-24 18:54:43 +01:00
|
|
|
|
pub:
|
2021-01-15 19:28:18 +01:00
|
|
|
|
path string // absolute path of the source file - '/projects/v/file.v'
|
|
|
|
|
path_base string // file name - 'file.v' (useful for tracing)
|
2021-01-12 04:38:43 +01:00
|
|
|
|
mod Module // the module of the source file (from `module xyz` at the top)
|
|
|
|
|
global_scope &Scope
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2020-12-07 18:13:03 +01:00
|
|
|
|
scope &Scope
|
2021-01-14 15:20:11 +01:00
|
|
|
|
stmts []Stmt // all the statements in the source file
|
|
|
|
|
imports []Import // all the imports
|
|
|
|
|
auto_imports []string // imports that were implicitely added
|
|
|
|
|
embedded_files []EmbeddedFile // list of files to embed in the binary
|
2020-12-28 12:52:04 +01:00
|
|
|
|
imported_symbols map[string]string // used for `import {symbol}`, it maps symbol => module.symbol
|
2021-01-03 21:10:25 +01:00
|
|
|
|
errors []errors.Error // all the checker errors in the file
|
2021-03-22 18:43:06 +01:00
|
|
|
|
warnings []errors.Warning // all the checker warnings in the file
|
|
|
|
|
notices []errors.Notice // all the checker notices in the file
|
2020-12-07 18:13:03 +01:00
|
|
|
|
generic_fns []&FnDecl
|
2021-04-01 08:58:33 +02:00
|
|
|
|
global_labels []string // from `asm { .globl labelname }`
|
2020-02-17 12:25:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-11 16:10:46 +01:00
|
|
|
|
pub struct IdentFn {
|
2020-02-17 12:25:18 +01:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-01-18 23:26:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-22 18:10:31 +02:00
|
|
|
|
// TODO: (joe) remove completely, use ident.obj
|
|
|
|
|
// instead which points to the scope object
|
2020-01-18 23:26:14 +01:00
|
|
|
|
pub struct IdentVar {
|
2020-02-06 13:57:35 +01:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-03-13 05:57:51 +01:00
|
|
|
|
is_mut bool
|
|
|
|
|
is_static bool
|
|
|
|
|
is_optional bool
|
2021-04-02 00:57:09 +02:00
|
|
|
|
share ShareType
|
2020-01-18 23:26:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-25 12:09:40 +01:00
|
|
|
|
pub type IdentInfo = IdentFn | IdentVar
|
2020-01-18 23:26:14 +01:00
|
|
|
|
|
|
|
|
|
pub enum IdentKind {
|
2020-02-17 12:25:18 +01:00
|
|
|
|
unresolved
|
2020-01-18 23:26:14 +01:00
|
|
|
|
blank_ident
|
|
|
|
|
variable
|
2020-02-03 07:02:54 +01:00
|
|
|
|
constant
|
2020-04-04 05:14:40 +02:00
|
|
|
|
global
|
2020-02-17 12:25:18 +01:00
|
|
|
|
function
|
2019-12-24 18:54:43 +01:00
|
|
|
|
}
|
2019-12-28 14:11:05 +01:00
|
|
|
|
|
2019-12-22 02:34:37 +01:00
|
|
|
|
// A single identifier
|
2019-12-28 14:11:05 +01:00
|
|
|
|
pub struct Ident {
|
|
|
|
|
pub:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
language Language
|
2019-12-31 10:53:30 +01:00
|
|
|
|
tok_kind token.Kind
|
2020-01-18 23:26:14 +01:00
|
|
|
|
pos token.Position
|
2020-11-27 03:08:42 +01:00
|
|
|
|
mut_pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
scope &Scope
|
|
|
|
|
obj ScopeObject
|
|
|
|
|
mod string
|
|
|
|
|
name string
|
|
|
|
|
kind IdentKind
|
|
|
|
|
info IdentInfo
|
|
|
|
|
is_mut bool
|
2019-12-22 02:34:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-07 14:49:14 +01:00
|
|
|
|
pub fn (i &Ident) var_info() IdentVar {
|
2020-11-25 12:09:40 +01:00
|
|
|
|
match mut i.info {
|
2020-02-06 17:38:02 +01:00
|
|
|
|
IdentVar {
|
2020-11-24 13:58:29 +01:00
|
|
|
|
return i.info
|
2020-02-06 17:38:02 +01:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// return IdentVar{}
|
|
|
|
|
panic('Ident.var_info(): info is not IdentVar variant')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 03:13:10 +02:00
|
|
|
|
// left op right
|
|
|
|
|
// See: token.Kind.is_infix
|
2020-01-22 21:34:38 +01:00
|
|
|
|
pub struct InfixExpr {
|
2019-12-22 02:34:37 +01:00
|
|
|
|
pub:
|
2021-03-12 16:09:01 +01:00
|
|
|
|
op token.Kind
|
|
|
|
|
pos token.Position
|
|
|
|
|
is_stmt bool
|
2020-10-21 21:58:40 +02:00
|
|
|
|
pub mut:
|
2020-07-14 18:52:28 +02:00
|
|
|
|
left Expr
|
|
|
|
|
right Expr
|
2021-04-02 00:57:09 +02:00
|
|
|
|
left_type Type
|
|
|
|
|
right_type Type
|
2020-07-14 18:52:28 +02:00
|
|
|
|
auto_locked string
|
2021-01-06 21:19:40 +01:00
|
|
|
|
or_block OrExpr
|
2019-12-22 02:34:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 03:13:10 +02:00
|
|
|
|
// ++, --
|
2020-01-06 16:13:12 +01:00
|
|
|
|
pub struct PostfixExpr {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
op token.Kind
|
|
|
|
|
expr Expr
|
|
|
|
|
pos token.Position
|
2020-07-13 14:01:32 +02:00
|
|
|
|
pub mut:
|
2020-07-14 18:52:28 +02:00
|
|
|
|
auto_locked string
|
2020-01-06 16:13:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 03:13:10 +02:00
|
|
|
|
// See: token.Kind.is_prefix
|
2020-01-06 16:13:12 +01:00
|
|
|
|
pub struct PrefixExpr {
|
|
|
|
|
pub:
|
2021-03-03 09:10:38 +01:00
|
|
|
|
op token.Kind
|
|
|
|
|
pos token.Position
|
2020-08-17 23:35:36 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
right_type Type
|
2021-03-03 09:10:38 +01:00
|
|
|
|
right Expr
|
2020-08-23 02:12:05 +02:00
|
|
|
|
or_block OrExpr
|
2021-03-03 09:10:38 +01:00
|
|
|
|
is_option bool // IfGuard
|
2020-01-06 16:13:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 12:14:10 +01:00
|
|
|
|
pub struct IndexExpr {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
pos token.Position
|
|
|
|
|
index Expr // [0], RangeExpr [start..end] or map[key]
|
|
|
|
|
or_expr OrExpr
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-02-16 14:08:01 +01:00
|
|
|
|
left Expr
|
2021-04-02 00:57:09 +02:00
|
|
|
|
left_type Type // array, map, fixed array
|
2020-04-09 15:33:46 +02:00
|
|
|
|
is_setter bool
|
2021-02-16 14:08:01 +01:00
|
|
|
|
is_map bool
|
|
|
|
|
is_array bool
|
|
|
|
|
is_farray bool
|
2021-03-03 09:10:38 +01:00
|
|
|
|
is_option bool // IfGuard
|
2020-01-07 12:14:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-28 19:16:04 +01:00
|
|
|
|
pub struct IfExpr {
|
2019-12-29 08:51:55 +01:00
|
|
|
|
pub:
|
2020-09-18 00:58:54 +02:00
|
|
|
|
is_comptime bool
|
2020-07-20 16:48:33 +02:00
|
|
|
|
tok_kind token.Kind
|
|
|
|
|
left Expr // `a` in `a := if ...`
|
|
|
|
|
pos token.Position
|
|
|
|
|
post_comments []Comment
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
branches []IfBranch // includes all `else if` branches
|
|
|
|
|
is_expr bool
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2021-01-12 04:38:43 +01:00
|
|
|
|
has_else bool
|
2020-03-20 14:39:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct IfBranch {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
cond Expr
|
|
|
|
|
pos token.Position
|
|
|
|
|
body_pos token.Position
|
|
|
|
|
comments []Comment
|
2020-07-08 15:17:28 +02:00
|
|
|
|
pub mut:
|
2020-11-27 03:08:42 +01:00
|
|
|
|
stmts []Stmt
|
|
|
|
|
smartcast bool // true when cond is `x is SumType`, set in checker.if_expr // no longer needed with union sum types TODO: remove
|
2020-12-12 09:01:12 +01:00
|
|
|
|
scope &Scope
|
2020-02-03 07:02:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 12:58:33 +02:00
|
|
|
|
pub struct UnsafeExpr {
|
|
|
|
|
pub:
|
2020-09-22 05:28:29 +02:00
|
|
|
|
expr Expr
|
|
|
|
|
pos token.Position
|
2020-07-12 12:58:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-04 12:44:25 +02:00
|
|
|
|
pub struct LockExpr {
|
|
|
|
|
pub:
|
|
|
|
|
stmts []Stmt
|
2021-02-03 15:16:52 +01:00
|
|
|
|
is_rlock []bool
|
2020-07-04 12:44:25 +02:00
|
|
|
|
pos token.Position
|
|
|
|
|
pub mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
lockeds []Ident // `x`, `y` in `lock x, y {`
|
|
|
|
|
is_expr bool
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-07-04 12:44:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-05 10:00:11 +01:00
|
|
|
|
pub struct MatchExpr {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
tok_kind token.Kind
|
|
|
|
|
cond Expr
|
|
|
|
|
branches []MatchBranch
|
|
|
|
|
pos token.Position
|
2021-01-30 11:57:57 +01:00
|
|
|
|
comments []Comment // comments before the first branch
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2020-11-25 12:09:40 +01:00
|
|
|
|
is_expr bool // returns a value
|
2021-04-02 00:57:09 +02:00
|
|
|
|
return_type Type
|
|
|
|
|
cond_type Type // type of `x` in `match x {`
|
|
|
|
|
expected_type Type // for debugging only
|
2020-11-25 12:09:40 +01:00
|
|
|
|
is_sum_type bool
|
2020-02-05 10:00:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 11:59:45 +01:00
|
|
|
|
pub struct MatchBranch {
|
|
|
|
|
pub:
|
2021-01-03 21:10:25 +01:00
|
|
|
|
exprs []Expr // left side
|
2020-11-11 21:53:21 +01:00
|
|
|
|
ecmnts [][]Comment // inline comments for each left side expr
|
2021-01-03 21:10:25 +01:00
|
|
|
|
stmts []Stmt // right side
|
2020-07-03 15:10:39 +02:00
|
|
|
|
pos token.Position
|
|
|
|
|
is_else bool
|
2021-01-30 11:57:57 +01:00
|
|
|
|
post_comments []Comment // comments below ´... }´
|
2020-12-12 09:01:12 +01:00
|
|
|
|
pub mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
scope &Scope
|
2020-03-04 11:59:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-16 15:34:57 +02:00
|
|
|
|
pub struct SelectExpr {
|
|
|
|
|
pub:
|
|
|
|
|
branches []SelectBranch
|
|
|
|
|
pos token.Position
|
2020-09-19 02:14:35 +02:00
|
|
|
|
has_exception bool
|
2020-09-16 15:34:57 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
is_expr bool // returns a value
|
|
|
|
|
expected_type Type // for debugging only
|
2020-09-16 15:34:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct SelectBranch {
|
|
|
|
|
pub:
|
2021-01-03 21:10:25 +01:00
|
|
|
|
stmt Stmt // `a := <-ch` or `ch <- a`
|
2020-09-16 15:34:57 +02:00
|
|
|
|
stmts []Stmt // right side
|
|
|
|
|
pos token.Position
|
|
|
|
|
comment Comment // comment above `select {`
|
|
|
|
|
is_else bool
|
|
|
|
|
is_timeout bool
|
|
|
|
|
post_comments []Comment
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 00:02:44 +02:00
|
|
|
|
pub enum CompForKind {
|
|
|
|
|
methods
|
|
|
|
|
fields
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-03 15:10:39 +02:00
|
|
|
|
pub struct CompFor {
|
|
|
|
|
pub:
|
|
|
|
|
val_var string
|
|
|
|
|
stmts []Stmt
|
2020-07-25 00:02:44 +02:00
|
|
|
|
kind CompForKind
|
2020-11-04 12:34:12 +01:00
|
|
|
|
pos token.Position
|
2021-02-26 07:28:19 +01:00
|
|
|
|
typ_pos token.Position
|
2020-07-03 15:10:39 +02:00
|
|
|
|
pub mut:
|
|
|
|
|
// expr Expr
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-07-03 15:10:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-30 06:16:59 +01:00
|
|
|
|
pub struct ForStmt {
|
|
|
|
|
pub:
|
2020-02-19 11:06:36 +01:00
|
|
|
|
cond Expr
|
|
|
|
|
stmts []Stmt
|
|
|
|
|
is_inf bool // `for {}`
|
2020-03-18 14:50:21 +01:00
|
|
|
|
pos token.Position
|
2020-11-20 12:23:48 +01:00
|
|
|
|
pub mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
label string // `label: for {`
|
|
|
|
|
scope &Scope
|
2020-01-07 00:14:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct ForInStmt {
|
|
|
|
|
pub:
|
2020-10-14 13:23:58 +02:00
|
|
|
|
key_var string
|
|
|
|
|
val_var string
|
|
|
|
|
cond Expr
|
|
|
|
|
is_range bool
|
|
|
|
|
high Expr // `10` in `for i in 0..10 {`
|
|
|
|
|
stmts []Stmt
|
|
|
|
|
pos token.Position
|
2020-10-13 16:27:24 +02:00
|
|
|
|
val_is_mut bool // `for mut val in vals {` means that modifying `val` will modify the array
|
2020-10-14 13:23:58 +02:00
|
|
|
|
// and the array cannot be indexed inside the loop
|
2020-10-15 22:12:59 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
key_type Type
|
|
|
|
|
val_type Type
|
|
|
|
|
cond_type Type
|
|
|
|
|
kind Kind // array/map/string
|
|
|
|
|
label string // `label: for {`
|
2021-01-12 04:38:43 +01:00
|
|
|
|
scope &Scope
|
2020-01-07 00:14:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct ForCStmt {
|
|
|
|
|
pub:
|
2020-03-11 02:44:30 +01:00
|
|
|
|
init Stmt // i := 0;
|
|
|
|
|
has_init bool
|
|
|
|
|
cond Expr // i < 10;
|
2020-04-10 09:00:14 +02:00
|
|
|
|
has_cond bool
|
2020-06-16 13:20:16 +02:00
|
|
|
|
inc Stmt // i++; i += 2
|
2020-04-10 09:00:14 +02:00
|
|
|
|
has_inc bool
|
2021-02-24 13:58:45 +01:00
|
|
|
|
is_multi bool // for a,b := 0,1; a < 10; a,b = a+b, a {...}
|
2020-03-11 02:44:30 +01:00
|
|
|
|
stmts []Stmt
|
2020-03-18 14:50:21 +01:00
|
|
|
|
pos token.Position
|
2020-11-20 12:23:48 +01:00
|
|
|
|
pub mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
label string // `label: for {`
|
|
|
|
|
scope &Scope
|
2019-12-30 06:16:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 09:54:15 +01:00
|
|
|
|
// #include etc
|
|
|
|
|
pub struct HashStmt {
|
|
|
|
|
pub:
|
2021-01-23 10:39:12 +01:00
|
|
|
|
mod string
|
|
|
|
|
pos token.Position
|
|
|
|
|
source_file string
|
2020-08-28 08:24:04 +02:00
|
|
|
|
pub mut:
|
2020-10-17 17:27:06 +02:00
|
|
|
|
val string // example: 'include <openssl/rand.h> # please install openssl // comment'
|
|
|
|
|
kind string // : 'include'
|
|
|
|
|
main string // : '<openssl/rand.h>'
|
|
|
|
|
msg string // : 'please install openssl'
|
2020-02-04 09:54:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 05:54:51 +02:00
|
|
|
|
/*
|
|
|
|
|
// filter(), map(), sort()
|
2020-02-10 14:42:57 +01:00
|
|
|
|
pub struct Lambda {
|
|
|
|
|
pub:
|
|
|
|
|
name string
|
|
|
|
|
}
|
2020-08-12 05:54:51 +02:00
|
|
|
|
*/
|
2020-12-28 12:52:04 +01:00
|
|
|
|
// variable assign statement
|
2019-12-28 19:16:04 +01:00
|
|
|
|
pub struct AssignStmt {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
op token.Kind // include: =,:=,+=,-=,*=,/= and so on; for a list of all the assign operators, see vlib/token/token.v
|
|
|
|
|
pos token.Position
|
|
|
|
|
comments []Comment
|
|
|
|
|
end_comments []Comment
|
2020-04-27 22:53:26 +02:00
|
|
|
|
pub mut:
|
2021-01-30 11:55:10 +01:00
|
|
|
|
right []Expr
|
2020-06-16 13:20:16 +02:00
|
|
|
|
left []Expr
|
2021-04-02 00:57:09 +02:00
|
|
|
|
left_types []Type
|
|
|
|
|
right_types []Type
|
2020-05-27 03:20:22 +02:00
|
|
|
|
is_static bool // for translated code only
|
2020-06-16 13:20:16 +02:00
|
|
|
|
is_simple bool // `x+=2` in `for x:=1; ; x+=2`
|
2020-05-26 18:00:51 +02:00
|
|
|
|
has_cross_var bool
|
2019-12-28 19:16:04 +01:00
|
|
|
|
}
|
2020-02-06 17:38:02 +01:00
|
|
|
|
|
2020-03-02 19:00:33 +01:00
|
|
|
|
pub struct AsCast {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
expr Expr
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2021-01-12 04:38:43 +01:00
|
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
expr_type Type
|
2020-03-02 19:00:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:52:04 +01:00
|
|
|
|
// an enum value, like OS.macos or .macos
|
2020-02-10 23:19:50 +01:00
|
|
|
|
pub struct EnumVal {
|
2020-02-10 20:33:34 +01:00
|
|
|
|
pub:
|
2020-02-25 15:02:34 +01:00
|
|
|
|
enum_name string
|
|
|
|
|
val string
|
2020-03-15 00:46:08 +01:00
|
|
|
|
mod string // for full path `mod_Enum_val`
|
2020-02-25 15:02:34 +01:00
|
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-02-10 23:19:50 +01:00
|
|
|
|
}
|
2020-02-10 20:33:34 +01:00
|
|
|
|
|
2020-12-28 12:52:04 +01:00
|
|
|
|
// enum field in enum declaration
|
2020-04-09 19:23:49 +02:00
|
|
|
|
pub struct EnumField {
|
2020-05-18 18:06:09 +02:00
|
|
|
|
pub:
|
2020-12-25 20:41:59 +01:00
|
|
|
|
name string
|
|
|
|
|
pos token.Position
|
2020-12-28 12:52:04 +01:00
|
|
|
|
comments []Comment // comment after Enumfield in the same line
|
|
|
|
|
next_comments []Comment // comments between current EnumField and next EnumField
|
2021-01-03 21:10:25 +01:00
|
|
|
|
expr Expr // the value of current EnumField; 123 in `ename = 123`
|
|
|
|
|
has_expr bool // true, when .expr has a value
|
2020-04-09 19:23:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:52:04 +01:00
|
|
|
|
// enum declaration
|
2020-02-10 23:19:50 +01:00
|
|
|
|
pub struct EnumDecl {
|
|
|
|
|
pub:
|
2020-07-10 16:43:02 +02:00
|
|
|
|
name string
|
|
|
|
|
is_pub bool
|
2021-04-02 00:57:09 +02:00
|
|
|
|
is_flag bool // true when the enum has [flag] tag,for bit field enum
|
|
|
|
|
is_multi_allowed bool // true when the enum has [_allow_multiple_values] tag
|
|
|
|
|
comments []Comment // comments before the first EnumField
|
|
|
|
|
fields []EnumField // all the enum fields
|
|
|
|
|
attrs []Attr // attributes of enum declaration
|
2020-07-10 16:43:02 +02:00
|
|
|
|
pos token.Position
|
2020-02-10 23:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 17:37:55 +01:00
|
|
|
|
pub struct AliasTypeDecl {
|
|
|
|
|
pub:
|
2020-03-07 22:37:03 +01:00
|
|
|
|
name string
|
|
|
|
|
is_pub bool
|
2021-04-02 00:57:09 +02:00
|
|
|
|
parent_type Type
|
2020-04-19 00:07:57 +02:00
|
|
|
|
pos token.Position
|
2020-11-20 10:51:50 +01:00
|
|
|
|
comments []Comment
|
2020-03-07 17:37:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 09:18:15 +01:00
|
|
|
|
// New implementation of sum types
|
2020-11-25 12:09:40 +01:00
|
|
|
|
pub struct SumTypeDecl {
|
2020-11-11 09:18:15 +01:00
|
|
|
|
pub:
|
2020-11-28 22:40:40 +01:00
|
|
|
|
name string
|
|
|
|
|
is_pub bool
|
|
|
|
|
pos token.Position
|
|
|
|
|
comments []Comment
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-11-11 09:18:15 +01:00
|
|
|
|
pub mut:
|
2020-11-28 22:40:40 +01:00
|
|
|
|
variants []SumTypeVariant
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct SumTypeVariant {
|
|
|
|
|
pub:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-11-28 22:40:40 +01:00
|
|
|
|
pos token.Position
|
2020-11-11 09:18:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-11 16:10:46 +01:00
|
|
|
|
pub struct FnTypeDecl {
|
|
|
|
|
pub:
|
2020-11-20 10:51:50 +01:00
|
|
|
|
name string
|
|
|
|
|
is_pub bool
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-11-20 10:51:50 +01:00
|
|
|
|
pos token.Position
|
|
|
|
|
comments []Comment
|
2020-03-11 16:10:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 15:33:46 +02:00
|
|
|
|
// TODO: handle this differently
|
|
|
|
|
// v1 excludes non current os ifdefs so
|
|
|
|
|
// the defer's never get added in the first place
|
2020-02-17 22:50:04 +01:00
|
|
|
|
pub struct DeferStmt {
|
2020-02-11 10:26:46 +01:00
|
|
|
|
pub:
|
2020-02-17 22:50:04 +01:00
|
|
|
|
stmts []Stmt
|
2020-11-04 12:34:12 +01:00
|
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-02-06 17:40:39 +01:00
|
|
|
|
ifdef string
|
|
|
|
|
idx_in_fn int = -1 // index in FnDecl.defer_stmts
|
2020-02-11 10:26:46 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 14:41:19 +01:00
|
|
|
|
// `(3+4)`
|
|
|
|
|
pub struct ParExpr {
|
|
|
|
|
pub:
|
|
|
|
|
expr Expr
|
2020-11-04 12:34:12 +01:00
|
|
|
|
pos token.Position
|
2020-02-28 14:41:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 08:33:42 +01:00
|
|
|
|
pub struct GoStmt {
|
|
|
|
|
pub:
|
2021-01-15 13:45:26 +01:00
|
|
|
|
pos token.Position
|
|
|
|
|
pub mut:
|
|
|
|
|
call_expr CallExpr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct GoExpr {
|
|
|
|
|
pub:
|
|
|
|
|
pos token.Position
|
|
|
|
|
pub mut:
|
|
|
|
|
go_stmt GoStmt
|
|
|
|
|
mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
return_type Type
|
2020-03-16 08:33:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 22:50:04 +01:00
|
|
|
|
pub struct GotoLabel {
|
2020-02-17 14:15:42 +01:00
|
|
|
|
pub:
|
2020-02-17 22:50:04 +01:00
|
|
|
|
name string
|
2020-11-04 12:34:12 +01:00
|
|
|
|
pos token.Position
|
2020-02-17 14:15:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct GotoStmt {
|
|
|
|
|
pub:
|
2020-02-17 22:50:04 +01:00
|
|
|
|
name string
|
2020-11-04 12:34:12 +01:00
|
|
|
|
pos token.Position
|
2020-02-17 14:15:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-30 09:38:12 +01:00
|
|
|
|
pub struct ArrayInit {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
pos token.Position // `[]` in []Type{} position
|
|
|
|
|
elem_type_pos token.Position // `Type` in []Type{} position
|
|
|
|
|
exprs []Expr // `[expr, expr]` or `[expr]Type{}` for fixed array
|
|
|
|
|
ecmnts [][]Comment // optional iembed comments after each expr
|
2021-02-21 19:18:19 +01:00
|
|
|
|
pre_cmnts []Comment
|
2021-01-12 04:38:43 +01:00
|
|
|
|
is_fixed bool
|
2021-01-14 03:51:13 +01:00
|
|
|
|
has_val bool // fixed size literal `[expr, expr]!`
|
2021-01-12 04:38:43 +01:00
|
|
|
|
mod string
|
|
|
|
|
len_expr Expr // len: expr
|
|
|
|
|
cap_expr Expr // cap: expr
|
|
|
|
|
default_expr Expr // init: expr
|
|
|
|
|
has_len bool
|
|
|
|
|
has_cap bool
|
|
|
|
|
has_default bool
|
2020-12-01 11:20:27 +01:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
expr_types []Type // [Dog, Cat] // also used for interface_types
|
|
|
|
|
elem_type Type // element type
|
|
|
|
|
typ Type // array type
|
2019-12-30 09:38:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 16:14:08 +01:00
|
|
|
|
pub struct ArrayDecompose {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
expr Expr
|
|
|
|
|
pos token.Position
|
2020-12-29 16:14:08 +01:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
expr_type Type
|
|
|
|
|
arg_type Type
|
2020-12-29 16:14:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 21:18:42 +02:00
|
|
|
|
pub struct ChanInit {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
pos token.Position
|
|
|
|
|
cap_expr Expr
|
|
|
|
|
has_cap bool
|
2020-08-14 21:18:42 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
|
|
|
|
elem_type Type
|
2020-08-14 21:18:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-22 14:13:19 +01:00
|
|
|
|
pub struct MapInit {
|
|
|
|
|
pub:
|
2021-02-22 12:04:48 +01:00
|
|
|
|
pos token.Position
|
|
|
|
|
keys []Expr
|
|
|
|
|
vals []Expr
|
|
|
|
|
comments [][]Comment // comments after key-value pairs
|
|
|
|
|
pre_cmnts []Comment // comments before the first key-value pair
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
|
|
|
|
key_type Type
|
|
|
|
|
value_type Type
|
2020-02-22 14:13:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 14:31:54 +01:00
|
|
|
|
// s[10..20]
|
|
|
|
|
pub struct RangeExpr {
|
|
|
|
|
pub:
|
2020-03-06 22:24:39 +01:00
|
|
|
|
low Expr
|
|
|
|
|
high Expr
|
|
|
|
|
has_high bool
|
|
|
|
|
has_low bool
|
2020-11-04 12:34:12 +01:00
|
|
|
|
pos token.Position
|
2020-02-02 14:31:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 00:57:09 +02:00
|
|
|
|
// NB: &string(x) gets parsed as PrefixExpr{ right: CastExpr{...} }
|
2020-08-10 18:05:26 +02:00
|
|
|
|
// TODO: that is very likely a parsing bug. It should get parsed as just
|
2021-04-02 00:57:09 +02:00
|
|
|
|
// CastExpr{...}, where .typname is '&string' instead.
|
2020-08-10 18:05:26 +02:00
|
|
|
|
// The current situation leads to special cases in vfmt and cgen
|
|
|
|
|
// (see prefix_expr_cast_expr in fmt.v, and .is_amp in cgen.v)
|
|
|
|
|
// .in_prexpr is also needed because of that, because the checker needs to
|
|
|
|
|
// show warnings about the deprecated C->V conversions `string(x)` and
|
|
|
|
|
// `string(x,y)`, while skipping the real pointer casts like `&string(x)`.
|
2020-02-10 14:43:17 +01:00
|
|
|
|
pub struct CastExpr {
|
|
|
|
|
pub:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
expr Expr // `buf` in `string(buf, n)`
|
|
|
|
|
arg Expr // `n` in `string(buf, n)`
|
|
|
|
|
typ Type // `string` TODO rename to `type_to_cast_to`
|
2021-01-12 04:38:43 +01:00
|
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typname string // TypeSymbol.name
|
|
|
|
|
expr_type Type // `byteptr`
|
2020-03-11 19:00:51 +01:00
|
|
|
|
has_arg bool
|
2021-04-02 00:57:09 +02:00
|
|
|
|
in_prexpr bool // is the parent node an PrefixExpr
|
2020-02-10 14:43:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 01:43:17 +01:00
|
|
|
|
pub struct AsmStmt {
|
|
|
|
|
pub:
|
|
|
|
|
arch pref.Arch
|
|
|
|
|
is_top_level bool
|
|
|
|
|
is_volatile bool
|
|
|
|
|
is_goto bool
|
|
|
|
|
clobbered []AsmClobbered
|
|
|
|
|
pos token.Position
|
|
|
|
|
pub mut:
|
2021-04-01 08:58:33 +02:00
|
|
|
|
templates []AsmTemplate
|
|
|
|
|
scope &Scope
|
|
|
|
|
output []AsmIO
|
|
|
|
|
input []AsmIO
|
|
|
|
|
global_labels []string // labels defined in assembly block, exported with `.globl`
|
|
|
|
|
local_labels []string // local to the assembly block
|
2021-03-17 01:43:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct AsmTemplate {
|
|
|
|
|
pub mut:
|
|
|
|
|
name string
|
|
|
|
|
is_label bool // `example_label:`
|
|
|
|
|
is_directive bool // .globl assembly_function
|
|
|
|
|
args []AsmArg
|
|
|
|
|
comments []Comment
|
|
|
|
|
pos token.Position
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 08:58:33 +02:00
|
|
|
|
// [eax+5] | j | displacement literal (e.g. 123 in [rax + 123] ) | eax | true | `a` | 0.594 | 123 | label_name
|
|
|
|
|
pub type AsmArg = AsmAddressing | AsmAlias | AsmDisp | AsmRegister | BoolLiteral | CharLiteral |
|
2021-03-17 01:43:17 +01:00
|
|
|
|
FloatLiteral | IntegerLiteral | string
|
|
|
|
|
|
|
|
|
|
pub struct AsmRegister {
|
|
|
|
|
pub:
|
|
|
|
|
name string // eax or r12d
|
|
|
|
|
mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2021-03-17 01:43:17 +01:00
|
|
|
|
size int
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 08:58:33 +02:00
|
|
|
|
pub struct AsmDisp {
|
|
|
|
|
pub:
|
|
|
|
|
val string
|
|
|
|
|
pos token.Position
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 01:43:17 +01:00
|
|
|
|
pub struct AsmAlias {
|
|
|
|
|
pub:
|
|
|
|
|
name string // a
|
|
|
|
|
pos token.Position
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct AsmAddressing {
|
|
|
|
|
pub:
|
2021-04-02 16:26:37 +02:00
|
|
|
|
scale int = -1 // 1, 2, 4, or 8 literal
|
2021-04-01 08:58:33 +02:00
|
|
|
|
mode AddressingMode
|
|
|
|
|
pos token.Position
|
2021-03-17 01:43:17 +01:00
|
|
|
|
pub mut:
|
2021-04-01 08:58:33 +02:00
|
|
|
|
displacement AsmArg // 8, 16 or 32 bit literal value
|
|
|
|
|
base AsmArg // gpr
|
|
|
|
|
index AsmArg // gpr
|
2021-03-17 01:43:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// adressing modes:
|
|
|
|
|
pub enum AddressingMode {
|
|
|
|
|
invalid
|
|
|
|
|
displacement // displacement
|
|
|
|
|
base // base
|
|
|
|
|
base_plus_displacement // base + displacement
|
|
|
|
|
index_times_scale_plus_displacement // (index ∗ scale) + displacement
|
|
|
|
|
base_plus_index_plus_displacement // base + (index ∗ scale) + displacement
|
|
|
|
|
base_plus_index_times_scale_plus_displacement // base + index + displacement
|
|
|
|
|
rip_plus_displacement // rip + displacement
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct AsmClobbered {
|
|
|
|
|
pub mut:
|
2021-04-01 08:58:33 +02:00
|
|
|
|
reg AsmRegister
|
2021-03-17 01:43:17 +01:00
|
|
|
|
comments []Comment
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// : [alias_a] '=r' (a) // this is a comment
|
|
|
|
|
pub struct AsmIO {
|
|
|
|
|
pub:
|
|
|
|
|
alias string // [alias_a]
|
|
|
|
|
constraint string // '=r'
|
|
|
|
|
expr Expr // (a)
|
|
|
|
|
comments []Comment // // this is a comment
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2021-03-17 01:43:17 +01:00
|
|
|
|
pos token.Position
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub const (
|
|
|
|
|
// reference: https://en.wikipedia.org/wiki/X86#/media/File:Table_of_x86_Registers_svg.svg
|
|
|
|
|
// map register size -> register name
|
|
|
|
|
x86_no_number_register_list = map{
|
|
|
|
|
8: ['al', 'ah', 'bl', 'bh', 'cl', 'ch', 'dl', 'dh', 'bpl', 'sil', 'dil', 'spl']
|
|
|
|
|
16: ['ax', 'bx', 'cx', 'dx', 'bp', 'si', 'di', 'sp', /* segment registers */ 'cs', 'ss',
|
|
|
|
|
'ds', 'es', 'fs', 'gs', 'flags', 'ip', /* task registers */ 'gdtr', 'idtr', 'tr', 'ldtr',
|
|
|
|
|
// CSR register 'msw', /* FP core registers */ 'cw', 'sw', 'tw', 'fp_ip', 'fp_dp',
|
|
|
|
|
'fp_cs', 'fp_ds', 'fp_opc']
|
|
|
|
|
32: [
|
|
|
|
|
'eax',
|
|
|
|
|
'ebx',
|
|
|
|
|
'ecx',
|
|
|
|
|
'edx',
|
|
|
|
|
'ebp',
|
|
|
|
|
'esi',
|
|
|
|
|
'edi',
|
|
|
|
|
'esp',
|
|
|
|
|
'eflags',
|
|
|
|
|
'eip', /* CSR register */
|
|
|
|
|
'mxcsr' /* 32-bit FP core registers 'fp_dp', 'fp_ip' (TODO: why are there duplicates?) */,
|
|
|
|
|
]
|
|
|
|
|
64: ['rax', 'rbx', 'rcx', 'rdx', 'rbp', 'rsi', 'rdi', 'rsp', 'rflags', 'rip']
|
|
|
|
|
}
|
|
|
|
|
// no comments because maps do not support comments
|
|
|
|
|
// r#*: gp registers added in 64-bit extensions, can only be from 8-15 actually
|
|
|
|
|
// *mm#: vector/simd registors
|
|
|
|
|
// st#: floating point numbers
|
|
|
|
|
// cr#: control/status registers
|
|
|
|
|
// dr#: debug registers
|
|
|
|
|
x86_with_number_register_list = map{
|
|
|
|
|
8: map{
|
|
|
|
|
'r#b': 16
|
|
|
|
|
}
|
|
|
|
|
16: map{
|
|
|
|
|
'r#w': 16
|
|
|
|
|
}
|
|
|
|
|
32: map{
|
|
|
|
|
'r#d': 16
|
|
|
|
|
}
|
|
|
|
|
64: map{
|
|
|
|
|
'r#': 16
|
|
|
|
|
'mm#': 16
|
|
|
|
|
'cr#': 16
|
|
|
|
|
'dr#': 16
|
|
|
|
|
}
|
|
|
|
|
80: map{
|
|
|
|
|
'st#': 16
|
|
|
|
|
}
|
|
|
|
|
128: map{
|
|
|
|
|
'xmm#': 32
|
|
|
|
|
}
|
|
|
|
|
256: map{
|
|
|
|
|
'ymm#': 32
|
|
|
|
|
}
|
|
|
|
|
512: map{
|
|
|
|
|
'zmm#': 32
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TODO: saved priviled registers for arm
|
2021-04-01 08:58:33 +02:00
|
|
|
|
pub const (
|
2021-03-17 01:43:17 +01:00
|
|
|
|
arm_no_number_register_list = ['fp' /* aka r11 */, /* not instruction pointer: */ 'ip' /* aka r12 */,
|
|
|
|
|
'sp' /* aka r13 */, 'lr' /* aka r14 */, /* this is instruction pointer ('program counter'): */
|
|
|
|
|
'pc' /* aka r15 */,
|
|
|
|
|
] // 'cpsr' and 'apsr' are special flags registers, but cannot be referred to directly
|
|
|
|
|
arm_with_number_register_list = map{
|
|
|
|
|
'r#': 16
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2021-04-01 08:58:33 +02:00
|
|
|
|
pub const (
|
2021-03-17 01:43:17 +01:00
|
|
|
|
riscv_no_number_register_list = ['zero', 'ra', 'sp', 'gp', 'tp']
|
|
|
|
|
riscv_with_number_register_list = map{
|
|
|
|
|
'x#': 32
|
|
|
|
|
't#': 3
|
|
|
|
|
's#': 12
|
|
|
|
|
'a#': 8
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2020-02-26 15:51:05 +01:00
|
|
|
|
pub struct AssertStmt {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
pos token.Position
|
2020-10-21 21:58:40 +02:00
|
|
|
|
pub mut:
|
|
|
|
|
expr Expr
|
2020-02-26 15:51:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 15:36:41 +01:00
|
|
|
|
// `if [x := opt()] {`
|
|
|
|
|
pub struct IfGuardExpr {
|
2020-02-27 18:02:40 +01:00
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
var_name string
|
|
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-03-03 09:10:38 +01:00
|
|
|
|
expr Expr
|
2021-04-02 00:57:09 +02:00
|
|
|
|
expr_type Type
|
2020-02-27 18:02:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-23 08:51:15 +02:00
|
|
|
|
pub enum OrKind {
|
|
|
|
|
absent
|
|
|
|
|
block
|
|
|
|
|
propagate
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 15:36:41 +01:00
|
|
|
|
// `or { ... }`
|
|
|
|
|
pub struct OrExpr {
|
|
|
|
|
pub:
|
2020-05-23 08:51:15 +02:00
|
|
|
|
stmts []Stmt
|
|
|
|
|
kind OrKind
|
2020-05-25 05:32:33 +02:00
|
|
|
|
pos token.Position
|
2020-02-28 15:36:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 03:00:20 +02:00
|
|
|
|
/*
|
2020-09-12 16:40:52 +02:00
|
|
|
|
// `or { ... }`
|
|
|
|
|
pub struct OrExpr2 {
|
|
|
|
|
pub:
|
|
|
|
|
call_expr CallExpr
|
|
|
|
|
stmts []Stmt // inside `or { }`
|
|
|
|
|
kind OrKind
|
|
|
|
|
pos token.Position
|
|
|
|
|
}
|
2020-09-13 03:00:20 +02:00
|
|
|
|
*/
|
2021-02-08 16:03:05 +01:00
|
|
|
|
|
|
|
|
|
// deprecated
|
2020-02-18 17:29:47 +01:00
|
|
|
|
pub struct Assoc {
|
|
|
|
|
pub:
|
2020-03-01 13:07:51 +01:00
|
|
|
|
var_name string
|
|
|
|
|
fields []string
|
|
|
|
|
exprs []Expr
|
|
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2021-01-12 04:38:43 +01:00
|
|
|
|
scope &Scope
|
2020-02-18 17:29:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 18:13:34 +01:00
|
|
|
|
pub struct SizeOf {
|
|
|
|
|
pub:
|
2021-01-13 06:13:39 +01:00
|
|
|
|
is_type bool
|
|
|
|
|
expr Expr // checker uses this to set typ
|
|
|
|
|
pos token.Position
|
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-02-18 18:13:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 12:57:09 +01:00
|
|
|
|
pub struct OffsetOf {
|
|
|
|
|
pub:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
struct_type Type
|
2021-01-30 12:57:09 +01:00
|
|
|
|
field string
|
|
|
|
|
pos token.Position
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 16:36:18 +02:00
|
|
|
|
pub struct Likely {
|
|
|
|
|
pub:
|
2020-06-16 12:14:22 +02:00
|
|
|
|
expr Expr
|
|
|
|
|
pos token.Position
|
2020-06-09 17:08:31 +02:00
|
|
|
|
is_likely bool // false for _unlikely_
|
2020-06-09 16:36:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 12:15:39 +01:00
|
|
|
|
pub struct TypeOf {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
expr Expr
|
|
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
expr_type Type
|
2020-03-19 12:15:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-06 18:09:28 +01:00
|
|
|
|
pub struct DumpExpr {
|
|
|
|
|
pub:
|
|
|
|
|
expr Expr
|
|
|
|
|
pos token.Position
|
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
expr_type Type
|
2021-03-06 18:09:28 +01:00
|
|
|
|
cname string // filled in the checker
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 02:08:10 +02:00
|
|
|
|
pub struct Comment {
|
2020-02-18 20:20:15 +01:00
|
|
|
|
pub:
|
2020-04-09 15:33:46 +02:00
|
|
|
|
text string
|
2020-04-05 02:08:10 +02:00
|
|
|
|
is_multi bool
|
2020-04-09 15:33:46 +02:00
|
|
|
|
line_nr int
|
|
|
|
|
pos token.Position
|
2020-02-18 20:20:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-01 14:57:54 +01:00
|
|
|
|
pub struct ConcatExpr {
|
|
|
|
|
pub:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
vals []Expr
|
|
|
|
|
pos token.Position
|
2020-05-15 23:14:53 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
return_type Type
|
2020-03-01 14:57:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 09:12:32 +01:00
|
|
|
|
// @FN, @STRUCT, @MOD etc. See full list in token.valid_at_tokens
|
|
|
|
|
pub struct AtExpr {
|
|
|
|
|
pub:
|
|
|
|
|
name string
|
|
|
|
|
pos token.Position
|
|
|
|
|
kind token.AtKind
|
|
|
|
|
pub mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
|
val string
|
2020-11-05 09:12:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 15:11:43 +01:00
|
|
|
|
pub struct ComptimeSelector {
|
|
|
|
|
pub:
|
|
|
|
|
has_parens bool // if $() is used, for vfmt
|
|
|
|
|
left Expr
|
|
|
|
|
field_expr Expr
|
2021-03-03 09:14:05 +01:00
|
|
|
|
pos token.Position
|
2021-01-05 15:11:43 +01:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
left_type Type
|
|
|
|
|
typ Type
|
2021-01-05 15:11:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-25 05:32:33 +02:00
|
|
|
|
pub struct ComptimeCall {
|
2020-06-06 10:05:26 +02:00
|
|
|
|
pub:
|
2021-03-03 09:14:05 +01:00
|
|
|
|
pos token.Position
|
2021-01-05 15:11:43 +01:00
|
|
|
|
has_parens bool // if $() is used, for vfmt
|
2020-06-06 10:05:26 +02:00
|
|
|
|
method_name string
|
2021-01-30 15:24:16 +01:00
|
|
|
|
method_pos token.Position
|
|
|
|
|
scope &Scope
|
2020-06-06 10:05:26 +02:00
|
|
|
|
left Expr
|
2020-07-03 15:10:39 +02:00
|
|
|
|
args_var string
|
2021-01-31 18:22:42 +01:00
|
|
|
|
//
|
|
|
|
|
is_vweb bool
|
|
|
|
|
vweb_tmpl File
|
|
|
|
|
//
|
|
|
|
|
is_embed bool
|
|
|
|
|
embed_file EmbeddedFile
|
|
|
|
|
//
|
|
|
|
|
is_env bool
|
|
|
|
|
env_pos token.Position
|
2020-06-06 10:05:26 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
sym TypeSymbol
|
|
|
|
|
result_type Type
|
2021-01-31 18:22:42 +01:00
|
|
|
|
env_value string
|
2021-03-11 14:04:34 +01:00
|
|
|
|
args []CallArg
|
2020-05-25 05:32:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 19:54:36 +01:00
|
|
|
|
pub struct None {
|
|
|
|
|
pub:
|
2020-06-16 12:14:22 +02:00
|
|
|
|
pos token.Position
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 14:32:14 +02:00
|
|
|
|
pub enum SqlStmtKind {
|
|
|
|
|
insert
|
|
|
|
|
update
|
|
|
|
|
delete
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct SqlStmt {
|
2020-06-19 16:43:32 +02:00
|
|
|
|
pub:
|
2020-06-27 16:19:12 +02:00
|
|
|
|
kind SqlStmtKind
|
2021-01-03 21:10:25 +01:00
|
|
|
|
db_expr Expr // `db` in `sql db {`
|
2020-06-19 16:43:32 +02:00
|
|
|
|
object_var_name string // `user`
|
2020-06-27 16:19:12 +02:00
|
|
|
|
pos token.Position
|
|
|
|
|
where_expr Expr
|
|
|
|
|
updated_columns []string // for `update set x=y`
|
2021-01-03 21:10:25 +01:00
|
|
|
|
update_exprs []Expr // for `update`
|
2020-06-25 14:43:07 +02:00
|
|
|
|
pub mut:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
table_expr TypeNode
|
|
|
|
|
fields []StructField
|
2021-02-04 20:28:33 +01:00
|
|
|
|
sub_structs map[int]SqlStmt
|
2020-06-19 16:43:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-16 12:14:22 +02:00
|
|
|
|
pub struct SqlExpr {
|
2020-06-17 00:59:33 +02:00
|
|
|
|
pub:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
typ Type
|
2020-06-27 16:19:12 +02:00
|
|
|
|
is_count bool
|
|
|
|
|
db_expr Expr // `db` in `sql db {`
|
|
|
|
|
has_where bool
|
|
|
|
|
has_offset bool
|
|
|
|
|
offset_expr Expr
|
2020-07-02 19:29:22 +02:00
|
|
|
|
has_order bool
|
|
|
|
|
order_expr Expr
|
|
|
|
|
has_desc bool
|
2020-06-27 16:19:12 +02:00
|
|
|
|
is_array bool
|
|
|
|
|
pos token.Position
|
|
|
|
|
has_limit bool
|
|
|
|
|
limit_expr Expr
|
2020-06-25 12:05:24 +02:00
|
|
|
|
pub mut:
|
2021-02-04 20:28:33 +01:00
|
|
|
|
where_expr Expr
|
2021-04-02 00:57:09 +02:00
|
|
|
|
table_expr TypeNode
|
|
|
|
|
fields []StructField
|
2021-02-04 20:28:33 +01:00
|
|
|
|
sub_structs map[int]SqlExpr
|
2020-02-19 19:54:36 +01:00
|
|
|
|
}
|
2020-03-18 08:41:49 +01:00
|
|
|
|
|
2021-03-30 09:33:29 +02:00
|
|
|
|
pub struct NodeError {
|
|
|
|
|
pub:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
idx int // index for referencing the related File error
|
2021-03-30 09:33:29 +02:00
|
|
|
|
pos token.Position
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 01:19:23 +01:00
|
|
|
|
[inline]
|
2020-06-16 13:20:16 +02:00
|
|
|
|
pub fn (expr Expr) is_blank_ident() bool {
|
2020-11-25 12:09:40 +01:00
|
|
|
|
match expr {
|
2020-06-19 11:46:08 +02:00
|
|
|
|
Ident { return expr.kind == .blank_ident }
|
2020-04-19 00:07:57 +02:00
|
|
|
|
else { return false }
|
2020-03-18 01:19:23 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 08:59:55 +02:00
|
|
|
|
pub fn (expr Expr) position() token.Position {
|
2020-04-17 02:38:39 +02:00
|
|
|
|
// all uncommented have to be implemented
|
2021-04-02 16:26:37 +02:00
|
|
|
|
// NB: please do not print here. the language server will hang
|
|
|
|
|
// as it uses STDIO primarly to communicate ~Ned
|
2020-11-25 12:09:40 +01:00
|
|
|
|
match expr {
|
2020-07-07 17:10:39 +02:00
|
|
|
|
AnonFn {
|
|
|
|
|
return expr.decl.pos
|
|
|
|
|
}
|
2021-03-31 10:13:15 +02:00
|
|
|
|
EmptyExpr {
|
2021-04-02 16:26:37 +02:00
|
|
|
|
// println('compiler bug, unhandled EmptyExpr position()')
|
2021-03-31 10:13:15 +02:00
|
|
|
|
return token.Position{}
|
|
|
|
|
}
|
2021-03-30 09:33:29 +02:00
|
|
|
|
NodeError, ArrayDecompose, ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, CastExpr,
|
|
|
|
|
ChanInit, CharLiteral, ConcatExpr, Comment, ComptimeCall, ComptimeSelector, EnumVal, DumpExpr,
|
|
|
|
|
FloatLiteral, GoExpr, Ident, IfExpr, IndexExpr, IntegerLiteral, Likely, LockExpr, MapInit,
|
|
|
|
|
MatchExpr, None, OffsetOf, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr, SelectExpr,
|
2021-04-02 00:57:09 +02:00
|
|
|
|
SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit, TypeNode,
|
|
|
|
|
TypeOf, UnsafeExpr {
|
2020-12-29 16:14:08 +01:00
|
|
|
|
return expr.pos
|
|
|
|
|
}
|
2020-11-04 12:34:12 +01:00
|
|
|
|
IfGuardExpr {
|
|
|
|
|
return expr.expr.position()
|
|
|
|
|
}
|
2020-04-17 02:38:39 +02:00
|
|
|
|
InfixExpr {
|
2020-06-19 11:46:08 +02:00
|
|
|
|
left_pos := expr.left.position()
|
|
|
|
|
right_pos := expr.right.position()
|
2020-04-17 02:38:39 +02:00
|
|
|
|
return token.Position{
|
2020-06-19 11:46:08 +02:00
|
|
|
|
line_nr: expr.pos.line_nr
|
2020-04-17 02:38:39 +02:00
|
|
|
|
pos: left_pos.pos
|
|
|
|
|
len: right_pos.pos - left_pos.pos + right_pos.len
|
2021-03-23 06:23:46 +01:00
|
|
|
|
col: left_pos.col
|
2021-01-19 14:49:40 +01:00
|
|
|
|
last_line: right_pos.last_line
|
2020-04-17 02:38:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-04 12:34:12 +01:00
|
|
|
|
CTempVar {
|
2020-04-22 20:20:49 +02:00
|
|
|
|
return token.Position{}
|
|
|
|
|
}
|
2020-11-04 12:34:12 +01:00
|
|
|
|
// Please, do NOT use else{} here.
|
|
|
|
|
// This match is exhaustive *on purpose*, to help force
|
|
|
|
|
// maintaining/implementing proper .pos fields.
|
2020-04-17 02:38:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-29 12:31:18 +02:00
|
|
|
|
|
2020-08-17 21:10:47 +02:00
|
|
|
|
pub fn (expr Expr) is_lvalue() bool {
|
2020-11-25 12:09:40 +01:00
|
|
|
|
match expr {
|
2020-09-05 12:00:35 +02:00
|
|
|
|
Ident { return true }
|
2020-10-21 21:58:40 +02:00
|
|
|
|
CTempVar { return true }
|
2020-09-05 12:00:35 +02:00
|
|
|
|
IndexExpr { return expr.left.is_lvalue() }
|
|
|
|
|
SelectorExpr { return expr.expr.is_lvalue() }
|
2021-02-04 08:14:43 +01:00
|
|
|
|
ParExpr { return expr.expr.is_lvalue() } // for var := &{...(*pointer_var)}
|
|
|
|
|
PrefixExpr { return expr.right.is_lvalue() }
|
2020-08-17 21:10:47 +02:00
|
|
|
|
else {}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-19 18:18:36 +02:00
|
|
|
|
pub fn (expr Expr) is_expr() bool {
|
2020-11-25 12:09:40 +01:00
|
|
|
|
match expr {
|
2020-09-22 05:28:29 +02:00
|
|
|
|
IfExpr { return expr.is_expr }
|
2021-02-28 23:21:03 +01:00
|
|
|
|
LockExpr { return expr.is_expr }
|
2020-09-22 05:28:29 +02:00
|
|
|
|
MatchExpr { return expr.is_expr }
|
2021-02-28 23:21:03 +01:00
|
|
|
|
SelectExpr { return expr.is_expr }
|
2020-09-19 18:18:36 +02:00
|
|
|
|
else {}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 04:46:06 +01:00
|
|
|
|
pub fn (expr Expr) is_lit() bool {
|
|
|
|
|
return match expr {
|
|
|
|
|
BoolLiteral, StringLiteral, IntegerLiteral { true }
|
|
|
|
|
else { false }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 11:15:36 +01:00
|
|
|
|
pub fn (expr Expr) is_auto_deref_var() bool {
|
2021-02-20 19:50:43 +01:00
|
|
|
|
match expr {
|
|
|
|
|
Ident {
|
|
|
|
|
if expr.obj is Var {
|
|
|
|
|
if expr.obj.is_auto_deref {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
PrefixExpr {
|
2021-02-21 11:15:36 +01:00
|
|
|
|
if expr.op == .amp && expr.right.is_auto_deref_var() {
|
2021-02-20 19:50:43 +01:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 16:06:22 +02:00
|
|
|
|
// check if stmt can be an expression in C
|
2020-10-14 13:23:58 +02:00
|
|
|
|
pub fn (stmt Stmt) check_c_expr() ? {
|
2020-11-25 12:09:40 +01:00
|
|
|
|
match stmt {
|
2020-10-14 13:23:58 +02:00
|
|
|
|
AssignStmt {
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-09-30 16:06:22 +02:00
|
|
|
|
ExprStmt {
|
2020-10-14 13:23:58 +02:00
|
|
|
|
if stmt.expr.is_expr() {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-01-07 20:32:02 +01:00
|
|
|
|
return error('unsupported statement (`$stmt.expr.type_name()`)')
|
2020-09-30 16:06:22 +02:00
|
|
|
|
}
|
|
|
|
|
else {}
|
|
|
|
|
}
|
2021-01-07 20:32:02 +01:00
|
|
|
|
return error('unsupported statement (`$stmt.type_name()`)')
|
2020-09-30 16:06:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 21:58:40 +02:00
|
|
|
|
// CTempVar is used in cgen only, to hold nodes for temporary variables
|
|
|
|
|
pub struct CTempVar {
|
|
|
|
|
pub:
|
2021-04-02 00:57:09 +02:00
|
|
|
|
name string // the name of the C temporary variable; used by g.expr(x)
|
|
|
|
|
orig Expr // the original expression, which produced the C temp variable; used by x.str()
|
|
|
|
|
typ Type // the type of the original expression
|
|
|
|
|
is_ptr bool // whether the type is a pointer
|
2020-10-21 21:58:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-09 05:36:38 +01:00
|
|
|
|
pub fn (node Node) position() token.Position {
|
|
|
|
|
match node {
|
2021-03-31 10:13:15 +02:00
|
|
|
|
NodeError {
|
|
|
|
|
return token.Position{}
|
|
|
|
|
}
|
2021-04-02 00:57:09 +02:00
|
|
|
|
EmptyNode {
|
|
|
|
|
return token.Position{}
|
|
|
|
|
}
|
2021-01-09 05:36:38 +01:00
|
|
|
|
Stmt {
|
2021-03-11 13:50:02 +01:00
|
|
|
|
mut pos := node.pos
|
2021-01-09 05:36:38 +01:00
|
|
|
|
if node is Import {
|
|
|
|
|
for sym in node.syms {
|
|
|
|
|
pos = pos.extend(sym.pos)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return pos
|
|
|
|
|
}
|
|
|
|
|
Expr {
|
|
|
|
|
return node.position()
|
|
|
|
|
}
|
|
|
|
|
StructField {
|
|
|
|
|
return node.pos.extend(node.type_pos)
|
|
|
|
|
}
|
2021-04-02 00:57:09 +02:00
|
|
|
|
MatchBranch, SelectBranch, EnumField, ConstField, StructInitField, GlobalField, Param {
|
2021-01-09 05:36:38 +01:00
|
|
|
|
return node.pos
|
|
|
|
|
}
|
|
|
|
|
IfBranch {
|
|
|
|
|
return node.pos.extend(node.body_pos)
|
|
|
|
|
}
|
|
|
|
|
ScopeObject {
|
|
|
|
|
match node {
|
2021-03-17 01:43:17 +01:00
|
|
|
|
ConstField, GlobalField, Var {
|
|
|
|
|
return node.pos
|
|
|
|
|
}
|
|
|
|
|
AsmRegister {
|
|
|
|
|
return token.Position{
|
|
|
|
|
len: -1
|
|
|
|
|
line_nr: -1
|
|
|
|
|
pos: -1
|
|
|
|
|
last_line: -1
|
2021-03-23 06:23:46 +01:00
|
|
|
|
col: -1
|
2021-03-17 01:43:17 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-09 05:36:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
File {
|
|
|
|
|
mut pos := token.Position{}
|
|
|
|
|
if node.stmts.len > 0 {
|
2021-03-11 13:50:02 +01:00
|
|
|
|
first_pos := node.stmts.first().pos
|
|
|
|
|
last_pos := node.stmts.last().pos
|
2021-01-09 05:36:38 +01:00
|
|
|
|
pos = first_pos.extend_with_last_line(last_pos, last_pos.line_nr)
|
|
|
|
|
}
|
|
|
|
|
return pos
|
|
|
|
|
}
|
2021-03-30 09:43:17 +02:00
|
|
|
|
CallArg {
|
|
|
|
|
return node.pos
|
|
|
|
|
}
|
2021-01-09 05:36:38 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn (node Node) children() []Node {
|
|
|
|
|
mut children := []Node{}
|
|
|
|
|
if node is Expr {
|
|
|
|
|
match node {
|
|
|
|
|
StringInterLiteral, Assoc, ArrayInit {
|
|
|
|
|
return node.exprs.map(Node(it))
|
|
|
|
|
}
|
2021-01-19 06:16:42 +01:00
|
|
|
|
SelectorExpr, PostfixExpr, UnsafeExpr, AsCast, ParExpr, IfGuardExpr, SizeOf, Likely,
|
|
|
|
|
TypeOf, ArrayDecompose {
|
2021-01-09 05:36:38 +01:00
|
|
|
|
children << node.expr
|
|
|
|
|
}
|
|
|
|
|
LockExpr, OrExpr {
|
|
|
|
|
return node.stmts.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
StructInit {
|
|
|
|
|
return node.fields.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
AnonFn {
|
|
|
|
|
children << Stmt(node.decl)
|
|
|
|
|
}
|
|
|
|
|
CallExpr {
|
|
|
|
|
children << node.left
|
2021-03-30 09:43:17 +02:00
|
|
|
|
children << node.args.map(Node(it))
|
2021-01-09 05:36:38 +01:00
|
|
|
|
children << Expr(node.or_block)
|
|
|
|
|
}
|
|
|
|
|
InfixExpr {
|
|
|
|
|
children << node.left
|
|
|
|
|
children << node.right
|
|
|
|
|
}
|
|
|
|
|
PrefixExpr {
|
|
|
|
|
children << node.right
|
|
|
|
|
}
|
|
|
|
|
IndexExpr {
|
|
|
|
|
children << node.left
|
|
|
|
|
children << node.index
|
|
|
|
|
}
|
|
|
|
|
IfExpr {
|
|
|
|
|
children << node.left
|
|
|
|
|
children << node.branches.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
MatchExpr {
|
|
|
|
|
children << node.cond
|
|
|
|
|
children << node.branches.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
SelectExpr {
|
|
|
|
|
return node.branches.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
ChanInit {
|
|
|
|
|
children << node.cap_expr
|
|
|
|
|
}
|
|
|
|
|
MapInit {
|
|
|
|
|
children << node.keys.map(Node(it))
|
|
|
|
|
children << node.vals.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
RangeExpr {
|
|
|
|
|
children << node.low
|
|
|
|
|
children << node.high
|
|
|
|
|
}
|
|
|
|
|
CastExpr {
|
|
|
|
|
children << node.expr
|
|
|
|
|
children << node.arg
|
|
|
|
|
}
|
|
|
|
|
ConcatExpr {
|
|
|
|
|
return node.vals.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
ComptimeCall, ComptimeSelector {
|
|
|
|
|
children << node.left
|
|
|
|
|
}
|
|
|
|
|
else {}
|
|
|
|
|
}
|
|
|
|
|
} else if node is Stmt {
|
|
|
|
|
match node {
|
|
|
|
|
Block, DeferStmt, ForCStmt, ForInStmt, ForStmt, CompFor {
|
|
|
|
|
return node.stmts.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
ExprStmt, AssertStmt {
|
|
|
|
|
children << node.expr
|
|
|
|
|
}
|
|
|
|
|
InterfaceDecl {
|
|
|
|
|
return node.methods.map(Node(Stmt(it)))
|
|
|
|
|
}
|
|
|
|
|
AssignStmt {
|
|
|
|
|
children << node.left.map(Node(it))
|
|
|
|
|
children << node.right.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
Return {
|
|
|
|
|
return node.exprs.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
// NB: these four decl nodes cannot be merged as one branch
|
|
|
|
|
StructDecl {
|
|
|
|
|
return node.fields.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
GlobalDecl {
|
|
|
|
|
return node.fields.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
ConstDecl {
|
|
|
|
|
return node.fields.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
EnumDecl {
|
|
|
|
|
return node.fields.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
FnDecl {
|
|
|
|
|
if node.is_method {
|
|
|
|
|
children << Node(node.receiver)
|
|
|
|
|
}
|
|
|
|
|
children << node.params.map(Node(it))
|
|
|
|
|
children << node.stmts.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
else {}
|
|
|
|
|
}
|
|
|
|
|
} else if node is ScopeObject {
|
|
|
|
|
match node {
|
|
|
|
|
GlobalField, ConstField, Var { children << node.expr }
|
2021-03-17 01:43:17 +01:00
|
|
|
|
AsmRegister {}
|
2021-01-09 05:36:38 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
match node {
|
2021-04-02 16:26:37 +02:00
|
|
|
|
GlobalField, ConstField, EnumField, StructInitField, CallArg {
|
2021-01-09 05:36:38 +01:00
|
|
|
|
children << node.expr
|
|
|
|
|
}
|
|
|
|
|
SelectBranch {
|
|
|
|
|
children << node.stmt
|
|
|
|
|
children << node.stmts.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
IfBranch, File {
|
|
|
|
|
return node.stmts.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
MatchBranch {
|
|
|
|
|
children << node.stmts.map(Node(it))
|
|
|
|
|
children << node.exprs.map(Node(it))
|
|
|
|
|
}
|
|
|
|
|
else {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return children
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 14:08:01 +01:00
|
|
|
|
// helper for dealing with `m[k1][k2][k3][k3] = value`
|
|
|
|
|
pub fn (mut lx IndexExpr) recursive_mapset_is_setter(val bool) {
|
|
|
|
|
lx.is_setter = val
|
|
|
|
|
if mut lx.left is IndexExpr {
|
|
|
|
|
if lx.left.is_map {
|
|
|
|
|
lx.left.recursive_mapset_is_setter(val)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-17 01:43:17 +01:00
|
|
|
|
|
|
|
|
|
// return all the registers for a give architecture
|
2021-04-02 00:57:09 +02:00
|
|
|
|
pub fn all_registers(mut t Table, arch pref.Arch) map[string]ScopeObject {
|
2021-03-17 01:43:17 +01:00
|
|
|
|
mut res := map[string]ScopeObject{}
|
|
|
|
|
match arch {
|
|
|
|
|
.amd64, .i386 {
|
|
|
|
|
for bit_size, array in ast.x86_no_number_register_list {
|
|
|
|
|
for name in array {
|
|
|
|
|
res[name] = AsmRegister{
|
|
|
|
|
name: name
|
|
|
|
|
typ: t.bitsize_to_type(bit_size)
|
|
|
|
|
size: bit_size
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for bit_size, array in ast.x86_with_number_register_list {
|
|
|
|
|
for name, max_num in array {
|
|
|
|
|
for i in 0 .. max_num {
|
|
|
|
|
hash_index := name.index('#') or {
|
2021-04-02 00:57:09 +02:00
|
|
|
|
panic('all_registers: no hashtag found')
|
2021-03-17 01:43:17 +01:00
|
|
|
|
}
|
|
|
|
|
assembled_name := '${name[..hash_index]}$i${name[hash_index + 1..]}'
|
|
|
|
|
res[assembled_name] = AsmRegister{
|
|
|
|
|
name: assembled_name
|
|
|
|
|
typ: t.bitsize_to_type(bit_size)
|
|
|
|
|
size: bit_size
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.aarch32 {
|
|
|
|
|
aarch32 := gen_all_registers(mut t, ast.arm_no_number_register_list, ast.arm_with_number_register_list,
|
|
|
|
|
32)
|
|
|
|
|
for k, v in aarch32 {
|
|
|
|
|
res[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.aarch64 {
|
|
|
|
|
aarch64 := gen_all_registers(mut t, ast.arm_no_number_register_list, ast.arm_with_number_register_list,
|
|
|
|
|
64)
|
|
|
|
|
for k, v in aarch64 {
|
|
|
|
|
res[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.rv32 {
|
|
|
|
|
rv32 := gen_all_registers(mut t, ast.riscv_no_number_register_list, ast.riscv_with_number_register_list,
|
|
|
|
|
32)
|
|
|
|
|
for k, v in rv32 {
|
|
|
|
|
res[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.rv64 {
|
|
|
|
|
rv64 := gen_all_registers(mut t, ast.riscv_no_number_register_list, ast.riscv_with_number_register_list,
|
|
|
|
|
64)
|
|
|
|
|
for k, v in rv64 {
|
|
|
|
|
res[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else { // TODO
|
2021-04-02 00:57:09 +02:00
|
|
|
|
panic('all_registers: unhandled arch')
|
2021-03-17 01:43:17 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// only for arm and riscv because x86 has different sized registers
|
2021-04-02 00:57:09 +02:00
|
|
|
|
fn gen_all_registers(mut t Table, without_numbers []string, with_numbers map[string]int, bit_size int) map[string]ScopeObject {
|
2021-03-17 01:43:17 +01:00
|
|
|
|
mut res := map[string]ScopeObject{}
|
|
|
|
|
for name in without_numbers {
|
|
|
|
|
res[name] = AsmRegister{
|
|
|
|
|
name: name
|
|
|
|
|
typ: t.bitsize_to_type(bit_size)
|
|
|
|
|
size: bit_size
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for name, max_num in with_numbers {
|
|
|
|
|
for i in 0 .. max_num {
|
2021-04-02 00:57:09 +02:00
|
|
|
|
hash_index := name.index('#') or { panic('all_registers: no hashtag found') }
|
2021-03-17 01:43:17 +01:00
|
|
|
|
assembled_name := '${name[..hash_index]}$i${name[hash_index + 1..]}'
|
|
|
|
|
res[assembled_name] = AsmRegister{
|
|
|
|
|
name: assembled_name
|
|
|
|
|
typ: t.bitsize_to_type(bit_size)
|
|
|
|
|
size: bit_size
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|