/
parser-quirk-mem.bas
270 lines (231 loc) · 6.66 KB
/
parser-quirk-mem.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'' memory operations
''
'' chng: nov/2006 written [v1ctor]
#include once "fb.bi"
#include once "fbint.bi"
#include once "parser.bi"
#include once "ast.bi"
'':::::
''cOperatorNew = NEW DataType|Constructor()
'' | NEW DataType[Expr] .
''
function cOperatorNew( ) as ASTNODE ptr
dim as integer dtype = any
dim as FBSYMBOL ptr subtype = any, tmp = any
dim as integer has_ctor = any, has_defctor = any, do_clear = any
dim as ASTNODE ptr initexpr = any, elementsexpr = any, placementexpr = any
dim as ASTNODE ptr expr = any
dim as AST_OP op = any
do_clear = TRUE
op = AST_OP_NEW
elementsexpr = NULL
initexpr = NULL
placementexpr = NULL
'' NEW
lexSkipToken( LEXCHECK_POST_SUFFIX )
'' '('?
if( hMatch( CHAR_LPRNT ) ) then
'' placement new
placementexpr = cExpression( )
if( placementexpr = NULL ) then
errReport( FB_ERRMSG_EXPECTEDEXPRESSION, TRUE )
end if
'' ')'
if( hMatch( CHAR_RPRNT ) = FALSE ) then
errReport( FB_ERRMSG_EXPECTEDRPRNT )
end if
end if
'' DataType
hSymbolType( dtype, subtype, 0 )
select case( typeGetDtAndPtrOnly( dtype ) )
case FB_DATATYPE_FIXSTR, FB_DATATYPE_CHAR, FB_DATATYPE_WCHAR
errReport( FB_ERRMSG_NEWCANTBEUSEDWITHFIXLENSTRINGS, TRUE )
dtype = FB_DATATYPE_STRING
end select
'' Disallow creating objects of abstract classes
hComplainIfAbstractClass( dtype, subtype )
has_ctor = typeHasCtor( dtype, subtype )
has_defctor = typeHasDefCtor( dtype, subtype )
'' '['?
if( lexGetToken( ) = CHAR_LBRACKET ) then
lexSkipToken( )
elementsexpr = cExpression( )
if( elementsexpr = NULL ) then
errReport( FB_ERRMSG_EXPECTEDEXPRESSION )
else
op = AST_OP_NEW_VEC
end if
'' ']'
if( lexGetToken( ) <> CHAR_RBRACKET ) then
errReport( FB_ERRMSG_EXPECTEDRBRACKET )
hSkipUntil( CHAR_RBRACKET )
else
lexSkipToken( )
end if
'' '{'?
if( lexGetToken( ) = CHAR_LBRACE ) then
lexSkipToken( )
'' ANY?
if( lexGetToken( ) = FB_TK_ANY ) then
if( has_defctor ) then
errReportWarn( FB_WARNINGMSG_ANYINITHASNOEFFECT )
end if
'' Disallow ANY for STRING, like cVarDecl()
if( typeGetDtAndPtrOnly( dtype ) = FB_DATATYPE_STRING ) then
errReport( FB_ERRMSG_INVALIDDATATYPES )
else
do_clear = FALSE
end if
lexSkipToken( LEXCHECK_POST_SUFFIX )
else
errReport( FB_ERRMSG_VECTORCANTBEINITIALIZED )
end if
'' '}'
if( lexGetToken( ) <> CHAR_RBRACE ) then
errReport( FB_ERRMSG_EXPECTEDRBRACE )
'' error recovery: skip until next '}'
hSkipUntil( CHAR_RBRACE, TRUE )
else
lexSkipToken( )
end if
end if
end if
'' not a vector?
if( elementsexpr = NULL ) then
elementsexpr = astNewCONSTi( 1, FB_DATATYPE_UINT )
else
'' Constant?
if( astIsCONST( elementsexpr ) ) then
'' check at compile time
elementsexpr = astNewCONSTi( cConstIntExprRanged( elementsexpr, FB_DATATYPE_UINT ), FB_DATATYPE_UINT )
else
'' make sure it's a uinteger, otherwise it may crash later, fixes bug #2533376 (counting_pine)
elementsexpr = astNewCONV( FB_DATATYPE_UINT, NULL, elementsexpr, AST_CONVOPT_SIGNCONV )
end if
if( elementsexpr = NULL ) then
errReport( FB_ERRMSG_TYPEMISMATCH, TRUE )
elementsexpr = astNewCONSTi( 1, FB_DATATYPE_UINT )
end if
end if
'' temp pointer
tmp = symbAddTempVar( typeAddrOf( dtype ), subtype )
'' Constructor?
if( has_ctor ) then
'' '('?
if( lexGetToken( ) = CHAR_LPRNT ) then
'' ctor + vector? not allowed..
if( op = AST_OP_NEW_VEC ) then
errReport( FB_ERRMSG_EXPLICITCTORCALLINVECTOR, TRUE )
else
initexpr = cCtorCall( subtype )
end if
else
dim as FBSYMBOL ptr ctor = symbGetCompDefCtor( subtype )
'' no default ctor?
if( ctor = NULL ) then
errReport( FB_ERRMSG_NODEFAULTCTORDEFINED )
else
'' only if not a vector
if( op <> AST_OP_NEW_VEC ) then
initexpr = cCtorCall( subtype )
else
'' Check visibility of the default constructor
if( symbCheckAccess( ctor ) = FALSE ) then
errReport( FB_ERRMSG_NOACCESSTODEFAULTCTOR )
end if
end if
end if
end if
else
'' '('?
if( lexGetToken( ) = CHAR_LPRNT ) then
'' vector? not allowed..
if( op = AST_OP_NEW_VEC ) then
errReport( FB_ERRMSG_VECTORCANTBEINITIALIZED, TRUE )
end if
'' ANY?
if( lexGetLookAhead( 1 ) = FB_TK_ANY ) then
'' '('
lexSkipToken( )
'' Disallow ANY for STRING, like cVarDecl()
if( typeGetDtAndPtrOnly( dtype ) = FB_DATATYPE_STRING ) then
errReport( FB_ERRMSG_INVALIDDATATYPES )
else
do_clear = FALSE
end if
'' ANY
lexSkipToken( LEXCHECK_POST_SUFFIX )
'' ')'
if( lexGetToken( ) <> CHAR_RPRNT ) then
errReport( FB_ERRMSG_EXPECTEDRPRNT )
'' error recovery: skip until next ')'
hSkipUntil( CHAR_RPRNT, TRUE )
else
lexSkipToken( )
end if
else
initexpr = cInitializer( tmp, FB_INIOPT_ISINI or FB_INIOPT_UPCAST, dtype, subtype )
if( initexpr = NULL ) then
errReport( FB_ERRMSG_EXPECTEDEXPRESSION )
end if
end if
end if
end if
expr = astBuildNewOp( op, tmp, elementsexpr, initexpr, _
dtype, subtype, do_clear, placementexpr )
if( expr = NULL ) then
errReport( FB_ERRMSG_INVALIDDATATYPES )
end if
'' return the pointer
function = astNewLINK( expr, astNewVAR( tmp ), AST_LINK_RETURN_RIGHT )
end function
'' DELETE ['[]'] expr
sub cOperatorDelete( )
dim as ASTNODE ptr ptrexpr = any
dim as integer dtype = any, op = any
dim as FBSYMBOL ptr subtype = any
'' DELETE
lexSkipToken( LEXCHECK_POST_SUFFIX )
op = AST_OP_DEL
'' '[' ']'?
if( lexGetToken( ) = CHAR_LBRACKET ) then
lexSkipToken( )
op = AST_OP_DEL_VEC
if( lexGetToken( ) <> CHAR_RBRACKET ) then
errReport( FB_ERRMSG_EXPECTEDRBRACKET )
hSkipUntil( CHAR_RBRACKET )
return
end if
lexSkipToken( )
end if
ptrexpr = cVarOrDeref( FB_VAREXPROPT_ISEXPR )
if( ptrexpr = NULL ) then
errReport( FB_ERRMSG_EXPECTEDIDENTIFIER )
hSkipStmt( )
return
end if
dtype = astGetFullType( ptrexpr )
subtype = astGetSubType( ptrexpr )
'' not a ptr?
if( typeIsPtr( dtype ) = FALSE ) then
errReport( FB_ERRMSG_EXPECTEDPOINTER )
return
end if
select case( typeGetDtAndPtrOnly( dtype ) )
case typeAddrOf( FB_DATATYPE_VOID )
'' Warn about ANY PTR
errReportWarn( FB_WARNINGMSG_DELETEANYPTR )
case typeAddrOf( FB_DATATYPE_FWDREF )
'' Disallow DELETE on forward reference ptrs
'' (don't know whether the real type will have a dtor or not)
errReport( FB_ERRMSG_INCOMPLETETYPE, TRUE )
end select
'' Check visibility
if( typeHasDtor( typeDeref( dtype ), subtype ) ) then
'' Check visibility of the destructor
if( symbCheckAccess( symbGetCompDtor1( subtype ) ) = FALSE ) then
errReport( FB_ERRMSG_NOACCESSTODTOR )
end if
end if
astAdd( astBuildDeleteOp( op, ptrexpr ) )
end sub