parse.y 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  1. /* YACC parser for C syntax.
  2. Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. /* To whomever it may concern: I have heard that such a thing was once
  18. written by AT&T, but I have never seen it. */
  19. %expect 19
  20. /* These are the 19 conflicts you should get in parse.output;
  21. the state numbers may vary if minor changes in the grammar are made.
  22. State 41 contains 1 shift/reduce conflict. (Two ways to recover from error.)
  23. State 96 contains 1 shift/reduce conflict. (Two ways to recover from error.)
  24. State 100 contains 1 shift/reduce conflict. (Two ways to recover from error.)
  25. State 116 contains 1 shift/reduce conflict. (See comment at component_decl.)
  26. State 166 contains 2 shift/reduce conflicts. (make notype_declarator longer.)
  27. State 241 contains 2 shift/reduce conflicts. (make absdcl1 longer if poss.)
  28. State 275 contains 2 shift/reduce conflicts. (same for after_type_declarator).
  29. State 306 contains 2 shift/reduce conflicts. (similar for absdcl1 again.)
  30. State 314 contains 2 shift/reduce conflicts. (like 241, other context.)
  31. State 318 contains 2 shift/reduce conflicts. (like 241, other context.)
  32. State 362 contains 1 shift/reduce conflict. (dangling else.)
  33. State 369 contains 2 shift/reduce conflicts. (like 166 for parm_declarator).
  34. */
  35. %{
  36. #include "config.h"
  37. #include "tree.h"
  38. #include "parse.h"
  39. #include "c-tree.h"
  40. #include "flags.h"
  41. #include <stdio.h>
  42. /* Cause the `yydebug' variable to be defined. */
  43. #define YYDEBUG
  44. %}
  45. %start program
  46. %union {long itype; tree ttype; enum tree_code code; char *cptr; }
  47. /* All identifiers that are not reserved words
  48. and are not declared typedefs in the current block */
  49. %token IDENTIFIER
  50. /* All identifiers that are declared typedefs in the current block.
  51. In some contexts, they are treated just like IDENTIFIER,
  52. but they can also serve as typespecs in declarations. */
  53. %token TYPENAME
  54. /* Reserved words that specify storage class.
  55. yylval contains an IDENTIFIER_NODE which indicates which one. */
  56. %token SCSPEC
  57. /* Reserved words that specify type.
  58. yylval contains an IDENTIFIER_NODE which indicates which one. */
  59. %token TYPESPEC
  60. /* Reserved words that qualify type: "const" or "volatile".
  61. yylval contains an IDENTIFIER_NODE which indicates which one. */
  62. %token TYPE_QUAL
  63. /* Character or numeric constants.
  64. yylval is the node for the constant. */
  65. %token CONSTANT
  66. /* String constants in raw form.
  67. yylval is a STRING_CST node. */
  68. %token STRING
  69. /* "...", used for functions with variable arglists. */
  70. %token ELLIPSIS
  71. /* the reserved words */
  72. %token SIZEOF ENUM STRUCT UNION IF ELSE WHILE DO FOR SWITCH CASE DEFAULT
  73. %token BREAK CONTINUE RETURN GOTO ASM TYPEOF ALIGNOF
  74. /* Define the operator tokens and their precedences.
  75. The value is an integer because, if used, it is the tree code
  76. to use in the expression made from the operator. */
  77. %right <code> ASSIGN '='
  78. %right <code> '?' ':'
  79. %left <code> OROR
  80. %left <code> ANDAND
  81. %left <code> '|'
  82. %left <code> '^'
  83. %left <code> '&'
  84. %left <code> EQCOMPARE
  85. %left <code> ARITHCOMPARE
  86. %left <code> LSHIFT RSHIFT
  87. %left <code> '+' '-'
  88. %left <code> '*' '/' '%'
  89. %right <code> UNARY PLUSPLUS MINUSMINUS
  90. %left HYPERUNARY
  91. %left <code> POINTSAT '.'
  92. %type <code> unop
  93. %type <ttype> identifier IDENTIFIER TYPENAME CONSTANT expr nonnull_exprlist exprlist
  94. %type <ttype> expr_no_commas primary string STRING
  95. %type <ttype> typed_declspecs reserved_declspecs
  96. %type <ttype> typed_typespecs reserved_typespecquals
  97. %type <ttype> declmods typespec typespecqual_reserved
  98. %type <ttype> SCSPEC TYPESPEC TYPE_QUAL nonempty_type_quals maybe_type_qual
  99. %type <ttype> initdecls notype_initdecls initdcl notype_initdcl
  100. %type <ttype> init initlist maybeasm
  101. %type <ttype> asm_operands asm_operand
  102. %type <ttype> declarator
  103. %type <ttype> notype_declarator after_type_declarator
  104. %type <ttype> parm_declarator
  105. %type <ttype> structsp component_decl_list component_decl components component_declarator
  106. %type <ttype> enumlist enumerator
  107. %type <ttype> typename absdcl absdcl1 type_quals
  108. %type <ttype> xexpr parms parm identifiers
  109. %type <ttype> parmlist parmlist_1 parmlist_2
  110. %type <ttype> parmlist_or_identifiers parmlist_or_identifiers_1
  111. %type <itype> setspecs
  112. %{
  113. /* the declaration found for the last IDENTIFIER token read in.
  114. yylex must look this up to detect typedefs, which get token type TYPENAME,
  115. so it is left around in case the identifier is not a typedef but is
  116. used in a context which makes it a reference to a variable. */
  117. static tree lastiddecl;
  118. static tree make_pointer_declarator ();
  119. static tree combine_strings ();
  120. static void reinit_parse_for_function ();
  121. extern double atof ();
  122. /* List of types and structure classes of the current declaration */
  123. tree current_declspecs;
  124. char *input_filename; /* source file current line is coming from */
  125. char *main_input_filename; /* top-level source file */
  126. %}
  127. %%
  128. program: /* empty */
  129. | extdefs
  130. ;
  131. /* the reason for the strange actions in this rule
  132. is so that notype_initdecls when reached via datadef
  133. can find a valid list of type and sc specs in $0. */
  134. extdefs:
  135. {$<ttype>$ = NULL_TREE; } extdef
  136. | extdefs {$<ttype>$ = NULL_TREE; } extdef
  137. ;
  138. extdef:
  139. fndef
  140. | datadef
  141. | ASM '(' string ')' ';'
  142. { if (pedantic)
  143. warning ("ANSI C forbids use of `asm' keyword");
  144. if (TREE_CHAIN ($3)) $3 = combine_strings ($3);
  145. assemble_asm ($3); }
  146. ;
  147. datadef:
  148. setspecs notype_initdecls ';'
  149. { if (pedantic)
  150. error ("ANSI C forbids data definition lacking type or storage class");
  151. else
  152. warning ("data definition lacks type or storage class"); }
  153. | declmods setspecs notype_initdecls ';'
  154. {}
  155. | typed_declspecs setspecs initdecls ';'
  156. {}
  157. | declmods ';'
  158. { error ("empty declaration"); }
  159. | typed_declspecs ';'
  160. { shadow_tag ($1); }
  161. | error ';'
  162. | error '}'
  163. | ';'
  164. ;
  165. fndef:
  166. typed_declspecs setspecs declarator
  167. { if (! start_function ($1, $3))
  168. YYFAIL;
  169. reinit_parse_for_function (); }
  170. xdecls
  171. { store_parm_decls (); }
  172. compstmt
  173. { finish_function (input_filename, @7.first_line); }
  174. | typed_declspecs setspecs declarator error
  175. { }
  176. | declmods setspecs notype_declarator
  177. { if (! start_function ($1, $3))
  178. YYFAIL;
  179. reinit_parse_for_function (); }
  180. xdecls
  181. { store_parm_decls (); }
  182. compstmt
  183. { finish_function (input_filename, @7.first_line); }
  184. | declmods setspecs notype_declarator error
  185. { }
  186. | setspecs notype_declarator
  187. { if (! start_function (0, $2))
  188. YYFAIL;
  189. reinit_parse_for_function (); }
  190. xdecls
  191. { store_parm_decls (); }
  192. compstmt
  193. { finish_function (input_filename, @6.first_line); }
  194. | setspecs notype_declarator error
  195. { }
  196. ;
  197. identifier:
  198. IDENTIFIER
  199. | TYPENAME
  200. ;
  201. unop: '&'
  202. { $$ = ADDR_EXPR; }
  203. | '-'
  204. { $$ = NEGATE_EXPR; }
  205. | '+'
  206. { $$ = CONVERT_EXPR; }
  207. | PLUSPLUS
  208. { $$ = PREINCREMENT_EXPR; }
  209. | MINUSMINUS
  210. { $$ = PREDECREMENT_EXPR; }
  211. | '~'
  212. { $$ = BIT_NOT_EXPR; }
  213. | '!'
  214. { $$ = TRUTH_NOT_EXPR; }
  215. ;
  216. expr: nonnull_exprlist
  217. { $$ = build_compound_expr ($1); }
  218. ;
  219. exprlist:
  220. /* empty */
  221. { $$ = NULL_TREE; }
  222. | nonnull_exprlist
  223. ;
  224. nonnull_exprlist:
  225. expr_no_commas
  226. { $$ = build_tree_list (NULL_TREE, $1); }
  227. | nonnull_exprlist ',' expr_no_commas
  228. { chainon ($1, build_tree_list (NULL_TREE, $3)); }
  229. ;
  230. expr_no_commas:
  231. primary
  232. | '*' expr_no_commas %prec UNARY
  233. { $$ = build_indirect_ref ($2, "unary *"); }
  234. | unop expr_no_commas %prec UNARY
  235. { $$ = build_unary_op ($1, $2, 0); }
  236. | '(' typename ')' expr_no_commas %prec UNARY
  237. { tree type = groktypename ($2);
  238. $$ = build_c_cast (type, $4); }
  239. | '(' typename ')' '{' initlist maybecomma '}' %prec UNARY
  240. { tree type = groktypename ($2);
  241. if (pedantic)
  242. warning ("ANSI C forbids constructor-expressions");
  243. $$ = digest_init (type, build_nt (CONSTRUCTOR, NULL_TREE, nreverse ($5)), 0);
  244. if (TREE_CODE (type) == ARRAY_TYPE && TYPE_SIZE (type) == 0)
  245. {
  246. int failure = complete_array_type (type, $$, 1);
  247. if (failure)
  248. abort ();
  249. }
  250. }
  251. | SIZEOF expr_no_commas %prec UNARY
  252. { if (TREE_CODE ($2) == COMPONENT_REF
  253. && TREE_PACKED (TREE_OPERAND ($2, 1)))
  254. error ("sizeof applied to a bit-field");
  255. $$ = c_sizeof (TREE_TYPE ($2)); }
  256. | SIZEOF '(' typename ')' %prec HYPERUNARY
  257. { $$ = c_sizeof (groktypename ($3)); }
  258. | ALIGNOF expr_no_commas %prec UNARY
  259. { if (TREE_CODE ($2) == COMPONENT_REF
  260. && TREE_PACKED (TREE_OPERAND ($2, 1)))
  261. error ("__alignof applied to a bit-field");
  262. $$ = c_alignof (TREE_TYPE ($2)); }
  263. | ALIGNOF '(' typename ')' %prec HYPERUNARY
  264. { $$ = c_alignof (groktypename ($3)); }
  265. | expr_no_commas '+' expr_no_commas
  266. { $$ = build_binary_op ($2, $1, $3); }
  267. | expr_no_commas '-' expr_no_commas
  268. { $$ = build_binary_op ($2, $1, $3); }
  269. | expr_no_commas '*' expr_no_commas
  270. { $$ = build_binary_op ($2, $1, $3); }
  271. | expr_no_commas '/' expr_no_commas
  272. { $$ = build_binary_op ($2, $1, $3); }
  273. | expr_no_commas '%' expr_no_commas
  274. { $$ = build_binary_op ($2, $1, $3); }
  275. | expr_no_commas LSHIFT expr_no_commas
  276. { $$ = build_binary_op ($2, $1, $3); }
  277. | expr_no_commas RSHIFT expr_no_commas
  278. { $$ = build_binary_op ($2, $1, $3); }
  279. | expr_no_commas ARITHCOMPARE expr_no_commas
  280. { $$ = build_binary_op ($2, $1, $3); }
  281. | expr_no_commas EQCOMPARE expr_no_commas
  282. { $$ = build_binary_op ($2, $1, $3); }
  283. | expr_no_commas '&' expr_no_commas
  284. { $$ = build_binary_op ($2, $1, $3); }
  285. | expr_no_commas '|' expr_no_commas
  286. { $$ = build_binary_op ($2, $1, $3); }
  287. | expr_no_commas '^' expr_no_commas
  288. { $$ = build_binary_op ($2, $1, $3); }
  289. | expr_no_commas ANDAND expr_no_commas
  290. { $$ = build_binary_op (TRUTH_ANDIF_EXPR, $1, $3); }
  291. | expr_no_commas OROR expr_no_commas
  292. { $$ = build_binary_op (TRUTH_ORIF_EXPR, $1, $3); }
  293. | expr_no_commas '?' xexpr ':' expr_no_commas
  294. { $$ = build_conditional_expr ($1, $3, $5); }
  295. | expr_no_commas '=' expr_no_commas
  296. { $$ = build_modify_expr ($1, NOP_EXPR, $3); }
  297. | expr_no_commas ASSIGN expr_no_commas
  298. { $$ = build_modify_expr ($1, $2, $3); }
  299. ;
  300. primary:
  301. IDENTIFIER
  302. { $$ = lastiddecl;
  303. if (!$$)
  304. {
  305. if (yychar == YYEMPTY)
  306. yychar = YYLEX;
  307. if (yychar == '(')
  308. $$ = implicitly_declare ($1);
  309. else
  310. {
  311. if (IDENTIFIER_GLOBAL_VALUE ($1) != error_mark_node)
  312. error ("undeclared variable `%s' (first use here)",
  313. IDENTIFIER_POINTER ($1));
  314. $$ = error_mark_node;
  315. /* Prevent repeated error messages. */
  316. IDENTIFIER_GLOBAL_VALUE ($1) = error_mark_node;
  317. }
  318. }
  319. if (TREE_CODE ($$) == CONST_DECL)
  320. $$ = DECL_INITIAL ($$);
  321. }
  322. | CONSTANT
  323. | string
  324. { $$ = combine_strings ($1); }
  325. | '(' expr ')'
  326. { $$ = $2; }
  327. | '(' error ')'
  328. { $$ = error_mark_node; }
  329. | '('
  330. { if (current_function_decl == 0)
  331. {
  332. error ("braced-group within expression allowed only inside a function");
  333. YYFAIL;
  334. }
  335. expand_start_stmt_expr (); }
  336. compstmt ')'
  337. { if (pedantic)
  338. warning ("ANSI C forbids braced-groups within expressions");
  339. $$ = get_last_expr ();
  340. expand_end_stmt_expr (); }
  341. | primary '(' exprlist ')' %prec '.'
  342. { $$ = build_function_call ($1, $3); }
  343. | primary '[' expr ']' %prec '.'
  344. { $$ = build_array_ref ($1, $3); }
  345. | primary '.' identifier
  346. { $$ = build_component_ref ($1, $3); }
  347. | primary POINTSAT identifier
  348. { $$ = build_component_ref (build_indirect_ref ($1, "->"), $3); }
  349. | primary PLUSPLUS
  350. { $$ = build_unary_op (POSTINCREMENT_EXPR, $1, 0); }
  351. | primary MINUSMINUS
  352. { $$ = build_unary_op (POSTDECREMENT_EXPR, $1, 0); }
  353. ;
  354. /* Produces a STRING_CST with perhaps more STRING_CSTs chained onto it. */
  355. string:
  356. STRING
  357. | string STRING
  358. { $$ = chainon ($1, $2); }
  359. ;
  360. xdecls:
  361. /* empty */
  362. | decls
  363. ;
  364. decls:
  365. decl
  366. | errstmt
  367. | decls decl
  368. | decl errstmt
  369. ;
  370. /* records the type and storage class specs to use for processing
  371. the declarators that follow */
  372. setspecs: /* empty */
  373. { current_declspecs = $<ttype>0;
  374. $$ = suspend_momentary (); }
  375. ;
  376. decl:
  377. typed_declspecs setspecs initdecls ';'
  378. { resume_momentary ($2); }
  379. | declmods setspecs notype_initdecls ';'
  380. { resume_momentary ($2); }
  381. | typed_declspecs ';'
  382. { shadow_tag ($1); }
  383. | declmods ';'
  384. { warning ("empty declaration"); }
  385. ;
  386. /* Declspecs which contain at least one type specifier or typedef name.
  387. (Just `const' or `volatile' is not enough.)
  388. A typedef'd name following these is taken as a name to be declared. */
  389. typed_declspecs:
  390. typespec reserved_declspecs
  391. { $$ = tree_cons (NULL_TREE, $1, $2); }
  392. | declmods typespec reserved_declspecs
  393. { $$ = chainon ($3, tree_cons (NULL_TREE, $2, $1)); }
  394. ;
  395. reserved_declspecs: /* empty */
  396. { $$ = NULL_TREE; }
  397. | reserved_declspecs typespecqual_reserved
  398. { $$ = tree_cons (NULL_TREE, $2, $1); }
  399. | reserved_declspecs SCSPEC
  400. { $$ = tree_cons (NULL_TREE, $2, $1); }
  401. ;
  402. /* List of just storage classes and type modifiers.
  403. A declaration can start with just this, but then it cannot be used
  404. to redeclare a typedef-name. */
  405. declmods:
  406. TYPE_QUAL
  407. { $$ = tree_cons (NULL_TREE, $1, NULL_TREE); }
  408. | SCSPEC
  409. { $$ = tree_cons (NULL_TREE, $1, NULL_TREE); }
  410. | declmods TYPE_QUAL
  411. { $$ = tree_cons (NULL_TREE, $2, $1); }
  412. | declmods SCSPEC
  413. { $$ = tree_cons (NULL_TREE, $2, $1); }
  414. ;
  415. /* Used instead of declspecs where storage classes are not allowed
  416. (that is, for typenames and structure components).
  417. Don't accept a typedef-name if anything but a modifier precedes it. */
  418. typed_typespecs:
  419. typespec reserved_typespecquals
  420. { $$ = tree_cons (NULL_TREE, $1, $2); }
  421. | nonempty_type_quals typespec reserved_typespecquals
  422. { $$ = chainon ($3, tree_cons (NULL_TREE, $2, $1)); }
  423. ;
  424. reserved_typespecquals: /* empty */
  425. { $$ = NULL_TREE; }
  426. | reserved_typespecquals typespecqual_reserved
  427. { $$ = tree_cons (NULL_TREE, $2, $1); }
  428. ;
  429. /* A typespec (but not a type qualifier).
  430. Once we have seen one of these in a declaration,
  431. if a typedef name appears then it is being redeclared. */
  432. typespec: TYPESPEC
  433. | structsp
  434. | TYPENAME
  435. | TYPEOF '(' expr ')'
  436. { $$ = TREE_TYPE ($3);
  437. if (pedantic)
  438. warning ("ANSI C forbids `typeof'") }
  439. | TYPEOF '(' typename ')'
  440. { $$ = groktypename ($3);
  441. if (pedantic)
  442. warning ("ANSI C forbids `typeof'") }
  443. ;
  444. /* A typespec that is a reserved word, or a type qualifier. */
  445. typespecqual_reserved: TYPESPEC
  446. | TYPE_QUAL
  447. | structsp
  448. ;
  449. initdecls:
  450. initdcl
  451. | initdecls ',' initdcl
  452. ;
  453. notype_initdecls:
  454. notype_initdcl
  455. | notype_initdecls ',' initdcl
  456. ;
  457. maybeasm:
  458. /* empty */
  459. { $$ = NULL_TREE; }
  460. | ASM '(' string ')'
  461. { if (TREE_CHAIN ($3)) $3 = combine_strings ($3);
  462. $$ = $3;
  463. if (pedantic)
  464. warning ("ANSI C forbids use of `asm' keyword");
  465. }
  466. ;
  467. initdcl:
  468. declarator maybeasm '='
  469. { $<ttype>$ = start_decl ($1, current_declspecs, 1); }
  470. init
  471. /* Note how the declaration of the variable is in effect while its init is parsed! */
  472. { finish_decl ($<ttype>4, $5, $2); }
  473. | declarator maybeasm
  474. { tree d = start_decl ($1, current_declspecs, 0);
  475. finish_decl (d, NULL_TREE, $2); }
  476. ;
  477. notype_initdcl:
  478. notype_declarator maybeasm '='
  479. { $<ttype>$ = start_decl ($1, current_declspecs, 1); }
  480. init
  481. /* Note how the declaration of the variable is in effect while its init is parsed! */
  482. { finish_decl ($<ttype>4, $5, $2); }
  483. | notype_declarator maybeasm
  484. { tree d = start_decl ($1, current_declspecs, 0);
  485. finish_decl (d, NULL_TREE, $2); }
  486. ;
  487. init:
  488. expr_no_commas
  489. | '{' initlist '}'
  490. { $$ = build_nt (CONSTRUCTOR, NULL_TREE, nreverse ($2)); }
  491. | '{' initlist ',' '}'
  492. { $$ = build_nt (CONSTRUCTOR, NULL_TREE, nreverse ($2)); }
  493. | error
  494. { $$ = NULL_TREE; }
  495. ;
  496. /* This chain is built in reverse order,
  497. and put in forward order where initlist is used. */
  498. initlist:
  499. init
  500. { $$ = build_tree_list (NULL_TREE, $1); }
  501. | initlist ',' init
  502. { $$ = tree_cons (NULL_TREE, $3, $1); }
  503. ;
  504. /* Any kind of declarator (thus, all declarators allowed
  505. after an explicit typespec). */
  506. declarator:
  507. after_type_declarator
  508. | notype_declarator
  509. ;
  510. /* A declarator that is allowed only after an explicit typespec. */
  511. after_type_declarator:
  512. '(' after_type_declarator ')'
  513. { $$ = $2; }
  514. | after_type_declarator '(' parmlist_or_identifiers %prec '.'
  515. { $$ = build_nt (CALL_EXPR, $1, $3, NULL_TREE); }
  516. /* | after_type_declarator '(' error ')' %prec '.'
  517. { $$ = build_nt (CALL_EXPR, $1, NULL_TREE, NULL_TREE);
  518. poplevel (0, 0, 0); } */
  519. | after_type_declarator '[' expr ']' %prec '.'
  520. { $$ = build_nt (ARRAY_REF, $1, $3); }
  521. | after_type_declarator '[' ']' %prec '.'
  522. { $$ = build_nt (ARRAY_REF, $1, NULL_TREE); }
  523. | '*' type_quals after_type_declarator %prec UNARY
  524. { $$ = make_pointer_declarator ($2, $3); }
  525. | TYPENAME
  526. ;
  527. /* Kinds of declarator that can appear in a parameter list
  528. in addition to notype_declarator. This is like after_type_declarator
  529. but does not allow a typedef name in parentheses as an identifier
  530. (because it would conflict with a function with that typedef as arg). */
  531. parm_declarator:
  532. parm_declarator '(' parmlist_or_identifiers %prec '.'
  533. { $$ = build_nt (CALL_EXPR, $1, $3, NULL_TREE); }
  534. /* | parm_declarator '(' error ')' %prec '.'
  535. { $$ = build_nt (CALL_EXPR, $1, NULL_TREE, NULL_TREE);
  536. poplevel (0, 0, 0); } */
  537. | parm_declarator '[' expr ']' %prec '.'
  538. { $$ = build_nt (ARRAY_REF, $1, $3); }
  539. | parm_declarator '[' ']' %prec '.'
  540. { $$ = build_nt (ARRAY_REF, $1, NULL_TREE); }
  541. | '*' type_quals parm_declarator %prec UNARY
  542. { $$ = make_pointer_declarator ($2, $3); }
  543. | TYPENAME
  544. ;
  545. /* A declarator allowed whether or not there has been
  546. an explicit typespec. These cannot redeclare a typedef-name. */
  547. notype_declarator:
  548. notype_declarator '(' parmlist_or_identifiers %prec '.'
  549. { $$ = build_nt (CALL_EXPR, $1, $3, NULL_TREE); }
  550. /* | notype_declarator '(' error ')' %prec '.'
  551. { $$ = build_nt (CALL_EXPR, $1, NULL_TREE, NULL_TREE);
  552. poplevel (0, 0, 0); } */
  553. | '(' notype_declarator ')'
  554. { $$ = $2; }
  555. | '*' type_quals notype_declarator %prec UNARY
  556. { $$ = make_pointer_declarator ($2, $3); }
  557. | notype_declarator '[' expr ']' %prec '.'
  558. { $$ = build_nt (ARRAY_REF, $1, $3); }
  559. | notype_declarator '[' ']' %prec '.'
  560. { $$ = build_nt (ARRAY_REF, $1, NULL_TREE); }
  561. | IDENTIFIER
  562. ;
  563. structsp:
  564. STRUCT identifier '{'
  565. { $$ = start_struct (RECORD_TYPE, $2);
  566. /* Start scope of tag before parsing components. */
  567. }
  568. component_decl_list '}'
  569. { $$ = finish_struct ($<ttype>4, $5);
  570. /* Really define the structure. */
  571. }
  572. | STRUCT '{' component_decl_list '}'
  573. { $$ = finish_struct (start_struct (RECORD_TYPE, NULL_TREE),
  574. $3); }
  575. | STRUCT identifier
  576. { $$ = xref_tag (RECORD_TYPE, $2); }
  577. | UNION identifier '{'
  578. { $$ = start_struct (UNION_TYPE, $2); }
  579. component_decl_list '}'
  580. { $$ = finish_struct ($<ttype>4, $5); }
  581. | UNION '{' component_decl_list '}'
  582. { $$ = finish_struct (start_struct (UNION_TYPE, NULL_TREE),
  583. $3); }
  584. | UNION identifier
  585. { $$ = xref_tag (UNION_TYPE, $2); }
  586. | ENUM identifier '{'
  587. { $<itype>3 = suspend_momentary ();
  588. $$ = start_enum ($2); }
  589. enumlist maybecomma '}'
  590. { $$ = finish_enum ($<ttype>4, nreverse ($5));
  591. resume_momentary ($<itype>3); }
  592. | ENUM '{'
  593. { $<itype>2 = suspend_momentary ();
  594. $$ = start_enum (NULL_TREE); }
  595. enumlist maybecomma '}'
  596. { $$ = finish_enum ($<ttype>3, nreverse ($4));
  597. resume_momentary ($<itype>2); }
  598. | ENUM identifier
  599. { $$ = xref_tag (ENUMERAL_TYPE, $2); }
  600. ;
  601. maybecomma:
  602. /* empty */
  603. | ','
  604. ;
  605. component_decl_list: /* empty */
  606. { $$ = NULL_TREE; }
  607. | component_decl_list component_decl ';'
  608. { $$ = chainon ($1, $2); }
  609. | component_decl_list ';'
  610. { if (pedantic)
  611. warning ("extra semicolon in struct or union specified"); }
  612. ;
  613. /* There is a shift-reduce conflict here, because `components' may
  614. start with a `typename'. It happens that shifting (the default resolution)
  615. does the right thing, because it treats the `typename' as part of
  616. a `typed_typespecs'.
  617. It is possible that this same technique would allow the distinction
  618. between `notype_initdecls' and `initdecls' to be eliminated.
  619. But I am being cautious and not trying it. */
  620. component_decl:
  621. typed_typespecs setspecs components
  622. { $$ = $3;
  623. resume_momentary ($2); }
  624. | nonempty_type_quals setspecs components
  625. { $$ = $3;
  626. resume_momentary ($2); }
  627. | error
  628. { $$ = NULL_TREE; }
  629. ;
  630. components:
  631. /* empty */
  632. { $$ = NULL_TREE; }
  633. | component_declarator
  634. | components ',' component_declarator
  635. { $$ = chainon ($1, $3); }
  636. ;
  637. component_declarator:
  638. declarator
  639. { $$ = grokfield (input_filename, @1.first_line, $1, current_declspecs, NULL_TREE); }
  640. | declarator ':' expr_no_commas
  641. { $$ = grokfield (input_filename, @1.first_line, $1, current_declspecs, $3); }
  642. | ':' expr_no_commas
  643. { $$ = grokfield (input_filename, @1.first_line, NULL_TREE, current_declspecs, $2); }
  644. ;
  645. /* We chain the enumerators in reverse order.
  646. They are put in forward order where enumlist is used.
  647. (The order used to be significant, but no longer is so.
  648. However, we still maintain the order, just to be clean.) */
  649. enumlist:
  650. enumerator
  651. | enumlist ',' enumerator
  652. { $$ = chainon ($3, $1); }
  653. ;
  654. enumerator:
  655. identifier
  656. { $$ = build_enumerator ($1, NULL_TREE); }
  657. | identifier '=' expr_no_commas
  658. { $$ = build_enumerator ($1, $3); }
  659. ;
  660. typename:
  661. typed_typespecs absdcl
  662. { $$ = build_tree_list ($1, $2); }
  663. | nonempty_type_quals absdcl
  664. { $$ = build_tree_list ($1, $2); }
  665. ;
  666. absdcl: /* an absolute declarator */
  667. /* empty */
  668. { $$ = NULL_TREE; }
  669. | absdcl1
  670. ;
  671. nonempty_type_quals:
  672. TYPE_QUAL
  673. { $$ = tree_cons (NULL_TREE, $1, NULL_TREE); }
  674. | nonempty_type_quals TYPE_QUAL
  675. { $$ = tree_cons (NULL_TREE, $2, $1); }
  676. ;
  677. type_quals:
  678. /* empty */
  679. { $$ = NULL_TREE; }
  680. | type_quals TYPE_QUAL
  681. { $$ = tree_cons (NULL_TREE, $2, $1); }
  682. ;
  683. absdcl1: /* a nonempty absolute declarator */
  684. '(' absdcl1 ')'
  685. { $$ = $2; }
  686. /* `(typedef)1' is `int'. */
  687. | '*' type_quals absdcl1 %prec UNARY
  688. { $$ = make_pointer_declarator ($2, $3); }
  689. | '*' type_quals %prec UNARY
  690. { $$ = make_pointer_declarator ($2, NULL_TREE); }
  691. | absdcl1 '(' parmlist %prec '.'
  692. { $$ = build_nt (CALL_EXPR, $1, $3, NULL_TREE); }
  693. | absdcl1 '[' expr ']' %prec '.'
  694. { $$ = build_nt (ARRAY_REF, $1, $3); }
  695. | absdcl1 '[' ']' %prec '.'
  696. { $$ = build_nt (ARRAY_REF, $1, NULL_TREE); }
  697. | '(' parmlist %prec '.'
  698. { $$ = build_nt (CALL_EXPR, NULL_TREE, $2, NULL_TREE); }
  699. | '[' expr ']' %prec '.'
  700. { $$ = build_nt (ARRAY_REF, NULL_TREE, $2); }
  701. | '[' ']' %prec '.'
  702. { $$ = build_nt (ARRAY_REF, NULL_TREE, NULL_TREE); }
  703. ;
  704. /* at least one statement, the first of which parses without error. */
  705. /* stmts is used only after decls, so an invalid first statement
  706. is actually regarded as an invalid decl and part of the decls. */
  707. stmts:
  708. stmt
  709. | stmts stmt
  710. | stmts errstmt
  711. ;
  712. xstmts:
  713. /* empty */
  714. | stmts
  715. ;
  716. errstmt: error ';'
  717. ;
  718. pushlevel: /* empty */
  719. { pushlevel (0);
  720. clear_last_expr ();
  721. push_momentary ();
  722. expand_start_bindings (0); }
  723. ;
  724. compstmt: '{' '}'
  725. | '{' pushlevel decls xstmts '}'
  726. { expand_end_bindings (getdecls (), 1);
  727. poplevel (1, 1, 0);
  728. pop_momentary (); }
  729. | '{' pushlevel error '}'
  730. { expand_end_bindings (getdecls (), 0);
  731. poplevel (0, 0, 0);
  732. pop_momentary (); }
  733. | '{' pushlevel stmts '}'
  734. { expand_end_bindings (getdecls (), 0);
  735. poplevel (0, 0, 0);
  736. pop_momentary (); }
  737. ;
  738. simple_if:
  739. IF '(' expr ')'
  740. { emit_note (input_filename, @1.first_line);
  741. expand_start_cond (truthvalue_conversion ($3), 0); }
  742. stmt
  743. ;
  744. stmt:
  745. compstmt
  746. | expr ';'
  747. { emit_note (input_filename, @1.first_line);
  748. expand_expr_stmt ($1);
  749. clear_momentary (); }
  750. | simple_if ELSE
  751. { expand_start_else (); }
  752. stmt
  753. { expand_end_else (); }
  754. | simple_if
  755. { expand_end_cond (); }
  756. | WHILE
  757. { emit_note (input_filename, @1.first_line);
  758. expand_start_loop (1); }
  759. '(' expr ')'
  760. { expand_exit_loop_if_false (truthvalue_conversion ($4)); }
  761. stmt
  762. { expand_end_loop (); }
  763. | DO
  764. { emit_note (input_filename, @1.first_line);
  765. expand_start_loop_continue_elsewhere (1); }
  766. stmt WHILE
  767. { expand_loop_continue_here (); }
  768. '(' expr ')' ';'
  769. { emit_note (input_filename, @7.first_line);
  770. expand_exit_loop_if_false (truthvalue_conversion ($7));
  771. expand_end_loop ();
  772. clear_momentary (); }
  773. | FOR
  774. '(' xexpr ';'
  775. { emit_note (input_filename, @1.first_line);
  776. if ($3) expand_expr_stmt ($3);
  777. expand_start_loop_continue_elsewhere (1); }
  778. xexpr ';'
  779. { emit_note (input_filename, @6.first_line);
  780. if ($6)
  781. expand_exit_loop_if_false (truthvalue_conversion ($6)); }
  782. xexpr ')'
  783. /* Don't let the tree nodes for $9 be discarded
  784. by clear_momentary during the parsing of the next stmt. */
  785. { push_momentary (); }
  786. stmt
  787. { emit_note (input_filename, @9.first_line);
  788. expand_loop_continue_here ();
  789. if ($9)
  790. {
  791. expand_expr_stmt ($9);
  792. }
  793. pop_momentary ();
  794. expand_end_loop (); }
  795. | SWITCH '(' expr ')'
  796. { emit_note (input_filename, @1.first_line);
  797. c_expand_start_case ($3);
  798. /* Don't let the tree nodes for $3 be discarded by
  799. clear_momentary during the parsing of the next stmt. */
  800. push_momentary (); }
  801. stmt
  802. { expand_end_case ();
  803. pop_momentary (); }
  804. | CASE expr ':'
  805. { register tree value = fold ($2);
  806. register tree label
  807. = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
  808. if (TREE_CODE (value) != INTEGER_CST
  809. && value != error_mark_node)
  810. {
  811. error ("case label does not reduce to an integer constant");
  812. value = error_mark_node;
  813. }
  814. else
  815. /* Promote char or short to int. */
  816. value = default_conversion (value);
  817. if (value != error_mark_node)
  818. {
  819. int success = pushcase (value, label);
  820. if (success == 1)
  821. error ("case label not within a switch statement");
  822. else if (success == 2)
  823. error ("duplicate case value");
  824. else if (success == 3)
  825. warning ("case value out of range");
  826. }
  827. }
  828. stmt
  829. | DEFAULT ':'
  830. {
  831. register tree label
  832. = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
  833. int success = pushcase (NULL_TREE, label);
  834. if (success == 1)
  835. error ("default label not within a switch statement");
  836. else if (success == 2)
  837. error ("multiple default labels in one switch");
  838. }
  839. stmt
  840. | BREAK ';'
  841. { emit_note (input_filename, @1.first_line);
  842. if ( ! expand_exit_something ())
  843. error ("break statement not within loop or switch"); }
  844. | CONTINUE ';'
  845. { emit_note (input_filename, @1.first_line);
  846. if (! expand_continue_loop ())
  847. error ("continue statement not within a loop"); }
  848. | RETURN ';'
  849. { emit_note (input_filename, @1.first_line);
  850. c_expand_return (NULL_TREE); }
  851. | RETURN expr ';'
  852. { emit_note (input_filename, @1.first_line);
  853. c_expand_return ($2); }
  854. | ASM maybe_type_qual '(' string ')' ';'
  855. { if (pedantic)
  856. warning ("ANSI C forbids use of `asm' keyword");
  857. emit_note (input_filename, @1.first_line);
  858. if (TREE_CHAIN ($4)) $4 = combine_strings ($4);
  859. expand_asm ($4); }
  860. /* This is the case with just output operands. */
  861. | ASM maybe_type_qual '(' string ':' asm_operands ')' ';'
  862. { if (pedantic)
  863. warning ("ANSI C forbids use of `asm' keyword");
  864. emit_note (input_filename, @1.first_line);
  865. if (TREE_CHAIN ($4)) $4 = combine_strings ($4);
  866. c_expand_asm_operands ($4, $6, NULL_TREE,
  867. $2 == ridpointers[(int)RID_VOLATILE]); }
  868. /* This is the case with input operands as well. */
  869. | ASM maybe_type_qual '(' string ':' asm_operands ':' asm_operands ')' ';'
  870. { if (pedantic)
  871. warning ("ANSI C forbids use of `asm' keyword");
  872. emit_note (input_filename, @1.first_line);
  873. if (TREE_CHAIN ($4)) $4 = combine_strings ($4);
  874. c_expand_asm_operands ($4, $6, $8,
  875. $2 == ridpointers[(int)RID_VOLATILE]); }
  876. | GOTO identifier ';'
  877. { tree decl;
  878. emit_note (input_filename, @1.first_line);
  879. decl = lookup_label ($2);
  880. expand_goto (decl); }
  881. | identifier ':'
  882. { tree label = define_label (input_filename, @1.first_line, $1);
  883. if (label)
  884. expand_label (label); }
  885. stmt
  886. | ';'
  887. ;
  888. maybe_type_qual:
  889. /* empty */
  890. | TYPE_QUAL
  891. ;
  892. xexpr:
  893. /* empty */
  894. { $$ = NULL_TREE; }
  895. | expr
  896. ;
  897. /* These are the operands other than the first string and comma
  898. in asm ("addextend %2,%1", "=dm" (x), "0" (y), "g" (*x)) */
  899. asm_operands: asm_operand
  900. | asm_operands ',' asm_operand
  901. { $$ = chainon ($1, $3); }
  902. ;
  903. asm_operand:
  904. | STRING '(' expr ')'
  905. { $$ = build_tree_list ($1, $3); }
  906. ;
  907. /* This is what appears inside the parens in a function declarator.
  908. Its value is a list of ..._TYPE nodes.
  909. The caller must do `poplevel (0, 0, 0);' after return from this.
  910. (The caller does it so that error recovery to the closeparen
  911. can also do it.) */
  912. parmlist:
  913. { pushlevel (0); }
  914. parmlist_1
  915. { $$ = $2; poplevel (0); }
  916. ;
  917. /* This is referred to where either a parmlist or an identifier list is ok.
  918. Its value is a list of ..._TYPE nodes or a list of identifiers.
  919. The caller must do `poplevel (0, 0, 0);' after return from this. */
  920. parmlist_or_identifiers:
  921. { pushlevel (0); }
  922. parmlist_or_identifiers_1
  923. { $$ = $2; poplevel (0, 0, 0); }
  924. ;
  925. parmlist_or_identifiers_1:
  926. parmlist_2 ')'
  927. | identifiers ')'
  928. | error ')'
  929. { $$ = NULL_TREE; }
  930. ;
  931. parmlist_1:
  932. parmlist_2 ')'
  933. | error ')'
  934. { $$ = NULL_TREE; }
  935. ;
  936. /* This is what appears inside the parens in a function declarator.
  937. Is value is represented in the format that grokdeclarator expects. */
  938. parmlist_2: /* empty */
  939. { $$ = get_parm_types (0); }
  940. | parms
  941. { $$ = get_parm_types (1); }
  942. | parms ',' ELLIPSIS
  943. { $$ = get_parm_types (0); }
  944. ;
  945. parms:
  946. parm
  947. { push_parm_decl ($1); }
  948. | parms ',' parm
  949. { push_parm_decl ($3); }
  950. ;
  951. /* A single parameter declaration or parameter type name,
  952. as found in a parmlist. */
  953. parm:
  954. typed_declspecs parm_declarator
  955. { $$ = build_tree_list ($1, $2) ; }
  956. | typed_declspecs notype_declarator
  957. { $$ = build_tree_list ($1, $2) ; }
  958. | typed_declspecs absdcl
  959. { $$ = build_tree_list ($1, $2); }
  960. | declmods notype_declarator
  961. { $$ = build_tree_list ($1, $2) ; }
  962. | declmods absdcl
  963. { $$ = build_tree_list ($1, $2); }
  964. ;
  965. /* A nonempty list of identifiers. */
  966. identifiers:
  967. IDENTIFIER
  968. { $$ = build_tree_list (NULL_TREE, $1); }
  969. | identifiers ',' IDENTIFIER
  970. { $$ = chainon ($1, build_tree_list (NULL_TREE, $3)); }
  971. ;
  972. %%
  973. /* Return something to represent absolute declarators containing a *.
  974. TARGET is the absolute declarator that the * contains.
  975. TYPE_QUALS is a list of modifiers such as const or volatile
  976. to apply to the pointer type, represented as identifiers.
  977. We return an INDIRECT_REF whose "contents" are TARGET
  978. and whose type is the modifier list. */
  979. static tree
  980. make_pointer_declarator (type_quals, target)
  981. tree type_quals, target;
  982. {
  983. return build (INDIRECT_REF, type_quals, target);
  984. }
  985. /* Given a chain of STRING_CST nodes,
  986. concatenate them into one STRING_CST
  987. and give it a suitable array-of-chars data type. */
  988. static tree
  989. combine_strings (strings)
  990. tree strings;
  991. {
  992. register tree value, t;
  993. register int length = 1;
  994. int wide_length = 0;
  995. int wide_flag = 0;
  996. if (TREE_CHAIN (strings))
  997. {
  998. /* More than one in the chain, so concatenate. */
  999. register char *p, *q;
  1000. /* Don't include the \0 at the end of each substring,
  1001. except for the last one.
  1002. Count wide strings and ordinary strings separately. */
  1003. for (t = strings; t; t = TREE_CHAIN (t))
  1004. {
  1005. if (TREE_TYPE (t) == int_array_type_node)
  1006. {
  1007. wide_length += (TREE_STRING_LENGTH (t) - 1);
  1008. wide_flag = 1;
  1009. }
  1010. else
  1011. length += (TREE_STRING_LENGTH (t) - 1);
  1012. }
  1013. /* If anything is wide, the non-wides will be converted,
  1014. which makes them take more space. */
  1015. if (wide_flag)
  1016. length = length * UNITS_PER_WORD + wide_length;
  1017. p = (char *) oballoc (length);
  1018. /* Copy the individual strings into the new combined string.
  1019. If the combined string is wide, convert the chars to ints
  1020. for any individual strings that are not wide. */
  1021. q = p;
  1022. for (t = strings; t; t = TREE_CHAIN (t))
  1023. {
  1024. int len = TREE_STRING_LENGTH (t) - 1;
  1025. if ((TREE_TYPE (t) == int_array_type_node) == wide_flag)
  1026. {
  1027. bcopy (TREE_STRING_POINTER (t), q, len);
  1028. q += len;
  1029. }
  1030. else
  1031. {
  1032. int i;
  1033. for (i = 0; i < len; i++)
  1034. ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
  1035. q += len * UNITS_PER_WORD;
  1036. }
  1037. }
  1038. *q = 0;
  1039. value = make_node (STRING_CST);
  1040. TREE_STRING_POINTER (value) = p;
  1041. TREE_STRING_LENGTH (value) = length;
  1042. TREE_LITERAL (value) = 1;
  1043. }
  1044. else
  1045. {
  1046. value = strings;
  1047. length = TREE_STRING_LENGTH (value);
  1048. if (TREE_TYPE (value) == int_array_type_node)
  1049. wide_flag = 1;
  1050. }
  1051. TREE_TYPE (value)
  1052. = build_array_type (wide_flag ? integer_type_node : char_type_node,
  1053. make_index_type (build_int_2 (length - 1, 0)));
  1054. TREE_LITERAL (value) = 1;
  1055. TREE_STATIC (value) = 1;
  1056. return value;
  1057. }
  1058. int lineno; /* current line number in file being read */
  1059. FILE *finput; /* input file.
  1060. Normally a pipe from the preprocessor. */
  1061. /* lexical analyzer */
  1062. static int maxtoken; /* Current nominal length of token buffer */
  1063. static char *token_buffer; /* Pointer to token buffer.
  1064. Actual allocated length is maxtoken + 2. */
  1065. static dollar_seen = 0; /* Nonzero if have warned about `$'. */
  1066. #define MAXRESERVED 9
  1067. /* frw[I] is index in `reswords' of the first word whose length is I;
  1068. frw[I+1] is one plus the index of the last word whose length is I.
  1069. The length of frw must be MAXRESERVED + 2 so there is an element
  1070. at MAXRESERVED+1 for the case I == MAXRESERVED. */
  1071. static char frw[MAXRESERVED+2] =
  1072. { 0, 0, 0, 2, 5, 13, 19, 29, 32, 36, 37 };
  1073. /* Table of reserved words. */
  1074. struct resword { char *name; short token; enum rid rid;};
  1075. #define NORID RID_UNUSED
  1076. static struct resword reswords[]
  1077. = {{"if", IF, NORID},
  1078. {"do", DO, NORID},
  1079. {"int", TYPESPEC, RID_INT},
  1080. {"for", FOR, NORID},
  1081. {"asm", ASM, NORID},
  1082. {"case", CASE, NORID},
  1083. {"char", TYPESPEC, RID_CHAR},
  1084. {"auto", SCSPEC, RID_AUTO},
  1085. {"goto", GOTO, NORID},
  1086. {"else", ELSE, NORID},
  1087. {"long", TYPESPEC, RID_LONG},
  1088. {"void", TYPESPEC, RID_VOID},
  1089. {"enum", ENUM, NORID},
  1090. {"float", TYPESPEC, RID_FLOAT},
  1091. {"short", TYPESPEC, RID_SHORT},
  1092. {"union", UNION, NORID},
  1093. {"break", BREAK, NORID},
  1094. {"while", WHILE, NORID},
  1095. {"const", TYPE_QUAL, RID_CONST},
  1096. {"double", TYPESPEC, RID_DOUBLE},
  1097. {"static", SCSPEC, RID_STATIC},
  1098. {"extern", SCSPEC, RID_EXTERN},
  1099. {"struct", STRUCT, NORID},
  1100. {"return", RETURN, NORID},
  1101. {"sizeof", SIZEOF, NORID},
  1102. {"typeof", TYPEOF, NORID},
  1103. {"switch", SWITCH, NORID},
  1104. {"signed", TYPESPEC, RID_SIGNED},
  1105. {"inline", SCSPEC, RID_INLINE},
  1106. {"typedef", SCSPEC, RID_TYPEDEF},
  1107. {"default", DEFAULT, NORID},
  1108. {"noalias", TYPE_QUAL, RID_NOALIAS},
  1109. {"unsigned", TYPESPEC, RID_UNSIGNED},
  1110. {"continue", CONTINUE, NORID},
  1111. {"register", SCSPEC, RID_REGISTER},
  1112. {"volatile", TYPE_QUAL, RID_VOLATILE},
  1113. {"__alignof", ALIGNOF, NORID}};
  1114. /* The elements of `ridpointers' are identifier nodes
  1115. for the reserved type names and storage classes.
  1116. It is indexed by a RID_... value. */
  1117. tree ridpointers[(int) RID_MAX];
  1118. static tree line_identifier; /* The identifier node named "line" */
  1119. void check_newline ();
  1120. void
  1121. init_lex ()
  1122. {
  1123. extern char *malloc ();
  1124. /* Start it at 0, because check_newline is called at the very beginning
  1125. and will increment it to 1. */
  1126. lineno = 0;
  1127. line_identifier = get_identifier ("line");
  1128. maxtoken = 40;
  1129. token_buffer = malloc (maxtoken + 2);
  1130. ridpointers[(int) RID_INT] = get_identifier ("int");
  1131. ridpointers[(int) RID_CHAR] = get_identifier ("char");
  1132. ridpointers[(int) RID_VOID] = get_identifier ("void");
  1133. ridpointers[(int) RID_FLOAT] = get_identifier ("float");
  1134. ridpointers[(int) RID_DOUBLE] = get_identifier ("double");
  1135. ridpointers[(int) RID_SHORT] = get_identifier ("short");
  1136. ridpointers[(int) RID_LONG] = get_identifier ("long");
  1137. ridpointers[(int) RID_UNSIGNED] = get_identifier ("unsigned");
  1138. ridpointers[(int) RID_SIGNED] = get_identifier ("signed");
  1139. ridpointers[(int) RID_INLINE] = get_identifier ("inline");
  1140. ridpointers[(int) RID_CONST] = get_identifier ("const");
  1141. ridpointers[(int) RID_VOLATILE] = get_identifier ("volatile");
  1142. ridpointers[(int) RID_AUTO] = get_identifier ("auto");
  1143. ridpointers[(int) RID_STATIC] = get_identifier ("static");
  1144. ridpointers[(int) RID_EXTERN] = get_identifier ("extern");
  1145. ridpointers[(int) RID_TYPEDEF] = get_identifier ("typedef");
  1146. ridpointers[(int) RID_REGISTER] = get_identifier ("register");
  1147. }
  1148. static void
  1149. reinit_parse_for_function ()
  1150. {
  1151. }
  1152. static int
  1153. skip_white_space ()
  1154. {
  1155. register int c;
  1156. register int inside;
  1157. c = getc (finput);
  1158. for (;;)
  1159. {
  1160. switch (c)
  1161. {
  1162. case '/':
  1163. c = getc (finput);
  1164. if (c != '*')
  1165. {
  1166. ungetc (c, finput);
  1167. return '/';
  1168. }
  1169. c = getc (finput);
  1170. inside = 1;
  1171. while (inside)
  1172. {
  1173. if (c == '*')
  1174. {
  1175. while (c == '*')
  1176. c = getc (finput);
  1177. if (c == '/')
  1178. {
  1179. inside = 0;
  1180. c = getc (finput);
  1181. }
  1182. }
  1183. else if (c == '\n')
  1184. {
  1185. lineno++;
  1186. c = getc (finput);
  1187. }
  1188. else if (c == EOF)
  1189. {
  1190. error ("unterminated comment");
  1191. break;
  1192. }
  1193. else
  1194. c = getc (finput);
  1195. }
  1196. break;
  1197. case '\n':
  1198. check_newline ();
  1199. case ' ':
  1200. case '\t':
  1201. case '\f':
  1202. case '\r':
  1203. case '\b':
  1204. c = getc (finput);
  1205. break;
  1206. case '\\':
  1207. c = getc (finput);
  1208. if (c == '\n')
  1209. lineno++;
  1210. else
  1211. error ("stray '\\' in program");
  1212. c = getc (finput);
  1213. break;
  1214. default:
  1215. return (c);
  1216. }
  1217. }
  1218. }
  1219. /* Make the token buffer longer, preserving the data in it.
  1220. P should point to just beyond the last valid character in the old buffer.
  1221. The value we return is a pointer to the new buffer
  1222. at a place corresponding to P. */
  1223. static char *
  1224. extend_token_buffer (p)
  1225. char *p;
  1226. {
  1227. int offset = p - token_buffer;
  1228. maxtoken = maxtoken * 2 + 10;
  1229. token_buffer = (char *) realloc (token_buffer, maxtoken + 2);
  1230. if (token_buffer == 0)
  1231. fatal ("virtual memory exceeded");
  1232. return token_buffer + offset;
  1233. }
  1234. /* At the beginning of a line, increment the line number
  1235. and handle a #line directive immediately following */
  1236. void
  1237. check_newline ()
  1238. {
  1239. register int c;
  1240. register int token;
  1241. while (1)
  1242. {
  1243. lineno++;
  1244. /* Read first nonwhite char on the line. */
  1245. c = getc (finput);
  1246. while (c == ' ' || c == '\t')
  1247. c = getc (finput);
  1248. if (c != '#')
  1249. {
  1250. /* If not #, unread it. */
  1251. ungetc (c, finput);
  1252. return;
  1253. }
  1254. /* Read first nonwhite char after the `#'. */
  1255. c = getc (finput);
  1256. while (c == ' ' || c == '\t')
  1257. c = getc (finput);
  1258. /* If a letter follows, then if the word here is `line', skip
  1259. it and ignore it; otherwise, ignore the line, with an error
  1260. if the word isn't `pragma'. */
  1261. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
  1262. {
  1263. if (c == 'p')
  1264. {
  1265. if (getc (finput) == 'r'
  1266. && getc (finput) == 'a'
  1267. && getc (finput) == 'g'
  1268. && getc (finput) == 'm'
  1269. && getc (finput) == 'a'
  1270. && ((c = getc (finput)) == ' ' || c == '\t'))
  1271. goto noerror;
  1272. }
  1273. else if (c == 'l')
  1274. {
  1275. if (getc (finput) == 'i'
  1276. && getc (finput) == 'n'
  1277. && getc (finput) == 'e'
  1278. && ((c = getc (finput)) == ' ' || c == '\t'))
  1279. goto linenum;
  1280. }
  1281. error ("undefined or invalid # directive");
  1282. noerror:
  1283. while ((c = getc (finput)) && c != '\n');
  1284. continue;
  1285. }
  1286. linenum:
  1287. /* Here we have either `#line' or `# <nonletter>'.
  1288. In either case, it should be a line number; a digit should follow.
  1289. while (c == ' ' || c == '\t')
  1290. c = getc (finput);
  1291. /* If the # is the only nonwhite char on the line,
  1292. just ignore it. Check the new newline. */
  1293. if (c == '\n')
  1294. continue;
  1295. /* Something follows the #; read a token. */
  1296. ungetc (c, finput);
  1297. token = yylex ();
  1298. if (token == CONSTANT
  1299. && TREE_CODE (yylval.ttype) == INTEGER_CST)
  1300. {
  1301. /* subtract one, because it is the following line that
  1302. gets the specified number */
  1303. int l = TREE_INT_CST_LOW (yylval.ttype) - 1;
  1304. /* Is this the last nonwhite stuff on the line? */
  1305. c = getc (finput);
  1306. while (c == ' ' || c == '\t')
  1307. c = getc (finput);
  1308. if (c == '\n')
  1309. {
  1310. /* No more: store the line number and check following line. */
  1311. lineno = l;
  1312. continue;
  1313. }
  1314. ungetc (c, finput);
  1315. /* More follows: it must be a string constant (filename). */
  1316. token = yylex ();
  1317. if (token != STRING || TREE_CODE (yylval.ttype) != STRING_CST)
  1318. {
  1319. error ("invalid #line");
  1320. return;
  1321. }
  1322. input_filename
  1323. = (char *) permalloc (TREE_STRING_LENGTH (yylval.ttype) + 1);
  1324. strcpy (input_filename, TREE_STRING_POINTER (yylval.ttype));
  1325. lineno = l;
  1326. if (main_input_filename == 0)
  1327. main_input_filename = input_filename;
  1328. }
  1329. else
  1330. error ("invalid #line");
  1331. /* skip the rest of this line. */
  1332. while ((c = getc (finput)) != '\n');
  1333. }
  1334. }
  1335. #define isalnum(char) ((char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') || (char >= '0' && char <= '9'))
  1336. #define isdigit(char) (char >= '0' && char <= '9')
  1337. #define ENDFILE -1 /* token that represents end-of-file */
  1338. static int
  1339. readescape ()
  1340. {
  1341. register int c = getc (finput);
  1342. register int count, code;
  1343. switch (c)
  1344. {
  1345. case 'x':
  1346. code = 0;
  1347. count = 0;
  1348. while (1)
  1349. {
  1350. c = getc (finput);
  1351. if (!(c >= 'a' && c <= 'f')
  1352. && !(c >= 'A' && c <= 'F')
  1353. && !(c >= '0' && c <= '9'))
  1354. {
  1355. ungetc (c, finput);
  1356. break;
  1357. }
  1358. code *= 16;
  1359. if (c >= 'a' && c <= 'f')
  1360. code += c - 'a' + 10;
  1361. if (c >= 'A' && c <= 'F')
  1362. code += c - 'A' + 10;
  1363. if (c >= '0' && c <= '9')
  1364. code += c - '0';
  1365. count++;
  1366. }
  1367. if (count == 0)
  1368. error ("\\x used with no following hex digits");
  1369. return code;
  1370. case '0': case '1': case '2': case '3': case '4':
  1371. case '5': case '6': case '7':
  1372. code = 0;
  1373. count = 0;
  1374. while ((c <= '7') && (c >= '0') && (count++ < 3))
  1375. {
  1376. code = (code * 8) + (c - '0');
  1377. c = getc (finput);
  1378. }
  1379. ungetc (c, finput);
  1380. return code;
  1381. case '\\': case '\'': case '"':
  1382. return c;
  1383. case '\n':
  1384. lineno++;
  1385. return -1;
  1386. case 'n':
  1387. return TARGET_NEWLINE;
  1388. case 't':
  1389. return TARGET_TAB;
  1390. case 'r':
  1391. return TARGET_CR;
  1392. case 'f':
  1393. return TARGET_FF;
  1394. case 'b':
  1395. return TARGET_BS;
  1396. case 'a':
  1397. return TARGET_BELL;
  1398. case 'v':
  1399. return TARGET_VT;
  1400. case 'E':
  1401. return 033;
  1402. case '?':
  1403. return c;
  1404. }
  1405. if (c >= 040 && c <= 0177)
  1406. warning ("unknown escape sequence `\\%c'", c);
  1407. else
  1408. warning ("unknown escape sequence: `\\' followed by char code 0x%x", c);
  1409. return c;
  1410. }
  1411. void
  1412. yyerror ()
  1413. {
  1414. /* We can't print string and character constants well
  1415. because the token_buffer contains the result of processing escapes. */
  1416. if (token_buffer[0] == 0)
  1417. error ("parse error at end of input");
  1418. else if (token_buffer[0] == '"')
  1419. error ("parse error at string constant");
  1420. else if (token_buffer[0] == '\'')
  1421. error ("parse error at character constant");
  1422. else
  1423. error ("parse error at `%s'", token_buffer);
  1424. }
  1425. static int
  1426. yylex ()
  1427. {
  1428. register int c;
  1429. register char *p;
  1430. register int value;
  1431. int wide_flag = 0;
  1432. token_buffer[0] = c = skip_white_space ();
  1433. token_buffer[1] = 0;
  1434. yylloc.first_line = lineno;
  1435. switch (c)
  1436. {
  1437. case EOF:
  1438. token_buffer[0] = 0;
  1439. value = ENDFILE;
  1440. break;
  1441. case 'L':
  1442. /* Capital L may start a wide-string or wide-character constant. */
  1443. {
  1444. register int c = getc (finput);
  1445. if (c == '\'')
  1446. {
  1447. wide_flag = 1;
  1448. goto char_constant;
  1449. }
  1450. if (c == '"')
  1451. {
  1452. wide_flag = 1;
  1453. goto string_constant;
  1454. }
  1455. ungetc (c, finput);
  1456. }
  1457. case 'A': case 'B': case 'C': case 'D': case 'E':
  1458. case 'F': case 'G': case 'H': case 'I': case 'J':
  1459. case 'K': case 'M': case 'N': case 'O':
  1460. case 'P': case 'Q': case 'R': case 'S': case 'T':
  1461. case 'U': case 'V': case 'W': case 'X': case 'Y':
  1462. case 'Z':
  1463. case 'a': case 'b': case 'c': case 'd': case 'e':
  1464. case 'f': case 'g': case 'h': case 'i': case 'j':
  1465. case 'k': case 'l': case 'm': case 'n': case 'o':
  1466. case 'p': case 'q': case 'r': case 's': case 't':
  1467. case 'u': case 'v': case 'w': case 'x': case 'y':
  1468. case 'z':
  1469. case '_':
  1470. case '$':
  1471. p = token_buffer;
  1472. while (isalnum (c) || c == '_' || c == '$')
  1473. {
  1474. if (p >= token_buffer + maxtoken)
  1475. p = extend_token_buffer (p);
  1476. if (c == '$')
  1477. {
  1478. if (pedantic)
  1479. {
  1480. if (! dollar_seen)
  1481. warning ("ANSI C forbids `$' (first use here)");
  1482. dollar_seen = 1;
  1483. }
  1484. }
  1485. *p++ = c;
  1486. c = getc (finput);
  1487. }
  1488. *p = 0;
  1489. ungetc (c, finput);
  1490. value = IDENTIFIER;
  1491. yylval.itype = 0;
  1492. /* Try to recognize a keyword. */
  1493. if (p - token_buffer <= MAXRESERVED)
  1494. {
  1495. register int lim = frw [p - token_buffer + 1];
  1496. register int i = frw[p - token_buffer];
  1497. register struct resword *p = &reswords[i];
  1498. for (; i < lim; i++, p++)
  1499. if (p->name[0] == token_buffer[0]
  1500. && !strcmp (p->name, token_buffer))
  1501. {
  1502. if (p->rid)
  1503. yylval.ttype = ridpointers[(int) p->rid];
  1504. if ((! flag_no_asm
  1505. || ((int) p->token != ASM
  1506. && (int) p->token != TYPEOF
  1507. && strcmp (p->name, "inline")))
  1508. /* -ftraditional means don't recognize
  1509. typeof, const, volatile, noalias, signed or inline. */
  1510. && (! flag_traditional
  1511. || ((int) p->token != TYPE_QUAL
  1512. && (int) p->token != TYPEOF
  1513. && strcmp (p->name, "signed")
  1514. && strcmp (p->name, "inline"))))
  1515. value = (int) p->token;
  1516. break;
  1517. }
  1518. }
  1519. /* If we did not find a keyword, look for an identifier
  1520. (or a typename). */
  1521. if (value == IDENTIFIER)
  1522. {
  1523. yylval.ttype = get_identifier (token_buffer);
  1524. lastiddecl = lookup_name (yylval.ttype);
  1525. if (lastiddecl != 0 && TREE_CODE (lastiddecl) == TYPE_DECL)
  1526. value = TYPENAME;
  1527. }
  1528. break;
  1529. case '0': case '1': case '2': case '3': case '4':
  1530. case '5': case '6': case '7': case '8': case '9':
  1531. case '.':
  1532. {
  1533. int base = 10;
  1534. int count = 0;
  1535. int largest_digit = 0;
  1536. int numdigits = 0;
  1537. /* for multi-precision arithmetic,
  1538. we store only 8 live bits in each short,
  1539. giving us 64 bits of reliable precision */
  1540. short shorts[8];
  1541. int floatflag = 0; /* Set 1 if we learn this is a floating constant */
  1542. for (count = 0; count < 8; count++)
  1543. shorts[count] = 0;
  1544. p = token_buffer;
  1545. *p++ = c;
  1546. if (c == '0')
  1547. {
  1548. *p++ = (c = getc (finput));
  1549. if ((c == 'x') || (c == 'X'))
  1550. {
  1551. base = 16;
  1552. *p++ = (c = getc (finput));
  1553. }
  1554. else
  1555. {
  1556. base = 8;
  1557. numdigits++;
  1558. }
  1559. }
  1560. /* Read all the digits-and-decimal-points. */
  1561. while (c == '.'
  1562. || (isalnum (c) && (c != 'l') && (c != 'L')
  1563. && (c != 'u') && (c != 'U')
  1564. && (!floatflag || ((c != 'f') && (c != 'F')))))
  1565. {
  1566. if (c == '.')
  1567. {
  1568. if (base == 16)
  1569. error ("floating constant may not be in radix 16");
  1570. floatflag = 1;
  1571. base = 10;
  1572. *p++ = c = getc (finput);
  1573. /* Accept '.' as the start of a floating-point number
  1574. only when it is followed by a digit.
  1575. Otherwise, unread the following non-digit
  1576. and use the '.' as a structural token. */
  1577. if (p == token_buffer + 2 && !isdigit (c))
  1578. {
  1579. if (c == '.')
  1580. {
  1581. c = getc (finput);
  1582. if (c == '.')
  1583. {
  1584. *p++ = c;
  1585. *p = 0;
  1586. return ELLIPSIS;
  1587. }
  1588. error ("parse error at `..'");
  1589. }
  1590. ungetc (c, finput);
  1591. token_buffer[1] = 0;
  1592. value = '.';
  1593. goto done;
  1594. }
  1595. }
  1596. else
  1597. {
  1598. /* It is not a decimal point.
  1599. It should be a digit (perhaps a hex digit). */
  1600. if (isdigit (c))
  1601. {
  1602. c = c - '0';
  1603. }
  1604. else if (base <= 10)
  1605. {
  1606. if ((c&~040) == 'E')
  1607. {
  1608. base = 10;
  1609. floatflag = 1;
  1610. break; /* start of exponent */
  1611. }
  1612. error ("nondigits in number and not hexadecimal");
  1613. c = 0;
  1614. }
  1615. else if (c >= 'a')
  1616. {
  1617. c = c - 'a' + 10;
  1618. }
  1619. else
  1620. {
  1621. c = c - 'A' + 10;
  1622. }
  1623. if (c >= largest_digit)
  1624. largest_digit = c;
  1625. numdigits++;
  1626. for (count = 0; count < 8; count++)
  1627. {
  1628. (shorts[count] *= base);
  1629. if (count)
  1630. {
  1631. shorts[count] += (shorts[count-1] >> 8);
  1632. shorts[count-1] &= (1<<8)-1;
  1633. }
  1634. else shorts[0] += c;
  1635. }
  1636. if (p >= token_buffer + maxtoken - 3)
  1637. p = extend_token_buffer (p);
  1638. *p++ = (c = getc (finput));
  1639. }
  1640. }
  1641. if (numdigits == 0)
  1642. error ("numeric constant with no digits");
  1643. if (largest_digit >= base)
  1644. error ("numeric constant contains digits beyond the radix");
  1645. /* Remove terminating char from the token buffer and delimit the string */
  1646. *--p = 0;
  1647. if (floatflag)
  1648. {
  1649. tree type = double_type_node;
  1650. char f_seen = 0;
  1651. char l_seen = 0;
  1652. /* Read explicit exponent if any, and put it in tokenbuf. */
  1653. if ((c == 'e') || (c == 'E'))
  1654. {
  1655. if (p >= token_buffer + maxtoken - 3)
  1656. p = extend_token_buffer (p);
  1657. *p++ = c;
  1658. c = getc (finput);
  1659. if ((c == '+') || (c == '-'))
  1660. {
  1661. *p++ = c;
  1662. c = getc (finput);
  1663. }
  1664. if (! isdigit (c))
  1665. error ("floating constant exponent has no digits");
  1666. while (isdigit (c))
  1667. {
  1668. if (p >= token_buffer + maxtoken - 3)
  1669. p = extend_token_buffer (p);
  1670. *p++ = c;
  1671. c = getc (finput);
  1672. }
  1673. }
  1674. *p = 0;
  1675. yylval.ttype = build_real (atof (token_buffer));
  1676. while (1)
  1677. {
  1678. if (c == 'f' || c == 'F')
  1679. {
  1680. if (f_seen)
  1681. error ("two `f's in floating constant");
  1682. f_seen = 1;
  1683. type = float_type_node;
  1684. }
  1685. else if (c == 'l' || c == 'L')
  1686. {
  1687. if (l_seen)
  1688. error ("two `l's in floating constant");
  1689. l_seen = 1;
  1690. type = long_double_type_node;
  1691. }
  1692. else
  1693. {
  1694. if (isalnum (c))
  1695. {
  1696. error ("garbage at end of number");
  1697. while (isalnum (c))
  1698. {
  1699. if (p >= token_buffer + maxtoken - 3)
  1700. p = extend_token_buffer (p);
  1701. *p++ = c;
  1702. c = getc (finput);
  1703. }
  1704. }
  1705. break;
  1706. }
  1707. if (p >= token_buffer + maxtoken - 3)
  1708. p = extend_token_buffer (p);
  1709. *p++ = c;
  1710. c = getc (finput);
  1711. }
  1712. ungetc (c, finput);
  1713. *p = 0;
  1714. TREE_TYPE (yylval.ttype) = type;
  1715. }
  1716. else
  1717. {
  1718. tree type;
  1719. int spec_unsigned = 0;
  1720. int spec_long = 0;
  1721. while (1)
  1722. {
  1723. if (c == 'u' || c == 'U')
  1724. {
  1725. if (spec_unsigned)
  1726. error ("two `u's in integer constant");
  1727. spec_unsigned = 1;
  1728. }
  1729. else if (c == 'l' || c == 'L')
  1730. {
  1731. if (spec_long)
  1732. error ("two `l's in integer constant");
  1733. spec_long = 1;
  1734. }
  1735. else
  1736. {
  1737. if (isalnum (c))
  1738. {
  1739. error ("garbage at end of number");
  1740. while (isalnum (c))
  1741. {
  1742. if (p >= token_buffer + maxtoken - 3)
  1743. p = extend_token_buffer (p);
  1744. *p++ = c;
  1745. c = getc (finput);
  1746. }
  1747. }
  1748. break;
  1749. }
  1750. if (p >= token_buffer + maxtoken - 3)
  1751. p = extend_token_buffer (p);
  1752. *p++ = c;
  1753. c = getc (finput);
  1754. }
  1755. ungetc (c, finput);
  1756. if (shorts[7] | shorts[6] | shorts[5] | shorts[4])
  1757. warning ("integer constant out of range");
  1758. /* This is simplified by the fact that our constant
  1759. is always positive. */
  1760. yylval.ttype
  1761. = build_int_2 ((shorts[3]<<24) + (shorts[2]<<16) + (shorts[1]<<8) + shorts[0],
  1762. 0);
  1763. if (!spec_long && !spec_unsigned
  1764. && int_fits_type_p (yylval.ttype, integer_type_node))
  1765. type = integer_type_node;
  1766. else if (!spec_long && base != 10
  1767. && int_fits_type_p (yylval.ttype, unsigned_type_node))
  1768. type = unsigned_type_node;
  1769. else if (!spec_unsigned
  1770. && int_fits_type_p (yylval.ttype, long_integer_type_node))
  1771. type = long_integer_type_node;
  1772. else
  1773. {
  1774. type = long_unsigned_type_node;
  1775. if (! int_fits_type_p (yylval.ttype, long_unsigned_type_node))
  1776. warning ("integer constant out of range");
  1777. }
  1778. TREE_TYPE (yylval.ttype) = type;
  1779. }
  1780. value = CONSTANT; break;
  1781. }
  1782. case '\'':
  1783. char_constant:
  1784. c = getc (finput);
  1785. {
  1786. register int code = 0;
  1787. tryagain:
  1788. if (c == '\\')
  1789. {
  1790. c = readescape ();
  1791. if (c < 0)
  1792. goto tryagain;
  1793. }
  1794. else if (c == '\n')
  1795. {
  1796. if (pedantic)
  1797. warning ("ANSI C forbids newline in character constant");
  1798. lineno++;
  1799. }
  1800. code = c;
  1801. token_buffer[1] = c;
  1802. token_buffer[2] = '\'';
  1803. token_buffer[3] = 0;
  1804. c = getc (finput);
  1805. if (c != '\'')
  1806. error ("malformatted character constant");
  1807. /* If char type is signed, sign-extend the constant. */
  1808. if (TREE_UNSIGNED (char_type_node)
  1809. || ((code >> (BITS_PER_UNIT - 1)) & 1) == 0)
  1810. yylval.ttype = build_int_2 (code & ((1 << BITS_PER_UNIT) - 1), 0);
  1811. else
  1812. yylval.ttype = build_int_2 (code | ((-1) << BITS_PER_UNIT), -1);
  1813. TREE_TYPE (yylval.ttype) = integer_type_node;
  1814. value = CONSTANT; break;
  1815. }
  1816. case '"':
  1817. string_constant:
  1818. {
  1819. c = getc (finput);
  1820. p = token_buffer + 1;
  1821. while (c != '"')
  1822. {
  1823. if (c == '\\')
  1824. {
  1825. c = readescape ();
  1826. if (c < 0)
  1827. goto skipnewline;
  1828. }
  1829. else if (c == '\n')
  1830. {
  1831. if (pedantic)
  1832. warning ("ANSI C forbids newline in string constant");
  1833. lineno++;
  1834. }
  1835. if (p == token_buffer + maxtoken)
  1836. p = extend_token_buffer (p);
  1837. *p++ = c;
  1838. skipnewline:
  1839. c = getc (finput);
  1840. }
  1841. *p = 0;
  1842. if (wide_flag)
  1843. {
  1844. /* If this is a L"..." wide-string, convert each char
  1845. to an int, making a vector of ints. */
  1846. int *widebuf = (int *) alloca (p - token_buffer);
  1847. char *p1 = token_buffer + 1;
  1848. for (; p1 == p; p1++)
  1849. widebuf[p1 - token_buffer - 1] = *p1;
  1850. yylval.ttype = build_string ((p - token_buffer) * sizeof (int),
  1851. widebuf);
  1852. TREE_TYPE (yylval.ttype) = int_array_type_node;
  1853. }
  1854. else
  1855. {
  1856. yylval.ttype = build_string (p - token_buffer, token_buffer + 1);
  1857. TREE_TYPE (yylval.ttype) = char_array_type_node;
  1858. }
  1859. *p++ = '"';
  1860. *p = 0;
  1861. value = STRING; break;
  1862. }
  1863. case '+':
  1864. case '-':
  1865. case '&':
  1866. case '|':
  1867. case '<':
  1868. case '>':
  1869. case '*':
  1870. case '/':
  1871. case '%':
  1872. case '^':
  1873. case '!':
  1874. case '=':
  1875. {
  1876. register int c1;
  1877. combine:
  1878. switch (c)
  1879. {
  1880. case '+':
  1881. yylval.code = PLUS_EXPR; break;
  1882. case '-':
  1883. yylval.code = MINUS_EXPR; break;
  1884. case '&':
  1885. yylval.code = BIT_AND_EXPR; break;
  1886. case '|':
  1887. yylval.code = BIT_IOR_EXPR; break;
  1888. case '*':
  1889. yylval.code = MULT_EXPR; break;
  1890. case '/':
  1891. yylval.code = TRUNC_DIV_EXPR; break;
  1892. case '%':
  1893. yylval.code = TRUNC_MOD_EXPR; break;
  1894. case '^':
  1895. yylval.code = BIT_XOR_EXPR; break;
  1896. case LSHIFT:
  1897. yylval.code = LSHIFT_EXPR; break;
  1898. case RSHIFT:
  1899. yylval.code = RSHIFT_EXPR; break;
  1900. case '<':
  1901. yylval.code = LT_EXPR; break;
  1902. case '>':
  1903. yylval.code = GT_EXPR; break;
  1904. }
  1905. token_buffer[1] = c1 = getc (finput);
  1906. token_buffer[2] = 0;
  1907. if (c1 == '=')
  1908. {
  1909. switch (c)
  1910. {
  1911. case '<':
  1912. value = ARITHCOMPARE; yylval.code = LE_EXPR; goto done;
  1913. case '>':
  1914. value = ARITHCOMPARE; yylval.code = GE_EXPR; goto done;
  1915. case '!':
  1916. value = EQCOMPARE; yylval.code = NE_EXPR; goto done;
  1917. case '=':
  1918. value = EQCOMPARE; yylval.code = EQ_EXPR; goto done;
  1919. }
  1920. value = ASSIGN; goto done;
  1921. }
  1922. else if (c == c1)
  1923. switch (c)
  1924. {
  1925. case '+':
  1926. value = PLUSPLUS; goto done;
  1927. case '-':
  1928. value = MINUSMINUS; goto done;
  1929. case '&':
  1930. value = ANDAND; goto done;
  1931. case '|':
  1932. value = OROR; goto done;
  1933. case '<':
  1934. c = LSHIFT;
  1935. goto combine;
  1936. case '>':
  1937. c = RSHIFT;
  1938. goto combine;
  1939. }
  1940. else if ((c == '-') && (c1 == '>'))
  1941. { value = POINTSAT; goto done; }
  1942. ungetc (c1, finput);
  1943. token_buffer[1] = 0;
  1944. if ((c == '<') || (c == '>'))
  1945. value = ARITHCOMPARE;
  1946. else value = c;
  1947. goto done;
  1948. }
  1949. default:
  1950. value = c;
  1951. }
  1952. done:
  1953. yylloc.last_line = lineno;
  1954. return value;
  1955. }