FreeBASIC  0.91.0
ast-node-decl.bas
Go to the documentation of this file.
1 '' AST decl nodes (VAR, CONST, etc)
2 ''
3 '' chng: jun/2006 written [v1ctor]
4 
5 
6 #include once "fb.bi"
7 #include once "fbint.bi"
8 #include once "ir.bi"
9 #include once "ast.bi"
10 
11 '':::::
12 function hCtorList _
13  ( _
14  byval sym as FBSYMBOL ptr _
15  ) as ASTNODE ptr
16 
17  dim as FBSYMBOL ptr cnt = any, label = any, this_ = any, subtype = any
18  dim as ASTNODE ptr tree = NULL
19 
20  subtype = symbGetSubtype( sym )
21 
22  cnt = symbAddTempVar( FB_DATATYPE_INTEGER )
23  label = symbAddLabel( NULL )
24  this_ = symbAddTempVar( typeAddrOf( symbGetType( sym ) ), subtype )
25 
26  '' fld = @sym(0)
27  tree = astNewLINK( tree, astBuildVarAssign( this_, astNewADDROF( astNewVAR( sym ) ) ) )
28 
29  '' for cnt = 0 to symbGetArrayElements( sym )-1
30  tree = astBuildForBegin( tree, cnt, label, 0 )
31 
32  '' sym.constructor( )
33  tree = astNewLINK( tree, astBuildCtorCall( symbGetSubtype( sym ), astBuildVarDeref( this_ ) ) )
34 
35  '' this_ += 1
36  tree = astNewLINK( tree, astBuildVarInc( this_, 1 ) )
37 
38  '' next
39  tree = astBuildForEnd( tree, cnt, label, astNewCONSTi( symbGetArrayElements( sym ) ) )
40 
41  function = tree
42 
43 end function
44 
45 function hDefaultInit( byval sym as FBSYMBOL ptr ) as ASTNODE ptr
46  '' static, shared (includes extern/public) or common? do nothing..
47  if( (symbGetAttrib( sym ) and (FB_SYMBATTRIB_STATIC or _
48  FB_SYMBATTRIB_SHARED or _
49  FB_SYMBATTRIB_COMMON or _
50  FB_SYMBATTRIB_DYNAMIC)) <> 0 ) then
51  exit function
52  end if
53 
54  '' local..
55 
56  '' Do not initialize?
57  if( symbGetDontInit( sym ) ) then
58  exit function
59  end if
60 
61  '' has a default ctor?
62  if( symbHasDefCtor( sym ) ) then
63  '' proc result? astProcEnd() will take care of this
64  if( (symbGetAttrib( sym ) and FB_SYMBATTRIB_FUNCRESULT) <> 0 ) then
65  exit function
66  end if
67 
68  '' scalar?
69  if( (symbGetArrayDimensions( sym ) = 0) or _
70  (symbGetArrayElements( sym ) = 1) ) then
71  '' sym.constructor( )
72  function = astBuildCtorCall( symbGetSubtype( sym ), astNewVAR( sym ) )
73  '' array..
74  else
75  function = hCtorList( sym )
76  end if
77 
78  exit function
79  end if
80 
81  function = astNewMEM( AST_OP_MEMCLEAR, astNewVAR( sym ), _
82  astNewCONSTi( symbGetLen( sym ) * symbGetArrayElements( sym ) ) )
83 end function
84 
85 function astNewDECL _
86  ( _
87  byval sym as FBSYMBOL ptr, _
88  byval do_defaultinit as integer _
89  ) as ASTNODE ptr
90 
91  dim as ASTNODE ptr n = any
92 
93  '' alloc new node
94  n = astNewNode( AST_NODECLASS_DECL, FB_DATATYPE_INVALID )
95 
96  n->sym = sym
97  if( do_defaultinit ) then
98  n->l = hDefaultInit( sym )
99  end if
100 
101  function = n
102 end function
103 
104 function astLoadDECL( byval n as ASTNODE ptr ) as IRVREG ptr
105  if( ast.doemit ) then
106  '' Emit local variable declarations in-line for the C backend
107  if( symbIsLocal( n->sym ) ) then
108  irEmitDECL( n->sym )
109  end if
110  end if
111 
112  '' call ctor?
113  if( n->l <> NULL ) then
114  astLoad( n->l )
115  astDelNode( n->l )
116  end if
117 
118  function = NULL
119 end function
120