FreeBASIC  0.91.0
symb-const.bas
Go to the documentation of this file.
1 '' symbol table module for constants
2 ''
3 '' chng: sep/2004 written [v1ctor]
4 '' jan/2005 updated to use real linked-lists [v1ctor]
5 
6 
7 #include once "fb.bi"
8 #include once "fbint.bi"
9 #include once "hash.bi"
10 #include once "list.bi"
11 #include once "ir.bi"
12 
13 '':::::
14 function symbAddConst _
15  ( _
16  byval symbol as zstring ptr, _
17  byval dtype as integer, _
18  byval subtype as FBSYMBOL ptr, _
19  byval value as FBVALUE ptr, _
20  byval attrib as integer _
21  ) as FBSYMBOL ptr
22 
23  dim as FBSYMBOL ptr sym = any
24 
25  function = NULL
26 
27  sym = symbNewSymbol( FB_SYMBOPT_DOHASH, _
28  NULL, _
29  NULL, NULL, _
30  FB_SYMBCLASS_CONST, _
31  symbol, NULL, _
32  dtype, subtype, _
33  attrib )
34  if( sym = NULL ) then
35  exit function
36  end if
37 
38  sym->val = *value
39 
40  function = sym
41 end function
42 
43 function symbAllocFloatConst _
44  ( _
45  byval value as double, _
46  byval dtype as integer _
47  ) as FBSYMBOL ptr
48 
49  static as zstring * FB_MAXINTNAMELEN+1 id, id_alias
50  static as FBARRAYDIM dTB(0)
51  dim as FBSYMBOL ptr s = any
52 
53  function = NULL
54 
55  '' can't use STR() because GAS doesn't support the 1.#INF notation
56  dim as string svalue = hFloatToHex( value, dtype )
57 
58  id = "{fbnc}"
59  id += svalue
60 
61  '' preserve case, 'D', 'd', 'E', 'e' will become 'e' in lexer
62  s = symbLookupByNameAndSuffix( @symbGetGlobalNamespc( ), @id, dtype, TRUE, FALSE )
63  if( s <> NULL ) then
64  return s
65  end if
66 
67  id_alias = *symbUniqueId( )
68 
69  '' it must be declare as SHARED, because even if currently inside an
70  '' proc, the global symbol tb should be used, so just one constant
71  '' will be ever allocated over the module
72  s = symbAddVar( @id, @id_alias, dtype, NULL, 0, 0, dTB(), _
73  FB_SYMBATTRIB_SHARED or FB_SYMBATTRIB_LITCONST, _
74  FB_SYMBOPT_MOVETOGLOB or FB_SYMBOPT_PRESERVECASE or FB_SYMBOPT_NODUPCHECK )
75 
76  s->var_.littext = ZstrAllocate( len( svalue ) )
77  *s->var_.littext = svalue
78 
79  function = s
80 end function
81 
82 function symbAllocIntConst _
83  ( _
84  byval value as integer, _
85  byval dtype as integer _
86  ) as FBSYMBOL ptr static
87 
88  static as zstring * FB_MAXINTNAMELEN+1 id, id_alias
89  dim as FBSYMBOL ptr s
90  dim as FBARRAYDIM dTB(0)
91  dim as string svalue
92 
93  function = NULL
94 
95  svalue = "0x" + Hex(value)
96 
97  id = "{fbnc}"
98  id += svalue
99 
100  '' preserve case, 'D', 'd', 'E', 'e' will become 'e' in lexer
101  s = symbLookupByNameAndSuffix( @symbGetGlobalNamespc( ), @id, dtype, TRUE, FALSE )
102  if( s <> NULL ) then
103  return s
104  end if
105 
106  id_alias = *symbUniqueId( )
107 
108  '' it must be declare as SHARED, because even if currently inside an
109  '' proc, the global symbol tb should be used, so just one constant
110  '' will be ever allocated over the module
111  s = symbAddVar( @id, @id_alias, dtype, NULL, 0, 0, dTB(), _
112  FB_SYMBATTRIB_SHARED or FB_SYMBATTRIB_LITCONST, _
113  FB_SYMBOPT_MOVETOGLOB or FB_SYMBOPT_PRESERVECASE )
114 
115  s->var_.littext = ZstrAllocate( len( svalue ) )
116  *s->var_.littext = svalue
117 
118  function = s
119 end function
120 
121 '':::::
122 function symbAllocLongIntConst _
123  ( _
124  byval value as longint, _
125  byval dtype as integer _
126  ) as FBSYMBOL ptr static
127 
128  static as zstring * FB_MAXINTNAMELEN+1 id, id_alias
129  dim as FBSYMBOL ptr s
130  dim as FBARRAYDIM dTB(0)
131  dim as string svalue
132 
133  function = NULL
134 
135  svalue = "0x" + Hex(value)
136 
137  id = "{fbnc}"
138  id += svalue
139 
140  '' preserve case, 'D', 'd', 'E', 'e' will become 'e' in lexer
141  s = symbLookupByNameAndSuffix( @symbGetGlobalNamespc( ), @id, dtype, TRUE, FALSE )
142  if( s <> NULL ) then
143  return s
144  end if
145 
146  id_alias = *symbUniqueId( )
147 
148  '' it must be declare as SHARED, because even if currently inside an
149  '' proc, the global symbol tb should be used, so just one constant
150  '' will be ever allocated over the module
151  s = symbAddVar( @id, @id_alias, dtype, NULL, 0, 0, dTB(), _
152  FB_SYMBATTRIB_SHARED or FB_SYMBATTRIB_LITCONST, _
153  FB_SYMBOPT_MOVETOGLOB or FB_SYMBOPT_PRESERVECASE )
154 
155  s->var_.littext = ZstrAllocate( len( svalue ) )
156  *s->var_.littext = svalue
157 
158  function = s
159 end function
160 
161 '':::::
162 function symbAllocStrConst _
163  ( _
164  byval sname as zstring ptr, _
165  byval lgt as integer _
166  ) as FBSYMBOL ptr
167 
168  static as zstring * FB_MAXINTNAMELEN+1 id, id_alias
169  static as FBARRAYDIM dTB(0)
170  dim as FBSYMBOL ptr s = any
171  dim as integer strlen = any
172 
173  function = NULL
174 
175  '' the lgt passed isn't the real one because it doesn't
176  '' take into acount the escape characters
177  strlen = len( *sname )
178  if( lgt < 0 ) then
179  lgt = strlen
180  end if
181 
182  if( strlen <= FB_MAXNAMELEN-6 ) then
183  id = "{fbsc}"
184  id += *sname
185  else
186  id = *symbUniqueId( )
187  end if
188 
189  s = symbLookupByNameAndClass( @symbGetGlobalNamespc( ), @id, FB_SYMBCLASS_VAR, TRUE, FALSE )
190  if( s <> NULL ) then
191  return s
192  end if
193 
194  id_alias = *symbUniqueId( )
195 
196  '' lgt += the null-char (rtlib wrappers will take it into account)
197 
198  '' it must be declare as SHARED, see symbAllocFloatConst()
199  s = symbAddVar( @id, @id_alias, FB_DATATYPE_CHAR, NULL, lgt + 1, 0, dTB(), _
200  FB_SYMBATTRIB_SHARED or FB_SYMBATTRIB_LITCONST, _
201  FB_SYMBOPT_MOVETOGLOB or FB_SYMBOPT_PRESERVECASE or FB_SYMBOPT_NODUPCHECK )
202 
203  s->var_.littext = ZstrAllocate( strlen )
204  *s->var_.littext = *sname
205 
206  function = s
207 end function
208 
209 function symbAllocWStrConst _
210  ( _
211  byval sname as wstring ptr, _
212  byval lgt as integer _
213  ) as FBSYMBOL ptr
214 
215  static as zstring * FB_MAXINTNAMELEN+1 id, id_alias
216  static as FBARRAYDIM dTB(0)
217  dim as FBSYMBOL ptr s = any
218  dim as integer strlen = any
219  dim as integer wcharlen = any
220 
221  function = NULL
222 
223  '' the lgt passed isn't the real one because it doesn't
224  '' take into acount the escape characters
225  strlen = len( *sname )
226  if( lgt < 0 ) then
227  lgt = strlen
228  end if
229 
230  wcharlen = typeGetSize( FB_DATATYPE_WCHAR )
231  '' hEscapeW() can use up to (4 * wcharlen) ascii chars per unicode char
232  '' (up to one '\ooo' per byte of wchar)
233  if( strlen * ((1+3) * wcharlen) <= FB_MAXNAMELEN-6 ) then
234  id = "{fbwc}"
235  id += *hEscapeW( sname )
236  else
237  id = *symbUniqueId( )
238  end if
239 
240  s = symbLookupByNameAndClass( @symbGetGlobalNamespc( ), @id, FB_SYMBCLASS_VAR, TRUE, FALSE )
241  if( s <> NULL ) then
242  return s
243  end if
244 
245  id_alias = *symbUniqueId( )
246 
247  '' lgt = (lgt + null-char) * sizeof( wstring ) (see parser-decl-symbinit.bas)
248  '' it must be declare as SHARED, see symbAllocFloatConst()
249  s = symbAddVar( @id, @id_alias, FB_DATATYPE_WCHAR, NULL, (lgt+1) * len( wstring ), 0, dTB(), _
250  FB_SYMBATTRIB_SHARED or FB_SYMBATTRIB_LITCONST, _
251  FB_SYMBOPT_MOVETOGLOB or FB_SYMBOPT_PRESERVECASE or FB_SYMBOPT_NODUPCHECK )
252 
253  s->var_.littextw = WstrAllocate( strlen )
254  *s->var_.littextw = *sname
255 
256  function = s
257 end function
258 
259 sub symbDelConst( byval s as FBSYMBOL ptr )
260  if( s = NULL ) then
261  exit sub
262  end if
263 
264  '' if it's a string, the symbol attached will be deleted be delVar()
265  symbFreeSymbol( s )
266 end sub
267 
268 function symbGetConstValueAsStr( byval s as FBSYMBOL ptr ) as string
269  select case( symbGetType( s ) )
270  case FB_DATATYPE_STRING, FB_DATATYPE_FIXSTR, FB_DATATYPE_CHAR
271  function = *symbGetConstStr( s )->var_.littext
272 
273  case FB_DATATYPE_DOUBLE
274  function = str( symbGetConstFloat( s ) )
275 
276  case FB_DATATYPE_SINGLE
277  function = str( csng( symbGetConstFloat( s ) ) )
278 
279  case else
280  if( typeIsSigned( s->typ ) ) then
281  function = str( symbGetConstInt( s ) )
282  else
283  function = str( cunsg( symbGetConstInt( s ) ) )
284  end if
285  end select
286 end function
287 
288 function symbCloneConst( byval sym as FBSYMBOL ptr ) as FBSYMBOL ptr
289  '' no need to make a copy of fbvalue.str, if it's a literal,
290  '' it will be a non-local var
291  function = symbAddConst( NULL, symbGetType( sym ), symbGetSubtype( sym ), _
292  symbGetConstVal( sym ), symbGetAttrib( sym ) )
293 end function
294