2020-01-23 21:04:46 +01:00
|
|
|
// Copyright (c) 2019-2020 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
|
|
|
|
import v.table
|
2020-05-10 11:26:57 +02:00
|
|
|
import v.errors
|
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-11-25 12:09:40 +01:00
|
|
|
pub type Expr = AnonFn | ArrayInit | AsCast | Assoc | AtExpr | BoolLiteral | CTempVar |
|
2020-11-05 09:12:32 +01:00
|
|
|
CallExpr | CastExpr | ChanInit | CharLiteral | Comment | ComptimeCall | ConcatExpr | EnumVal |
|
|
|
|
FloatLiteral | Ident | IfExpr | IfGuardExpr | IndexExpr | InfixExpr | IntegerLiteral |
|
|
|
|
Likely | LockExpr | MapInit | MatchExpr | None | OrExpr | ParExpr | PostfixExpr | PrefixExpr |
|
|
|
|
RangeExpr | SelectExpr | SelectorExpr | SizeOf | SqlExpr | StringInterLiteral | StringLiteral |
|
|
|
|
StructInit | Type | TypeOf | UnsafeExpr
|
2020-05-18 18:08:28 +02:00
|
|
|
|
2020-11-25 12:09:40 +01:00
|
|
|
pub type Stmt = AssertStmt | AssignStmt | Block | BranchStmt | CompFor | ConstDecl | DeferStmt |
|
|
|
|
EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt | ForStmt | GlobalDecl | GoStmt |
|
|
|
|
GotoLabel | GotoStmt | HashStmt | Import | InterfaceDecl | Module | 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.
|
2020-11-25 12:09:40 +01:00
|
|
|
pub type ScopeObject = ConstField | GlobalField | Var
|
2020-04-04 05:14:40 +02:00
|
|
|
|
2020-03-03 15:06:21 +01:00
|
|
|
pub struct Type {
|
2020-03-02 10:53:38 +01:00
|
|
|
pub:
|
2020-03-04 15:48:43 +01:00
|
|
|
typ table.Type
|
2020-04-18 00:19:33 +02:00
|
|
|
pos token.Position
|
2020-03-02 10:53:38 +01:00
|
|
|
}
|
|
|
|
|
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:
|
2020-07-17 19:13:22 +02:00
|
|
|
typ table.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
|
|
|
|
language table.Language
|
|
|
|
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:
|
2020-03-21 07:01:06 +01:00
|
|
|
expr_types []table.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:
|
2020-05-18 18:06:09 +02:00
|
|
|
pos token.Position
|
2020-08-25 08:48:28 +02:00
|
|
|
expr Expr // expr.field_name
|
2020-05-18 18:06:09 +02:00
|
|
|
field_name string
|
2020-11-27 03:08:42 +01:00
|
|
|
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
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-06-25 22:23:19 +02:00
|
|
|
expr_type table.Type // type of `Foo` in `Foo.bar`
|
2020-06-27 16:19:12 +02:00
|
|
|
typ table.Type // type of the entire thing (`Foo.bar`)
|
2020-11-09 14:35:26 +01:00
|
|
|
name_type table.Type // T in `T.name` or typeof in `typeof(expr).name`
|
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 {
|
|
|
|
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:
|
2020-04-19 00:07:57 +02:00
|
|
|
name string
|
|
|
|
path string
|
|
|
|
expr Expr
|
2020-05-16 16:12:23 +02:00
|
|
|
pos token.Position
|
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
|
2020-08-04 20:10:22 +02:00
|
|
|
attrs []table.Attr
|
2020-05-18 18:06:09 +02:00
|
|
|
is_public bool
|
2020-10-30 07:09:26 +01:00
|
|
|
is_embed bool
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-10-30 07:09:26 +01:00
|
|
|
name string
|
2020-04-12 12:35:54 +02:00
|
|
|
typ table.Type
|
2020-04-05 02:08:10 +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:
|
2020-02-10 08:32:08 +01:00
|
|
|
typ table.Type
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
|
2020-04-04 05:14:40 +02:00
|
|
|
pub struct ConstField {
|
2020-02-03 07:02:54 +01:00
|
|
|
pub:
|
2020-07-08 13:19:58 +02:00
|
|
|
mod string
|
2020-06-23 18:01:56 +02:00
|
|
|
name string
|
|
|
|
expr Expr
|
|
|
|
is_pub bool
|
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-06-23 18:01:56 +02:00
|
|
|
typ table.Type
|
|
|
|
comments []Comment
|
2020-04-04 05:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ConstDecl {
|
|
|
|
pub:
|
2020-09-05 12:00:35 +02:00
|
|
|
is_pub bool
|
|
|
|
pos token.Position
|
2020-04-27 22:53:26 +02:00
|
|
|
pub mut:
|
2020-09-05 12:00:35 +02:00
|
|
|
fields []ConstField
|
2020-08-27 14:07:49 +02:00
|
|
|
end_comments []Comment
|
2019-12-30 06:16:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StructDecl {
|
|
|
|
pub:
|
2020-06-23 18:01:56 +02:00
|
|
|
pos token.Position
|
|
|
|
name string
|
2020-11-11 16:48:42 +01:00
|
|
|
gen_types []table.Type
|
2020-06-23 18:01:56 +02:00
|
|
|
is_pub bool
|
|
|
|
mut_pos int // mut:
|
|
|
|
pub_pos int // pub:
|
|
|
|
pub_mut_pos int // pub mut:
|
|
|
|
language table.Language
|
|
|
|
is_union bool
|
2020-08-04 20:10:22 +02:00
|
|
|
attrs []table.Attr
|
2020-06-23 18:01:56 +02:00
|
|
|
end_comments []Comment
|
2020-10-30 07:09:26 +01:00
|
|
|
pub mut:
|
|
|
|
fields []StructField
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StructEmbedding {
|
|
|
|
pub:
|
|
|
|
name string
|
|
|
|
typ table.Type
|
|
|
|
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
|
|
|
|
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
|
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
|
2020-04-17 02:38:39 +02:00
|
|
|
typ table.Type
|
|
|
|
expected_type table.Type
|
|
|
|
}
|
|
|
|
|
2019-12-30 06:16:59 +01:00
|
|
|
pub struct StructInit {
|
|
|
|
pub:
|
2020-11-05 17:49:52 +01:00
|
|
|
pos token.Position
|
|
|
|
is_short bool
|
|
|
|
pre_comments []Comment
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-11-05 17:49:52 +01:00
|
|
|
typ table.Type
|
|
|
|
fields []StructInitField
|
2019-12-30 06:16:59 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 09:15:32 +01:00
|
|
|
// import statement
|
|
|
|
pub struct Import {
|
|
|
|
pub:
|
2020-01-18 23:26:14 +01:00
|
|
|
pos token.Position
|
|
|
|
mod string
|
|
|
|
alias string
|
2020-07-18 21:34:38 +02:00
|
|
|
pub mut:
|
|
|
|
syms []ImportSymbol
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-17 21:59:19 +02:00
|
|
|
pub struct AnonFn {
|
|
|
|
pub:
|
2020-07-14 18:52:28 +02:00
|
|
|
decl FnDecl
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-07-14 18:52:28 +02:00
|
|
|
typ table.Type
|
2020-04-17 21:59:19 +02:00
|
|
|
}
|
|
|
|
|
2019-12-27 13:57:49 +01:00
|
|
|
pub struct FnDecl {
|
|
|
|
pub:
|
2020-03-05 16:13:14 +01:00
|
|
|
name string
|
2020-06-11 20:02:27 +02:00
|
|
|
mod string
|
2020-09-27 03:32:56 +02:00
|
|
|
params []table.Param
|
2020-03-05 16:13:14 +01:00
|
|
|
is_deprecated bool
|
|
|
|
is_pub bool
|
|
|
|
is_variadic bool
|
2020-04-17 21:59:19 +02:00
|
|
|
is_anon bool
|
2020-03-05 16:13:14 +01:00
|
|
|
receiver Field
|
2020-05-07 10:14:51 +02:00
|
|
|
receiver_pos token.Position
|
2020-03-05 16:13:14 +01:00
|
|
|
is_method bool
|
2020-11-08 09:14:24 +01:00
|
|
|
method_idx int
|
2020-03-05 16:13:14 +01:00
|
|
|
rec_mut bool // is receiver mutable
|
2020-07-04 12:44:25 +02:00
|
|
|
rec_share table.ShareType
|
2020-05-19 17:12:47 +02:00
|
|
|
language table.Language
|
2020-03-06 16:31:40 +01:00
|
|
|
no_body bool // just a definition `fn C.malloc()`
|
2020-04-10 18:11:43 +02:00
|
|
|
is_builtin bool // this function is defined in builtin/strconv
|
2020-03-21 19:52:19 +01:00
|
|
|
pos token.Position
|
2020-05-04 12:31:34 +02:00
|
|
|
body_pos token.Position
|
2020-05-03 21:13:40 +02:00
|
|
|
file string
|
2020-05-21 03:58:50 +02:00
|
|
|
is_generic bool
|
2020-08-24 09:04:50 +02:00
|
|
|
is_direct_arr bool // direct array access
|
2020-08-04 20:10:22 +02:00
|
|
|
attrs []table.Attr
|
2020-04-27 22:53:26 +02:00
|
|
|
pub mut:
|
2020-07-08 16:01:17 +02:00
|
|
|
stmts []Stmt
|
2020-04-27 22:53:26 +02:00
|
|
|
return_type table.Type
|
2020-11-05 17:23:45 +01:00
|
|
|
comments []Comment // comments *after* the header, but *before* `{`; used for InterfaceDecl
|
2020-11-08 09:14:24 +01:00
|
|
|
source_file &File = 0
|
2019-12-29 07:24:17 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-29 07:24:17 +01:00
|
|
|
pub struct CallExpr {
|
|
|
|
pub:
|
2020-04-09 15:33:46 +02:00
|
|
|
pos token.Position
|
|
|
|
left Expr // `user` in `user.register()`
|
|
|
|
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
|
2020-04-03 15:18:17 +02:00
|
|
|
expected_arg_types []table.Type
|
2020-05-19 17:12:47 +02:00
|
|
|
language table.Language
|
2020-04-09 15:33:46 +02:00
|
|
|
or_block OrExpr
|
|
|
|
left_type table.Type // type of `user`
|
|
|
|
receiver_type table.Type // User
|
|
|
|
return_type table.Type
|
2020-04-25 15:57:11 +02:00
|
|
|
should_be_skipped bool
|
2020-05-21 03:58:50 +02:00
|
|
|
generic_type table.Type // TODO array, to support multiple types
|
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-03-14 11:11:43 +01:00
|
|
|
}
|
|
|
|
|
2020-09-05 12:00:35 +02:00
|
|
|
/*
|
|
|
|
pub struct AutofreeArgVar {
|
|
|
|
name string
|
|
|
|
idx int
|
|
|
|
}
|
|
|
|
*/
|
2020-03-14 11:11:43 +01:00
|
|
|
pub struct CallArg {
|
|
|
|
pub:
|
2020-09-05 12:00:35 +02:00
|
|
|
is_mut bool
|
|
|
|
share table.ShareType
|
|
|
|
expr Expr
|
|
|
|
comments []Comment
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-09-05 12:00:35 +02:00
|
|
|
typ table.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
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2020-07-17 19:13:22 +02:00
|
|
|
types []table.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
|
|
|
|
share table.ShareType
|
|
|
|
is_mut bool
|
|
|
|
is_autofree_tmp bool
|
|
|
|
is_arg bool // fn args should not be autofreed
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-10-29 01:09:38 +01:00
|
|
|
typ table.Type
|
2020-11-23 16:16:13 +01:00
|
|
|
sum_type_casts []table.Type // nested sum types require nested smart casting, for that a list of types is needed
|
2020-10-29 01:09:38 +01:00
|
|
|
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)
|
2020-12-06 08:38:21 +01:00
|
|
|
is_or bool // `x := foo() or { ... }`
|
2020-12-06 08:19:14 +01:00
|
|
|
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:
|
2020-11-23 16:16:13 +01:00
|
|
|
struct_type table.Type // type of struct
|
|
|
|
name string
|
|
|
|
pos token.Position
|
|
|
|
typ table.Type
|
|
|
|
sum_type_casts []table.Type // nested sum types require nested smart casting, for that a list of types is needed
|
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:
|
2020-04-09 15:33:46 +02:00
|
|
|
typ table.Type
|
2020-10-03 07:03:44 +02:00
|
|
|
comments []Comment
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct GlobalDecl {
|
|
|
|
pub:
|
|
|
|
pos token.Position
|
|
|
|
pub mut:
|
|
|
|
fields []GlobalField
|
|
|
|
end_comments []Comment
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 12:10:46 +01:00
|
|
|
pub struct File {
|
2019-12-24 18:54:43 +01:00
|
|
|
pub:
|
2020-12-07 18:13:03 +01:00
|
|
|
path string
|
|
|
|
mod Module
|
|
|
|
global_scope &Scope
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-12-07 18:13:03 +01:00
|
|
|
scope &Scope
|
|
|
|
stmts []Stmt
|
|
|
|
imports []Import
|
|
|
|
imported_symbols map[string]string // 'Type' => 'module.Type'
|
|
|
|
errors []errors.Error
|
|
|
|
warnings []errors.Warning
|
|
|
|
generic_fns []&FnDecl
|
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:
|
2020-03-11 16:10:46 +01:00
|
|
|
typ table.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:
|
2020-03-13 05:57:51 +01:00
|
|
|
typ table.Type
|
|
|
|
is_mut bool
|
|
|
|
is_static bool
|
|
|
|
is_optional bool
|
2020-07-04 12:44:25 +02:00
|
|
|
share table.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:
|
2020-05-19 17:12:47 +02:00
|
|
|
language table.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:
|
2020-07-22 18:10:31 +02:00
|
|
|
obj ScopeObject
|
2020-07-01 00:53:53 +02:00
|
|
|
mod string
|
2020-03-06 00:10:01 +01:00
|
|
|
name string
|
2020-01-18 23:26:14 +01:00
|
|
|
kind IdentKind
|
|
|
|
info IdentInfo
|
2020-04-27 22:53:26 +02:00
|
|
|
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:
|
2020-07-14 18:52:28 +02:00
|
|
|
op token.Kind
|
|
|
|
pos token.Position
|
2020-10-21 21:58:40 +02:00
|
|
|
pub mut:
|
2020-07-14 18:52:28 +02:00
|
|
|
left Expr
|
|
|
|
right Expr
|
|
|
|
left_type table.Type
|
|
|
|
right_type table.Type
|
|
|
|
auto_locked string
|
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:
|
2020-07-14 18:52:28 +02: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:
|
2020-08-17 23:35:36 +02:00
|
|
|
op token.Kind
|
|
|
|
right Expr
|
|
|
|
pos token.Position
|
|
|
|
pub mut:
|
|
|
|
right_type table.Type
|
2020-08-23 02:12:05 +02:00
|
|
|
or_block OrExpr
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
2020-01-07 12:14:10 +01:00
|
|
|
pub struct IndexExpr {
|
|
|
|
pub:
|
2020-04-09 15:33:46 +02:00
|
|
|
pos token.Position
|
|
|
|
left Expr
|
2020-10-05 17:40:51 +02:00
|
|
|
index Expr // [0], RangeExpr [start..end] or map[key]
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-04-03 10:41:01 +02:00
|
|
|
left_type table.Type // array, map, fixed array
|
2020-04-09 15:33:46 +02:00
|
|
|
is_setter bool
|
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:
|
2020-07-20 16:48:33 +02:00
|
|
|
branches []IfBranch // includes all `else if` branches
|
|
|
|
is_expr bool
|
|
|
|
typ table.Type
|
|
|
|
has_else bool
|
2020-03-20 14:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct IfBranch {
|
|
|
|
pub:
|
2020-11-27 03:08:42 +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-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
|
|
|
|
is_rlock bool
|
|
|
|
pos token.Position
|
|
|
|
pub mut:
|
|
|
|
lockeds []Ident // `x`, `y` in `lock x, y {`
|
|
|
|
is_expr bool
|
|
|
|
typ table.Type
|
|
|
|
}
|
|
|
|
|
2020-02-05 10:00:11 +01:00
|
|
|
pub struct MatchExpr {
|
|
|
|
pub:
|
2020-11-25 12:09:40 +01:00
|
|
|
tok_kind token.Kind
|
|
|
|
cond Expr
|
|
|
|
branches []MatchBranch
|
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-11-25 12:09:40 +01:00
|
|
|
is_expr bool // returns a value
|
|
|
|
return_type table.Type
|
|
|
|
cond_type table.Type // type of `x` in `match x {`
|
|
|
|
expected_type table.Type // for debugging only
|
|
|
|
is_sum_type bool
|
2020-02-05 10:00:11 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 11:59:45 +01:00
|
|
|
pub struct MatchBranch {
|
|
|
|
pub:
|
2020-07-03 15:10:39 +02:00
|
|
|
exprs []Expr // left side
|
2020-11-11 21:53:21 +01:00
|
|
|
ecmnts [][]Comment // inline comments for each left side expr
|
2020-07-03 15:10:39 +02:00
|
|
|
stmts []Stmt // right side
|
|
|
|
pos token.Position
|
2020-10-14 21:48:49 +02:00
|
|
|
comments []Comment // comment above `xxx {`
|
2020-07-03 15:10:39 +02:00
|
|
|
is_else bool
|
2020-07-02 15:44:03 +02:00
|
|
|
post_comments []Comment
|
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:
|
|
|
|
is_expr bool // returns a value
|
|
|
|
expected_type table.Type // for debugging only
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SelectBranch {
|
|
|
|
pub:
|
|
|
|
stmt Stmt // `a := <-ch` or `ch <- a`
|
|
|
|
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
|
2020-07-03 15:10:39 +02:00
|
|
|
pub mut:
|
|
|
|
// expr Expr
|
|
|
|
typ table.Type
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
label string // `label: for {`
|
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:
|
2020-10-14 13:23:58 +02:00
|
|
|
key_type table.Type
|
|
|
|
val_type table.Type
|
|
|
|
cond_type table.Type
|
|
|
|
kind table.Kind // array/map/string
|
2020-11-20 12:23:48 +01:00
|
|
|
label string // `label: for {`
|
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
|
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:
|
|
|
|
label string // `label: for {`
|
2019-12-30 06:16:59 +01:00
|
|
|
}
|
|
|
|
|
2020-02-04 09:54:15 +01:00
|
|
|
// #include etc
|
|
|
|
pub struct HashStmt {
|
|
|
|
pub:
|
2020-10-15 22:12:59 +02:00
|
|
|
mod string
|
|
|
|
pos token.Position
|
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
|
|
|
*/
|
2019-12-28 19:16:04 +01:00
|
|
|
pub struct AssignStmt {
|
|
|
|
pub:
|
2020-05-27 03:20:22 +02:00
|
|
|
right []Expr
|
|
|
|
op token.Kind
|
|
|
|
pos token.Position
|
2020-07-17 19:13:22 +02:00
|
|
|
comments []Comment
|
2020-04-27 22:53:26 +02:00
|
|
|
pub mut:
|
2020-06-16 13:20:16 +02:00
|
|
|
left []Expr
|
2020-05-27 03:20:22 +02:00
|
|
|
left_types []table.Type
|
|
|
|
right_types []table.Type
|
|
|
|
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:
|
2020-03-18 13:55:46 +01:00
|
|
|
expr Expr
|
|
|
|
typ table.Type
|
2020-03-18 14:50:21 +01:00
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-03-18 13:55:46 +01:00
|
|
|
expr_type table.Type
|
2020-03-02 19:00:33 +01:00
|
|
|
}
|
|
|
|
|
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:
|
2020-03-15 02:51:31 +01:00
|
|
|
typ table.Type
|
2020-02-10 23:19:50 +01:00
|
|
|
}
|
2020-02-10 20:33:34 +01:00
|
|
|
|
2020-04-09 19:23:49 +02:00
|
|
|
pub struct EnumField {
|
2020-05-18 18:06:09 +02:00
|
|
|
pub:
|
2020-04-10 14:44:01 +02:00
|
|
|
name string
|
|
|
|
pos token.Position
|
2020-07-02 15:44:03 +02:00
|
|
|
comments []Comment
|
2020-04-10 14:44:01 +02:00
|
|
|
expr Expr
|
|
|
|
has_expr bool
|
2020-04-09 19:23:49 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
is_flag bool // true when the enum has [flag] tag
|
|
|
|
is_multi_allowed bool
|
|
|
|
comments []Comment // enum Abc { /* comments */ ... }
|
|
|
|
fields []EnumField
|
2020-10-03 20:33:02 +02:00
|
|
|
attrs []table.Attr
|
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
|
2020-03-07 17:37:55 +01:00
|
|
|
parent_type table.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
|
2020-11-11 09:18:15 +01:00
|
|
|
pub mut:
|
2020-11-28 22:40:40 +01:00
|
|
|
variants []SumTypeVariant
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SumTypeVariant {
|
|
|
|
pub:
|
|
|
|
typ table.Type
|
|
|
|
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
|
|
|
|
typ table.Type
|
|
|
|
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:
|
2020-03-27 07:21:22 +01:00
|
|
|
ifdef string
|
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:
|
2020-04-03 15:18:17 +02:00
|
|
|
call_expr Expr
|
2020-11-04 12:34:12 +01:00
|
|
|
pos token.Position
|
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:
|
2020-12-01 11:20:27 +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
|
|
|
|
is_fixed bool
|
|
|
|
has_val bool // fixed size literal `[expr, expr]!!`
|
|
|
|
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
|
|
|
|
pub mut:
|
|
|
|
expr_types []table.Type // [Dog, Cat] // also used for interface_types
|
|
|
|
is_interface bool // array of interfaces e.g. `[]Animal` `[Dog{}, Cat{}]`
|
|
|
|
interface_type table.Type // Animal
|
|
|
|
elem_type table.Type // element type
|
|
|
|
typ table.Type // array type
|
2019-12-30 09:38:12 +01:00
|
|
|
}
|
|
|
|
|
2020-08-14 21:18:42 +02:00
|
|
|
pub struct ChanInit {
|
|
|
|
pub:
|
2020-09-05 12:00:35 +02:00
|
|
|
pos token.Position
|
|
|
|
cap_expr Expr
|
|
|
|
has_cap bool
|
2020-08-14 21:18:42 +02:00
|
|
|
pub mut:
|
2020-09-05 12:00:35 +02:00
|
|
|
typ table.Type
|
|
|
|
elem_type table.Type
|
2020-08-14 21:18:42 +02:00
|
|
|
}
|
|
|
|
|
2020-02-22 14:13:19 +01:00
|
|
|
pub struct MapInit {
|
|
|
|
pub:
|
2020-03-07 16:23:10 +01:00
|
|
|
pos token.Position
|
|
|
|
keys []Expr
|
|
|
|
vals []Expr
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-03-07 16:23:10 +01:00
|
|
|
typ table.Type
|
2020-03-07 08:13:00 +01:00
|
|
|
key_type table.Type
|
|
|
|
value_type table.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
|
|
|
}
|
|
|
|
|
2020-08-10 18:05:26 +02:00
|
|
|
// NB: &string(x) gets parsed as ast.PrefixExpr{ right: ast.CastExpr{...} }
|
|
|
|
// TODO: that is very likely a parsing bug. It should get parsed as just
|
|
|
|
// ast.CastExpr{...}, where .typname is '&string' instead.
|
|
|
|
// 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:
|
2020-08-01 23:17:00 +02:00
|
|
|
expr Expr // `buf` in `string(buf, n)`
|
2020-03-11 19:00:51 +01:00
|
|
|
arg Expr // `n` in `string(buf, n)`
|
2020-06-25 14:55:53 +02:00
|
|
|
typ table.Type // `string` TODO rename to `type_to_cast_to`
|
2020-05-06 12:05:24 +02:00
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-04-27 22:53:26 +02:00
|
|
|
typname string
|
2020-03-11 19:00:51 +01:00
|
|
|
expr_type table.Type // `byteptr`
|
|
|
|
has_arg bool
|
2020-08-10 18:05:26 +02:00
|
|
|
in_prexpr bool // is the parent node an ast.PrefixExpr
|
2020-02-10 14:43:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-26 15:51:05 +01:00
|
|
|
pub struct AssertStmt {
|
|
|
|
pub:
|
2020-03-26 10:44:59 +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:
|
2020-03-17 16:40:41 +01:00
|
|
|
var_name string
|
|
|
|
expr Expr
|
2020-11-04 12:34:12 +01:00
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-03-17 16:40:41 +01:00
|
|
|
expr_type table.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
|
|
|
*/
|
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:
|
2020-03-19 09:52:33 +01:00
|
|
|
typ table.Type
|
2020-02-18 17:29:47 +01:00
|
|
|
}
|
|
|
|
|
2020-02-18 18:13:34 +01:00
|
|
|
pub struct SizeOf {
|
|
|
|
pub:
|
2020-07-01 00:53:53 +02:00
|
|
|
is_type bool
|
2020-03-18 08:41:49 +01:00
|
|
|
typ table.Type
|
2020-02-18 18:13:34 +01:00
|
|
|
type_name string
|
2020-07-01 00:53:53 +02:00
|
|
|
expr Expr
|
|
|
|
pos token.Position
|
2020-02-18 18:13:34 +01:00
|
|
|
}
|
|
|
|
|
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:
|
2020-03-31 19:43:11 +02:00
|
|
|
expr Expr
|
2020-11-04 12:34:12 +01:00
|
|
|
pos token.Position
|
2020-05-09 15:16:48 +02:00
|
|
|
pub mut:
|
2020-03-28 17:37:22 +01:00
|
|
|
expr_type table.Type
|
2020-03-19 12:15:39 +01:00
|
|
|
}
|
|
|
|
|
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:
|
2020-05-18 18:06:09 +02:00
|
|
|
vals []Expr
|
2020-11-04 12:34:12 +01:00
|
|
|
pos token.Position
|
2020-05-15 23:14:53 +02:00
|
|
|
pub mut:
|
|
|
|
return_type table.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:
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
|
2020-05-25 05:32:33 +02:00
|
|
|
pub struct ComptimeCall {
|
2020-06-06 10:05:26 +02:00
|
|
|
pub:
|
|
|
|
method_name string
|
|
|
|
left Expr
|
2020-06-06 21:36:24 +02:00
|
|
|
is_vweb bool
|
2020-06-07 12:26:45 +02:00
|
|
|
vweb_tmpl File
|
2020-07-03 15:10:39 +02:00
|
|
|
args_var string
|
2020-06-06 10:05:26 +02:00
|
|
|
pub mut:
|
|
|
|
sym table.TypeSymbol
|
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
|
|
|
|
foo int // todo
|
|
|
|
}
|
|
|
|
|
2020-06-19 16:43:32 +02:00
|
|
|
/*
|
|
|
|
pub enum SqlExprKind {
|
|
|
|
select_
|
|
|
|
insert
|
|
|
|
update
|
|
|
|
}
|
|
|
|
*/
|
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
|
|
|
|
db_expr Expr // `db` in `sql db {`
|
2020-06-19 16:43:32 +02:00
|
|
|
object_var_name string // `user`
|
2020-06-19 17:05:57 +02:00
|
|
|
table_type table.Type
|
2020-06-27 16:19:12 +02:00
|
|
|
pos token.Position
|
|
|
|
where_expr Expr
|
|
|
|
updated_columns []string // for `update set x=y`
|
|
|
|
update_exprs []Expr // for `update`
|
2020-06-25 14:43:07 +02:00
|
|
|
pub mut:
|
2020-06-27 16:19:12 +02:00
|
|
|
table_name string
|
|
|
|
fields []table.Field
|
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:
|
2020-06-27 16:19:12 +02:00
|
|
|
typ table.Type
|
|
|
|
is_count bool
|
|
|
|
db_expr Expr // `db` in `sql db {`
|
|
|
|
where_expr Expr
|
|
|
|
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
|
|
|
|
table_type table.Type
|
|
|
|
pos token.Position
|
|
|
|
has_limit bool
|
|
|
|
limit_expr Expr
|
2020-06-25 12:05:24 +02:00
|
|
|
pub mut:
|
2020-06-27 16:19:12 +02:00
|
|
|
table_name string
|
|
|
|
fields []table.Field
|
2020-02-19 19:54:36 +01:00
|
|
|
}
|
2020-03-18 08:41:49 +01:00
|
|
|
|
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
|
2020-11-25 12:09:40 +01:00
|
|
|
match expr {
|
2020-09-22 05:28:29 +02:00
|
|
|
// KEKW2
|
2020-07-07 17:10:39 +02:00
|
|
|
AnonFn {
|
|
|
|
return expr.decl.pos
|
|
|
|
}
|
2020-11-05 09:12:32 +01:00
|
|
|
ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, CastExpr, ChanInit, CharLiteral, ConcatExpr, Comment, EnumVal, FloatLiteral, Ident, IfExpr, IndexExpr, IntegerLiteral, Likely, LockExpr, MapInit, MatchExpr, None, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr, SelectExpr, SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit, Type, TypeOf, UnsafeExpr {
|
2020-06-19 11:46:08 +02:00
|
|
|
return expr.pos
|
2020-04-22 20:20:49 +02:00
|
|
|
}
|
2020-11-04 12:34:12 +01:00
|
|
|
IfGuardExpr {
|
|
|
|
return expr.expr.position()
|
|
|
|
}
|
|
|
|
ComptimeCall {
|
|
|
|
return expr.left.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
|
|
|
|
}
|
|
|
|
}
|
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() }
|
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 }
|
|
|
|
MatchExpr { return expr.is_expr }
|
2020-09-19 18:18:36 +02:00
|
|
|
else {}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2020-09-30 16:06:22 +02:00
|
|
|
return error('unsupported statement (`${typeof(stmt.expr)}`)')
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
return error('unsupported statement (`${typeof(stmt)}`)')
|
|
|
|
}
|
|
|
|
|
2020-10-21 21:58:40 +02:00
|
|
|
// CTempVar is used in cgen only, to hold nodes for temporary variables
|
|
|
|
pub struct CTempVar {
|
|
|
|
pub:
|
|
|
|
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 table.Type // the type of the original expression
|
|
|
|
is_ptr bool // whether the type is a pointer
|
|
|
|
}
|
|
|
|
|
2020-05-11 08:59:55 +02:00
|
|
|
pub fn (stmt Stmt) position() token.Position {
|
2020-11-25 12:09:40 +01:00
|
|
|
match stmt {
|
2020-11-04 12:34:12 +01:00
|
|
|
AssertStmt, AssignStmt, Block, BranchStmt, CompFor, ConstDecl, DeferStmt, EnumDecl, ExprStmt, FnDecl, ForCStmt, ForInStmt, ForStmt, GotoLabel, GotoStmt, Import, Return, StructDecl, GlobalDecl, HashStmt, InterfaceDecl, Module, SqlStmt { return stmt.pos }
|
2020-10-29 17:21:37 +01:00
|
|
|
GoStmt { return stmt.call_expr.position() }
|
2020-11-25 12:09:40 +01:00
|
|
|
TypeDecl { match stmt {
|
|
|
|
AliasTypeDecl, FnTypeDecl, SumTypeDecl { return stmt.pos }
|
2020-10-29 17:21:37 +01:00
|
|
|
} }
|
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-29 12:31:18 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-02 21:51:55 +02:00
|
|
|
|
|
|
|
// TODO: remove this fugly hack :-|
|
2020-05-03 21:13:40 +02:00
|
|
|
// fe2ex/1 and ex2fe/1 are used to convert back and forth from
|
2020-05-02 21:51:55 +02:00
|
|
|
// table.FExpr to ast.Expr , which in turn is needed to break
|
|
|
|
// a dependency cycle between v.ast and v.table, for the single
|
|
|
|
// field table.Field.default_expr, which should be ast.Expr
|
|
|
|
pub fn fe2ex(x table.FExpr) Expr {
|
|
|
|
res := Expr{}
|
2020-09-22 05:28:29 +02:00
|
|
|
unsafe {C.memcpy(&res, &x, sizeof(Expr))}
|
2020-05-02 21:51:55 +02:00
|
|
|
return res
|
|
|
|
}
|
2020-05-18 18:06:09 +02:00
|
|
|
|
2020-05-02 21:51:55 +02:00
|
|
|
pub fn ex2fe(x Expr) table.FExpr {
|
|
|
|
res := table.FExpr{}
|
2020-09-22 05:28:29 +02:00
|
|
|
unsafe {C.memcpy(&res, &x, sizeof(table.FExpr))}
|
2020-05-02 21:51:55 +02:00
|
|
|
return res
|
|
|
|
}
|