4 * Copyright (C) 2002, 2006, 2007, 2008, 2009 Yoann Padioleau
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.
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.
19 module LP = Lexer_parser
20 open Lexer_parser (* for the fields *)
22 open Semantic_c (* Semantic exn *)
25 (*****************************************************************************)
27 (*****************************************************************************)
29 if !Flag_parsing_c.verbose_parsing
30 then Common.warning ("PARSING: " ^ s) v
33 let pr2, pr2_once = Common.mk_pr2_wrappers Flag_parsing_c.verbose_parsing
35 (*****************************************************************************)
36 (* Parse helpers functions *)
37 (*****************************************************************************)
39 (*-------------------------------------------------------------------------- *)
41 (*-------------------------------------------------------------------------- *)
43 type shortLong = Short | Long | LongLong
46 storageD: storagebis wrap;
47 typeD: ((sign option) * (shortLong option) * (typeCbis option)) wrap;
48 qualifD: typeQualifierbis 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 *)
57 typeD = (None, None, None), [];
61 let fake_pi = Common.fake_parse_info
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))
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
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}
85 | ((Middle3 Short,ii), ({typeD = ((a,Some Short,c),ii2)} as v)) ->
86 warning "duplicate 'short'" v
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
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}
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}
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}
113 internal_error "there is no noconst or novolatile keyword"
115 let addQualifD ((qu,ii), ({qualifD = (v,ii2)} as x)) =
116 { x with qualifD = (addQualif (qu, v),ii::ii2) }
119 (*-------------------------------------------------------------------------- *)
120 (* Declaration/Function related *)
121 (*-------------------------------------------------------------------------- *)
124 (* stdC: type section, basic integer types (and ritchie)
125 * To understand the code, just look at the result (right part of the PM)
128 let (fixDeclSpecForDecl: decl -> (fullType * (storage wrap))) = function
129 {storageD = (st,iist);
132 inlineD = (inline,iinl);
136 | (None,None,None) ->
137 (* generate fake_info, otherwise type_annotater can crash in
140 warning "type defaults to 'int'" (defaultInt, [fakeInfo fake_pi])
141 | (None, None, Some t) -> (t, iit)
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
154 | (Some _,_, Some _) ->
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))
162 | (_, Some _, Some _) ->
164 raise (Semantic ("long, short valid only for int or float", fake_pi))
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) *)
179 ,((st, inline),iist++iinl)
182 let fixDeclSpecForParam = function ({storageD = (st,iist)} as r) ->
183 let ((qu,ty) as v,_st) = fixDeclSpecForDecl r in
185 | (Sto Register) -> (v, true), iist
186 | NoSto -> (v, false), iist
189 (Semantic ("storage class specified for parameter of function",
192 let fixDeclSpecForMacro = function ({storageD = (st,iist)} as r) ->
193 let ((qu,ty) as v,_st) = fixDeclSpecForDecl r in
198 (Semantic ("storage class specified for macro type decl",
202 let fixDeclSpecForFuncDef x =
203 let (returnType,storage) = fixDeclSpecForDecl x in
204 (match fst (unwrap storage) with
206 raise (Semantic ("function definition declared 'typedef'", fake_pi))
207 | _ -> (returnType, storage)
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)
220 let (fixOldCDecl: fullType -> fullType) = fun ty ->
221 match Ast_c.unwrap_typeC ty with
222 | FunctionType (fullt, (params, (b, iib))) ->
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. *)
229 | [{p_namei = None; p_type = ty2},_] ->
230 (match Ast_c.unwrap_typeC ty2 with
234 pr2 ("SEMANTIC:parameter name omitted, but I continue");
239 (params +> List.iter (fun (param,_) ->
241 | {p_namei = None} ->
242 (* if majuscule, then certainly macro-parameter *)
243 pr2 ("SEMANTIC:parameter name omitted, but I continue");
249 (* todo? can we declare prototype in the decl or structdef,
250 ... => length <> but good kan meme *)
252 (* gcc say parse error but dont see why *)
253 raise (Semantic ("seems this is not a function", fake_pi))
256 let fixFunc (typ, compound, old_style_opt) =
257 let (cp,iicp) = compound in
259 let (name, ty, (st,iist), attrs) = typ in
261 let (qu, tybis) = ty in
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
267 let iistart = Ast_c.fakeInfo () in
268 assert (qu =*= nullQualif);
271 | [{p_namei= None; p_type = ty2}, _] ->
272 (match Ast_c.unwrap_typeC ty2 with
273 | BaseType Void -> ()
275 (* failwith "internal errror: fixOldCDecl not good" *)
279 params +> List.iter (function
280 | ({p_namei = Some s}, _) -> ()
282 (* failwith "internal errror: fixOldCDecl not good" *)
285 (* bugfix: cf tests_c/function_pointer4.c.
286 * Apparemment en C on peut syntaxiquement ecrire ca:
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).
300 (match Ast_c.unwrap_typeC fullt with
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))
310 (* it must be nullQualif,cos parser construct only this*)
312 f_type = (fullt, (params, abool));
316 f_old_c_style = old_style_opt;
318 (iifunc++iicp++[iistart]++iist)
322 ("you are trying to do a function definition but you dont give " ^
323 "any parameter", fake_pi))
326 (*-------------------------------------------------------------------------- *)
327 (* parse_typedef_fix2 *)
328 (*-------------------------------------------------------------------------- *)
331 if !Flag_parsing_c.debug_etdt then pr2 ("<" ^ s);
332 LP.disable_typedef ()
335 if !Flag_parsing_c.debug_etdt then pr2 (">" ^ s);
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)) ->
345 | [{p_namei=None; p_type=ty2}, _] ->
346 (match Ast_c.unwrap_typeC ty2 with
347 | BaseType Void -> ()
349 (* failwith "internal errror: fixOldCDecl not good" *)
353 params +> List.iter (function
354 | ({p_namei= Some name}, _) ->
355 LP.add_ident (Ast_c.str_of_name s)
358 (* failwith "internal errror: fixOldCDecl not good" *)
365 (*-------------------------------------------------------------------------- *)
367 (*-------------------------------------------------------------------------- *)
369 let mk_e e ii = Ast_c.mk_e e ii
371 let mk_string_wrap (s,info) = (s, [info])
375 /*(*****************************************************************************)*/
377 /*(*************************************************************************)*/
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)
388 %token <Ast_c.info> TUnknown /*(* unrecognized token *)*/
390 /*(* coupling: Token_helpers.is_real_comment *)*/
391 %token <Ast_c.info> TCommentSpace TCommentNewline TComment
393 /*(*-----------------------------------------*)*/
394 /*(* the normal tokens *)*/
395 /*(*-----------------------------------------*)*/
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
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
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.
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
424 %token <Ast_c.info> TPtVirg
426 TOrLog TAndLog TOr TXor TAnd TEqEq TNotEq TInf TSup TInfEq TSupEq
428 TPlus TMinus TMul TDiv TMod
431 Tchar Tshort Tint Tdouble Tfloat Tlong Tunsigned Tsigned Tvoid
432 Tsize_t Tssize_t Tptrdiff_t
433 Tauto Tregister Textern Tstatic
437 Tbreak Telse Tswitch Tcase Tcontinue Tfor Tdo Tif Twhile Treturn
439 Tsizeof Tnew Tdelete TOParCplusplusInit
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
454 /*(*-----------------------------------------*)*/
455 /*(* cppext: extra tokens *)*/
456 /*(*-----------------------------------------*)*/
457 /*(* coupling with Token_helpers.is_cpp_token *)*/
460 /*(*---------------*)*/
462 /*(*---------------*)*/
464 %token <Ast_c.info> TDefine
465 %token <(string * Ast_c.info)> TDefParamVariadic
467 /*(* disappear after fix_tokens_define *)*/
468 %token <Ast_c.info> TCppEscapedNewline
470 %token <Ast_c.info> TCppConcatOp
472 /*(* appear after fix_tokens_define *)*/
473 %token <Ast_c.info> TOParDefine
474 %token <Ast_c.info> TOBraceDefineInit
476 %token <(string * Ast_c.info)> TIdentDefine /*(* same *)*/
477 %token <Ast_c.info> TDefEOL /*(* same *)*/
480 /*(*---------------*)*/
482 /*(*---------------*)*/
485 /*(* used only in lexer_c, then transformed in comment or splitted in tokens *)*/
486 %token <(string * string * bool ref * Ast_c.info)> TInclude
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
493 /*(*---------------*)*/
495 /*(*---------------*)*/
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
503 /*(*---------------*)*/
505 /*(*---------------*)*/
507 %token <Ast_c.info> TUndef
509 %token <Ast_c.info> TCppDirectiveOther
511 /*(*---------------*)*/
513 /*(*---------------*)*/
515 /*(* appear after fix_tokens_cpp, cf also parsing_hacks#hint *)*/
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
525 %token <(string * Ast_c.info)> TMacroIterator
527 %token <(string * Ast_c.info)> TMacroTop
528 %token <(string * Ast_c.info)> TMacroStructDecl
531 %token <(string * Ast_c.info)> TMacroAttrStorage
534 /*(*---------------*)*/
536 /*(*---------------*)*/
539 /*(* should disappear after parsing_hack *)*/
540 %token <Ast_c.info> TCommentSkipTagStart TCommentSkipTagEnd
543 /*(* appear after parsing_hack *)*/
544 %token <Ast_c.info> TCParEOL
546 %token <Ast_c.info> TAction
549 /*(* TCommentMisc still useful ? obsolete ? *)*/
550 %token <Ast_c.info> TCommentMisc
551 %token <(Token_c.cppcommentkind * Ast_c.info)> TCommentCpp
554 /*(*-----------------------------------------*)*/
555 %token <Ast_c.info> EOF
557 /*(*-----------------------------------------*)*/
559 /*(* must be at the top so that it has the lowest priority *)*/
570 %left TInf TSup TInfEq TSupEq
575 /*(*************************************************************************)*/
576 /*(* Rules type declaration *)*/
577 /*(*************************************************************************)*/
579 %start main celem statement expr type_name
580 %type <Ast_c.program> main
581 %type <Ast_c.toplevel> celem
583 %type <Ast_c.statement> statement
584 %type <Ast_c.expression> expr
585 %type <Ast_c.fullType> type_name
588 /*(*************************************************************************)*/
591 * toplevel (obsolete)
597 * - left part (type_spec, qualif),
598 * - right part (declarator, abstract declarator)
599 * - aux part (parameters)
600 * declaration, storage, initializers
606 * generic workarounds (obrace, cbrace for context setting)
610 /*(*************************************************************************)*/
612 /*(*************************************************************************)*/
614 /*(*************************************************************************)*/
615 /*(* no more used; now that use error recovery *)*/
618 translation_unit EOF { $1 }
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] }
628 /*(*************************************************************************)*/
630 /*(*************************************************************************)*/
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.
638 | TypedefIdent { $1 }
645 (* cppext: string concatenation of idents
646 * also cppext: gccext: ##args for variadic macro
651 { RegularName (mk_string_wrap $1) }
652 | ident_extra_cpp { $1 }
656 { RegularName (mk_string_wrap $1) }
658 { RegularName (mk_string_wrap $1) }
659 | ident_extra_cpp { $1 }
662 | TIdent TCppConcatOp identifier_cpp_list
664 CppConcatenatedName (
666 | [] -> raise Impossible
667 | (x,concatnull)::xs ->
668 assert(null concatnull);
669 (mk_string_wrap $1, [])::(x,[$2])::xs
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) }
678 | TIdent { [mk_string_wrap $1, []] }
679 | identifier_cpp_list TCppConcatOp TIdent { $1 ++ [mk_string_wrap $3, [$2]] }
681 /*(*************************************************************************)*/
683 /*(*************************************************************************)*/
687 | expr TComma assign_expr { mk_e (Sequence ($1,$3)) [$2] }
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
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]}
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
704 | arith_expr TWhy gcc_opt_expr TDotDot assign_expr
705 { mk_e (CondExpr ($1,$3,$5)) [$2;$4] }
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] }
731 | topar2 type_name tcpar2 cast_expr { mk_e(Cast ($2, $4)) [$1;$3] }
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 | Tdelete cast_expr { mk_e(Delete $2) [$1] }
744 | TIdent TOPar argument_list_ne TCPar
745 { let fn = mk_e(Ident (RegularName (mk_string_wrap $1))) [] in
746 Left (mk_e(FunCall (fn, $3)) [$2;$4]) }
748 { let fn = mk_e(Ident (RegularName (mk_string_wrap $1))) [] in
749 Left(mk_e(FunCall (fn, [])) [$2;$3]) }
750 | TypedefIdent TOPar argument_list_ne TCPar
751 { let fn = mk_e(Ident (RegularName (mk_string_wrap $1))) [] in
752 Left (mk_e(FunCall (fn, $3)) [$2;$4]) }
753 | TypedefIdent TOPar TCPar
754 { let fn = mk_e(Ident (RegularName (mk_string_wrap $1))) [] in
755 Left (mk_e(FunCall (fn, [])) [$2;$3]) }
757 { let ty = addTypeD ($1,nullDecl) in
758 let ((returnType,hasreg), iihasreg) = fixDeclSpecForParam ty in
759 Right (ArgType { p_namei = None; p_type = returnType;
760 p_register = hasreg, iihasreg;
763 | new_argument TOCro expr TCCro
766 Left(e) -> Left(mk_e(ArrayAccess (e, $3)) [$2;$4])
767 | Right(ArgType(ty)) -> (* lots of hacks to make the right type *)
768 let fty = mk_ty (Array (Some $3, ty.Ast_c.p_type)) [$2;$4] in
769 let pty = { ty with p_type = fty } in
771 | _ -> raise Impossible
775 | TAnd { GetRef, $1 }
777 | TPlus { UnPlus, $1 }
778 | TMinus { UnMinus, $1 }
779 | TTilde { Tilde, $1 }
781 /*(* gccext: have that a lot in old kernel to get address of local label.
782 * cf gcc manual "local labels as values".
784 | TAndLog { GetRefLabel, $1 }
787 | primary_expr { $1 }
788 | postfix_expr TOCro expr TCCro
789 { mk_e(ArrayAccess ($1, $3)) [$2;$4] }
790 | postfix_expr TOPar argument_list_ne TCPar
791 { mk_e(FunCall ($1, $3)) [$2;$4] }
792 | postfix_expr TOPar TCPar { mk_e(FunCall ($1, [])) [$2;$3] }
793 | postfix_expr TDot ident_cpp { mk_e(RecordAccess ($1,$3)) [$2] }
794 | postfix_expr TPtrOp ident_cpp { mk_e(RecordPtAccess ($1,$3)) [$2] }
795 | postfix_expr TInc { mk_e(Postfix ($1, Inc)) [$2] }
796 | postfix_expr TDec { mk_e(Postfix ($1, Dec)) [$2] }
798 /*(* gccext: also called compound literals *)*/
799 | topar2 type_name tcpar2 TOBrace TCBrace
800 { mk_e(Constructor ($2, [])) [$1;$3;$4;$5] }
801 | topar2 type_name tcpar2 TOBrace initialize_list gcc_comma_opt TCBrace
802 { mk_e(Constructor ($2, List.rev $5)) ([$1;$3;$4;$7] ++ $6) }
806 | identifier_cpp { mk_e(Ident ($1)) [] }
808 { let (str,(sign,base)) = fst $1 in
809 mk_e(Constant (Int (str,Si(sign,base)))) [snd $1] }
810 | TFloat { mk_e(Constant (Float (fst $1))) [snd $1] }
811 | TString { mk_e(Constant (String (fst $1))) [snd $1] }
812 | TChar { mk_e(Constant (Char (fst $1))) [snd $1] }
813 | TOPar expr TCPar { mk_e(ParenExpr ($2)) [$1;$3] } /*(* forunparser: *)*/
815 /*(* gccext: cppext: TODO better ast ? *)*/
816 | TMacroString { mk_e(Constant (MultiString [fst $1])) [snd $1] }
817 | string_elem string_list
818 { mk_e(Constant (MultiString ["TODO: MultiString"])) ($1 ++ $2) }
820 /*(* gccext: allow statement as expressions via ({ statement }) *)*/
821 | TOPar compound TCPar { mk_e(StatementExpr ($2)) [$1;$3] }
825 /*(*----------------------------*)*/
827 /*(*----------------------------*)*/
830 /*(* to avoid conflicts have to introduce a _not_empty (ne) version *)*/
832 | assign_expr { Left $1 }
833 | parameter_decl { Right (ArgType $1) }
834 | action_higherordermacro_ne { Right (ArgAction $1) }
838 | assign_expr { Left $1 }
839 | parameter_decl { Right (ArgType $1) }
840 /*(* had conflicts before, but julia fixed them *)*/
841 | action_higherordermacro { Right (ArgAction $1) }
843 action_higherordermacro_ne:
846 then ActMisc [Ast_c.fakeInfo()]
851 action_higherordermacro:
854 then ActMisc [Ast_c.fakeInfo()]
859 /*(*----------------------------*)*/
860 /*(* workarounds *)*/
861 /*(*----------------------------*)*/
863 /*(* would like evalInt $1 but require too much info *)*/
864 const_expr: cond_expr { $1 }
867 topar2: TOPar { et "topar2" (); $1 }
868 tcpar2: TCPar { et "tcpar2" (); $1 (*TODO? et ? sure ? c pas dt plutot ? *) }
872 /*(*************************************************************************)*/
874 /*(*************************************************************************)*/
876 statement: statement2 { mk_st (fst $1) (snd $1) }
879 | labeled { Labeled (fst $1), snd $1 }
880 | compound { Compound (fst $1), snd $1 }
881 | expr_statement { ExprStatement(fst $1), snd $1 }
882 | selection { Selection (fst $1), snd $1 ++ [fakeInfo()] }
883 | iteration { Iteration (fst $1), snd $1 ++ [fakeInfo()] }
884 | jump TPtVirg { Jump (fst $1), snd $1 ++ [$2] }
887 | Tasm TOPar asmbody TCPar TPtVirg { Asm $3, [$1;$2;$4;$5] }
888 | Tasm Tvolatile TOPar asmbody TCPar TPtVirg { Asm $4, [$1;$2;$3;$5;$6] }
891 | TMacroStmt { MacroStmt, [snd $1] }
896 /*(* note that case 1: case 2: i++; would be correctly parsed, but with
897 * a Case (1, (Case (2, i++))) :(
900 | ident_cpp TDotDot sw_stat_or_decl { Label ($1, $3), [$2] }
901 | Tcase const_expr TDotDot sw_stat_or_decl { Case ($2, $4), [$1; $3] }
902 | Tcase const_expr TEllipsis const_expr TDotDot sw_stat_or_decl
903 { CaseRange ($2, $4, $6), [$1;$3;$5] } /*(* gccext: allow range *)*/
904 | Tdefault TDotDot sw_stat_or_decl { Default $3, [$1; $2] }
907 | decl { mk_st (Decl ($1 Ast_c.LocalDecl)) Ast_c.noii }
912 /*(* gccext: allow toto: }
913 * was generating each 30 shift/Reduce conflicts,
914 * mais ca va, ca fait ce qu'il faut.
915 * update: julia fixed the problem by introducing end_labeled
916 * and modifying below stat_or_decl_list
919 { Label ($1, (mk_st (ExprStatement None) Ast_c.noii)), [$2] }
920 | Tcase const_expr TDotDot
921 { Case ($2, (mk_st (ExprStatement None) Ast_c.noii)), [$1;$3] }
923 { Default (mk_st (ExprStatement None) Ast_c.noii), [$1; $2] }
929 compound: tobrace compound2 tcbrace { $2, [$1; $3] }
933 (* cppext: because of cpp, some stuff looks like declaration but are in
934 * fact statement but too hard to figure out, and if parse them as
935 * expression, then we force to have first decls and then exprs, then
936 * will have a parse error. So easier to let mix decl/statement.
937 * Moreover it helps to not make such a difference between decl and
938 * statement for further coccinelle phases to factorize code.
942 | stat_or_decl_list { $1 }
946 | stat_or_decl { [$1] }
947 /*(* gccext: to avoid conflicts, cf end_labeled above *)*/
948 | end_labeled { [StmtElem (mk_st (Labeled (fst $1)) (snd $1))] }
949 /*(* old: conflicts | stat_or_decl_list stat_or_decl { $1 ++ [$2] } *)*/
950 | stat_or_decl stat_or_decl_list { $1 :: $2 }
953 | decl { StmtElem (mk_st (Decl ($1 Ast_c.LocalDecl)) Ast_c.noii) }
954 | statement { StmtElem $1 }
957 | function_definition { StmtElem (mk_st (NestedFunc $1) Ast_c.noii) }
961 { CppDirectiveStmt $1 }
962 | cpp_ifdef_directive/*(* stat_or_decl_list ...*)*/
967 | TPtVirg { None, [$1] }
968 | expr TPtVirg { Some $1, [$2] }
971 | Tif TOPar expr TCPar statement %prec SHIFTHERE
972 { If ($3, $5, (mk_st (ExprStatement None) Ast_c.noii)), [$1;$2;$4] }
973 | Tif TOPar expr TCPar statement Telse statement
974 { If ($3, $5, $7), [$1;$2;$4;$6] }
975 | Tswitch TOPar expr TCPar statement
976 { Switch ($3,$5), [$1;$2;$4] }
979 | Twhile TOPar expr TCPar statement
980 { While ($3,$5), [$1;$2;$4] }
981 | Tdo statement Twhile TOPar expr TCPar TPtVirg
982 { DoWhile ($2,$5), [$1;$3;$4;$6;$7] }
983 | Tfor TOPar expr_statement expr_statement TCPar statement
984 { For ($3,$4,(None, []),$6), [$1;$2;$5]}
985 | Tfor TOPar expr_statement expr_statement expr TCPar statement
986 { For ($3,$4,(Some $5, []),$7), [$1;$2;$6] }
987 /*(* c++ext: for(int i = 0; i < n; i++)*)*/
988 | Tfor TOPar decl expr_statement expr_opt TCPar statement
990 (* pr2 "DECL in for"; *)
991 MacroIteration ("toto", [], $7),[$1;$2;$6] (* TODOfake ast, TODO need decl2 ? *)
994 | TMacroIterator TOPar argument_list_ne TCPar statement
995 { MacroIteration (fst $1, $3, $5), [snd $1;$2;$4] }
996 | TMacroIterator TOPar TCPar statement
997 { MacroIteration (fst $1, [], $4), [snd $1;$2;$3] }
999 /*(* the ';' in the caller grammar rule will be appended to the infos *)*/
1001 | Tgoto ident_cpp { Goto ($2), [$1] }
1002 | Tcontinue { Continue, [$1] }
1003 | Tbreak { Break, [$1] }
1004 | Treturn { Return, [$1] }
1005 | Treturn expr { ReturnExpr $2, [$1] }
1006 | Tgoto TMul expr { GotoComputed $3, [$1;$2] }
1010 /*(*----------------------------*)*/
1012 /*(*----------------------------*)*/
1014 | TString { [snd $1] }
1015 /*(* cppext: ex= printk (KERN_INFO "xxx" UTS_RELEASE) *)*/
1016 | TMacroString { [snd $1] }
1020 | string_list colon_asm_list { $1, $2 }
1021 | string_list { $1, [] } /*(* in old kernel *)*/
1024 colon_asm: TDotDot colon_option_list { Colon $2, [$1] }
1027 | TString { ColonMisc, [snd $1] }
1028 | TString TOPar asm_expr TCPar { ColonExpr $3, [snd $1; $2;$4] }
1029 /*(* cppext: certainly a macro *)*/
1030 | TOCro identifier TCCro TString TOPar asm_expr TCPar
1031 { ColonExpr $6, [$1;snd $2;$3;snd $4; $5; $7 ] }
1032 | identifier { ColonMisc, [snd $1] }
1033 | /*(* empty *)*/ { ColonMisc, [] }
1035 asm_expr: assign_expr { $1 }
1037 /*(*************************************************************************)*/
1039 /*(*************************************************************************)*/
1042 /*(*-----------------------------------------------------------------------*)*/
1043 /*(* Type spec, left part of a type *)*/
1044 /*(*-----------------------------------------------------------------------*)*/
1046 | Tvoid { Right3 (BaseType Void), [$1] }
1047 | Tchar { Right3 (BaseType (IntType CChar)), [$1]}
1048 | Tint { Right3 (BaseType (IntType (Si (Signed,CInt)))), [$1]}
1049 | Tfloat { Right3 (BaseType (FloatType CFloat)), [$1]}
1050 | Tdouble { Right3 (BaseType (FloatType CDouble)), [$1] }
1051 | Tsize_t { Right3 (BaseType SizeType), [$1] }
1052 | Tssize_t { Right3 (BaseType SSizeType), [$1] }
1053 | Tptrdiff_t { Right3 (BaseType PtrDiffType), [$1] }
1054 | Tshort { Middle3 Short, [$1]}
1055 | Tlong { Middle3 Long, [$1]}
1056 | Tsigned { Left3 Signed, [$1]}
1057 | Tunsigned { Left3 UnSigned, [$1]}
1058 | struct_or_union_spec { Right3 (fst $1), snd $1 }
1059 | enum_spec { Right3 (fst $1), snd $1 }
1062 (* parse_typedef_fix1: cant put: TIdent {} cos it make the grammar
1063 * ambiguous, generates lots of conflicts => we must
1064 * use some tricks: we make the lexer and parser cooperate, cf lexerParser.ml.
1066 * parse_typedef_fix2: this is not enough, and you must use
1067 * parse_typedef_fix2 to fully manage typedef problems in grammar.
1069 * parse_typedef_fix3:
1071 * parse_typedef_fix4: try also to do now some consistency checking in
1075 { let name = RegularName (mk_string_wrap $1) in
1076 Right3 (TypeName (name, Ast_c.noTypedefDef())),[] }
1078 | Ttypeof TOPar assign_expr TCPar { Right3 (TypeOfExpr ($3)), [$1;$2;$4] }
1079 | Ttypeof TOPar type_name TCPar { Right3 (TypeOfType ($3)), [$1;$2;$4] }
1081 /*(*----------------------------*)*/
1082 /*(* workarounds *)*/
1083 /*(*----------------------------*)*/
1085 type_spec: type_spec2 { dt "type" (); $1 }
1087 /*(*-----------------------------------------------------------------------*)*/
1088 /*(* Qualifiers *)*/
1089 /*(*-----------------------------------------------------------------------*)*/
1092 | Tconst { {const=true ; volatile=false}, $1 }
1093 | Tvolatile { {const=false ; volatile=true}, $1 }
1095 | Trestrict { (* TODO *) {const=false ; volatile=false}, $1 }
1098 /*(*-----------------------------------------------------------------------*)*/
1099 /*(* gccext: attributes *)*/
1100 /*(*-----------------------------------------------------------------------*)*/
1103 | Tattribute TOPar /*stuff*/ TCPar { raise Todo }
1105 | TMacroAttr { Attribute (fst $1), [snd $1] }
1108 | TMacroAttrStorage { $1 }
1111 | type_qualif { $1 }
1113 | TMacroAttr { {const=true ; volatile=false}, snd $1 }
1115 /*(*-----------------------------------------------------------------------*)*/
1116 /*(* Declarator, right part of a type + second part of decl (the ident) *)*/
1117 /*(*-----------------------------------------------------------------------*)*/
1120 (* declarator return a couple:
1121 * (name, partial type (a function to be applied to return type))
1123 * when int* f(int) we must return Func(Pointer int,int) and not
1124 * Pointer (Func(int,int)
1128 | pointer direct_d { (fst $2, fun x -> x +> $1 +> (snd $2) ) }
1131 /*(* so must do int * const p; if the pointer is constant, not the pointee *)*/
1133 | tmul { fun x -> mk_ty (Pointer x) [$1] }
1134 | tmul pointer { fun x -> mk_ty (Pointer ($2 x)) [$1] }
1135 | tmul type_qualif_list
1136 { fun x -> ($2.qualifD, mk_tybis (Pointer x) [$1])}
1137 | tmul type_qualif_list pointer
1138 { fun x -> ($2.qualifD, mk_tybis (Pointer ($3 x)) [$1]) }
1143 { if !Flag.c_plus_plus
1146 let i = Ast_c.parse_info_of_info $1 in
1147 raise (Semantic("& not allowed in C types, try -c++ option", i)) }
1152 { ($1, fun x -> x) }
1153 | TOPar declarator TCPar /*(* forunparser: old: $2 *)*/
1154 { (fst $2, fun x -> mk_ty (ParenType ((snd $2) x)) [$1;$3]) }
1155 | direct_d tocro tccro
1156 { (fst $1,fun x->(snd $1) (mk_ty (Array (None,x)) [$2;$3])) }
1157 | direct_d tocro const_expr tccro
1158 { (fst $1,fun x->(snd $1) (mk_ty (Array (Some $3,x)) [$2;$4])) }
1159 | direct_d topar tcpar
1162 (mk_ty (FunctionType (x,(([],(false, []))))) [$2;$3]))
1164 | direct_d topar parameter_type_list tcpar
1165 { (fst $1,fun x->(snd $1)
1166 (mk_ty (FunctionType (x, $3)) [$2;$4]))
1170 /*(*----------------------------*)*/
1171 /*(* workarounds *)*/
1172 /*(*----------------------------*)*/
1174 tocro: TOCro { et "tocro" ();$1 }
1175 tccro: TCCro { dt "tccro" ();$1 }
1177 /*(*-----------------------------------------------------------------------*)*/
1178 abstract_declarator:
1180 | direct_abstract_declarator { $1 }
1181 | pointer direct_abstract_declarator { fun x -> x +> $2 +> $1 }
1183 direct_abstract_declarator:
1184 | TOPar abstract_declarator TCPar /*(* forunparser: old: $2 *)*/
1185 { fun x -> mk_ty (ParenType ($2 x)) [$1;$3] }
1188 { fun x -> mk_ty (Array (None, x)) [$1;$2] }
1189 | TOCro const_expr TCCro
1190 { fun x -> mk_ty (Array (Some $2, x)) [$1;$3] }
1191 | direct_abstract_declarator TOCro TCCro
1192 { fun x -> $1 (mk_ty (Array (None, x)) [$2;$3]) }
1193 | direct_abstract_declarator TOCro const_expr TCCro
1194 { fun x -> $1 (mk_ty (Array (Some $3,x)) [$2;$4]) }
1196 { fun x -> mk_ty (FunctionType (x, ([], (false, [])))) [$1;$2] }
1197 | topar parameter_type_list tcpar
1198 { fun x -> mk_ty (FunctionType (x, $2)) [$1;$3] }
1199 /*(* subtle: here must also use topar, not TOPar, otherwise if have for
1200 * instance (xxx ( * )(xxx)) cast, then the second xxx may still be a Tident
1201 * but we want to reduce topar, to set the InParameter so that
1202 * parsing_hack can get a chance to change the type of xxx into a typedef.
1203 * That's an example where parsing_hack and the lookahead of ocamlyacc does
1204 * not go very well together ... we got the info too late. We got
1205 * a similar pb with xxx xxx; declaration, cf parsing_hack.ml and the
1206 * "disable typedef cos special case ..." message.
1208 | direct_abstract_declarator topar tcpar
1209 { fun x -> $1 (mk_ty (FunctionType (x, (([], (false, []))))) [$2;$3]) }
1210 | direct_abstract_declarator topar parameter_type_list tcpar
1211 { fun x -> $1 (mk_ty (FunctionType (x, $3)) [$2;$4]) }
1213 /*(*-----------------------------------------------------------------------*)*/
1214 /*(* Parameters (use decl_spec not type_spec just for 'register') *)*/
1215 /*(*-----------------------------------------------------------------------*)*/
1216 parameter_type_list:
1217 | parameter_list { ($1, (false, []))}
1218 | parameter_list TComma TEllipsis { ($1, (true, [$2;$3])) }
1222 | decl_spec declaratorp
1223 { let ((returnType,hasreg),iihasreg) = fixDeclSpecForParam $1 in
1224 let (name, ftyp) = $2 in
1225 { p_namei = Some (name);
1226 p_type = ftyp returnType;
1227 p_register = (hasreg, iihasreg);
1230 | decl_spec abstract_declaratorp
1231 { let ((returnType,hasreg), iihasreg) = fixDeclSpecForParam $1 in
1233 p_type = $2 returnType;
1234 p_register = hasreg, iihasreg;
1238 { let ((returnType,hasreg), iihasreg) = fixDeclSpecForParam $1 in
1240 p_type = returnType;
1241 p_register = hasreg, iihasreg;
1246 /*(*----------------------------*)*/
1247 /*(* workarounds *)*/
1248 /*(*----------------------------*)*/
1250 parameter_decl: parameter_decl2 { et "param" (); $1 }
1251 | attributes parameter_decl2 { et "param" (); $2 }
1254 | declarator { LP.add_ident (str_of_name (fst $1)); $1 }
1256 | attributes declarator { LP.add_ident (str_of_name (fst $2)); $2 }
1257 | declarator attributes { LP.add_ident (str_of_name (fst $1)); $1 }
1259 abstract_declaratorp:
1260 | abstract_declarator { $1 }
1262 | attributes abstract_declarator { $2 }
1264 /*(*-----------------------------------------------------------------------*)*/
1265 /*(* helper type rules *)*/
1266 /*(*-----------------------------------------------------------------------*)*/
1268 /*(* for struct and also typename *)*/
1269 /*(* cant put decl_spec cos no storage is allowed for field struct *)*/
1271 | type_spec { addTypeD ($1, nullDecl) }
1272 | type_qualif { {nullDecl with qualifD = (fst $1,[snd $1])}}
1273 | type_spec spec_qualif_list { addTypeD ($1,$2) }
1274 | type_qualif spec_qualif_list { addQualifD ($1,$2) }
1276 spec_qualif_list: spec_qualif_list2 { dt "spec_qualif" (); $1 }
1279 /*(* for pointers in direct_declarator and abstract_declarator *)*/
1281 | type_qualif_attr { {nullDecl with qualifD = (fst $1,[snd $1])} }
1282 | type_qualif_list type_qualif_attr { addQualifD ($2,$1) }
1289 /*(*-----------------------------------------------------------------------*)*/
1290 /*(* xxx_type_id *)*/
1291 /*(*-----------------------------------------------------------------------*)*/
1295 { let (returnType, _) = fixDeclSpecForDecl $1 in returnType }
1296 | spec_qualif_list abstract_declaratort
1297 { let (returnType, _) = fixDeclSpecForDecl $1 in $2 returnType }
1301 abstract_declaratort:
1302 | abstract_declarator { $1 }
1304 | attributes abstract_declarator { $2 }
1307 /*(*************************************************************************)*/
1308 /*(* declaration and initializers *)*/
1309 /*(*************************************************************************)*/
1314 let (returnType,storage) = fixDeclSpecForDecl $1 in
1315 let iistart = Ast_c.fakeInfo () in
1316 DeclList ([{v_namei = None; v_type = returnType;
1317 v_storage = unwrap storage; v_local = local;
1318 v_attr = Ast_c.noattr;
1319 v_type_bis = ref None;
1321 ($2::iistart::snd storage))
1323 | decl_spec init_declarator_list TPtVirg
1325 let (returnType,storage) = fixDeclSpecForDecl $1 in
1326 let iistart = Ast_c.fakeInfo () in
1328 ($2 +> List.map (fun ((((name,f),attrs), ini), iivirg) ->
1329 let s = str_of_name name in
1330 if fst (unwrap storage) =*= StoTypedef
1331 then LP.add_typedef s;
1332 {v_namei = Some (name, ini);
1333 v_type = f returnType;
1334 v_storage = unwrap storage;
1337 v_type_bis = ref None;
1341 ), ($3::iistart::snd storage))
1345 | TMacroDecl TOPar argument_list TCPar TPtVirg
1347 MacroDecl ((fst $1, $3, true), [snd $1;$2;$4;$5;fakeInfo()]) }
1348 | Tstatic TMacroDecl TOPar argument_list TCPar TPtVirg
1350 MacroDecl ((fst $2, $4, true), [snd $2;$3;$5;$6;fakeInfo();$1]) }
1351 | Tstatic TMacroDeclConst TMacroDecl TOPar argument_list TCPar TPtVirg
1353 MacroDecl ((fst $3, $5, true), [snd $3;$4;$6;$7;fakeInfo();$1;$2])}
1356 /*(*-----------------------------------------------------------------------*)*/
1358 | storage_class_spec { {nullDecl with storageD = (fst $1, [snd $1]) } }
1359 | type_spec { addTypeD ($1,nullDecl) }
1360 | type_qualif { {nullDecl with qualifD = (fst $1, [snd $1]) } }
1361 | Tinline { {nullDecl with inlineD = (true, [$1]) } }
1362 | storage_class_spec decl_spec2 { addStorageD ($1, $2) }
1363 | type_spec decl_spec2 { addTypeD ($1, $2) }
1364 | type_qualif decl_spec2 { addQualifD ($1, $2) }
1365 | Tinline decl_spec2 { addInlineD ((true, $1), $2) }
1367 /*(* can simplify by putting all in _opt ? must have at least one otherwise
1368 * decl_list is ambiguous ? (no cos have ';' between decl)
1372 storage_class_spec2:
1373 | Tstatic { Sto Static, $1 }
1374 | Textern { Sto Extern, $1 }
1375 | Tauto { Sto Auto, $1 }
1376 | Tregister { Sto Register,$1 }
1377 | Ttypedef { StoTypedef, $1 }
1381 | storage_class_spec2 { $1 }
1382 | storage_class_spec2 attribute_storage_list { $1 (* TODO *) }
1386 /*(*----------------------------*)*/
1387 /*(* workarounds *)*/
1388 /*(*----------------------------*)*/
1390 decl: decl2 { et "decl" (); $1 }
1391 decl_spec: decl_spec2 { dt "declspec" (); $1 }
1393 /*(*-----------------------------------------------------------------------*)*/
1394 /*(* declarators (right part of type and variable) *)*/
1395 /*(*-----------------------------------------------------------------------*)*/
1397 | declaratori { ($1, NoInit) }
1398 | declaratori teq initialize { ($1, ValInit($2, $3)) }
1400 | declaratori TOParCplusplusInit argument_list TCPar
1401 { ($1, ConstrInit($3,[$2;$4])) }
1404 /*(*----------------------------*)*/
1405 /*(* workarounds *)*/
1406 /*(*----------------------------*)*/
1407 teq: TEq { et "teq" (); $1 }
1409 init_declarator: init_declarator2 { dt "init" (); $1 }
1412 /*(*----------------------------*)*/
1414 /*(*----------------------------*)*/
1417 | declarator { LP.add_ident (str_of_name (fst $1)); $1, Ast_c.noattr }
1419 | declarator gcc_asm_decl { LP.add_ident (str_of_name (fst $1)); $1, Ast_c.noattr }
1421 | attributes declarator { LP.add_ident (str_of_name (fst $2)); $2, $1 }
1422 | declarator attributes { LP.add_ident (str_of_name (fst $1)); $1, Ast_c.noattr (* TODO *) }
1427 | Tasm TOPar asmbody TCPar { }
1428 | Tasm Tvolatile TOPar asmbody TCPar { }
1431 /*(*-----------------------------------------------------------------------*)*/
1435 | tobrace_ini initialize_list gcc_comma_opt_struct tcbrace_ini
1436 { InitList (List.rev $2), [$1;$4]++$3 }
1437 | tobrace_ini tcbrace_ini
1438 { InitList [], [$1;$2] } /*(* gccext: *)*/
1442 (* opti: This time we use the weird order of non-terminal which requires in
1443 * the "caller" to do a List.rev cos quite critical. With this weird order it
1444 * allows yacc to use a constant stack space instead of exploding if we would
1445 * do a 'initialize2 Tcomma initialize_list'.
1449 | initialize2 { [$1, []] }
1450 | initialize_list TComma initialize2 { ($3, [$2])::$1 }
1453 /*(* gccext: condexpr and no assign_expr cos can have ambiguity with comma *)*/
1457 | tobrace_ini initialize_list gcc_comma_opt_struct tcbrace_ini
1458 { InitList (List.rev $2), [$1;$4]++$3 }
1459 | tobrace_ini tcbrace_ini
1460 { InitList [], [$1;$2] }
1462 /*(* gccext: labeled elements, a.k.a designators *)*/
1463 | designator_list TEq initialize2
1464 { InitDesignators ($1, $3), [$2] }
1466 /*(* gccext: old format *)*/
1467 | ident TDotDot initialize2
1468 { InitFieldOld (fst $1, $3), [snd $1; $2] } /*(* in old kernel *)*/
1470 | TOCro const_expr TCCro initialize2
1471 { InitIndexOld ($2, $4), [$1;$3] }
1476 /*(* they can be nested, can have a .x[3].y *)*/
1479 { DesignatorField (fst $2), [$1;snd $2] }
1480 | TOCro const_expr TCCro
1481 { DesignatorIndex ($2), [$1;$3] }
1482 | TOCro const_expr TEllipsis const_expr TCCro
1483 { DesignatorRange ($2, $4), [$1;$3;$5] }
1486 /*(*----------------------------*)*/
1487 /*(* workarounds *)*/
1488 /*(*----------------------------*)*/
1490 gcc_comma_opt_struct:
1492 | /*(* empty *)*/ { [Ast_c.fakeInfo() +> Ast_c.rewrap_str ","] }
1501 /*(*************************************************************************)*/
1503 /*(*************************************************************************)*/
1506 | struct_or_union ident tobrace_struct struct_decl_list_gcc tcbrace_struct
1507 { StructUnion (fst $1, Some (fst $2), $4), [snd $1;snd $2;$3;$5] }
1508 | struct_or_union tobrace_struct struct_decl_list_gcc tcbrace_struct
1509 { StructUnion (fst $1, None, $3), [snd $1;$2;$4] }
1510 | struct_or_union ident
1511 { StructUnionName (fst $1, fst $2), [snd $1;snd $2] }
1514 | Tstruct { Struct, $1 }
1515 | Tunion { Union, $1 }
1517 | Tstruct attributes { Struct, $1 (* TODO *) }
1518 | Tunion attributes { Union, $1 (* TODO *) }
1523 | field_declaration { DeclarationField $1 }
1524 | TPtVirg { EmptyField $1 }
1526 /*(* no conflict ? no need for a TMacroStruct ? apparently not as at struct
1527 * the rule are slightly different.
1529 | identifier TOPar argument_list TCPar TPtVirg
1530 { MacroDeclField ((fst $1, $3), [snd $1;$2;$4;$5;fakeInfo()]) }
1534 { CppDirectiveStruct $1 }
1535 | cpp_ifdef_directive/*(* struct_decl_list ... *)*/
1540 | spec_qualif_list struct_declarator_list TPtVirg
1542 let (returnType,storage) = fixDeclSpecForDecl $1 in
1543 if fst (unwrap storage) <> NoSto
1544 then internal_error "parsing dont allow this";
1546 FieldDeclList ($2 +> (List.map (fun (f, iivirg) ->
1547 f returnType, iivirg))
1549 (* dont need to check if typedef or func initialised cos
1550 * grammar dont allow typedef nor initialiser in struct
1554 | spec_qualif_list TPtVirg
1556 (* gccext: allow empty elements if it is a structdef or enumdef *)
1557 let (returnType,storage) = fixDeclSpecForDecl $1 in
1558 if fst (unwrap storage) <> NoSto
1559 then internal_error "parsing dont allow this";
1561 FieldDeclList ([(Simple (None, returnType)) , []], [$2])
1570 { (fun x -> Simple (Some (fst $1), (snd $1) x)) }
1571 | dotdot const_expr2
1572 { (fun x -> BitField (None, x, $1, $2)) }
1573 | declaratorsd dotdot const_expr2
1574 { (fun x -> BitField (Some (fst $1), ((snd $1) x), $2, $3)) }
1577 /*(*----------------------------*)*/
1578 /*(* workarounds *)*/
1579 /*(*----------------------------*)*/
1581 | declarator { (*also ? LP.add_ident (fst (fst $1)); *) $1 }
1583 | attributes declarator { $2 }
1584 | declarator attributes { $1 }
1589 struct_or_union_spec: s_or_u_spec2 { dt "su" (); $1 }
1590 struct_or_union: struct_or_union2 { et "su" (); $1 }
1591 struct_decl: struct_decl2 { et "struct" (); $1 }
1593 dotdot: TDotDot { et "dotdot" (); $1 }
1594 const_expr2: const_expr { dt "const_expr2" (); $1 }
1596 struct_decl_list_gcc:
1597 | struct_decl_list { $1 }
1598 | /*(* empty *)*/ { [] } /*(* gccext: allow empty struct *)*/
1601 /*(*************************************************************************)*/
1603 /*(*************************************************************************)*/
1605 | Tenum tobrace_enum enumerator_list gcc_comma_opt_struct tcbrace_enum
1606 { Enum (None, $3), [$1;$2;$5] ++ $4 }
1607 | Tenum ident tobrace_enum enumerator_list gcc_comma_opt_struct tcbrace_enum
1608 { Enum (Some (fst $2), $4), [$1; snd $2; $3;$6] ++ $5 }
1610 { EnumName (fst $2), [$1; snd $2] }
1613 | idente { $1, None }
1614 | idente TEq const_expr { $1, Some ($2, $3) }
1617 /*(*----------------------------*)*/
1618 /*(* workarounds *)*/
1619 /*(*----------------------------*)*/
1621 idente: ident_cpp { LP.add_ident (str_of_name $1); $1 }
1625 /*(*************************************************************************)*/
1627 /*(*************************************************************************)*/
1628 function_definition: function_def { fixFunc $1 }
1631 | decl { [$1 Ast_c.LocalDecl] }
1632 | decl_list decl { $1 ++ [$2 Ast_c.LocalDecl] }
1634 /* hack : to drop when a better solution is found */
1637 | cpp_directive_list cpp_directive { }
1640 | start_fun compound { LP.del_scope(); ($1, $2, None) }
1641 | start_fun cpp_directive_list compound { LP.del_scope(); ($1, $3, None) }
1642 | start_fun decl_list compound {
1643 (* TODO: undo the typedef added ? *)
1648 start_fun: start_fun2
1650 fix_add_params_ident $1;
1651 (* toreput? !LP._lexer_hint.toplevel <- false; *)
1655 start_fun2: decl_spec declaratorfd
1656 { let (returnType,storage) = fixDeclSpecForFuncDef $1 in
1657 let (id, attrs) = $2 in
1658 (fst id, fixOldCDecl ((snd id) returnType) , storage, attrs)
1663 | Tconstructorname topar tcpar {
1664 let id = RegularName (mk_string_wrap $1) in
1665 let ret = mk_ty NoType [] in
1666 let ty = mk_ty (FunctionType (ret, (([], (false, []))))) [$2;$3] in
1667 let storage = ((NoSto,false),[]) in
1669 (id, ty, storage, attrs) }
1670 | Tconstructorname topar parameter_type_list tcpar {
1671 let id = RegularName (mk_string_wrap $1) in
1672 let ret = mk_ty NoType [] in
1673 let ty = mk_ty (FunctionType (ret, $3)) [$2;$4] in
1674 let storage = ((NoSto,false),[]) in
1676 (id, ty, storage, attrs) }
1678 /*(*----------------------------*)*/
1679 /*(* workarounds *)*/
1680 /*(*----------------------------*)*/
1682 /* It would be very nice if we could make declarator aware that this is
1683 coming from a function definition. Then on the ( and ) cases, it could
1684 set the state to something other than InParameter. Then the case
1685 (TIdent (s, i1)::(TComma _|TCPar _)::_ , (TComma _ |TOPar _)::_ )
1686 in parsing_hacks.ml would not have to consider K&R variable declarations
1687 as typedefs. Unfortunately, doing something about this problem seems to
1688 introduce conflicts in the parser. */
1691 | declarator { et "declaratorfd" (); $1, Ast_c.noattr }
1693 | attributes declarator { et "declaratorfd" (); $2, $1 }
1694 | declarator attributes { et "declaratorfd" (); $1, Ast_c.noattr }
1698 /*(*************************************************************************)*/
1699 /*(* cpp directives *)*/
1700 /*(*************************************************************************)*/
1703 | TIncludeStart TIncludeFilename
1705 let (i1, in_ifdef) = $1 in
1708 (* redo some lexing work :( *)
1711 | _ when s =~ "^\"\\(.*\\)\"$" ->
1712 Local (Common.split "/" (matched1 s))
1713 | _ when s =~ "^\\<\\(.*\\)\\>$" ->
1714 NonLocal (Common.split "/" (matched1 s))
1718 Include { i_include = (inc_file, [i1;i2]);
1719 i_rel_pos = Ast_c.noRelPos();
1720 i_is_in_ifdef = !in_ifdef;
1721 i_content = Ast_c.noi_content;
1725 | TDefine TIdentDefine define_val TDefEOL
1726 { Define ((fst $2, [$1; snd $2;$4]), (DefineVar, $3)) }
1729 (* The TOParDefine is introduced to avoid ambiguity with previous rules.
1730 * A TOParDefine is a TOPar that was just next to the ident.
1732 | TDefine TIdentDefine TOParDefine param_define_list TCPar define_val TDefEOL
1734 ((fst $2, [$1; snd $2; $7]),
1735 (DefineFunc ($4, [$3;$5]), $6))
1738 | TUndef TIdentDefine TDefEOL
1739 { Define((fst $2, [$1; snd $2; $3]), (Undef,DefineEmpty)) }
1740 | TCppDirectiveOther { PragmaAndCo ([$1]) }
1746 /*(* perhaps better to use assign_expr ? but in that case need
1747 * do a assign_expr_of_string in parse_c
1750 | expr { DefineExpr $1 }
1751 | statement { DefineStmt $1 }
1752 | decl { DefineStmt (mk_st (Decl ($1 Ast_c.NotLocalDecl)) Ast_c.noii) }
1755 * | TypedefIdent { DefineType (nQ,(TypeName(fst $1,noTypedefDef()),[snd $1]))}
1757 * | spec_qualif_list TMul
1758 * { let (returnType, _) = fixDeclSpecForDecl $1 in DefineType returnType }
1762 { let returnType = fixDeclSpecForMacro $1 in
1763 DefineType returnType
1765 | decl_spec abstract_declarator
1766 { let returnType = fixDeclSpecForMacro $1 in
1767 let typ = $2 returnType in
1771 /* can be in conflict with decl_spec, maybe change fixDeclSpecForMacro
1772 * to also allow storage ?
1773 | storage_class_spec { DefineTodo }
1774 | Tinline { DefineTodo }
1777 /*(* a few special cases *)*/
1778 | stat_or_decl stat_or_decl_list { DefineTodo }
1780 | statement statement { DefineTodo }
1781 | decl function_definition { DefineTodo }
1787 | function_definition { DefineFunction $1 }
1789 | TOBraceDefineInit initialize_list gcc_comma_opt_struct TCBrace comma_opt
1790 { DefineInit (InitList (List.rev $2), [$1;$4]++$3++$5) }
1792 /*(* note: had a conflict before when were putting TInt instead of expr *)*/
1793 | Tdo statement Twhile TOPar expr TCPar
1797 then pr2 "WEIRD: in macro and have not a while(0)";
1799 DefineDoWhileZero (($2,$5), [$1;$3;$4;$6])
1802 | Tasm TOPar asmbody TCPar { DefineTodo }
1803 | Tasm Tvolatile TOPar asmbody TCPar { DefineTodo }
1805 /*(* aliases macro *)*/
1806 | TMacroAttr { DefineTodo }
1808 | /*(* empty *)*/ { DefineEmpty }
1814 | TIdent { mk_string_wrap $1 }
1815 | TypedefIdent { mk_string_wrap $1 }
1816 | TDefParamVariadic { mk_string_wrap $1 }
1817 | TEllipsis { "...", [$1] }
1818 /*(* they reuse keywords :( *)*/
1819 | Tregister { "register", [$1] }
1824 cpp_ifdef_directive:
1826 { let (tag,ii) = $1 in
1827 IfdefDirective ((Ifdef, IfdefTag (Common.some !tag)), [ii]) }
1829 { let (tag,ii) = $1 in
1830 IfdefDirective ((IfdefElse, IfdefTag (Common.some !tag)), [ii]) }
1832 { let (tag,ii) = $1 in
1833 IfdefDirective ((IfdefElseif, IfdefTag (Common.some !tag)), [ii]) }
1835 { let (tag,ii) = $1 in
1836 IfdefDirective ((IfdefEndif, IfdefTag (Common.some !tag)), [ii]) }
1839 { let (_b, tag,ii) = $1 in
1840 IfdefDirective ((Ifdef, IfdefTag (Common.some !tag)), [ii]) }
1842 { let (_b, tag,ii) = $1 in
1843 IfdefDirective ((Ifdef, IfdefTag (Common.some !tag)), [ii]) }
1845 { let (_b, tag,ii) = $1 in
1846 IfdefDirective ((Ifdef, IfdefTag (Common.some !tag)), [ii]) }
1851 /*(* no conflict ? no need for a TMacroTop ? apparently not as at toplevel
1852 * the rule are slightly different, they cant be statement and so expr
1853 * at the top, only decl or function definition.
1855 | identifier TOPar argument_list TCPar TPtVirg
1857 Declaration(MacroDecl((fst $1, $3, true), [snd $1;$2;$4;$5;fakeInfo()]))
1858 (* old: MacroTop (fst $1, $3, [snd $1;$2;$4;$5]) *)
1861 /*(* TCParEOL to fix the end-of-stream bug of ocamlyacc *)*/
1862 | identifier TOPar argument_list TCParEOL
1863 { Declaration (MacroDecl ((fst $1, $3, false), [snd $1;$2;$4;fakeInfo()])) }
1865 /*(* ex: EXPORT_NO_SYMBOLS; *)*/
1866 | identifier TPtVirg { EmptyDef [snd $1;$2] }
1870 /*(*************************************************************************)*/
1872 /*(*************************************************************************)*/
1874 external_declaration:
1875 | function_definition { Definition $1 }
1876 | decl { Declaration ($1 Ast_c.NotLocalDecl) }
1880 | external_declaration { $1 }
1887 | cpp_ifdef_directive /* (*external_declaration_list ...*)*/
1890 /*(* can have asm declaration at toplevel *)*/
1891 | Tasm TOPar asmbody TCPar TPtVirg { EmptyDef [$1;$2;$4;$5] }
1894 (* in ~/kernels/src/linux-2.5.2/drivers/isdn/hisax/isdnl3.c sometimes
1895 * the function ends with }; instead of just }
1896 * can also remove this rule and report "parse error" pb to morton
1898 | TPtVirg { EmptyDef [$1] }
1901 | EOF { FinalDef $1 }
1906 /*(*************************************************************************)*/
1907 /*(* some generic workarounds *)*/
1908 /*(*************************************************************************)*/
1910 tobrace: TOBrace { LP.push_context LP.InFunction; LP.new_scope (); $1 }
1911 tcbrace: TCBrace { LP.pop_context(); LP.del_scope (); $1 }
1913 tobrace_enum: TOBrace { LP.push_context LP.InEnum; $1 }
1914 tcbrace_enum: TCBrace { LP.pop_context (); $1 }
1916 tobrace_ini: TOBrace { LP.push_context LP.InInitializer; $1 }
1917 tcbrace_ini: TCBrace { LP.pop_context (); $1 }
1919 tobrace_struct: TOBrace { LP.push_context LP.InStruct; $1}
1920 tcbrace_struct: TCBrace { LP.pop_context (); $1 }
1926 { LP.new_scope ();et "topar" ();
1927 LP.push_context LP.InParameter;
1931 { LP.del_scope ();dt "tcpar" ();
1939 /*(*************************************************************************)*/
1940 /*(* xxx_list, xxx_opt *)*/
1941 /*(*************************************************************************)*/
1947 | statement_list { ([], $1) }
1948 | decl_list { ($1, []) }
1949 | decl_list statement_list { ($1,$2) }
1951 statement_list: stat_or_decl_list { $1 }
1958 | decl_list decl { $1 ++ [$2] }
1961 | statement { [$1] }
1962 | statement_list statement { $1 ++ [$2] }
1970 | string_elem { $1 }
1971 | string_list string_elem { $1 ++ $2 }
1974 | colon_asm { [$1] }
1975 | colon_asm_list colon_asm { $1 ++ [$2] }
1978 | colon_option { [$1, []] }
1979 | colon_option_list TComma colon_option { $1 ++ [$3, [$2]] }
1983 | argument_ne { [$1, []] }
1984 | argument_list_ne TComma argument { $1 ++ [$3, [$2]] }
1987 | argument { [$1, []] }
1988 | argument_list TComma argument { $1 ++ [$3, [$2]] }
1992 | assign_expr { [$1, []] }
1993 | expression_list TComma assign_expr { $1 ++ [$3, [$2]] }
1998 | struct_decl { [$1] }
1999 | struct_decl_list struct_decl { $1 ++ [$2] }
2002 struct_declarator_list:
2003 | struct_declarator { [$1, []] }
2004 | struct_declarator_list TComma struct_declarator { $1 ++ [$3, [$2]] }
2008 | enumerator { [$1, []] }
2009 | enumerator_list TComma enumerator { $1 ++ [$3, [$2]] }
2012 init_declarator_list:
2013 | init_declarator { [$1, []] }
2014 | init_declarator_list TComma init_declarator { $1 ++ [$3, [$2]] }
2018 | parameter_decl { [$1, []] }
2019 | parameter_list TComma parameter_decl { $1 ++ [$3, [$2]] }
2023 | TAction taction_list_ne { $1 :: $2 }
2026 /*old: was generating conflict, hence now taction_list_ne
2027 | (* empty *) { [] }
2029 | taction_list TAction { $1 ++ [$2] }
2032 | TAction taction_list { $1 :: $2 }
2035 | /*(* empty *)*/ { [] }
2036 | param_define { [$1, []] }
2037 | param_define_list TComma param_define { $1 ++ [$3, [$2]] }
2040 | designator { [$1] }
2041 | designator_list designator { $1 ++ [$2] }
2044 | attribute { [$1] }
2045 | attribute_list attribute { $1 ++ [$2] }
2047 attribute_storage_list:
2048 | attribute_storage { [$1] }
2049 | attribute_storage_list attribute_storage { $1 ++ [$2] }
2052 attributes: attribute_list { $1 }
2056 /*(* gccext: which allow a trailing ',' in enum, as in perl *)*/
2059 | /*(* empty *)*/ { [] }
2063 | /*(* empty *)*/ { [] }
2073 | /*(* empty *)*/ { None }
2084 | /*(* empty *)*/ { None }