1
1
use super :: module:: { CodeGenBuilder , LocalIRValue } ;
2
- use ast:: AccessSpecifier ;
3
- use inkwell:: {
4
- module:: Linkage ,
5
- types:: { BasicTypeEnum , StructType } ,
6
- values:: GlobalValue ,
7
- } ;
8
- use typed_ast:: { SymbolID , TypedExpression , TypedFuncDef , TypedGlobalVariable , TypedStatement , TypedStruct } ;
2
+ use inkwell:: types:: { BasicTypeEnum , StructType } ;
3
+ use typed_ast:: { SymbolID , TypedBlockStatement , TypedStatement , TypedStruct } ;
9
4
10
5
impl < ' a > CodeGenBuilder < ' a > {
11
6
pub ( crate ) fn build_toplevel_statements ( & self , stmts : & Vec < TypedStatement > ) {
@@ -15,7 +10,7 @@ impl<'a> CodeGenBuilder<'a> {
15
10
match stmt {
16
11
TypedStatement :: FuncDef ( typed_func_def) => self . build_func_def ( typed_func_def) ,
17
12
TypedStatement :: Struct ( typed_struct) => self . build_struct_def ( typed_struct) ,
18
- TypedStatement :: Enum ( typed_enum) => todo ! ( ) ,
13
+ TypedStatement :: Enum ( typed_enum) => self . build_enum_def ( typed_enum ) ,
19
14
TypedStatement :: Interface ( typed_interface) => todo ! ( ) ,
20
15
// already handled in build_forward_decls
21
16
TypedStatement :: GlobalVariable ( _) => continue ,
@@ -104,53 +99,42 @@ impl<'a> CodeGenBuilder<'a> {
104
99
self . llvmctx . opaque_struct_type ( name)
105
100
}
106
101
107
- fn build_global_variable_linkage ( & self , vis : AccessSpecifier ) -> Linkage {
108
- match vis {
109
- AccessSpecifier :: PublicExtern => Linkage :: Common ,
110
- AccessSpecifier :: Extern => Linkage :: Common ,
111
- AccessSpecifier :: Public => Linkage :: External ,
112
- AccessSpecifier :: Internal => Linkage :: Private ,
113
- AccessSpecifier :: Inline => unreachable ! ( ) ,
114
- AccessSpecifier :: PublicInline => unreachable ! ( ) ,
115
- }
116
- }
117
-
118
- fn build_global_var_decl ( & self , global_var : & TypedGlobalVariable ) -> GlobalValue < ' a > {
119
- let linkage = self . build_global_variable_linkage ( global_var. vis . clone ( ) ) ;
102
+ pub ( crate ) fn build_block_statement ( & self , block_stmt : & TypedBlockStatement ) {
103
+ let local_scope_opt = Some (
104
+ self . resolver
105
+ . get_scope_ref ( self . module_id , block_stmt. scope_id )
106
+ . unwrap ( ) ,
107
+ ) ;
120
108
121
- let mut global_var_type = {
122
- if let Some ( concrete_type) = & global_var. ty {
123
- Some ( self . build_concrete_type ( None , concrete_type. clone ( ) ) )
124
- } else {
125
- None
109
+ for stmt in & block_stmt. exprs {
110
+ match stmt {
111
+ TypedStatement :: Variable ( typed_variable) => {
112
+ self . build_local_variable ( local_scope_opt. clone ( ) , typed_variable)
113
+ }
114
+ TypedStatement :: If ( typed_if) => todo ! ( ) ,
115
+ TypedStatement :: Return ( typed_return) => todo ! ( ) ,
116
+ TypedStatement :: Break ( typed_break) => todo ! ( ) ,
117
+ TypedStatement :: Continue ( typed_continue) => todo ! ( ) ,
118
+ TypedStatement :: For ( typed_for) => todo ! ( ) ,
119
+ TypedStatement :: Foreach ( typed_foreach) => todo ! ( ) ,
120
+ TypedStatement :: Switch ( typed_switch) => todo ! ( ) ,
121
+ TypedStatement :: Struct ( typed_struct) => todo ! ( ) ,
122
+ TypedStatement :: Enum ( typed_enum) => todo ! ( ) ,
123
+ TypedStatement :: Expression ( typed_expr) => {
124
+ self . build_expr ( local_scope_opt. clone ( ) , typed_expr) ;
125
+ }
126
+ TypedStatement :: BlockStatement ( typed_block_statement) => {
127
+ self . build_block_statement ( typed_block_statement) ;
128
+ }
129
+ TypedStatement :: Interface ( typed_interface) => todo ! ( ) ,
130
+ // Skipped statements
131
+ TypedStatement :: Typedef ( _) => continue ,
132
+ // Invalid statements
133
+ TypedStatement :: FuncDef ( _) => unreachable ! ( ) ,
134
+ TypedStatement :: FuncDecl ( _) => unreachable ! ( ) ,
135
+ TypedStatement :: Import ( _) => unreachable ! ( ) ,
136
+ TypedStatement :: GlobalVariable ( _) => unreachable ! ( ) ,
126
137
}
127
- } ;
128
-
129
- if global_var_type. is_none ( ) {
130
- let typed_expr: TypedExpression = global_var. expr . clone ( ) . unwrap ( ) ;
131
- global_var_type = Some ( self . build_concrete_type ( None , typed_expr. concrete_type . unwrap ( ) ) ) ;
132
138
}
133
-
134
- let global_var_type: BasicTypeEnum < ' a > = global_var_type. unwrap ( ) . try_into ( ) . unwrap ( ) ;
135
-
136
- let llvmmodule = self . llvmmodule . borrow ( ) ;
137
- let global_var_value = llvmmodule. add_global ( global_var_type, None , & global_var. name ) ;
138
- global_var_value. set_linkage ( linkage) ;
139
- drop ( llvmmodule) ;
140
- global_var_value
141
- }
142
-
143
- fn build_func_def ( & self , func_def : & TypedFuncDef ) {
144
- let irreg = self . irreg . borrow ( ) ;
145
- let local_ir_value = irreg. get ( & func_def. symbol_id ) . unwrap ( ) ;
146
-
147
- let fn_value = local_ir_value. as_func ( ) . unwrap ( ) ;
148
-
149
- let entry_block = self . llvmctx . append_basic_block ( * fn_value, "entry" ) ;
150
- self . llvmbuilder . position_at_end ( entry_block) ;
151
-
152
- // TODO build body block
153
-
154
- drop ( irreg) ;
155
139
}
156
140
}
0 commit comments