75dde11e8fa5d0670c2729bf5112a25507e633c5
[bpt/coccinelle.git] / parsing_c / parser_c.mly
1 %{
2 (* Yoann Padioleau
3 *
4 * Copyright (C) 2002, 2006, 2007, 2008, 2009 Yoann Padioleau
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License (GPL)
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * file license.txt for more details.
14 *)
15 open Common
16
17 open Ast_c
18
19 module LP = Lexer_parser
20 open Lexer_parser (* for the fields *)
21
22 open Semantic_c (* Semantic exn *)
23
24
25 (*****************************************************************************)
26 (* Wrappers *)
27 (*****************************************************************************)
28 let warning s v =
29 if !Flag_parsing_c.verbose_parsing
30 then Common.warning ("PARSING: " ^ s) v
31 else v
32
33 let pr2, pr2_once = Common.mk_pr2_wrappers Flag_parsing_c.verbose_parsing
34
35 (*****************************************************************************)
36 (* Parse helpers functions *)
37 (*****************************************************************************)
38
39 (*-------------------------------------------------------------------------- *)
40 (* Type related *)
41 (*-------------------------------------------------------------------------- *)
42
43 type shortLong = Short | Long | LongLong
44
45 type decl = {
46 storageD: storagebis wrap;
47 typeD: ((sign option) * (shortLong option) * (typeCbis option)) wrap;
48 qualifD: typeQualifierbis wrap;
49 inlineD: bool wrap;
50 (* note: have a full_info: parse_info list; to remember ordering
51 * between storage, qualifier, type ? well this info is already in
52 * the Ast_c.info, just have to sort them to get good order *)
53 }
54
55 let nullDecl = {
56 storageD = NoSto, [];
57 typeD = (None, None, None), [];
58 qualifD = nullQualif;
59 inlineD = false, [];
60 }
61 let fake_pi = Common.fake_parse_info
62
63 let addStorageD = function
64 | ((x,ii), ({storageD = (NoSto,[])} as v)) -> { v with storageD = (x, [ii]) }
65 | ((x,ii), ({storageD = (y, ii2)} as v)) ->
66 if x =*= y then warning "duplicate storage classes" v
67 else raise (Semantic ("multiple storage classes", fake_pi))
68
69 let addInlineD = function
70 | ((true,ii), ({inlineD = (false,[])} as v)) -> { v with inlineD=(true,[ii])}
71 | ((true,ii), ({inlineD = (true, ii2)} as v)) -> warning "duplicate inline" v
72 | _ -> raise Impossible
73
74
75 let addTypeD = function
76 | ((Left3 Signed,ii) ,({typeD = ((Some Signed, b,c),ii2)} as v)) ->
77 warning "duplicate 'signed'" v
78 | ((Left3 UnSigned,ii) ,({typeD = ((Some UnSigned,b,c),ii2)} as v)) ->
79 warning "duplicate 'unsigned'" v
80 | ((Left3 _,ii), ({typeD = ((Some _,b,c),ii2)} as _v)) ->
81 raise (Semantic ("both signed and unsigned specified", fake_pi))
82 | ((Left3 x,ii), ({typeD = ((None,b,c),ii2)} as v)) ->
83 {v with typeD = (Some x,b,c),ii ++ ii2}
84
85 | ((Middle3 Short,ii), ({typeD = ((a,Some Short,c),ii2)} as v)) ->
86 warning "duplicate 'short'" v
87
88
89 (* gccext: long long allowed *)
90 | ((Middle3 Long,ii), ({typeD = ((a,Some Long ,c),ii2)} as v)) ->
91 { v with typeD = (a, Some LongLong, c),ii++ii2 }
92 | ((Middle3 Long,ii), ({typeD = ((a,Some LongLong ,c),ii2)} as v)) ->
93 warning "triplicate 'long'" v
94
95
96 | ((Middle3 _,ii), ({typeD = ((a,Some _,c),ii2)} as _v)) ->
97 raise (Semantic ("both long and short specified", fake_pi))
98 | ((Middle3 x,ii), ({typeD = ((a,None,c),ii2)} as v)) ->
99 {v with typeD = (a, Some x,c),ii++ii2}
100
101 | ((Right3 t,ii), ({typeD = ((a,b,Some _),ii2)} as _v)) ->
102 raise (Semantic ("two or more data types", fake_pi))
103 | ((Right3 t,ii), ({typeD = ((a,b,None),ii2)} as v)) ->
104 {v with typeD = (a,b, Some t),ii++ii2}
105
106
107 let addQualif = function
108 | ({const=true}, ({const=true} as x)) -> warning "duplicate 'const'" x
109 | ({volatile=true},({volatile=true} as x))-> warning "duplicate 'volatile'" x
110 | ({const=true}, v) -> {v with const=true}
111 | ({volatile=true}, v) -> {v with volatile=true}
112 | _ ->
113 internal_error "there is no noconst or novolatile keyword"
114
115 let addQualifD ((qu,ii), ({qualifD = (v,ii2)} as x)) =
116 { x with qualifD = (addQualif (qu, v),ii::ii2) }
117
118
119 (*-------------------------------------------------------------------------- *)
120 (* Declaration/Function related *)
121 (*-------------------------------------------------------------------------- *)
122
123
124 (* stdC: type section, basic integer types (and ritchie)
125 * To understand the code, just look at the result (right part of the PM)
126 * and go back.
127 *)
128 let (fixDeclSpecForDecl: decl -> (fullType * (storage wrap))) = function
129 {storageD = (st,iist);
130 qualifD = (qu,iiq);
131 typeD = (ty,iit);
132 inlineD = (inline,iinl);
133 } ->
134 let ty',iit' =
135 (match ty with
136 | (None,None,None) ->
137 (* generate fake_info, otherwise type_annotater can crash in
138 * offset.
139 *)
140 warning "type defaults to 'int'" (defaultInt, [fakeInfo fake_pi])
141 | (None, None, Some t) -> (t, iit)
142
143 | (Some sign, None, (None| Some (BaseType (IntType (Si (_,CInt)))))) ->
144 BaseType(IntType (Si (sign, CInt))), iit
145 | ((None|Some Signed),Some x,(None|Some(BaseType(IntType (Si (_,CInt)))))) ->
146 BaseType(IntType (Si (Signed, [Short,CShort; Long, CLong; LongLong, CLongLong] +> List.assoc x))), iit
147 | (Some UnSigned, Some x, (None| Some (BaseType (IntType (Si (_,CInt))))))->
148 BaseType(IntType (Si (UnSigned, [Short,CShort; Long, CLong; LongLong, CLongLong] +> List.assoc x))), iit
149 | (Some sign, None, (Some (BaseType (IntType CChar)))) ->
150 BaseType(IntType (Si (sign, CChar2))), iit
151 | (None, Some Long,(Some(BaseType(FloatType CDouble)))) ->
152 BaseType (FloatType (CLongDouble)), iit
153
154 | (Some _,_, Some _) ->
155 (*mine*)
156 raise (Semantic ("signed, unsigned valid only for char and int", fake_pi))
157 | (_,Some _,(Some(BaseType(FloatType (CFloat|CLongDouble))))) ->
158 raise (Semantic ("long or short specified with floatint type", fake_pi))
159 | (_,Some Short,(Some(BaseType(FloatType CDouble)))) ->
160 raise (Semantic ("the only valid combination is long double", fake_pi))
161
162 | (_, Some _, Some _) ->
163 (* mine *)
164 raise (Semantic ("long, short valid only for int or float", fake_pi))
165
166 (* if do short uint i, then gcc say parse error, strange ? it is
167 * not a parse error, it is just that we dont allow with typedef
168 * either short/long or signed/unsigned. In fact, with
169 * parse_typedef_fix2 (with et() and dt()) now I say too parse
170 * error so this code is executed only when do short struct
171 * {....} and never with a typedef cos now we parse short uint i
172 * as short ident ident => parse error (cos after first short i
173 * pass in dt() mode) *)
174
175 )
176 in
177 ((qu, iiq),
178 (ty', iit'))
179 ,((st, inline),iist++iinl)
180
181
182 let fixDeclSpecForParam = function ({storageD = (st,iist)} as r) ->
183 let ((qu,ty) as v,_st) = fixDeclSpecForDecl r in
184 match st with
185 | (Sto Register) -> (v, true), iist
186 | NoSto -> (v, false), iist
187 | _ ->
188 raise
189 (Semantic ("storage class specified for parameter of function",
190 fake_pi))
191
192 let fixDeclSpecForMacro = function ({storageD = (st,iist)} as r) ->
193 let ((qu,ty) as v,_st) = fixDeclSpecForDecl r in
194 match st with
195 | NoSto -> v
196 | _ ->
197 raise
198 (Semantic ("storage class specified for macro type decl",
199 fake_pi))
200
201
202 let fixDeclSpecForFuncDef x =
203 let (returnType,storage) = fixDeclSpecForDecl x in
204 (match fst (unwrap storage) with
205 | StoTypedef ->
206 raise (Semantic ("function definition declared 'typedef'", fake_pi))
207 | _ -> (returnType, storage)
208 )
209
210
211 (* parameter: (this is the context where we give parameter only when
212 * in func DEFINITION not in funct DECLARATION) We must have a name.
213 * This function ensure that we give only parameterTypeDecl with well
214 * formed Classic constructor todo?: do we accept other declaration
215 * in ? so I must add them to the compound of the deffunc. I dont
216 * have to handle typedef pb here cos C forbid to do VF f { ... }
217 * with VF a typedef of func cos here we dont see the name of the
218 * argument (in the typedef)
219 *)
220 let (fixOldCDecl: fullType -> fullType) = fun ty ->
221 match Ast_c.unwrap_typeC ty with
222 | FunctionType (fullt, (params, (b, iib))) ->
223
224 (* stdC: If the prototype declaration declares a parameter for a
225 * function that you are defining (it is part of a function
226 * definition), then you must write a name within the declarator.
227 * Otherwise, you can omit the name. *)
228 (match params with
229 | [{p_namei = None; p_type = ty2},_] ->
230 (match Ast_c.unwrap_typeC ty2 with
231 | BaseType Void ->
232 ty
233 | _ ->
234 pr2 ("SEMANTIC:parameter name omitted, but I continue");
235 ty
236 )
237
238 | params ->
239 (params +> List.iter (fun (param,_) ->
240 match param with
241 | {p_namei = None} ->
242 (* if majuscule, then certainly macro-parameter *)
243 pr2 ("SEMANTIC:parameter name omitted, but I continue");
244 | _ -> ()
245 ));
246 ty
247 )
248
249 (* todo? can we declare prototype in the decl or structdef,
250 ... => length <> but good kan meme *)
251 | _ ->
252 (* gcc say parse error but dont see why *)
253 raise (Semantic ("seems this is not a function", fake_pi))
254
255
256 let fixFunc (typ, compound, old_style_opt) =
257 let (cp,iicp) = compound in
258
259 let (name, ty, (st,iist), attrs) = typ in
260
261 let (qu, tybis) = ty in
262
263 match Ast_c.unwrap_typeC ty with
264 | FunctionType (fullt, (params,abool)) ->
265 let iifunc = Ast_c.get_ii_typeC_take_care tybis in
266
267 let iistart = Ast_c.fakeInfo () in
268 assert (qu =*= nullQualif);
269
270 (match params with
271 | [{p_namei= None; p_type = ty2}, _] ->
272 (match Ast_c.unwrap_typeC ty2 with
273 | BaseType Void -> ()
274 | _ ->
275 (* failwith "internal errror: fixOldCDecl not good" *)
276 ()
277 )
278 | params ->
279 params +> List.iter (function
280 | ({p_namei = Some s}, _) -> ()
281 | _ -> ()
282 (* failwith "internal errror: fixOldCDecl not good" *)
283 )
284 );
285 (* bugfix: cf tests_c/function_pointer4.c.
286 * Apparemment en C on peut syntaxiquement ecrire ca:
287 *
288 * void a(int)(int x);
289 * mais apres gcc gueule au niveau semantique avec:
290 * xxx.c:1: error: 'a' declared as function returning a function
291 * Je ne faisais pas cette verif. Sur du code comme
292 * void METH(foo)(int x) { ...} , le parser croit (a tort) que foo
293 * est un typedef, et donc c'est parsé comme l'exemple precedent,
294 * ce qui ensuite confuse l'unparser qui n'est pas habitué
295 * a avoir dans le returnType un FunctionType et qui donc
296 * pr_elem les ii dans le mauvais sens ce qui genere au final
297 * une exception. Hence this fix to at least detect the error
298 * at parsing time (not unparsing time).
299 *)
300 (match Ast_c.unwrap_typeC fullt with
301 | FunctionType _ ->
302 let s = Ast_c.str_of_name name in
303 let iis = Ast_c.info_of_name name in
304 pr2 (spf "WEIRD: %s declared as function returning a function." s);
305 pr2 (spf "This is probably because of a macro. Extend standard.h");
306 raise (Semantic (spf "error: %s " s, Ast_c.parse_info_of_info iis))
307 | _ -> ()
308 );
309
310 (* it must be nullQualif,cos parser construct only this*)
311 {f_name = name;
312 f_type = (fullt, (params, abool));
313 f_storage = st;
314 f_body = cp;
315 f_attr = attrs;
316 f_old_c_style = old_style_opt;
317 },
318 (iifunc++iicp++[iistart]++iist)
319 | _ ->
320 raise
321 (Semantic
322 ("you are trying to do a function definition but you dont give " ^
323 "any parameter", fake_pi))
324
325
326 (*-------------------------------------------------------------------------- *)
327 (* parse_typedef_fix2 *)
328 (*-------------------------------------------------------------------------- *)
329
330 let dt s () =
331 if !Flag_parsing_c.debug_etdt then pr2 ("<" ^ s);
332 LP.disable_typedef ()
333
334 let et s () =
335 if !Flag_parsing_c.debug_etdt then pr2 (">" ^ s);
336 LP.enable_typedef ()
337
338
339 let fix_add_params_ident x =
340 let (s, ty, st, _attrs) = x in
341 match Ast_c.unwrap_typeC ty with
342 | FunctionType (fullt, (params, bool)) ->
343
344 (match params with
345 | [{p_namei=None; p_type=ty2}, _] ->
346 (match Ast_c.unwrap_typeC ty2 with
347 | BaseType Void -> ()
348 | _ ->
349 (* failwith "internal errror: fixOldCDecl not good" *)
350 ()
351 )
352 | params ->
353 params +> List.iter (function
354 | ({p_namei= Some name}, _) ->
355 LP.add_ident (Ast_c.str_of_name s)
356 | _ ->
357 ()
358 (* failwith "internal errror: fixOldCDecl not good" *)
359 )
360 )
361 | _ -> ()
362
363
364
365 (*-------------------------------------------------------------------------- *)
366 (* shortcuts *)
367 (*-------------------------------------------------------------------------- *)
368
369 let mk_e e ii = Ast_c.mk_e e ii
370
371 let mk_string_wrap (s,info) = (s, [info])
372
373 %}
374
375 /*(*****************************************************************************)*/
376 /*(* Tokens *)*/
377 /*(*************************************************************************)*/
378
379 /*
380 (*
381 * Some tokens are not even used in this file because they are filtered
382 * in some intermediate phase. But they still must be declared because
383 * ocamllex may generate them, or some intermediate phase may also
384 * generate them (like some functions in parsing_hacks.ml)
385 *)
386 */
387
388 %token <Ast_c.info> TUnknown /*(* unrecognized token *)*/
389
390 /*(* coupling: Token_helpers.is_real_comment *)*/
391 %token <Ast_c.info> TCommentSpace TCommentNewline TComment
392
393 /*(*-----------------------------------------*)*/
394 /*(* the normal tokens *)*/
395 /*(*-----------------------------------------*)*/
396
397 %token <(string * (Ast_c.sign * Ast_c.base)) * Ast_c.info> TInt
398 %token <(string * Ast_c.floatType) * Ast_c.info> TFloat
399 %token <(string * Ast_c.isWchar) * Ast_c.info> TChar
400 %token <(string * Ast_c.isWchar) * Ast_c.info> TString
401
402 %token <string * Ast_c.info> TIdent
403 %token <string * Ast_c.info> Tconstructorname /* parsing_hack for c++ */
404 /*(* appears mostly after some fix_xxx in parsing_hack *)*/
405 %token <string * Ast_c.info> TypedefIdent
406
407
408 /*
409 (* Some tokens like TOPar and TCPar are used as synchronisation stuff,
410 * in parsing_hack.ml. So if define special tokens like TOParDefine and
411 * TCParEOL, then take care to also modify in Token_helpers.
412 *)
413 */
414
415 %token <Ast_c.info> TOPar TCPar TOBrace TCBrace TOCro TCCro
416 %token <Ast_c.info> TDot TComma TPtrOp
417 %token <Ast_c.info> TInc TDec
418 %token <Ast_c.assignOp * Ast_c.info> TAssign
419 %token <Ast_c.info> TEq
420 %token <Ast_c.info> TWhy TTilde TBang
421 %token <Ast_c.info> TEllipsis
422 %token <Ast_c.info> TDotDot
423
424 %token <Ast_c.info> TPtVirg
425 %token <Ast_c.info>
426 TOrLog TAndLog TOr TXor TAnd TEqEq TNotEq TInf TSup TInfEq TSupEq
427 TShl TShr
428 TPlus TMinus TMul TDiv TMod
429
430 %token <Ast_c.info>
431 Tchar Tshort Tint Tdouble Tfloat Tlong Tunsigned Tsigned Tvoid
432 Tsize_t Tssize_t Tptrdiff_t
433 Tauto Tregister Textern Tstatic
434 Ttypedef
435 Tconst Tvolatile
436 Tstruct Tunion Tenum
437 Tbreak Telse Tswitch Tcase Tcontinue Tfor Tdo Tif Twhile Treturn
438 Tgoto Tdefault
439 Tsizeof Tnew
440
441 /*(* C99 *)*/
442 %token <Ast_c.info>
443 Trestrict
444
445 /*(*-----------------------------------------*)*/
446 /*(* gccext: extra tokens *)*/
447 /*(*-----------------------------------------*)*/
448 %token <Ast_c.info> Tasm
449 %token <Ast_c.info> Tattribute
450 %token <Ast_c.info> TattributeNoarg
451 %token <Ast_c.info> Tinline
452 %token <Ast_c.info> Ttypeof
453
454 /*(*-----------------------------------------*)*/
455 /*(* cppext: extra tokens *)*/
456 /*(*-----------------------------------------*)*/
457 /*(* coupling with Token_helpers.is_cpp_token *)*/
458
459
460 /*(*---------------*)*/
461 /*(* define *)*/
462 /*(*---------------*)*/
463
464 %token <Ast_c.info> TDefine
465 %token <(string * Ast_c.info)> TDefParamVariadic
466
467 /*(* disappear after fix_tokens_define *)*/
468 %token <Ast_c.info> TCppEscapedNewline
469
470 %token <Ast_c.info> TCppConcatOp
471
472 /*(* appear after fix_tokens_define *)*/
473 %token <Ast_c.info> TOParDefine
474 %token <Ast_c.info> TOBraceDefineInit
475
476 %token <(string * Ast_c.info)> TIdentDefine /*(* same *)*/
477 %token <Ast_c.info> TDefEOL /*(* same *)*/
478
479
480 /*(*---------------*)*/
481 /*(* include *)*/
482 /*(*---------------*)*/
483
484
485 /*(* used only in lexer_c, then transformed in comment or splitted in tokens *)*/
486 %token <(string * string * bool ref * Ast_c.info)> TInclude
487
488 /*(* tokens coming from above, generated in parse_c from TInclude, etc *)*/
489 %token <(Ast_c.info * bool ref)> TIncludeStart
490 %token <(string * Ast_c.info)> TIncludeFilename
491
492
493 /*(*---------------*)*/
494 /*(* ifdef *)*/
495 /*(*---------------*)*/
496
497 /*(* coupling: Token_helpers.is_cpp_instruction *)*/
498 %token <((int * int) option ref * Ast_c.info)>
499 TIfdef TIfdefelse TIfdefelif TEndif
500 %token <(bool * (int * int) option ref * Ast_c.info)>
501 TIfdefBool TIfdefMisc TIfdefVersion
502
503 /*(*---------------*)*/
504 /*(* other *)*/
505 /*(*---------------*)*/
506
507 %token <Ast_c.info> TUndef
508
509 %token <Ast_c.info> TCppDirectiveOther
510
511 /*(*---------------*)*/
512 /*(* macro use *)*/
513 /*(*---------------*)*/
514
515 /*(* appear after fix_tokens_cpp, cf also parsing_hacks#hint *)*/
516
517 %token <(string * Ast_c.info)> TMacroAttr
518 %token <(string * Ast_c.info)> TMacroStmt
519 %token <(string * Ast_c.info)> TMacroIdentBuilder
520 /*(* no need value for the moment *)*/
521 %token <(string * Ast_c.info)> TMacroString
522 %token <(string * Ast_c.info)> TMacroDecl
523 %token <Ast_c.info> TMacroDeclConst
524
525 %token <(string * Ast_c.info)> TMacroIterator
526 /*(*
527 %token <(string * Ast_c.info)> TMacroTop
528 %token <(string * Ast_c.info)> TMacroStructDecl
529 *)*/
530
531 %token <(string * Ast_c.info)> TMacroAttrStorage
532
533
534 /*(*---------------*)*/
535 /*(* other *)*/
536 /*(*---------------*)*/
537
538
539 /*(* should disappear after parsing_hack *)*/
540 %token <Ast_c.info> TCommentSkipTagStart TCommentSkipTagEnd
541
542
543 /*(* appear after parsing_hack *)*/
544 %token <Ast_c.info> TCParEOL
545
546 %token <Ast_c.info> TAction
547
548
549 /*(* TCommentMisc still useful ? obsolete ? *)*/
550 %token <Ast_c.info> TCommentMisc
551 %token <(Token_c.cppcommentkind * Ast_c.info)> TCommentCpp
552
553
554 /*(*-----------------------------------------*)*/
555 %token <Ast_c.info> EOF
556
557 /*(*-----------------------------------------*)*/
558
559 /*(* must be at the top so that it has the lowest priority *)*/
560 %nonassoc SHIFTHERE
561
562 %nonassoc Telse
563
564 %left TOrLog
565 %left TAndLog
566 %left TOr
567 %left TXor
568 %left TAnd
569 %left TEqEq TNotEq
570 %left TInf TSup TInfEq TSupEq
571 %left TShl TShr
572 %left TPlus TMinus
573 %left TMul TDiv TMod
574
575 /*(*************************************************************************)*/
576 /*(* Rules type declaration *)*/
577 /*(*************************************************************************)*/
578
579 %start main celem statement expr type_name
580 %type <Ast_c.program> main
581 %type <Ast_c.toplevel> celem
582
583 %type <Ast_c.statement> statement
584 %type <Ast_c.expression> expr
585 %type <Ast_c.fullType> type_name
586
587 %%
588 /*(*************************************************************************)*/
589 /*
590 (* TOC:
591 * toplevel (obsolete)
592 *
593 * ident
594 * expression
595 * statement
596 * types with
597 * - left part (type_spec, qualif),
598 * - right part (declarator, abstract declarator)
599 * - aux part (parameters)
600 * declaration, storage, initializers
601 * struct
602 * enum
603 * cpp directives
604 * celem (=~ main)
605 *
606 * generic workarounds (obrace, cbrace for context setting)
607 * xxx_list, xxx_opt
608 *)
609 */
610 /*(*************************************************************************)*/
611
612 /*(*************************************************************************)*/
613 /*(* toplevel *)*/
614 /*(*************************************************************************)*/
615 /*(* no more used; now that use error recovery *)*/
616
617 main:
618 translation_unit EOF { $1 }
619
620 translation_unit:
621 | external_declaration
622 { !LP._lexer_hint.context_stack <- [LP.InTopLevel]; [$1] }
623 | translation_unit external_declaration
624 { !LP._lexer_hint.context_stack <- [LP.InTopLevel]; $1 ++ [$2] }
625
626
627
628 /*(*************************************************************************)*/
629 /*(* ident *)*/
630 /*(*************************************************************************)*/
631
632 /*(* Why this ? Why not s/ident/TIdent ? cos there is multiple namespaces in C,
633 * so a label can have the same name that a typedef, same for field and tags
634 * hence sometimes the use of ident instead of TIdent.
635 *)*/
636 ident:
637 | TIdent { $1 }
638 | TypedefIdent { $1 }
639
640
641 identifier:
642 | TIdent { $1 }
643
644 /*
645 (* cppext: string concatenation of idents
646 * also cppext: gccext: ##args for variadic macro
647 *)
648 */
649 identifier_cpp:
650 | TIdent
651 { RegularName (mk_string_wrap $1) }
652 | ident_extra_cpp { $1 }
653
654 ident_cpp:
655 | TIdent
656 { RegularName (mk_string_wrap $1) }
657 | TypedefIdent
658 { RegularName (mk_string_wrap $1) }
659 | ident_extra_cpp { $1 }
660
661 ident_extra_cpp:
662 | TIdent TCppConcatOp identifier_cpp_list
663 {
664 CppConcatenatedName (
665 match $3 with
666 | [] -> raise Impossible
667 | (x,concatnull)::xs ->
668 assert(null concatnull);
669 (mk_string_wrap $1, [])::(x,[$2])::xs
670 )
671 }
672 | TCppConcatOp TIdent
673 { CppVariadicName (fst $2, [$1; snd $2]) }
674 | TMacroIdentBuilder TOPar param_define_list TCPar
675 { CppIdentBuilder ((fst $1, [snd $1;$2;$4]), $3) }
676
677 identifier_cpp_list:
678 | TIdent { [mk_string_wrap $1, []] }
679 | identifier_cpp_list TCppConcatOp TIdent { $1 ++ [mk_string_wrap $3, [$2]] }
680
681 /*(*************************************************************************)*/
682 /*(* expr *)*/
683 /*(*************************************************************************)*/
684
685 expr:
686 | assign_expr { $1 }
687 | expr TComma assign_expr { mk_e (Sequence ($1,$3)) [$2] }
688
689 /*(* bugfix: in C grammar they put unary_expr, but in fact it must be
690 * cast_expr, otherwise (int * ) xxx = &yy; is not allowed
691 *)*/
692 assign_expr:
693 | cond_expr { $1 }
694 | cast_expr TAssign assign_expr { mk_e(Assignment ($1,fst $2,$3)) [snd $2]}
695 | cast_expr TEq assign_expr { mk_e(Assignment ($1,SimpleAssign,$3)) [$2]}
696
697 /*(* gccext: allow optional then part hence gcc_opt_expr
698 * bugfix: in C grammar they put TDotDot cond_expr, but in fact it must be
699 * assign_expr, otherwise pnp ? x : x = 0x388 is not allowed
700 *)*/
701 cond_expr:
702 | arith_expr
703 { $1 }
704 | arith_expr TWhy gcc_opt_expr TDotDot assign_expr
705 { mk_e (CondExpr ($1,$3,$5)) [$2;$4] }
706
707
708 arith_expr:
709 | cast_expr { $1 }
710 | arith_expr TMul arith_expr { mk_e(Binary ($1, Arith Mul, $3)) [$2] }
711 | arith_expr TDiv arith_expr { mk_e(Binary ($1, Arith Div, $3)) [$2] }
712 | arith_expr TMod arith_expr { mk_e(Binary ($1, Arith Mod, $3)) [$2] }
713 | arith_expr TPlus arith_expr { mk_e(Binary ($1, Arith Plus, $3)) [$2] }
714 | arith_expr TMinus arith_expr { mk_e(Binary ($1, Arith Minus, $3)) [$2] }
715 | arith_expr TShl arith_expr { mk_e(Binary ($1, Arith DecLeft, $3)) [$2] }
716 | arith_expr TShr arith_expr { mk_e(Binary ($1, Arith DecRight, $3)) [$2] }
717 | arith_expr TInf arith_expr { mk_e(Binary ($1, Logical Inf, $3)) [$2] }
718 | arith_expr TSup arith_expr { mk_e(Binary ($1, Logical Sup, $3)) [$2] }
719 | arith_expr TInfEq arith_expr { mk_e(Binary ($1, Logical InfEq, $3)) [$2] }
720 | arith_expr TSupEq arith_expr { mk_e(Binary ($1, Logical SupEq, $3)) [$2] }
721 | arith_expr TEqEq arith_expr { mk_e(Binary ($1, Logical Eq, $3)) [$2] }
722 | arith_expr TNotEq arith_expr { mk_e(Binary ($1, Logical NotEq, $3)) [$2] }
723 | arith_expr TAnd arith_expr { mk_e(Binary ($1, Arith And, $3)) [$2] }
724 | arith_expr TOr arith_expr { mk_e(Binary ($1, Arith Or, $3)) [$2] }
725 | arith_expr TXor arith_expr { mk_e(Binary ($1, Arith Xor, $3)) [$2] }
726 | arith_expr TAndLog arith_expr { mk_e(Binary ($1, Logical AndLog, $3)) [$2] }
727 | arith_expr TOrLog arith_expr { mk_e(Binary ($1, Logical OrLog, $3)) [$2] }
728
729 cast_expr:
730 | unary_expr { $1 }
731 | topar2 type_name tcpar2 cast_expr { mk_e(Cast ($2, $4)) [$1;$3] }
732
733 unary_expr:
734 | postfix_expr { $1 }
735 | TInc unary_expr { mk_e(Infix ($2, Inc)) [$1] }
736 | TDec unary_expr { mk_e(Infix ($2, Dec)) [$1] }
737 | unary_op cast_expr { mk_e(Unary ($2, fst $1)) [snd $1] }
738 | Tsizeof unary_expr { mk_e(SizeOfExpr ($2)) [$1] }
739 | Tsizeof topar2 type_name tcpar2 { mk_e(SizeOfType ($3)) [$1;$2;$4] }
740 | Tnew new_argument { mk_e(New $2) [$1] }
741
742 new_argument:
743 | postfix_expr { Left $1 }
744 | decl_spec
745 { let ((returnType,hasreg), iihasreg) = fixDeclSpecForParam $1 in
746 Right (ArgType { p_namei = None; p_type = returnType;
747 p_register = hasreg, iihasreg;
748 } )
749 }
750
751 unary_op:
752 | TAnd { GetRef, $1 }
753 | TMul { DeRef, $1 }
754 | TPlus { UnPlus, $1 }
755 | TMinus { UnMinus, $1 }
756 | TTilde { Tilde, $1 }
757 | TBang { Not, $1 }
758 /*(* gccext: have that a lot in old kernel to get address of local label.
759 * cf gcc manual "local labels as values".
760 *)*/
761 | TAndLog { GetRefLabel, $1 }
762
763 postfix_expr:
764 | primary_expr { $1 }
765 | postfix_expr TOCro expr TCCro
766 { mk_e(ArrayAccess ($1, $3)) [$2;$4] }
767 | postfix_expr TOPar argument_list_ne TCPar
768 { mk_e(FunCall ($1, $3)) [$2;$4] }
769 | postfix_expr TOPar TCPar { mk_e(FunCall ($1, [])) [$2;$3] }
770 | postfix_expr TDot ident_cpp { mk_e(RecordAccess ($1,$3)) [$2] }
771 | postfix_expr TPtrOp ident_cpp { mk_e(RecordPtAccess ($1,$3)) [$2] }
772 | postfix_expr TInc { mk_e(Postfix ($1, Inc)) [$2] }
773 | postfix_expr TDec { mk_e(Postfix ($1, Dec)) [$2] }
774
775 /*(* gccext: also called compound literals *)*/
776 | topar2 type_name tcpar2 TOBrace TCBrace
777 { mk_e(Constructor ($2, [])) [$1;$3;$4;$5] }
778 | topar2 type_name tcpar2 TOBrace initialize_list gcc_comma_opt TCBrace
779 { mk_e(Constructor ($2, List.rev $5)) ([$1;$3;$4;$7] ++ $6) }
780
781 primary_expr:
782 | identifier_cpp { mk_e(Ident ($1)) [] }
783 | TInt
784 { let (str,(sign,base)) = fst $1 in
785 mk_e(Constant (Int (str,Si(sign,base)))) [snd $1] }
786 | TFloat { mk_e(Constant (Float (fst $1))) [snd $1] }
787 | TString { mk_e(Constant (String (fst $1))) [snd $1] }
788 | TChar { mk_e(Constant (Char (fst $1))) [snd $1] }
789 | TOPar expr TCPar { mk_e(ParenExpr ($2)) [$1;$3] } /*(* forunparser: *)*/
790
791 /*(* gccext: cppext: TODO better ast ? *)*/
792 | TMacroString { mk_e(Constant (MultiString [fst $1])) [snd $1] }
793 | string_elem string_list
794 { mk_e(Constant (MultiString ["TODO: MultiString"])) ($1 ++ $2) }
795
796 /*(* gccext: allow statement as expressions via ({ statement }) *)*/
797 | TOPar compound TCPar { mk_e(StatementExpr ($2)) [$1;$3] }
798
799
800
801 /*(*----------------------------*)*/
802 /*(* cppext: *)*/
803 /*(*----------------------------*)*/
804
805 /*(* cppext: *)*/
806 /*(* to avoid conflicts have to introduce a _not_empty (ne) version *)*/
807 argument_ne:
808 | assign_expr { Left $1 }
809 | parameter_decl { Right (ArgType $1) }
810 | action_higherordermacro_ne { Right (ArgAction $1) }
811
812
813 argument:
814 | assign_expr { Left $1 }
815 | parameter_decl { Right (ArgType $1) }
816 /*(* had conflicts before, but julia fixed them *)*/
817 | action_higherordermacro { Right (ArgAction $1) }
818
819 action_higherordermacro_ne:
820 | taction_list_ne
821 { if null $1
822 then ActMisc [Ast_c.fakeInfo()]
823 else ActMisc $1
824 }
825
826
827 action_higherordermacro:
828 | taction_list
829 { if null $1
830 then ActMisc [Ast_c.fakeInfo()]
831 else ActMisc $1
832 }
833
834
835 /*(*----------------------------*)*/
836 /*(* workarounds *)*/
837 /*(*----------------------------*)*/
838
839 /*(* would like evalInt $1 but require too much info *)*/
840 const_expr: cond_expr { $1 }
841
842
843 topar2: TOPar { et "topar2" (); $1 }
844 tcpar2: TCPar { et "tcpar2" (); $1 (*TODO? et ? sure ? c pas dt plutot ? *) }
845
846
847
848 /*(*************************************************************************)*/
849 /*(* statement *)*/
850 /*(*************************************************************************)*/
851
852 statement: statement2 { mk_st (fst $1) (snd $1) }
853
854 statement2:
855 | labeled { Labeled (fst $1), snd $1 }
856 | compound { Compound (fst $1), snd $1 }
857 | expr_statement { ExprStatement(fst $1), snd $1 }
858 | selection { Selection (fst $1), snd $1 ++ [fakeInfo()] }
859 | iteration { Iteration (fst $1), snd $1 ++ [fakeInfo()] }
860 | jump TPtVirg { Jump (fst $1), snd $1 ++ [$2] }
861
862 /*(* gccext: *)*/
863 | Tasm TOPar asmbody TCPar TPtVirg { Asm $3, [$1;$2;$4;$5] }
864 | Tasm Tvolatile TOPar asmbody TCPar TPtVirg { Asm $4, [$1;$2;$3;$5;$6] }
865
866 /*(* cppext: *)*/
867 | TMacroStmt { MacroStmt, [snd $1] }
868
869
870
871
872 /*(* note that case 1: case 2: i++; would be correctly parsed, but with
873 * a Case (1, (Case (2, i++))) :(
874 *)*/
875 labeled:
876 | ident_cpp TDotDot statement { Label ($1, $3), [$2] }
877 | Tcase const_expr TDotDot statement { Case ($2, $4), [$1; $3] }
878 | Tcase const_expr TEllipsis const_expr TDotDot statement
879 { CaseRange ($2, $4, $6), [$1;$3;$5] } /*(* gccext: allow range *)*/
880 | Tdefault TDotDot statement { Default $3, [$1; $2] }
881
882 end_labeled:
883 /*(* gccext: allow toto: }
884 * was generating each 30 shift/Reduce conflicts,
885 * mais ca va, ca fait ce qu'il faut.
886 * update: julia fixed the problem by introducing end_labeled
887 * and modifying below stat_or_decl_list
888 *)*/
889 | ident_cpp TDotDot
890 { Label ($1, (mk_st (ExprStatement None) Ast_c.noii)), [$2] }
891 | Tcase const_expr TDotDot
892 { Case ($2, (mk_st (ExprStatement None) Ast_c.noii)), [$1;$3] }
893 | Tdefault TDotDot
894 { Default (mk_st (ExprStatement None) Ast_c.noii), [$1; $2] }
895
896
897
898
899
900 compound: tobrace compound2 tcbrace { $2, [$1; $3] }
901
902
903 /*
904 (* cppext: because of cpp, some stuff looks like declaration but are in
905 * fact statement but too hard to figure out, and if parse them as
906 * expression, then we force to have first decls and then exprs, then
907 * will have a parse error. So easier to let mix decl/statement.
908 * Moreover it helps to not make such a difference between decl and
909 * statement for further coccinelle phases to factorize code.
910 *)*/
911 compound2:
912 | { ([]) }
913 | stat_or_decl_list { $1 }
914
915
916 stat_or_decl_list:
917 | stat_or_decl { [$1] }
918 /*(* gccext: to avoid conflicts, cf end_labeled above *)*/
919 | end_labeled { [StmtElem (mk_st (Labeled (fst $1)) (snd $1))] }
920 /*(* old: conflicts | stat_or_decl_list stat_or_decl { $1 ++ [$2] } *)*/
921 | stat_or_decl stat_or_decl_list { $1 :: $2 }
922
923 stat_or_decl:
924 | decl { StmtElem (mk_st (Decl ($1 Ast_c.LocalDecl)) Ast_c.noii) }
925 | statement { StmtElem $1 }
926
927 /*(* gccext: *)*/
928 | function_definition { StmtElem (mk_st (NestedFunc $1) Ast_c.noii) }
929
930 /* (* cppext: *)*/
931 | cpp_directive
932 { CppDirectiveStmt $1 }
933 | cpp_ifdef_directive/*(* stat_or_decl_list ...*)*/
934 { IfdefStmt $1 }
935
936
937 expr_statement:
938 | TPtVirg { None, [$1] }
939 | expr TPtVirg { Some $1, [$2] }
940
941 selection:
942 | Tif TOPar expr TCPar statement %prec SHIFTHERE
943 { If ($3, $5, (mk_st (ExprStatement None) Ast_c.noii)), [$1;$2;$4] }
944 | Tif TOPar expr TCPar statement Telse statement
945 { If ($3, $5, $7), [$1;$2;$4;$6] }
946 | Tswitch TOPar expr TCPar statement
947 { Switch ($3,$5), [$1;$2;$4] }
948
949 iteration:
950 | Twhile TOPar expr TCPar statement
951 { While ($3,$5), [$1;$2;$4] }
952 | Tdo statement Twhile TOPar expr TCPar TPtVirg
953 { DoWhile ($2,$5), [$1;$3;$4;$6;$7] }
954 | Tfor TOPar expr_statement expr_statement TCPar statement
955 { For ($3,$4,(None, []),$6), [$1;$2;$5]}
956 | Tfor TOPar expr_statement expr_statement expr TCPar statement
957 { For ($3,$4,(Some $5, []),$7), [$1;$2;$6] }
958 /*(* c++ext: for(int i = 0; i < n; i++)*)*/
959 | Tfor TOPar decl expr_statement expr_opt TCPar statement
960 {
961 (* pr2 "DECL in for"; *)
962 MacroIteration ("toto", [], $7),[$1;$2;$6] (* TODOfake ast, TODO need decl2 ? *)
963 }
964 /*(* cppext: *)*/
965 | TMacroIterator TOPar argument_list_ne TCPar statement
966 { MacroIteration (fst $1, $3, $5), [snd $1;$2;$4] }
967 | TMacroIterator TOPar TCPar statement
968 { MacroIteration (fst $1, [], $4), [snd $1;$2;$3] }
969
970 /*(* the ';' in the caller grammar rule will be appended to the infos *)*/
971 jump:
972 | Tgoto ident_cpp { Goto ($2), [$1] }
973 | Tcontinue { Continue, [$1] }
974 | Tbreak { Break, [$1] }
975 | Treturn { Return, [$1] }
976 | Treturn expr { ReturnExpr $2, [$1] }
977 | Tgoto TMul expr { GotoComputed $3, [$1;$2] }
978
979
980
981 /*(*----------------------------*)*/
982 /*(* gccext: *)*/
983 /*(*----------------------------*)*/
984 string_elem:
985 | TString { [snd $1] }
986 /*(* cppext: ex= printk (KERN_INFO "xxx" UTS_RELEASE) *)*/
987 | TMacroString { [snd $1] }
988
989
990 asmbody:
991 | string_list colon_asm_list { $1, $2 }
992 | string_list { $1, [] } /*(* in old kernel *)*/
993
994
995 colon_asm: TDotDot colon_option_list { Colon $2, [$1] }
996
997 colon_option:
998 | TString { ColonMisc, [snd $1] }
999 | TString TOPar asm_expr TCPar { ColonExpr $3, [snd $1; $2;$4] }
1000 /*(* cppext: certainly a macro *)*/
1001 | TOCro identifier TCCro TString TOPar asm_expr TCPar
1002 { ColonExpr $6, [$1;snd $2;$3;snd $4; $5; $7 ] }
1003 | identifier { ColonMisc, [snd $1] }
1004 | /*(* empty *)*/ { ColonMisc, [] }
1005
1006 asm_expr: assign_expr { $1 }
1007
1008 /*(*************************************************************************)*/
1009 /*(* types *)*/
1010 /*(*************************************************************************)*/
1011
1012
1013 /*(*-----------------------------------------------------------------------*)*/
1014 /*(* Type spec, left part of a type *)*/
1015 /*(*-----------------------------------------------------------------------*)*/
1016 type_spec2:
1017 | Tvoid { Right3 (BaseType Void), [$1] }
1018 | Tchar { Right3 (BaseType (IntType CChar)), [$1]}
1019 | Tint { Right3 (BaseType (IntType (Si (Signed,CInt)))), [$1]}
1020 | Tfloat { Right3 (BaseType (FloatType CFloat)), [$1]}
1021 | Tdouble { Right3 (BaseType (FloatType CDouble)), [$1] }
1022 | Tsize_t { Right3 (BaseType SizeType), [$1] }
1023 | Tssize_t { Right3 (BaseType SSizeType), [$1] }
1024 | Tptrdiff_t { Right3 (BaseType PtrDiffType), [$1] }
1025 | Tshort { Middle3 Short, [$1]}
1026 | Tlong { Middle3 Long, [$1]}
1027 | Tsigned { Left3 Signed, [$1]}
1028 | Tunsigned { Left3 UnSigned, [$1]}
1029 | struct_or_union_spec { Right3 (fst $1), snd $1 }
1030 | enum_spec { Right3 (fst $1), snd $1 }
1031
1032 /*
1033 (* parse_typedef_fix1: cant put: TIdent {} cos it make the grammar
1034 * ambiguous, generates lots of conflicts => we must
1035 * use some tricks: we make the lexer and parser cooperate, cf lexerParser.ml.
1036 *
1037 * parse_typedef_fix2: this is not enough, and you must use
1038 * parse_typedef_fix2 to fully manage typedef problems in grammar.
1039 *
1040 * parse_typedef_fix3:
1041 *
1042 * parse_typedef_fix4: try also to do now some consistency checking in
1043 * Parse_c
1044 *)*/
1045 | TypedefIdent
1046 { let name = RegularName (mk_string_wrap $1) in
1047 Right3 (TypeName (name, Ast_c.noTypedefDef())),[] }
1048
1049 | Ttypeof TOPar assign_expr TCPar { Right3 (TypeOfExpr ($3)), [$1;$2;$4] }
1050 | Ttypeof TOPar type_name TCPar { Right3 (TypeOfType ($3)), [$1;$2;$4] }
1051
1052 /*(*----------------------------*)*/
1053 /*(* workarounds *)*/
1054 /*(*----------------------------*)*/
1055
1056 type_spec: type_spec2 { dt "type" (); $1 }
1057
1058 /*(*-----------------------------------------------------------------------*)*/
1059 /*(* Qualifiers *)*/
1060 /*(*-----------------------------------------------------------------------*)*/
1061
1062 type_qualif:
1063 | Tconst { {const=true ; volatile=false}, $1 }
1064 | Tvolatile { {const=false ; volatile=true}, $1 }
1065 /*(* C99 *)*/
1066 | Trestrict { (* TODO *) {const=false ; volatile=false}, $1 }
1067
1068
1069 /*(*-----------------------------------------------------------------------*)*/
1070 /*(* gccext: attributes *)*/
1071 /*(*-----------------------------------------------------------------------*)*/
1072
1073 attribute:
1074 | Tattribute TOPar /*stuff*/ TCPar { raise Todo }
1075 /*(* cppext: *)*/
1076 | TMacroAttr { Attribute (fst $1), [snd $1] }
1077
1078 attribute_storage:
1079 | TMacroAttrStorage { $1 }
1080
1081 type_qualif_attr:
1082 | type_qualif { $1 }
1083 /*(*TODO !!!!! *)*/
1084 | TMacroAttr { {const=true ; volatile=false}, snd $1 }
1085
1086 /*(*-----------------------------------------------------------------------*)*/
1087 /*(* Declarator, right part of a type + second part of decl (the ident) *)*/
1088 /*(*-----------------------------------------------------------------------*)*/
1089
1090 /*
1091 (* declarator return a couple:
1092 * (name, partial type (a function to be applied to return type))
1093 *
1094 * when int* f(int) we must return Func(Pointer int,int) and not
1095 * Pointer (Func(int,int)
1096 *)*/
1097
1098 declarator:
1099 | pointer direct_d { (fst $2, fun x -> x +> $1 +> (snd $2) ) }
1100 | direct_d { $1 }
1101
1102 /*(* so must do int * const p; if the pointer is constant, not the pointee *)*/
1103 pointer:
1104 | tmul { fun x -> mk_ty (Pointer x) [$1] }
1105 | tmul pointer { fun x -> mk_ty (Pointer ($2 x)) [$1] }
1106 | tmul type_qualif_list
1107 { fun x -> ($2.qualifD, mk_tybis (Pointer x) [$1])}
1108 | tmul type_qualif_list pointer
1109 { fun x -> ($2.qualifD, mk_tybis (Pointer ($3 x)) [$1]) }
1110
1111 tmul:
1112 TMul { $1 }
1113 | TAnd
1114 { if !Flag.c_plus_plus
1115 then $1
1116 else
1117 let i = Ast_c.parse_info_of_info $1 in
1118 raise (Semantic("& not allowed in C types, try -c++ option", i)) }
1119
1120
1121 direct_d:
1122 | identifier_cpp
1123 { ($1, fun x -> x) }
1124 | TOPar declarator TCPar /*(* forunparser: old: $2 *)*/
1125 { (fst $2, fun x -> mk_ty (ParenType ((snd $2) x)) [$1;$3]) }
1126 | direct_d tocro tccro
1127 { (fst $1,fun x->(snd $1) (mk_ty (Array (None,x)) [$2;$3])) }
1128 | direct_d tocro const_expr tccro
1129 { (fst $1,fun x->(snd $1) (mk_ty (Array (Some $3,x)) [$2;$4])) }
1130 | direct_d topar tcpar
1131 { (fst $1,
1132 fun x->(snd $1)
1133 (mk_ty (FunctionType (x,(([],(false, []))))) [$2;$3]))
1134 }
1135 | direct_d topar parameter_type_list tcpar
1136 { (fst $1,fun x->(snd $1)
1137 (mk_ty (FunctionType (x, $3)) [$2;$4]))
1138 }
1139
1140
1141 /*(*----------------------------*)*/
1142 /*(* workarounds *)*/
1143 /*(*----------------------------*)*/
1144
1145 tocro: TOCro { et "tocro" ();$1 }
1146 tccro: TCCro { dt "tccro" ();$1 }
1147
1148 /*(*-----------------------------------------------------------------------*)*/
1149 abstract_declarator:
1150 | pointer { $1 }
1151 | direct_abstract_declarator { $1 }
1152 | pointer direct_abstract_declarator { fun x -> x +> $2 +> $1 }
1153
1154 direct_abstract_declarator:
1155 | TOPar abstract_declarator TCPar /*(* forunparser: old: $2 *)*/
1156 { fun x -> mk_ty (ParenType ($2 x)) [$1;$3] }
1157
1158 | TOCro TCCro
1159 { fun x -> mk_ty (Array (None, x)) [$1;$2] }
1160 | TOCro const_expr TCCro
1161 { fun x -> mk_ty (Array (Some $2, x)) [$1;$3] }
1162 | direct_abstract_declarator TOCro TCCro
1163 { fun x -> $1 (mk_ty (Array (None, x)) [$2;$3]) }
1164 | direct_abstract_declarator TOCro const_expr TCCro
1165 { fun x -> $1 (mk_ty (Array (Some $3,x)) [$2;$4]) }
1166 | TOPar TCPar
1167 { fun x -> mk_ty (FunctionType (x, ([], (false, [])))) [$1;$2] }
1168 | topar parameter_type_list tcpar
1169 { fun x -> mk_ty (FunctionType (x, $2)) [$1;$3] }
1170 /*(* subtle: here must also use topar, not TOPar, otherwise if have for
1171 * instance (xxx ( * )(xxx)) cast, then the second xxx may still be a Tident
1172 * but we want to reduce topar, to set the InParameter so that
1173 * parsing_hack can get a chance to change the type of xxx into a typedef.
1174 * That's an example where parsing_hack and the lookahead of ocamlyacc does
1175 * not go very well together ... we got the info too late. We got
1176 * a similar pb with xxx xxx; declaration, cf parsing_hack.ml and the
1177 * "disable typedef cos special case ..." message.
1178 *)*/
1179 | direct_abstract_declarator topar tcpar
1180 { fun x -> $1 (mk_ty (FunctionType (x, (([], (false, []))))) [$2;$3]) }
1181 | direct_abstract_declarator topar parameter_type_list tcpar
1182 { fun x -> $1 (mk_ty (FunctionType (x, $3)) [$2;$4]) }
1183
1184 /*(*-----------------------------------------------------------------------*)*/
1185 /*(* Parameters (use decl_spec not type_spec just for 'register') *)*/
1186 /*(*-----------------------------------------------------------------------*)*/
1187 parameter_type_list:
1188 | parameter_list { ($1, (false, []))}
1189 | parameter_list TComma TEllipsis { ($1, (true, [$2;$3])) }
1190
1191
1192 parameter_decl2:
1193 | decl_spec declaratorp
1194 { let ((returnType,hasreg),iihasreg) = fixDeclSpecForParam $1 in
1195 let (name, ftyp) = $2 in
1196 { p_namei = Some (name);
1197 p_type = ftyp returnType;
1198 p_register = (hasreg, iihasreg);
1199 }
1200 }
1201 | decl_spec abstract_declaratorp
1202 { let ((returnType,hasreg), iihasreg) = fixDeclSpecForParam $1 in
1203 { p_namei = None;
1204 p_type = $2 returnType;
1205 p_register = hasreg, iihasreg;
1206 }
1207 }
1208 | decl_spec
1209 { let ((returnType,hasreg), iihasreg) = fixDeclSpecForParam $1 in
1210 { p_namei = None;
1211 p_type = returnType;
1212 p_register = hasreg, iihasreg;
1213 }
1214 }
1215
1216
1217 /*(*----------------------------*)*/
1218 /*(* workarounds *)*/
1219 /*(*----------------------------*)*/
1220
1221 parameter_decl: parameter_decl2 { et "param" (); $1 }
1222 | attributes parameter_decl2 { et "param" (); $2 }
1223
1224 declaratorp:
1225 | declarator { LP.add_ident (str_of_name (fst $1)); $1 }
1226 /*(* gccext: *)*/
1227 | attributes declarator { LP.add_ident (str_of_name (fst $2)); $2 }
1228 | declarator attributes { LP.add_ident (str_of_name (fst $1)); $1 }
1229
1230 abstract_declaratorp:
1231 | abstract_declarator { $1 }
1232 /*(* gccext: *)*/
1233 | attributes abstract_declarator { $2 }
1234
1235 /*(*-----------------------------------------------------------------------*)*/
1236 /*(* helper type rules *)*/
1237 /*(*-----------------------------------------------------------------------*)*/
1238
1239 /*(* for struct and also typename *)*/
1240 /*(* cant put decl_spec cos no storage is allowed for field struct *)*/
1241 spec_qualif_list2:
1242 | type_spec { addTypeD ($1, nullDecl) }
1243 | type_qualif { {nullDecl with qualifD = (fst $1,[snd $1])}}
1244 | type_spec spec_qualif_list { addTypeD ($1,$2) }
1245 | type_qualif spec_qualif_list { addQualifD ($1,$2) }
1246
1247 spec_qualif_list: spec_qualif_list2 { dt "spec_qualif" (); $1 }
1248
1249
1250 /*(* for pointers in direct_declarator and abstract_declarator *)*/
1251 type_qualif_list:
1252 | type_qualif_attr { {nullDecl with qualifD = (fst $1,[snd $1])} }
1253 | type_qualif_list type_qualif_attr { addQualifD ($2,$1) }
1254
1255
1256
1257
1258
1259
1260 /*(*-----------------------------------------------------------------------*)*/
1261 /*(* xxx_type_id *)*/
1262 /*(*-----------------------------------------------------------------------*)*/
1263
1264 type_name:
1265 | spec_qualif_list
1266 { let (returnType, _) = fixDeclSpecForDecl $1 in returnType }
1267 | spec_qualif_list abstract_declaratort
1268 { let (returnType, _) = fixDeclSpecForDecl $1 in $2 returnType }
1269
1270
1271
1272 abstract_declaratort:
1273 | abstract_declarator { $1 }
1274 /*(* gccext: *)*/
1275 | attributes abstract_declarator { $2 }
1276
1277
1278 /*(*************************************************************************)*/
1279 /*(* declaration and initializers *)*/
1280 /*(*************************************************************************)*/
1281
1282 decl2:
1283 | decl_spec TPtVirg
1284 { function local ->
1285 let (returnType,storage) = fixDeclSpecForDecl $1 in
1286 let iistart = Ast_c.fakeInfo () in
1287 DeclList ([{v_namei = None; v_type = returnType;
1288 v_storage = unwrap storage; v_local = local;
1289 v_attr = Ast_c.noattr;
1290 v_type_bis = ref None;
1291 },[]],
1292 ($2::iistart::snd storage))
1293 }
1294 | decl_spec init_declarator_list TPtVirg
1295 { function local ->
1296 let (returnType,storage) = fixDeclSpecForDecl $1 in
1297 let iistart = Ast_c.fakeInfo () in
1298 DeclList (
1299 ($2 +> List.map (fun ((((name,f),attrs), ini), iivirg) ->
1300 let s = str_of_name name in
1301 let iniopt =
1302 match ini with
1303 | None -> None
1304 | Some (ini, iini) -> Some (iini, ini)
1305 in
1306 if fst (unwrap storage) =*= StoTypedef
1307 then LP.add_typedef s;
1308 {v_namei = Some (name, iniopt);
1309 v_type = f returnType;
1310 v_storage = unwrap storage;
1311 v_local = local;
1312 v_attr = attrs;
1313 v_type_bis = ref None;
1314 },
1315 iivirg
1316 )
1317 ), ($3::iistart::snd storage))
1318 }
1319 /*(* cppext: *)*/
1320
1321 | TMacroDecl TOPar argument_list TCPar TPtVirg
1322 { function _ ->
1323 MacroDecl ((fst $1, $3), [snd $1;$2;$4;$5;fakeInfo()]) }
1324 | Tstatic TMacroDecl TOPar argument_list TCPar TPtVirg
1325 { function _ ->
1326 MacroDecl ((fst $2, $4), [snd $2;$3;$5;$6;fakeInfo();$1]) }
1327 | Tstatic TMacroDeclConst TMacroDecl TOPar argument_list TCPar TPtVirg
1328 { function _ ->
1329 MacroDecl ((fst $3, $5), [snd $3;$4;$6;$7;fakeInfo();$1;$2])}
1330
1331
1332 /*(*-----------------------------------------------------------------------*)*/
1333 decl_spec2:
1334 | storage_class_spec { {nullDecl with storageD = (fst $1, [snd $1]) } }
1335 | type_spec { addTypeD ($1,nullDecl) }
1336 | type_qualif { {nullDecl with qualifD = (fst $1, [snd $1]) } }
1337 | Tinline { {nullDecl with inlineD = (true, [$1]) } }
1338 | storage_class_spec decl_spec2 { addStorageD ($1, $2) }
1339 | type_spec decl_spec2 { addTypeD ($1, $2) }
1340 | type_qualif decl_spec2 { addQualifD ($1, $2) }
1341 | Tinline decl_spec2 { addInlineD ((true, $1), $2) }
1342
1343 /*(* can simplify by putting all in _opt ? must have at least one otherwise
1344 * decl_list is ambiguous ? (no cos have ';' between decl)
1345 *)*/
1346
1347
1348 storage_class_spec2:
1349 | Tstatic { Sto Static, $1 }
1350 | Textern { Sto Extern, $1 }
1351 | Tauto { Sto Auto, $1 }
1352 | Tregister { Sto Register,$1 }
1353 | Ttypedef { StoTypedef, $1 }
1354
1355 storage_class_spec:
1356 /*(* gccext: *)*/
1357 | storage_class_spec2 { $1 }
1358 | storage_class_spec2 attribute_storage_list { $1 (* TODO *) }
1359
1360
1361
1362 /*(*----------------------------*)*/
1363 /*(* workarounds *)*/
1364 /*(*----------------------------*)*/
1365
1366 decl: decl2 { et "decl" (); $1 }
1367 decl_spec: decl_spec2 { dt "declspec" (); $1 }
1368
1369 /*(*-----------------------------------------------------------------------*)*/
1370 /*(* declarators (right part of type and variable) *)*/
1371 /*(*-----------------------------------------------------------------------*)*/
1372 init_declarator2:
1373 | declaratori { ($1, None) }
1374 | declaratori teq initialize { ($1, Some ($3, $2)) }
1375
1376
1377
1378 /*(*----------------------------*)*/
1379 /*(* workarounds *)*/
1380 /*(*----------------------------*)*/
1381 teq: TEq { et "teq" (); $1 }
1382
1383 init_declarator: init_declarator2 { dt "init" (); $1 }
1384
1385
1386 /*(*----------------------------*)*/
1387 /*(* gccext: *)*/
1388 /*(*----------------------------*)*/
1389
1390 declaratori:
1391 | declarator { LP.add_ident (str_of_name (fst $1)); $1, Ast_c.noattr }
1392 /*(* gccext: *)*/
1393 | declarator gcc_asm_decl { LP.add_ident (str_of_name (fst $1)); $1, Ast_c.noattr }
1394 /*(* gccext: *)*/
1395 | attributes declarator { LP.add_ident (str_of_name (fst $2)); $2, $1 }
1396 | declarator attributes { LP.add_ident (str_of_name (fst $1)); $1, Ast_c.noattr (* TODO *) }
1397
1398
1399
1400 gcc_asm_decl:
1401 | Tasm TOPar asmbody TCPar { }
1402 | Tasm Tvolatile TOPar asmbody TCPar { }
1403
1404
1405 /*(*-----------------------------------------------------------------------*)*/
1406 initialize:
1407 | assign_expr
1408 { InitExpr $1, [] }
1409 | tobrace_ini initialize_list gcc_comma_opt_struct tcbrace_ini
1410 { InitList (List.rev $2), [$1;$4]++$3 }
1411 | tobrace_ini tcbrace_ini
1412 { InitList [], [$1;$2] } /*(* gccext: *)*/
1413
1414
1415 /*
1416 (* opti: This time we use the weird order of non-terminal which requires in
1417 * the "caller" to do a List.rev cos quite critical. With this weird order it
1418 * allows yacc to use a constant stack space instead of exploding if we would
1419 * do a 'initialize2 Tcomma initialize_list'.
1420 *)
1421 */
1422 initialize_list:
1423 | initialize2 { [$1, []] }
1424 | initialize_list TComma initialize2 { ($3, [$2])::$1 }
1425
1426
1427 /*(* gccext: condexpr and no assign_expr cos can have ambiguity with comma *)*/
1428 initialize2:
1429 | cond_expr
1430 { InitExpr $1, [] }
1431 | tobrace_ini initialize_list gcc_comma_opt_struct tcbrace_ini
1432 { InitList (List.rev $2), [$1;$4]++$3 }
1433 | tobrace_ini tcbrace_ini
1434 { InitList [], [$1;$2] }
1435
1436 /*(* gccext: labeled elements, a.k.a designators *)*/
1437 | designator_list TEq initialize2
1438 { InitDesignators ($1, $3), [$2] }
1439
1440 /*(* gccext: old format *)*/
1441 | ident TDotDot initialize2
1442 { InitFieldOld (fst $1, $3), [snd $1; $2] } /*(* in old kernel *)*/
1443 /* conflict
1444 | TOCro const_expr TCCro initialize2
1445 { InitIndexOld ($2, $4), [$1;$3] }
1446 */
1447
1448
1449
1450 /*(* they can be nested, can have a .x[3].y *)*/
1451 designator:
1452 | TDot ident
1453 { DesignatorField (fst $2), [$1;snd $2] }
1454 | TOCro const_expr TCCro
1455 { DesignatorIndex ($2), [$1;$3] }
1456 | TOCro const_expr TEllipsis const_expr TCCro
1457 { DesignatorRange ($2, $4), [$1;$3;$5] }
1458
1459
1460 /*(*----------------------------*)*/
1461 /*(* workarounds *)*/
1462 /*(*----------------------------*)*/
1463
1464 gcc_comma_opt_struct:
1465 | TComma { [$1] }
1466 | /*(* empty *)*/ { [Ast_c.fakeInfo() +> Ast_c.rewrap_str ","] }
1467
1468
1469
1470
1471
1472
1473
1474
1475 /*(*************************************************************************)*/
1476 /*(* struct *)*/
1477 /*(*************************************************************************)*/
1478
1479 s_or_u_spec2:
1480 | struct_or_union ident tobrace_struct struct_decl_list_gcc tcbrace_struct
1481 { StructUnion (fst $1, Some (fst $2), $4), [snd $1;snd $2;$3;$5] }
1482 | struct_or_union tobrace_struct struct_decl_list_gcc tcbrace_struct
1483 { StructUnion (fst $1, None, $3), [snd $1;$2;$4] }
1484 | struct_or_union ident
1485 { StructUnionName (fst $1, fst $2), [snd $1;snd $2] }
1486
1487 struct_or_union2:
1488 | Tstruct { Struct, $1 }
1489 | Tunion { Union, $1 }
1490 /*(* gccext: *)*/
1491 | Tstruct attributes { Struct, $1 (* TODO *) }
1492 | Tunion attributes { Union, $1 (* TODO *) }
1493
1494
1495
1496 struct_decl2:
1497 | field_declaration { DeclarationField $1 }
1498 | TPtVirg { EmptyField $1 }
1499
1500 /*(* no conflict ? no need for a TMacroStruct ? apparently not as at struct
1501 * the rule are slightly different.
1502 *)*/
1503 | identifier TOPar argument_list TCPar TPtVirg
1504 { MacroDeclField ((fst $1, $3), [snd $1;$2;$4;$5;fakeInfo()]) }
1505
1506 /*(* cppext: *)*/
1507 | cpp_directive
1508 { CppDirectiveStruct $1 }
1509 | cpp_ifdef_directive/*(* struct_decl_list ... *)*/
1510 { IfdefStruct $1 }
1511
1512
1513 field_declaration:
1514 | spec_qualif_list struct_declarator_list TPtVirg
1515 {
1516 let (returnType,storage) = fixDeclSpecForDecl $1 in
1517 if fst (unwrap storage) <> NoSto
1518 then internal_error "parsing dont allow this";
1519
1520 FieldDeclList ($2 +> (List.map (fun (f, iivirg) ->
1521 f returnType, iivirg))
1522 ,[$3])
1523 (* dont need to check if typedef or func initialised cos
1524 * grammar dont allow typedef nor initialiser in struct
1525 *)
1526 }
1527
1528 | spec_qualif_list TPtVirg
1529 {
1530 (* gccext: allow empty elements if it is a structdef or enumdef *)
1531 let (returnType,storage) = fixDeclSpecForDecl $1 in
1532 if fst (unwrap storage) <> NoSto
1533 then internal_error "parsing dont allow this";
1534
1535 FieldDeclList ([(Simple (None, returnType)) , []], [$2])
1536 }
1537
1538
1539
1540
1541
1542 struct_declarator:
1543 | declaratorsd
1544 { (fun x -> Simple (Some (fst $1), (snd $1) x)) }
1545 | dotdot const_expr2
1546 { (fun x -> BitField (None, x, $1, $2)) }
1547 | declaratorsd dotdot const_expr2
1548 { (fun x -> BitField (Some (fst $1), ((snd $1) x), $2, $3)) }
1549
1550
1551 /*(*----------------------------*)*/
1552 /*(* workarounds *)*/
1553 /*(*----------------------------*)*/
1554 declaratorsd:
1555 | declarator { (*also ? LP.add_ident (fst (fst $1)); *) $1 }
1556 /*(* gccext: *)*/
1557 | attributes declarator { $2 }
1558 | declarator attributes { $1 }
1559
1560
1561
1562
1563 struct_or_union_spec: s_or_u_spec2 { dt "su" (); $1 }
1564 struct_or_union: struct_or_union2 { et "su" (); $1 }
1565 struct_decl: struct_decl2 { et "struct" (); $1 }
1566
1567 dotdot: TDotDot { et "dotdot" (); $1 }
1568 const_expr2: const_expr { dt "const_expr2" (); $1 }
1569
1570 struct_decl_list_gcc:
1571 | struct_decl_list { $1 }
1572 | /*(* empty *)*/ { [] } /*(* gccext: allow empty struct *)*/
1573
1574
1575 /*(*************************************************************************)*/
1576 /*(* enum *)*/
1577 /*(*************************************************************************)*/
1578 enum_spec:
1579 | Tenum tobrace_enum enumerator_list gcc_comma_opt_struct tcbrace_enum
1580 { Enum (None, $3), [$1;$2;$5] ++ $4 }
1581 | Tenum ident tobrace_enum enumerator_list gcc_comma_opt_struct tcbrace_enum
1582 { Enum (Some (fst $2), $4), [$1; snd $2; $3;$6] ++ $5 }
1583 | Tenum ident
1584 { EnumName (fst $2), [$1; snd $2] }
1585
1586 enumerator:
1587 | idente { $1, None }
1588 | idente TEq const_expr { $1, Some ($2, $3) }
1589
1590
1591 /*(*----------------------------*)*/
1592 /*(* workarounds *)*/
1593 /*(*----------------------------*)*/
1594
1595 idente: ident_cpp { LP.add_ident (str_of_name $1); $1 }
1596
1597
1598
1599 /*(*************************************************************************)*/
1600 /*(* function *)*/
1601 /*(*************************************************************************)*/
1602 function_definition: function_def { fixFunc $1 }
1603
1604 decl_list:
1605 | decl { [$1 Ast_c.LocalDecl] }
1606 | decl_list decl { $1 ++ [$2 Ast_c.LocalDecl] }
1607
1608 /* hack : to drop when a better solution is found */
1609 cpp_directive_list:
1610 | cpp_directive { }
1611 | cpp_directive_list cpp_directive { }
1612
1613 function_def:
1614 | start_fun compound { LP.del_scope(); ($1, $2, None) }
1615 | start_fun cpp_directive_list compound { LP.del_scope(); ($1, $3, None) }
1616 | start_fun decl_list compound {
1617 (* TODO: undo the typedef added ? *)
1618 LP.del_scope();
1619 ($1, $3, Some $2)
1620 }
1621
1622 start_fun: start_fun2
1623 { LP.new_scope();
1624 fix_add_params_ident $1;
1625 (* toreput? !LP._lexer_hint.toplevel <- false; *)
1626 $1
1627 }
1628
1629 start_fun2: decl_spec declaratorfd
1630 { let (returnType,storage) = fixDeclSpecForFuncDef $1 in
1631 let (id, attrs) = $2 in
1632 (fst id, fixOldCDecl ((snd id) returnType) , storage, attrs)
1633 }
1634 | ctor_dtor { $1 }
1635
1636 ctor_dtor:
1637 | Tconstructorname topar tcpar {
1638 let id = RegularName (mk_string_wrap $1) in
1639 let ret = mk_ty NoType [] in
1640 let ty = mk_ty (FunctionType (ret, (([], (false, []))))) [$2;$3] in
1641 let storage = ((NoSto,false),[]) in
1642 let attrs = [] in
1643 (id, ty, storage, attrs) }
1644 | Tconstructorname topar parameter_type_list tcpar {
1645 let id = RegularName (mk_string_wrap $1) in
1646 let ret = mk_ty NoType [] in
1647 let ty = mk_ty (FunctionType (ret, $3)) [$2;$4] in
1648 let storage = ((NoSto,false),[]) in
1649 let attrs = [] in
1650 (id, ty, storage, attrs) }
1651
1652 /*(*----------------------------*)*/
1653 /*(* workarounds *)*/
1654 /*(*----------------------------*)*/
1655
1656 /* It would be very nice if we could make declarator aware that this is
1657 coming from a function definition. Then on the ( and ) cases, it could
1658 set the state to something other than InParameter. Then the case
1659 (TIdent (s, i1)::(TComma _|TCPar _)::_ , (TComma _ |TOPar _)::_ )
1660 in parsing_hacks.ml would not have to consider K&R variable declarations
1661 as typedefs. Unfortunately, doing something about this problem seems to
1662 introduce conflicts in the parser. */
1663
1664 declaratorfd:
1665 | declarator { et "declaratorfd" (); $1, Ast_c.noattr }
1666 /*(* gccext: *)*/
1667 | attributes declarator { et "declaratorfd" (); $2, $1 }
1668 | declarator attributes { et "declaratorfd" (); $1, Ast_c.noattr }
1669
1670
1671
1672 /*(*************************************************************************)*/
1673 /*(* cpp directives *)*/
1674 /*(*************************************************************************)*/
1675
1676 cpp_directive:
1677 | TIncludeStart TIncludeFilename
1678 {
1679 let (i1, in_ifdef) = $1 in
1680 let (s, i2) = $2 in
1681
1682 (* redo some lexing work :( *)
1683 let inc_file =
1684 match () with
1685 | _ when s =~ "^\"\\(.*\\)\"$" ->
1686 Local (Common.split "/" (matched1 s))
1687 | _ when s =~ "^\\<\\(.*\\)\\>$" ->
1688 NonLocal (Common.split "/" (matched1 s))
1689 | _ ->
1690 Weird s
1691 in
1692 Include { i_include = (inc_file, [i1;i2]);
1693 i_rel_pos = Ast_c.noRelPos();
1694 i_is_in_ifdef = !in_ifdef;
1695 i_content = Ast_c.noi_content;
1696 }
1697 }
1698
1699 | TDefine TIdentDefine define_val TDefEOL
1700 { Define ((fst $2, [$1; snd $2;$4]), (DefineVar, $3)) }
1701
1702 /*
1703 (* The TOParDefine is introduced to avoid ambiguity with previous rules.
1704 * A TOParDefine is a TOPar that was just next to the ident.
1705 *)*/
1706 | TDefine TIdentDefine TOParDefine param_define_list TCPar define_val TDefEOL
1707 { Define
1708 ((fst $2, [$1; snd $2; $7]),
1709 (DefineFunc ($4, [$3;$5]), $6))
1710 }
1711
1712 | TUndef TIdentDefine TDefEOL
1713 { Define((fst $2, [$1; snd $2; $3]), (Undef,DefineEmpty)) }
1714 | TCppDirectiveOther { PragmaAndCo ([$1]) }
1715
1716
1717
1718
1719
1720 /*(* perhaps better to use assign_expr ? but in that case need
1721 * do a assign_expr_of_string in parse_c
1722 *)*/
1723 define_val:
1724 | expr { DefineExpr $1 }
1725 | statement { DefineStmt $1 }
1726 | decl { DefineStmt (mk_st (Decl ($1 Ast_c.NotLocalDecl)) Ast_c.noii) }
1727
1728 /*(*old:
1729 * | TypedefIdent { DefineType (nQ,(TypeName(fst $1,noTypedefDef()),[snd $1]))}
1730 * get conflicts:
1731 * | spec_qualif_list TMul
1732 * { let (returnType, _) = fixDeclSpecForDecl $1 in DefineType returnType }
1733 *)
1734 */
1735 | decl_spec
1736 { let returnType = fixDeclSpecForMacro $1 in
1737 DefineType returnType
1738 }
1739 | decl_spec abstract_declarator
1740 { let returnType = fixDeclSpecForMacro $1 in
1741 let typ = $2 returnType in
1742 DefineType typ
1743 }
1744
1745 /* can be in conflict with decl_spec, maybe change fixDeclSpecForMacro
1746 * to also allow storage ?
1747 | storage_class_spec { DefineTodo }
1748 | Tinline { DefineTodo }
1749 */
1750
1751 /*(* a few special cases *)*/
1752 | stat_or_decl stat_or_decl_list { DefineTodo }
1753 /*
1754 | statement statement { DefineTodo }
1755 | decl function_definition { DefineTodo }
1756 */
1757
1758
1759
1760
1761 | function_definition { DefineFunction $1 }
1762
1763 | TOBraceDefineInit initialize_list gcc_comma_opt_struct TCBrace comma_opt
1764 { DefineInit (InitList (List.rev $2), [$1;$4]++$3++$5) }
1765
1766 /*(* note: had a conflict before when were putting TInt instead of expr *)*/
1767 | Tdo statement Twhile TOPar expr TCPar
1768 {
1769 (* TOREPUT
1770 if fst $5 <> "0"
1771 then pr2 "WEIRD: in macro and have not a while(0)";
1772 *)
1773 DefineDoWhileZero (($2,$5), [$1;$3;$4;$6])
1774 }
1775
1776 | Tasm TOPar asmbody TCPar { DefineTodo }
1777 | Tasm Tvolatile TOPar asmbody TCPar { DefineTodo }
1778
1779 /*(* aliases macro *)*/
1780 | TMacroAttr { DefineTodo }
1781
1782 | /*(* empty *)*/ { DefineEmpty }
1783
1784
1785
1786
1787 param_define:
1788 | TIdent { mk_string_wrap $1 }
1789 | TypedefIdent { mk_string_wrap $1 }
1790 | TDefParamVariadic { mk_string_wrap $1 }
1791 | TEllipsis { "...", [$1] }
1792 /*(* they reuse keywords :( *)*/
1793 | Tregister { "register", [$1] }
1794
1795
1796
1797
1798 cpp_ifdef_directive:
1799 | TIfdef
1800 { let (tag,ii) = $1 in
1801 IfdefDirective ((Ifdef, IfdefTag (Common.some !tag)), [ii]) }
1802 | TIfdefelse
1803 { let (tag,ii) = $1 in
1804 IfdefDirective ((IfdefElse, IfdefTag (Common.some !tag)), [ii]) }
1805 | TIfdefelif
1806 { let (tag,ii) = $1 in
1807 IfdefDirective ((IfdefElseif, IfdefTag (Common.some !tag)), [ii]) }
1808 | TEndif
1809 { let (tag,ii) = $1 in
1810 IfdefDirective ((IfdefEndif, IfdefTag (Common.some !tag)), [ii]) }
1811
1812 | TIfdefBool
1813 { let (_b, tag,ii) = $1 in
1814 IfdefDirective ((Ifdef, IfdefTag (Common.some !tag)), [ii]) }
1815 | TIfdefMisc
1816 { let (_b, tag,ii) = $1 in
1817 IfdefDirective ((Ifdef, IfdefTag (Common.some !tag)), [ii]) }
1818 | TIfdefVersion
1819 { let (_b, tag,ii) = $1 in
1820 IfdefDirective ((Ifdef, IfdefTag (Common.some !tag)), [ii]) }
1821
1822
1823 /*(* cppext: *)*/
1824 cpp_other:
1825 /*(* no conflict ? no need for a TMacroTop ? apparently not as at toplevel
1826 * the rule are slightly different, they cant be statement and so expr
1827 * at the top, only decl or function definition.
1828 *)*/
1829 | identifier TOPar argument_list TCPar TPtVirg
1830 {
1831 Declaration (MacroDecl ((fst $1, $3), [snd $1;$2;$4;$5;fakeInfo()]))
1832 (* old: MacroTop (fst $1, $3, [snd $1;$2;$4;$5]) *)
1833 }
1834
1835 /*(* TCParEOL to fix the end-of-stream bug of ocamlyacc *)*/
1836 | identifier TOPar argument_list TCParEOL
1837 { MacroTop (fst $1, $3, [snd $1;$2;$4;fakeInfo()]) }
1838
1839 /*(* ex: EXPORT_NO_SYMBOLS; *)*/
1840 | identifier TPtVirg { EmptyDef [snd $1;$2] }
1841
1842
1843
1844 /*(*************************************************************************)*/
1845 /*(* celem *)*/
1846 /*(*************************************************************************)*/
1847
1848 external_declaration:
1849 | function_definition { Definition $1 }
1850 | decl { Declaration ($1 Ast_c.NotLocalDecl) }
1851
1852
1853 celem:
1854 | external_declaration { $1 }
1855
1856 /*(* cppext: *)*/
1857 | cpp_directive
1858 { CppTop $1 }
1859 | cpp_other
1860 { $1 }
1861 | cpp_ifdef_directive /* (*external_declaration_list ...*)*/
1862 { IfdefTop $1 }
1863
1864 /*(* can have asm declaration at toplevel *)*/
1865 | Tasm TOPar asmbody TCPar TPtVirg { EmptyDef [$1;$2;$4;$5] }
1866
1867 /*
1868 (* in ~/kernels/src/linux-2.5.2/drivers/isdn/hisax/isdnl3.c sometimes
1869 * the function ends with }; instead of just }
1870 * can also remove this rule and report "parse error" pb to morton
1871 *)*/
1872 | TPtVirg { EmptyDef [$1] }
1873
1874
1875 | EOF { FinalDef $1 }
1876
1877
1878
1879
1880 /*(*************************************************************************)*/
1881 /*(* some generic workarounds *)*/
1882 /*(*************************************************************************)*/
1883
1884 tobrace: TOBrace { LP.push_context LP.InFunction; LP.new_scope (); $1 }
1885 tcbrace: TCBrace { LP.pop_context(); LP.del_scope (); $1 }
1886
1887 tobrace_enum: TOBrace { LP.push_context LP.InEnum; $1 }
1888 tcbrace_enum: TCBrace { LP.pop_context (); $1 }
1889
1890 tobrace_ini: TOBrace { LP.push_context LP.InInitializer; $1 }
1891 tcbrace_ini: TCBrace { LP.pop_context (); $1 }
1892
1893 tobrace_struct: TOBrace { LP.push_context LP.InStruct; $1}
1894 tcbrace_struct: TCBrace { LP.pop_context (); $1 }
1895
1896
1897
1898
1899 topar: TOPar
1900 { LP.new_scope ();et "topar" ();
1901 LP.push_context LP.InParameter;
1902 $1
1903 }
1904 tcpar: TCPar
1905 { LP.del_scope ();dt "tcpar" ();
1906 LP.pop_context ();
1907 $1
1908 }
1909
1910
1911
1912
1913 /*(*************************************************************************)*/
1914 /*(* xxx_list, xxx_opt *)*/
1915 /*(*************************************************************************)*/
1916
1917
1918 /*(* old:
1919 compound2:
1920 | { ([],[]) }
1921 | statement_list { ([], $1) }
1922 | decl_list { ($1, []) }
1923 | decl_list statement_list { ($1,$2) }
1924
1925 statement_list: stat_or_decl_list { $1 }
1926 *)*/
1927
1928
1929 /*(*
1930 decl_list:
1931 | decl { [$1] }
1932 | decl_list decl { $1 ++ [$2] }
1933
1934 statement_list:
1935 | statement { [$1] }
1936 | statement_list statement { $1 ++ [$2] }
1937 *)*/
1938
1939
1940
1941
1942
1943 string_list:
1944 | string_elem { $1 }
1945 | string_list string_elem { $1 ++ $2 }
1946
1947 colon_asm_list:
1948 | colon_asm { [$1] }
1949 | colon_asm_list colon_asm { $1 ++ [$2] }
1950
1951 colon_option_list:
1952 | colon_option { [$1, []] }
1953 | colon_option_list TComma colon_option { $1 ++ [$3, [$2]] }
1954
1955
1956 argument_list_ne:
1957 | argument_ne { [$1, []] }
1958 | argument_list_ne TComma argument { $1 ++ [$3, [$2]] }
1959
1960 argument_list:
1961 | argument { [$1, []] }
1962 | argument_list TComma argument { $1 ++ [$3, [$2]] }
1963
1964 /*(*
1965 expression_list:
1966 | assign_expr { [$1, []] }
1967 | expression_list TComma assign_expr { $1 ++ [$3, [$2]] }
1968 *)*/
1969
1970
1971 struct_decl_list:
1972 | struct_decl { [$1] }
1973 | struct_decl_list struct_decl { $1 ++ [$2] }
1974
1975
1976 struct_declarator_list:
1977 | struct_declarator { [$1, []] }
1978 | struct_declarator_list TComma struct_declarator { $1 ++ [$3, [$2]] }
1979
1980
1981 enumerator_list:
1982 | enumerator { [$1, []] }
1983 | enumerator_list TComma enumerator { $1 ++ [$3, [$2]] }
1984
1985
1986 init_declarator_list:
1987 | init_declarator { [$1, []] }
1988 | init_declarator_list TComma init_declarator { $1 ++ [$3, [$2]] }
1989
1990
1991 parameter_list:
1992 | parameter_decl { [$1, []] }
1993 | parameter_list TComma parameter_decl { $1 ++ [$3, [$2]] }
1994
1995 taction_list_ne:
1996 | TAction { [$1] }
1997 | TAction taction_list_ne { $1 :: $2 }
1998
1999 taction_list:
2000 /*old: was generating conflict, hence now taction_list_ne
2001 | (* empty *) { [] }
2002 | TAction { [$1] }
2003 | taction_list TAction { $1 ++ [$2] }
2004 */
2005 | { [] }
2006 | TAction taction_list { $1 :: $2 }
2007
2008 param_define_list:
2009 | /*(* empty *)*/ { [] }
2010 | param_define { [$1, []] }
2011 | param_define_list TComma param_define { $1 ++ [$3, [$2]] }
2012
2013 designator_list:
2014 | designator { [$1] }
2015 | designator_list designator { $1 ++ [$2] }
2016
2017 attribute_list:
2018 | attribute { [$1] }
2019 | attribute_list attribute { $1 ++ [$2] }
2020
2021 attribute_storage_list:
2022 | attribute_storage { [$1] }
2023 | attribute_storage_list attribute_storage { $1 ++ [$2] }
2024
2025
2026 attributes: attribute_list { $1 }
2027
2028
2029
2030 /*(* gccext: which allow a trailing ',' in enum, as in perl *)*/
2031 gcc_comma_opt:
2032 | TComma { [$1] }
2033 | /*(* empty *)*/ { [] }
2034
2035 comma_opt:
2036 | TComma { [$1] }
2037 | /*(* empty *)*/ { [] }
2038
2039 /*(*
2040 gcc_opt_virg:
2041 | TPtVirg { }
2042 | { }
2043 *)*/
2044
2045 gcc_opt_expr:
2046 | expr { Some $1 }
2047 | /*(* empty *)*/ { None }
2048
2049 /*(*
2050 opt_ptvirg:
2051 | TPtVirg { [$1] }
2052 | { [] }
2053 *)*/
2054
2055
2056 expr_opt:
2057 | expr { Some $1 }
2058 | /*(* empty *)*/ { None }
2059