Files
llgo/chore/llcppg/ast/ast.go

334 lines
5.7 KiB
Go
Raw Normal View History

2024-08-06 11:00:13 +08:00
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ast
2024-08-20 08:58:43 +08:00
import "github.com/goplus/llgo/chore/llcppg/token"
2024-08-16 15:03:00 +08:00
2024-08-06 11:00:13 +08:00
// =============================================================================
type Node interface {
}
type Expr interface {
Node
exprNode()
}
type Decl interface {
Node
declNode()
}
type Stmt interface {
Node
stmtNode()
}
2024-08-08 11:21:56 +08:00
type PPD interface { // preprocessing directive
Node
ppdNode()
}
2024-08-06 11:00:13 +08:00
// =============================================================================
// Expressions (Types are also expressions)
2024-08-14 18:27:15 +08:00
type BasicLitKind uint
const (
IntLit BasicLitKind = iota
FloatLit
CharLit
StringLit
)
type BasicLit struct {
Kind BasicLitKind
Value string
}
func (*BasicLit) exprNode() {}
2024-08-20 08:58:43 +08:00
// ------------------------------------------------
2024-08-06 11:00:13 +08:00
type TypeKind uint
const (
2024-08-09 10:13:59 +08:00
Void TypeKind = iota
Bool
2024-08-06 11:00:13 +08:00
Char
2024-08-09 10:13:59 +08:00
Char16
Char32
WChar
Int
Int128
2024-08-06 11:00:13 +08:00
Float
2024-08-09 10:13:59 +08:00
Float16
Float128
2024-08-06 11:00:13 +08:00
Complex
)
type TypeFlag uint
const (
Signed TypeFlag = 1 << iota
Unsigned
Long
LongLong
Double
Short
)
// [signed/unsigned/short/long/long long/double] [int]/char/float/complex/bool
type BuiltinType struct {
Kind TypeKind
Flags TypeFlag
}
func (*BuiltinType) exprNode() {}
// ------------------------------------------------
// Name
type Ident struct {
Name string
}
func (*Ident) exprNode() {}
// ------------------------------------------------
type Tag int
const (
Struct Tag = iota
Union
Enum
Class
)
// struct/union/enum/class Name
type TagExpr struct {
Tag Tag
Name *Ident
}
func (*TagExpr) exprNode() {}
// ------------------------------------------------
// (X)
type ParenExpr struct {
X Expr
}
func (*ParenExpr) exprNode() {}
// ------------------------------------------------
// Parent::X
type ScopingExpr struct {
Parent Expr
X Expr
}
func (*ScopingExpr) exprNode() {}
// ------------------------------------------------
// X*
type PointerType struct {
X Expr
}
func (*PointerType) exprNode() {}
// ------------------------------------------------
// X&
2024-08-20 12:02:12 +08:00
type LvalueRefType struct {
2024-08-06 11:00:13 +08:00
X Expr
}
2024-08-20 12:02:12 +08:00
func (*LvalueRefType) exprNode() {}
// X&&
type RvalueRefType struct {
X Expr
}
func (*RvalueRefType) exprNode() {}
2024-08-06 11:00:13 +08:00
// ------------------------------------------------
// Elt[Len]
// Elt[]
type ArrayType struct {
Elt Expr
Len Expr // optional
}
func (*ArrayType) exprNode() {}
// ------------------------------------------------
type Comment struct {
Text string // comment text (excluding '\n' for //-style comments)
}
func (*Comment) exprNode() {}
type CommentGroup struct {
List []*Comment // len(List) > 0
}
func (*CommentGroup) exprNode() {}
// ------------------------------------------------
type Field struct {
Doc *CommentGroup // associated documentation; or nil
Type Expr // field/method/parameter type; or nil
Names []*Ident // field/method/(type) parameter names; or nil
Comment *CommentGroup // line comments; or nil
}
func (*Field) exprNode() {}
type FieldList struct {
List []*Field // field list; or nil
}
func (*FieldList) exprNode() {}
// ------------------------------------------------
// Ret (*)(Params)
type FuncType struct {
Params *FieldList
Ret Expr
}
func (*FuncType) exprNode() {}
// ------------------------------------------------
// Template<Arg1, Arg2, ...>
type InstantiationType struct {
Template Expr
Args *FieldList
}
func (*InstantiationType) exprNode() {}
// =============================================================================
// Declarations
type Location struct {
File string
}
type DeclBase struct {
2024-08-06 15:09:39 +08:00
Doc *CommentGroup // associated documentation; or nil
2024-08-06 11:00:13 +08:00
Loc *Location
Parent Expr // namespace or class
}
2024-08-06 15:09:39 +08:00
// ------------------------------------------------
// typedef Type Name;
2024-08-06 11:00:13 +08:00
type TypedefDecl struct {
DeclBase
2024-08-06 15:09:39 +08:00
Type Expr
Name *Ident
2024-08-06 11:00:13 +08:00
}
func (*TypedefDecl) declNode() {}
// ------------------------------------------------
type EnumItem struct {
Name *Ident
Value Expr // optional
}
2024-08-08 11:21:56 +08:00
func (*EnumItem) exprNode() {}
2024-08-06 11:00:13 +08:00
// enum Name { Item1, Item2, ... };
type EnumTypeDecl struct {
DeclBase
Name *Ident
Items []*EnumItem
}
func (*EnumTypeDecl) declNode() {}
// ------------------------------------------------
// Ret Name(Params);
type FuncDecl struct {
DeclBase
Name *Ident
Type *FuncType
}
func (*FuncDecl) declNode() {}
// ------------------------------------------------
// struct/union/class Name { Field1, Field2, ... };
type TypeDecl struct {
DeclBase
Tag Tag
Fields *FieldList
Methods []*FuncDecl
}
func (*TypeDecl) declNode() {}
// =============================================================================
// AST File
2024-08-08 11:21:56 +08:00
type Include struct {
2024-08-08 11:49:55 +08:00
Path string `json:"path"`
2024-08-08 11:21:56 +08:00
}
func (*Include) ppdNode() {}
// ------------------------------------------------
2024-08-20 08:58:43 +08:00
type Token struct {
Token token.Token
Lit string
}
2024-08-08 11:21:56 +08:00
type Macro struct {
2024-08-20 08:58:43 +08:00
Name string
Tokens []*Token // Tokens[0].Lit is the macro name
2024-08-08 11:21:56 +08:00
}
func (*Macro) ppdNode() {}
// ------------------------------------------------
2024-08-06 11:00:13 +08:00
type File struct {
2024-08-08 11:49:55 +08:00
Decls []Decl `json:"decls"`
Includes []*Include `json:"includes,omitempty"`
Macros []*Macro `json:"macros,omitempty"`
2024-08-06 11:00:13 +08:00
}
// =============================================================================