Source file parser_cpp_mly_helper.ml
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
open Common
open Ast_cpp
module Ast = Ast_cpp
module Flag = Flag_parsing
let pr2, pr2_once = Common2.mk_pr2_wrappers Flag.verbose_parsing
let warning s v =
if !Flag.verbose_parsing
then Common2.warning ("PARSING: " ^ s) v
else v
exception Semantic of string * Ast_cpp.tok
type shortLong = Short | Long | LongLong
type decl = {
storageD: storage;
typeD: (sign option * shortLong option * typeCbis option) wrap;
qualifD: typeQualifier;
inlineD: bool wrap;
}
let nullDecl = {
storageD = NoSto;
typeD = (None, None, None), noii;
qualifD = Ast.nQ;
inlineD = false, noii;
}
let addStorageD x decl =
match decl with
| {storageD = NoSto; _} -> { decl with storageD = x }
| {storageD = (StoTypedef ii | Sto (_, ii)) as y; _} ->
if x = y
then decl +> warning "duplicate storage classes"
else raise (Semantic ("multiple storage classes", ii))
let addInlineD ii decl =
match decl with
| {inlineD = (false,[]); _} -> { decl with inlineD=(true,[ii])}
| {inlineD = (true, _ii2); _} -> decl +> warning "duplicate inline"
| _ -> raise Impossible
let addTypeD ty decl =
match ty, decl with
| (Left3 Signed,_ii), {typeD = ((Some Signed, _b,_c),_ii2); _} ->
decl +> warning "duplicate 'signed'"
| (Left3 UnSigned,_ii), {typeD = ((Some UnSigned,_b,_c),_ii2); _} ->
decl +> warning "duplicate 'unsigned'"
| (Left3 _,ii), {typeD = ((Some _,_b,_c),_ii2); _} ->
raise (Semantic ("both signed and unsigned specified", List.hd ii))
| (Left3 x,ii), {typeD = ((None,b,c),ii2); _} ->
{ decl with typeD = (Some x,b,c),ii @ ii2}
| (Middle3 Short,_ii), {typeD = ((_a,Some Short,_c),_ii2); _} ->
decl +> warning "duplicate 'short'"
| (Middle3 Long,ii), {typeD = ((a,Some Long,c),ii2); _}->
{ decl with typeD = (a, Some LongLong, c),ii@ii2 }
| (Middle3 Long,_ii), {typeD = ((_a,Some LongLong,_c),_ii2); _} ->
decl +> warning "triplicate 'long'"
| (Middle3 _,ii), {typeD = ((_a,Some _,_c),_ii2); _} ->
raise (Semantic ("both long and short specified", List.hd ii))
| (Middle3 x,ii), {typeD = ((a,None,c),ii2); _} ->
{ decl with typeD = (a, Some x,c),ii@ii2}
| (Right3 _t,ii), {typeD = ((_a,_b,Some _),_ii2); _} ->
raise (Semantic ("two or more data types", List.hd ii))
| (Right3 t,ii), {typeD = ((a,b,None),ii2); _} ->
{ decl with typeD = (a,b, Some t),ii@ii2}
let addQualif tq1 tq2 =
match tq1, tq2 with
| {const=Some _; _}, {const=Some _; _} ->
tq2 +> warning "duplicate 'const'"
| {volatile=Some _; _}, {volatile=Some _; _} ->
tq2 +> warning "duplicate 'volatile'"
| {const=Some x; _}, _ ->
{ tq2 with const = Some x}
| {volatile=Some x; _}, _ ->
{ tq2 with volatile = Some x}
| _ -> Common2.internal_error "there is no noconst or novolatile keyword"
let addQualifD qu qu2 =
{ qu2 with qualifD = addQualif qu qu2.qualifD }
let type_and_storage_from_decl
{storageD = st;
qualifD = qu;
typeD = (ty,iit);
inlineD = (inline,iinl);
} =
(qu,
(match ty with
| (None, None, None) ->
let decl =
{ v_namei = None; v_type = qu, (BaseType Void, iit); v_storage = st } in
raise (Semantic ("no type (could default to 'int')",
List.hd (Lib_parsing_cpp.ii_of_any (OneDecl decl))))
| (None, None, Some t) -> (t, iit)
| (Some sign, None, (None| Some (BaseType (IntType (Si (_,CInt)))))) ->
BaseType(IntType (Si (sign, CInt))), iit
| ((None|Some Signed),Some x,(None|Some(BaseType(IntType (Si (_,CInt)))))) ->
BaseType(IntType (Si (Signed, [Short,CShort; Long, CLong; LongLong, CLongLong] +> List.assoc x))), iit
| (Some UnSigned, Some x, (None| Some (BaseType (IntType (Si (_,CInt))))))->
BaseType(IntType (Si (UnSigned, [Short,CShort; Long, CLong; LongLong, CLongLong] +> List.assoc x))), iit
| (Some sign, None, (Some (BaseType (IntType CChar)))) -> BaseType(IntType (Si (sign, CChar2))), iit
| (None, Some Long,(Some(BaseType(FloatType CDouble)))) -> BaseType (FloatType (CLongDouble)), iit
| (Some _,_, Some _) ->
raise (Semantic("signed, unsigned valid only for char and int", List.hd iit))
| (_,Some _,(Some(BaseType(FloatType (CFloat|CLongDouble))))) ->
raise (Semantic ("long or short specified with floatint type", List.hd iit))
| (_,Some Short,(Some(BaseType(FloatType CDouble)))) ->
raise (Semantic ("the only valid combination is long double", List.hd iit))
| (_, Some _, Some _) ->
raise (Semantic ("long, short valid only for int or float", List.hd iit))
)), st, (inline, iinl)
let type_and_register_from_decl decl =
let {storageD = st; _} = decl in
let (t,_storage, _inline) = type_and_storage_from_decl decl in
match st with
| NoSto -> t, None
| Sto (Register, ii) -> t, Some ii
| StoTypedef ii | Sto (_, ii) ->
raise (Semantic ("storage class specified for parameter of function", ii))
let fixNameForParam (name, ftyp) =
match name with
| None, [], IdIdent id -> id, ftyp
| _ ->
let ii = Lib_parsing_cpp.ii_of_any (Name name) +> List.hd in
raise (Semantic ("parameter have qualifier", ii))
let type_and_storage_for_funcdef_from_decl decl =
let (returnType, storage, _inline) = type_and_storage_from_decl decl in
(match storage with
| StoTypedef tok ->
raise (Semantic ("function definition declared 'typedef'", tok))
| _x -> (returnType, storage)
)
let (fixOldCDecl: fullType -> fullType) = fun ty ->
match snd ty with
| FunctionType ({ft_params=params;_}),_iifunc ->
(match Ast.unparen params with
| [{p_name = None; p_type = ty2;_},_] ->
(match Ast.unwrap_typeC ty2 with
| BaseType Void -> ty
| _ ->
ty
)
| params ->
(params +> List.iter (fun (param,_) ->
match param with
| {p_name = None; p_type = _ty2; _} ->
()
| _ -> ()
));
ty
)
| _ ->
let ii = Lib_parsing_cpp.ii_of_any (Type ty) +> List.hd in
raise (Semantic ("seems this is not a function", ii))
let fixFunc ((name, ty, sto), cp) =
match ty with
| (aQ,(FunctionType ({ft_params=params; _} as ftyp),_iifunc)) ->
assert (aQ =*= nQ);
(match Ast.unparen params with
[{p_name= None; p_type = ty2;_}, _] ->
(match Ast.unwrap_typeC ty2 with
| BaseType Void -> ()
| _ -> ()
)
| params ->
params +> List.iter (function
| ({p_name = Some _s;_}, _) -> ()
| _ -> ()
)
);
{ f_name = name; f_type = ftyp; f_storage = sto; f_body = cp; }
| _ ->
let ii = Lib_parsing_cpp.ii_of_any (Type ty) +> List.hd in
raise (Semantic ("function definition without parameters", ii))
let fixFieldOrMethodDecl (xs, semicolon) =
match xs with
| [FieldDecl({
v_namei = Some (name, ini_opt);
v_type = (q, (FunctionType ft, ii_ft));
v_storage = sto;
}), _noiicomma] ->
MemberDecl (MethodDecl ({
v_namei = Some (name, None);
v_type = (q, (FunctionType ft, ii_ft));
v_storage = sto;
},
(match ini_opt with
| None -> None
| Some (EqInit(tokeq, InitExpr(C(Int "0"), iizero))) ->
Some (tokeq, List.hd iizero)
| _ ->
raise (Semantic ("can't assign expression to method decl", semicolon))
), semicolon
))
| _ -> MemberField (xs, semicolon)
let mk_e e ii = (e, ii)
let mk_funcall e1 args =
Call (e1, args)
let mk_constructor id (lp, params, rp) cp =
let params, _hasdots =
match params with
| Some (params, ellipsis) ->
params, ellipsis
| None -> [], None
in
let ftyp = {
ft_ret = nQ, (BaseType Void, noii);
ft_params= (lp, params, rp);
ft_dots = None;
ft_const = None;
ft_throw = None;
}
in
{ f_name = (None, noQscope, IdIdent id); f_type = ftyp;
f_storage = NoSto; f_body = cp
}
let mk_destructor tilde id (lp, _voidopt, rp) exnopt cp =
let ftyp = {
ft_ret = nQ, (BaseType Void, noii);
ft_params= (lp, [], rp);
ft_dots = None;
ft_const = None;
ft_throw = exnopt;
}
in
{ f_name = (None, noQscope, IdDestructor (tilde, id)); f_type = ftyp;
f_storage = NoSto; f_body = cp;
}
let opt_to_list_params params =
match params with
| Some (params, _ellipsis) ->
params
| None -> []