PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/sdcc/src/SDCC.y

#
Happy | 1943 lines | 1775 code | 168 blank | 0 comment | 0 complexity | c4e9c6b59b24ed9bbb32b93e2567b379 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. /*-----------------------------------------------------------------------
  2. SDCC.y - parser definition file for sdcc :
  3. Written By : Sandeep Dutta . sandeep.dutta@usa.net (1997)
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. In other words, you are welcome to use, share and improve this program.
  16. You are forbidden to forbid anyone else to use, share and improve
  17. what you give them. Help stamp out software-hoarding!
  18. -------------------------------------------------------------------------*/
  19. %{
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #include <string.h>
  23. #include "SDCCglobl.h"
  24. #include "SDCCsymt.h"
  25. #include "SDCChasht.h"
  26. #include "SDCCval.h"
  27. #include "SDCCmem.h"
  28. #include "SDCCast.h"
  29. #include "port.h"
  30. #include "newalloc.h"
  31. #include "SDCCerr.h"
  32. #include "SDCCutil.h"
  33. extern int yyerror (char *);
  34. extern FILE *yyin;
  35. int NestLevel = 0; /* current NestLevel */
  36. int stackPtr = 1; /* stack pointer */
  37. int xstackPtr = 0; /* xstack pointer */
  38. int reentrant = 0;
  39. int blockNo = 0; /* sequential block number */
  40. int currBlockno=0;
  41. int inCritical= 0;
  42. int seqPointNo= 1; /* sequence point number */
  43. int ignoreTypedefType=0;
  44. extern int yylex();
  45. int yyparse(void);
  46. extern int noLineno;
  47. char lbuff[1024]; /* local buffer */
  48. /* break & continue stacks */
  49. STACK_DCL(continueStack ,symbol *,MAX_NEST_LEVEL)
  50. STACK_DCL(breakStack ,symbol *,MAX_NEST_LEVEL)
  51. STACK_DCL(forStack ,symbol *,MAX_NEST_LEVEL)
  52. STACK_DCL(swStk ,ast *,MAX_NEST_LEVEL)
  53. STACK_DCL(blockNum,int,MAX_NEST_LEVEL*3)
  54. value *cenum = NULL; /* current enumeration type chain*/
  55. bool uselessDecl = TRUE;
  56. #define YYDEBUG 1
  57. %}
  58. %expect 6
  59. %union {
  60. symbol *sym; /* symbol table pointer */
  61. structdef *sdef; /* structure definition */
  62. char yychar[SDCC_NAME_MAX+1];
  63. sym_link *lnk; /* declarator or specifier */
  64. int yyint; /* integer value returned */
  65. value *val; /* for integer constant */
  66. initList *ilist; /* initial list */
  67. designation*dsgn; /* designator */
  68. const char *yystr; /* pointer to dynamicaly allocated string */
  69. ast *asts; /* expression tree */
  70. }
  71. %token <yychar> IDENTIFIER TYPE_NAME ADDRSPACE_NAME
  72. %token <val> CONSTANT
  73. %token SIZEOF TYPEOF OFFSETOF
  74. %token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
  75. %token AND_OP OR_OP
  76. %token <yyint> MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
  77. %token <yyint> SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
  78. %token <yyint> XOR_ASSIGN OR_ASSIGN
  79. %token TYPEDEF EXTERN STATIC AUTO REGISTER CODE EEPROM INTERRUPT SFR SFR16 SFR32 ADDRESSMOD
  80. %token AT SBIT REENTRANT USING XDATA DATA IDATA PDATA VAR_ARGS CRITICAL
  81. %token NONBANKED BANKED SHADOWREGS SD_WPARAM
  82. %token SD_BOOL SD_CHAR SD_SHORT SD_INT SD_LONG SIGNED UNSIGNED SD_FLOAT DOUBLE FIXED16X16 SD_CONST VOLATILE SD_VOID BIT
  83. %token STRUCT UNION ENUM RANGE SD_FAR
  84. %token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
  85. %token NAKED JAVANATIVE OVERLAY
  86. %token <yystr> STRING_LITERAL INLINEASM
  87. %token IFX ADDRESS_OF GET_VALUE_AT_ADDRESS SPIL UNSPIL GETHBIT GETABIT GETBYTE GETWORD
  88. %token BITWISEAND UNARYMINUS IPUSH IPOP PCALL ENDFUNCTION JUMPTABLE
  89. %token RRC RLC
  90. %token CAST CALL PARAM NULLOP BLOCK LABEL RECEIVE SEND ARRAYINIT
  91. %token DUMMY_READ_VOLATILE ENDCRITICAL SWAP INLINE NORETURN RESTRICT SMALLC
  92. %token ASM
  93. %type <yyint> Interrupt_storage
  94. %type <sym> identifier declarator declarator2 declarator3 enumerator_list enumerator
  95. %type <sym> struct_declarator function_declarator function_declarator2
  96. %type <sym> struct_declarator_list struct_declaration struct_declaration_list
  97. %type <sym> declaration init_declarator_list init_declarator
  98. %type <sym> declaration_list identifier_list
  99. %type <sym> declarator2_function_attributes while do for critical
  100. %type <sym> addressmod
  101. %type <lnk> pointer type_specifier_list type_specifier_list_ type_specifier type_name
  102. %type <lnk> storage_class_specifier struct_or_union_specifier function_specifier
  103. %type <lnk> declaration_specifiers declaration_specifiers_ sfr_reg_bit sfr_attributes
  104. %type <lnk> function_attribute function_attributes enum_specifier
  105. %type <lnk> abstract_declarator abstract_declarator2 unqualified_pointer
  106. %type <val> parameter_type_list parameter_list parameter_declaration opt_assign_expr
  107. %type <sdef> stag opt_stag
  108. %type <asts> primary_expr
  109. %type <asts> postfix_expr unary_expr offsetof_member_designator cast_expr multiplicative_expr
  110. %type <asts> additive_expr shift_expr relational_expr equality_expr
  111. %type <asts> and_expr exclusive_or_expr inclusive_or_expr logical_or_expr
  112. %type <asts> logical_and_expr conditional_expr assignment_expr constant_expr
  113. %type <asts> expr argument_expr_list function_definition expr_opt
  114. %type <asts> statement_list statement labeled_statement compound_statement
  115. %type <asts> expression_statement selection_statement iteration_statement
  116. %type <asts> jump_statement function_body else_statement string_literal_val
  117. %type <asts> critical_statement asm_statement
  118. %type <dsgn> designator designator_list designation designation_opt
  119. %type <ilist> initializer initializer_list
  120. %type <yyint> unary_operator assignment_operator struct_or_union
  121. %type <yystr> asm_string_literal
  122. %start file
  123. %%
  124. file
  125. : /* empty */
  126. {
  127. if (!options.lessPedantic)
  128. werror(W_EMPTY_SOURCE_FILE);
  129. }
  130. | program
  131. ;
  132. program
  133. : external_definition
  134. | program external_definition
  135. ;
  136. external_definition
  137. : function_definition
  138. {
  139. // blockNo = 0;
  140. }
  141. | declaration
  142. {
  143. ignoreTypedefType = 0;
  144. if ($1 && $1->type && IS_FUNC($1->type))
  145. {
  146. /* The only legal storage classes for
  147. * a function prototype (declaration)
  148. * are extern and static. extern is the
  149. * default. Thus, if this function isn't
  150. * explicitly marked static, mark it
  151. * extern.
  152. */
  153. if ($1->etype && IS_SPEC($1->etype) && !SPEC_STAT($1->etype))
  154. {
  155. SPEC_EXTR($1->etype) = 1;
  156. }
  157. }
  158. addSymChain (&$1);
  159. allocVariables ($1);
  160. cleanUpLevel (SymbolTab, 1);
  161. }
  162. | addressmod
  163. ;
  164. function_definition
  165. : function_declarator function_body { /* function type not specified */
  166. /* assume it to be 'int' */
  167. addDecl($1,0,newIntLink());
  168. $$ = createFunction($1,$2);
  169. }
  170. | declaration_specifiers function_declarator function_body
  171. {
  172. pointerTypes($2->type,copyLinkChain($1));
  173. addDecl($2,0,$1);
  174. $$ = createFunction($2,$3);
  175. }
  176. ;
  177. function_attribute
  178. : function_attributes
  179. | function_attributes function_attribute { $$ = mergeSpec($1,$2,"function_attribute"); }
  180. ;
  181. function_attributes
  182. : USING constant_expr {
  183. $$ = newLink(SPECIFIER);
  184. FUNC_REGBANK($$) = (int) ulFromVal(constExprValue($2,TRUE));
  185. }
  186. | REENTRANT { $$ = newLink (SPECIFIER);
  187. FUNC_ISREENT($$)=1;
  188. }
  189. | CRITICAL { $$ = newLink (SPECIFIER);
  190. FUNC_ISCRITICAL($$) = 1;
  191. }
  192. | NAKED { $$ = newLink (SPECIFIER);
  193. FUNC_ISNAKED($$)=1;
  194. }
  195. | JAVANATIVE { $$ = newLink (SPECIFIER);
  196. FUNC_ISJAVANATIVE($$)=1;
  197. }
  198. | OVERLAY { $$ = newLink (SPECIFIER);
  199. FUNC_ISOVERLAY($$)=1;
  200. }
  201. | NONBANKED {$$ = newLink (SPECIFIER);
  202. FUNC_NONBANKED($$) = 1;
  203. if (FUNC_BANKED($$)) {
  204. werror(W_BANKED_WITH_NONBANKED);
  205. }
  206. }
  207. | SHADOWREGS {$$ = newLink (SPECIFIER);
  208. FUNC_ISSHADOWREGS($$) = 1;
  209. }
  210. | SD_WPARAM {$$ = newLink (SPECIFIER);
  211. FUNC_ISWPARAM($$) = 1;
  212. }
  213. | BANKED {$$ = newLink (SPECIFIER);
  214. FUNC_BANKED($$) = 1;
  215. if (FUNC_NONBANKED($$)) {
  216. werror(W_BANKED_WITH_NONBANKED);
  217. }
  218. }
  219. | Interrupt_storage
  220. {
  221. $$ = newLink (SPECIFIER);
  222. FUNC_INTNO($$) = $1;
  223. FUNC_ISISR($$) = 1;
  224. }
  225. | SMALLC { $$ = newLink (SPECIFIER);
  226. FUNC_ISSMALLC($$)=1;
  227. }
  228. ;
  229. function_body
  230. : compound_statement
  231. | declaration_list compound_statement
  232. {
  233. werror (E_OLD_STYLE, ($1 ? $1->name: ""));
  234. exit (1);
  235. }
  236. ;
  237. offsetof_member_designator
  238. : identifier { $$ = newAst_VALUE (symbolVal ($1)); }
  239. | offsetof_member_designator '.' { ignoreTypedefType = 1; } identifier
  240. {
  241. ignoreTypedefType = 0;
  242. $4 = newSymbol ($4->name, NestLevel);
  243. $4->implicit = 1;
  244. $$ = newNode ('.', $1, newAst_VALUE (symbolVal ($4)));
  245. }
  246. | offsetof_member_designator '[' expr ']'
  247. {
  248. $$ = newNode ('[', $1, $3);
  249. }
  250. ;
  251. primary_expr
  252. : identifier { $$ = newAst_VALUE (symbolVal ($1)); }
  253. | CONSTANT { $$ = newAst_VALUE ($1); }
  254. | string_literal_val
  255. | '(' expr ')' { $$ = $2; }
  256. ;
  257. string_literal_val
  258. : STRING_LITERAL { $$ = newAst_VALUE (strVal ($1)); }
  259. ;
  260. postfix_expr
  261. : primary_expr
  262. | postfix_expr '[' expr ']' { $$ = newNode ('[', $1, $3); }
  263. | postfix_expr '(' ')' { $$ = newNode (CALL,$1,NULL);
  264. $$->left->funcName = 1;}
  265. | postfix_expr '(' argument_expr_list ')'
  266. {
  267. $$ = newNode (CALL,$1,$3); $$->left->funcName = 1;
  268. }
  269. | postfix_expr '.' { ignoreTypedefType = 1; } identifier
  270. {
  271. ignoreTypedefType = 0;
  272. $4 = newSymbol($4->name,NestLevel);
  273. $4->implicit = 1;
  274. $$ = newNode(PTR_OP,newNode('&',$1,NULL),newAst_VALUE(symbolVal($4)));
  275. }
  276. | postfix_expr PTR_OP { ignoreTypedefType = 1; } identifier
  277. {
  278. ignoreTypedefType = 0;
  279. $4 = newSymbol($4->name,NestLevel);
  280. $4->implicit = 1;
  281. $$ = newNode(PTR_OP,$1,newAst_VALUE(symbolVal($4)));
  282. }
  283. | postfix_expr INC_OP
  284. { $$ = newNode(INC_OP,$1,NULL);}
  285. | postfix_expr DEC_OP
  286. { $$ = newNode(DEC_OP,$1,NULL); }
  287. ;
  288. argument_expr_list
  289. : assignment_expr
  290. | assignment_expr ',' argument_expr_list { $$ = newNode(PARAM,$1,$3); }
  291. ;
  292. unary_expr
  293. : postfix_expr
  294. | INC_OP unary_expr { $$ = newNode (INC_OP, NULL, $2); }
  295. | DEC_OP unary_expr { $$ = newNode (DEC_OP, NULL, $2); }
  296. | unary_operator cast_expr { $$ = newNode ($1, $2, NULL); }
  297. | SIZEOF unary_expr { $$ = newNode (SIZEOF, NULL, $2); }
  298. | SIZEOF '(' type_name ')' { $$ = newAst_VALUE (sizeofOp ($3)); }
  299. | TYPEOF unary_expr { $$ = newNode (TYPEOF, NULL, $2); }
  300. | OFFSETOF '(' type_name ',' offsetof_member_designator ')' { $$ = offsetofOp($3, $5); }
  301. ;
  302. unary_operator
  303. : '&' { $$ = '&';}
  304. | '*' { $$ = '*';}
  305. | '+' { $$ = '+';}
  306. | '-' { $$ = '-';}
  307. | '~' { $$ = '~';}
  308. | '!' { $$ = '!';}
  309. ;
  310. cast_expr
  311. : unary_expr
  312. | '(' type_name ')' cast_expr { $$ = newNode(CAST,newAst_LINK($2),$4); }
  313. ;
  314. multiplicative_expr
  315. : cast_expr
  316. | multiplicative_expr '*' cast_expr { $$ = newNode('*',$1,$3);}
  317. | multiplicative_expr '/' cast_expr { $$ = newNode('/',$1,$3);}
  318. | multiplicative_expr '%' cast_expr { $$ = newNode('%',$1,$3);}
  319. ;
  320. additive_expr
  321. : multiplicative_expr
  322. | additive_expr '+' multiplicative_expr { $$=newNode('+',$1,$3);}
  323. | additive_expr '-' multiplicative_expr { $$=newNode('-',$1,$3);}
  324. ;
  325. shift_expr
  326. : additive_expr
  327. | shift_expr LEFT_OP additive_expr { $$ = newNode(LEFT_OP,$1,$3); }
  328. | shift_expr RIGHT_OP additive_expr { $$ = newNode(RIGHT_OP,$1,$3); }
  329. ;
  330. relational_expr
  331. : shift_expr
  332. | relational_expr '<' shift_expr { $$ = newNode('<', $1,$3);}
  333. | relational_expr '>' shift_expr { $$ = newNode('>', $1,$3);}
  334. | relational_expr LE_OP shift_expr { $$ = newNode(LE_OP,$1,$3);}
  335. | relational_expr GE_OP shift_expr { $$ = newNode(GE_OP,$1,$3);}
  336. ;
  337. equality_expr
  338. : relational_expr
  339. | equality_expr EQ_OP relational_expr { $$ = newNode(EQ_OP,$1,$3);}
  340. | equality_expr NE_OP relational_expr { $$ = newNode(NE_OP,$1,$3);}
  341. ;
  342. and_expr
  343. : equality_expr
  344. | and_expr '&' equality_expr { $$ = newNode('&',$1,$3);}
  345. ;
  346. exclusive_or_expr
  347. : and_expr
  348. | exclusive_or_expr '^' and_expr { $$ = newNode('^',$1,$3);}
  349. ;
  350. inclusive_or_expr
  351. : exclusive_or_expr
  352. | inclusive_or_expr '|' exclusive_or_expr { $$ = newNode('|',$1,$3);}
  353. ;
  354. logical_and_expr
  355. : inclusive_or_expr
  356. | logical_and_expr AND_OP { seqPointNo++;} inclusive_or_expr
  357. { $$ = newNode(AND_OP,$1,$4);}
  358. ;
  359. logical_or_expr
  360. : logical_and_expr
  361. | logical_or_expr OR_OP { seqPointNo++;} logical_and_expr
  362. { $$ = newNode(OR_OP,$1,$4); }
  363. ;
  364. conditional_expr
  365. : logical_or_expr
  366. | logical_or_expr '?' { seqPointNo++;} logical_or_expr ':' conditional_expr
  367. {
  368. $$ = newNode(':',$4,$6);
  369. $$ = newNode('?',$1,$$);
  370. }
  371. ;
  372. assignment_expr
  373. : conditional_expr
  374. | cast_expr assignment_operator assignment_expr
  375. {
  376. switch ($2) {
  377. case '=':
  378. $$ = newNode($2,$1,$3);
  379. break;
  380. case MUL_ASSIGN:
  381. $$ = createRMW($1, '*', $3);
  382. break;
  383. case DIV_ASSIGN:
  384. $$ = createRMW($1, '/', $3);
  385. break;
  386. case MOD_ASSIGN:
  387. $$ = createRMW($1, '%', $3);
  388. break;
  389. case ADD_ASSIGN:
  390. $$ = createRMW($1, '+', $3);
  391. break;
  392. case SUB_ASSIGN:
  393. $$ = createRMW($1, '-', $3);
  394. break;
  395. case LEFT_ASSIGN:
  396. $$ = createRMW($1, LEFT_OP, $3);
  397. break;
  398. case RIGHT_ASSIGN:
  399. $$ = createRMW($1, RIGHT_OP, $3);
  400. break;
  401. case AND_ASSIGN:
  402. $$ = createRMW($1, '&', $3);
  403. break;
  404. case XOR_ASSIGN:
  405. $$ = createRMW($1, '^', $3);
  406. break;
  407. case OR_ASSIGN:
  408. $$ = createRMW($1, '|', $3);
  409. break;
  410. default :
  411. $$ = NULL;
  412. }
  413. }
  414. ;
  415. assignment_operator
  416. : '=' { $$ = '=';}
  417. | MUL_ASSIGN
  418. | DIV_ASSIGN
  419. | MOD_ASSIGN
  420. | ADD_ASSIGN
  421. | SUB_ASSIGN
  422. | LEFT_ASSIGN
  423. | RIGHT_ASSIGN
  424. | AND_ASSIGN
  425. | XOR_ASSIGN
  426. | OR_ASSIGN
  427. ;
  428. expr
  429. : assignment_expr
  430. | expr ',' { seqPointNo++;} assignment_expr { $$ = newNode(',',$1,$4);}
  431. ;
  432. constant_expr
  433. : conditional_expr
  434. ;
  435. declaration
  436. : declaration_specifiers ';'
  437. {
  438. /* Special case: if incomplete struct/union declared without name, */
  439. /* make sure an incomplete type for it exists in the current scope */
  440. if (IS_STRUCT($1))
  441. {
  442. structdef *sdef = SPEC_STRUCT($1);
  443. structdef *osdef;
  444. osdef = findSymWithBlock (StructTab, sdef->tagsym, currBlockno);
  445. if (osdef && osdef->block != currBlockno)
  446. {
  447. sdef = newStruct(osdef->tagsym->name);
  448. sdef->level = NestLevel;
  449. sdef->block = currBlockno;
  450. sdef->tagsym = newSymbol (osdef->tagsym->name, NestLevel);
  451. addSym (StructTab, sdef, sdef->tag, sdef->level, currBlockno, 0);
  452. uselessDecl = FALSE;
  453. }
  454. }
  455. if (uselessDecl)
  456. werror(W_USELESS_DECL);
  457. uselessDecl = TRUE;
  458. $$ = NULL;
  459. }
  460. | declaration_specifiers init_declarator_list ';'
  461. {
  462. /* add the specifier list to the id */
  463. symbol *sym , *sym1;
  464. for (sym1 = sym = reverseSyms($2);sym != NULL;sym = sym->next) {
  465. sym_link *lnk = copyLinkChain($1);
  466. /* do the pointer stuff */
  467. pointerTypes(sym->type,lnk);
  468. addDecl (sym,0,lnk);
  469. }
  470. uselessDecl = TRUE;
  471. $$ = sym1;
  472. }
  473. ;
  474. declaration_specifiers : declaration_specifiers_ { $$ = finalizeSpec($1); };
  475. declaration_specifiers_
  476. : storage_class_specifier { $$ = $1; }
  477. | storage_class_specifier declaration_specifiers_ {
  478. /* if the decl $2 is not a specifier */
  479. /* find the spec and replace it */
  480. $$ = mergeDeclSpec($1, $2, "storage_class_specifier declaration_specifiers - skipped");
  481. }
  482. | type_specifier { $$ = $1; }
  483. | type_specifier declaration_specifiers_ {
  484. /* if the decl $2 is not a specifier */
  485. /* find the spec and replace it */
  486. $$ = mergeDeclSpec($1, $2, "type_specifier declaration_specifiers - skipped");
  487. }
  488. | function_specifier { $$ = $1; }
  489. | function_specifier declaration_specifiers_ {
  490. /* if the decl $2 is not a specifier */
  491. /* find the spec and replace it */
  492. $$ = mergeDeclSpec($1, $2, "function_specifier declaration_specifiers - skipped");
  493. }
  494. ;
  495. init_declarator_list
  496. : init_declarator
  497. | init_declarator_list ',' init_declarator { $3->next = $1; $$ = $3;}
  498. ;
  499. init_declarator
  500. : declarator { $1->ival = NULL; }
  501. | declarator '=' initializer { $1->ival = $3; }
  502. ;
  503. designation_opt
  504. : { $$ = NULL; }
  505. | designation
  506. ;
  507. designation
  508. : designator_list '=' { $$ = revDesignation($1); }
  509. ;
  510. designator_list
  511. : designator
  512. | designator_list designator { $2->next = $1; $$ = $2; }
  513. ;
  514. designator
  515. : '[' constant_expr ']'
  516. {
  517. value *tval;
  518. int elemno;
  519. tval = constExprValue($2, TRUE);
  520. /* if it is not a constant then Error */
  521. if (!tval || (SPEC_SCLS(tval->etype) != S_LITERAL))
  522. {
  523. werror (E_CONST_EXPECTED);
  524. elemno = 0; /* arbitrary fixup */
  525. }
  526. else
  527. {
  528. if ((elemno = (int) ulFromVal(tval)) < 0)
  529. {
  530. werror (E_BAD_DESIGNATOR);
  531. elemno = 0; /* arbitrary fixup */
  532. }
  533. }
  534. $$ = newDesignation(DESIGNATOR_ARRAY, &elemno);
  535. }
  536. | '.' identifier { $$ = newDesignation(DESIGNATOR_STRUCT,$2); }
  537. ;
  538. storage_class_specifier
  539. : TYPEDEF {
  540. $$ = newLink (SPECIFIER);
  541. SPEC_TYPEDEF($$) = 1;
  542. }
  543. | EXTERN {
  544. $$ = newLink(SPECIFIER);
  545. SPEC_EXTR($$) = 1;
  546. }
  547. | STATIC {
  548. $$ = newLink (SPECIFIER);
  549. SPEC_STAT($$) = 1;
  550. }
  551. | AUTO {
  552. $$ = newLink (SPECIFIER);
  553. SPEC_SCLS($$) = S_AUTO;
  554. }
  555. | REGISTER {
  556. $$ = newLink (SPECIFIER);
  557. SPEC_SCLS($$) = S_REGISTER;
  558. }
  559. ;
  560. function_specifier
  561. : INLINE {
  562. $$ = newLink (SPECIFIER);
  563. SPEC_INLINE($$) = 1;
  564. }
  565. | NORETURN {
  566. $$ = newLink (SPECIFIER);
  567. SPEC_NORETURN($$) = 1;
  568. }
  569. ;
  570. Interrupt_storage
  571. : INTERRUPT { $$ = INTNO_UNSPEC; }
  572. | INTERRUPT constant_expr
  573. { int intno = (int) ulFromVal(constExprValue($2,TRUE));
  574. if ((intno >= 0) && (intno <= INTNO_MAX))
  575. $$ = intno;
  576. else
  577. {
  578. werror(E_INT_BAD_INTNO, intno);
  579. $$ = INTNO_UNSPEC;
  580. }
  581. }
  582. ;
  583. type_specifier
  584. : SD_BOOL {
  585. $$=newLink(SPECIFIER);
  586. SPEC_NOUN($$) = V_BOOL;
  587. ignoreTypedefType = 1;
  588. }
  589. | SD_CHAR {
  590. $$=newLink(SPECIFIER);
  591. SPEC_NOUN($$) = V_CHAR;
  592. ignoreTypedefType = 1;
  593. }
  594. | SD_SHORT {
  595. $$=newLink(SPECIFIER);
  596. SPEC_SHORT($$) = 1;
  597. ignoreTypedefType = 1;
  598. }
  599. | SD_INT {
  600. $$=newLink(SPECIFIER);
  601. SPEC_NOUN($$) = V_INT;
  602. ignoreTypedefType = 1;
  603. }
  604. | SD_LONG {
  605. $$=newLink(SPECIFIER);
  606. SPEC_LONG($$) = 1;
  607. ignoreTypedefType = 1;
  608. }
  609. | SIGNED {
  610. $$=newLink(SPECIFIER);
  611. $$->select.s.b_signed = 1;
  612. ignoreTypedefType = 1;
  613. }
  614. | UNSIGNED {
  615. $$=newLink(SPECIFIER);
  616. SPEC_USIGN($$) = 1;
  617. ignoreTypedefType = 1;
  618. }
  619. | SD_VOID {
  620. $$=newLink(SPECIFIER);
  621. SPEC_NOUN($$) = V_VOID;
  622. ignoreTypedefType = 1;
  623. }
  624. | SD_CONST {
  625. $$=newLink(SPECIFIER);
  626. SPEC_CONST($$) = 1;
  627. }
  628. | VOLATILE {
  629. $$=newLink(SPECIFIER);
  630. SPEC_VOLATILE($$) = 1;
  631. }
  632. | RESTRICT {
  633. $$=newLink(SPECIFIER);
  634. SPEC_RESTRICT($$) = 1;
  635. }
  636. | ADDRSPACE_NAME {
  637. $$=newLink(SPECIFIER);
  638. SPEC_ADDRSPACE($$) = findSym (AddrspaceTab, 0, $1);
  639. }
  640. | SD_FLOAT {
  641. $$=newLink(SPECIFIER);
  642. SPEC_NOUN($$) = V_FLOAT;
  643. ignoreTypedefType = 1;
  644. }
  645. | FIXED16X16 {
  646. $$=newLink(SPECIFIER);
  647. SPEC_NOUN($$) = V_FIXED16X16;
  648. ignoreTypedefType = 1;
  649. }
  650. | XDATA {
  651. $$ = newLink (SPECIFIER);
  652. SPEC_SCLS($$) = S_XDATA;
  653. }
  654. | CODE {
  655. $$ = newLink (SPECIFIER);
  656. SPEC_SCLS($$) = S_CODE;
  657. }
  658. | EEPROM {
  659. $$ = newLink (SPECIFIER);
  660. SPEC_SCLS($$) = S_EEPROM;
  661. }
  662. | DATA {
  663. $$ = newLink (SPECIFIER);
  664. SPEC_SCLS($$) = S_DATA;
  665. }
  666. | IDATA {
  667. $$ = newLink (SPECIFIER);
  668. SPEC_SCLS($$) = S_IDATA;
  669. }
  670. | PDATA {
  671. $$ = newLink (SPECIFIER);
  672. SPEC_SCLS($$) = S_PDATA;
  673. }
  674. | BIT {
  675. $$=newLink(SPECIFIER);
  676. SPEC_NOUN($$) = V_BIT;
  677. SPEC_SCLS($$) = S_BIT;
  678. SPEC_BLEN($$) = 1;
  679. SPEC_BSTR($$) = 0;
  680. ignoreTypedefType = 1;
  681. }
  682. | AT constant_expr {
  683. $$=newLink(SPECIFIER);
  684. /* add this to the storage class specifier */
  685. SPEC_ABSA($$) = 1; /* set the absolute addr flag */
  686. /* now get the abs addr from value */
  687. SPEC_ADDR($$) = (unsigned int) ulFromVal(constExprValue($2,TRUE));
  688. }
  689. | struct_or_union_specifier {
  690. uselessDecl = FALSE;
  691. $$ = $1;
  692. ignoreTypedefType = 1;
  693. }
  694. | enum_specifier {
  695. cenum = NULL;
  696. uselessDecl = FALSE;
  697. ignoreTypedefType = 1;
  698. $$ = $1;
  699. }
  700. | TYPE_NAME
  701. {
  702. symbol *sym;
  703. sym_link *p;
  704. sym = findSym(TypedefTab,NULL,$1);
  705. $$ = p = copyLinkChain(sym ? sym->type : NULL);
  706. SPEC_TYPEDEF(getSpec(p)) = 0;
  707. ignoreTypedefType = 1;
  708. }
  709. | sfr_reg_bit
  710. ;
  711. sfr_reg_bit
  712. : SBIT {
  713. $$ = newLink(SPECIFIER);
  714. SPEC_NOUN($$) = V_SBIT;
  715. SPEC_SCLS($$) = S_SBIT;
  716. SPEC_BLEN($$) = 1;
  717. SPEC_BSTR($$) = 0;
  718. ignoreTypedefType = 1;
  719. }
  720. | sfr_attributes
  721. ;
  722. sfr_attributes
  723. : SFR {
  724. $$ = newLink(SPECIFIER);
  725. FUNC_REGBANK($$) = 0;
  726. SPEC_NOUN($$) = V_CHAR;
  727. SPEC_SCLS($$) = S_SFR;
  728. SPEC_USIGN($$) = 1;
  729. ignoreTypedefType = 1;
  730. }
  731. | SFR BANKED {
  732. $$ = newLink(SPECIFIER);
  733. FUNC_REGBANK($$) = 1;
  734. SPEC_NOUN($$) = V_CHAR;
  735. SPEC_SCLS($$) = S_SFR;
  736. SPEC_USIGN($$) = 1;
  737. ignoreTypedefType = 1;
  738. }
  739. ;
  740. sfr_attributes
  741. : SFR16 {
  742. $$ = newLink(SPECIFIER);
  743. FUNC_REGBANK($$) = 0;
  744. SPEC_NOUN($$) = V_INT;
  745. SPEC_SCLS($$) = S_SFR;
  746. SPEC_USIGN($$) = 1;
  747. ignoreTypedefType = 1;
  748. }
  749. ;
  750. sfr_attributes
  751. : SFR32 {
  752. $$ = newLink(SPECIFIER);
  753. FUNC_REGBANK($$) = 0;
  754. SPEC_NOUN($$) = V_INT;
  755. SPEC_SCLS($$) = S_SFR;
  756. SPEC_LONG($$) = 1;
  757. SPEC_USIGN($$) = 1;
  758. ignoreTypedefType = 1;
  759. }
  760. ;
  761. struct_or_union_specifier
  762. : struct_or_union opt_stag
  763. {
  764. structdef *sdef;
  765. if (! $2->tagsym)
  766. {
  767. /* no tag given, so new struct def for current scope */
  768. addSym (StructTab, $2, $2->tag, $2->level, currBlockno, 0);
  769. }
  770. else
  771. {
  772. sdef = findSymWithBlock (StructTab, $2->tagsym, currBlockno);
  773. if (sdef)
  774. {
  775. /* Error if a complete type already defined in this scope */
  776. if (sdef->block == currBlockno)
  777. {
  778. if (sdef->fields)
  779. {
  780. werror(E_STRUCT_REDEF, $2->tag);
  781. werrorfl(sdef->tagsym->fileDef, sdef->tagsym->lineDef, E_PREVIOUS_DEF);
  782. }
  783. else
  784. {
  785. $2 = sdef; /* We are completing an incomplete type */
  786. }
  787. }
  788. else
  789. {
  790. /* There is an existing struct def in an outer scope. */
  791. /* Create new struct def for current scope */
  792. addSym (StructTab, $2, $2->tag, $2->level, currBlockno, 0);
  793. }
  794. }
  795. else
  796. {
  797. /* There is no existing struct def at all. */
  798. /* Create new struct def for current scope */
  799. addSym (StructTab, $2, $2->tag, $2->level, currBlockno, 0);
  800. }
  801. }
  802. if (!$2->type)
  803. {
  804. $2->type = $1;
  805. }
  806. else
  807. {
  808. if ($2->type != $1)
  809. werror(E_BAD_TAG, $2->tag, $1==STRUCT ? "struct" : "union");
  810. }
  811. }
  812. '{' struct_declaration_list '}'
  813. {
  814. structdef *sdef;
  815. symbol *sym, *dsym;
  816. // check for errors in structure members
  817. for (sym=$5; sym; sym=sym->next)
  818. {
  819. if (IS_ABSOLUTE(sym->etype))
  820. {
  821. werrorfl(sym->fileDef, sym->lineDef, E_NOT_ALLOWED, "'at'");
  822. SPEC_ABSA(sym->etype) = 0;
  823. }
  824. if (IS_SPEC(sym->etype) && SPEC_SCLS(sym->etype))
  825. {
  826. werrorfl(sym->fileDef, sym->lineDef, E_NOT_ALLOWED, "storage class");
  827. printTypeChainRaw (sym->type, NULL);
  828. SPEC_SCLS(sym->etype) = 0;
  829. }
  830. for (dsym=sym->next; dsym; dsym=dsym->next)
  831. {
  832. if (*dsym->name && strcmp(sym->name, dsym->name)==0)
  833. {
  834. werrorfl(sym->fileDef, sym->lineDef, E_DUPLICATE_MEMBER,
  835. $1==STRUCT ? "struct" : "union", sym->name);
  836. werrorfl(dsym->fileDef, dsym->lineDef, E_PREVIOUS_DEF);
  837. }
  838. }
  839. }
  840. /* Create a structdef */
  841. sdef = $2;
  842. sdef->fields = reverseSyms($5); /* link the fields */
  843. sdef->size = compStructSize($1, sdef); /* update size of */
  844. promoteAnonStructs ($1, sdef);
  845. /* Create the specifier */
  846. $$ = newLink (SPECIFIER);
  847. SPEC_NOUN($$) = V_STRUCT;
  848. SPEC_STRUCT($$)= sdef;
  849. }
  850. | struct_or_union stag
  851. {
  852. structdef *sdef;
  853. sdef = findSymWithBlock (StructTab, $2->tagsym, currBlockno);
  854. if (sdef)
  855. $2 = sdef;
  856. else
  857. {
  858. /* new struct def for current scope */
  859. addSym (StructTab, $2, $2->tag, $2->level, currBlockno, 0);
  860. }
  861. $$ = newLink(SPECIFIER);
  862. SPEC_NOUN($$) = V_STRUCT;
  863. SPEC_STRUCT($$) = $2;
  864. if (!$2->type)
  865. {
  866. $2->type = $1;
  867. }
  868. else
  869. {
  870. if ($2->type != $1)
  871. werror(E_BAD_TAG, $2->tag, $1==STRUCT ? "struct" : "union");
  872. }
  873. }
  874. ;
  875. struct_or_union
  876. : STRUCT { $$ = STRUCT; ignoreTypedefType = 1; }
  877. | UNION { $$ = UNION; ignoreTypedefType = 1; }
  878. ;
  879. opt_stag
  880. : stag
  881. | { /* synthesize a name add to structtable */
  882. ignoreTypedefType = 0;
  883. $$ = newStruct(genSymName(NestLevel));
  884. $$->level = NestLevel;
  885. $$->block = currBlockno;
  886. $$->tagsym = NULL;
  887. //addSym (StructTab, $$, $$->tag, $$->level, currBlockno, 0);
  888. }
  889. ;
  890. stag
  891. : identifier
  892. { /* add name to structure table */
  893. ignoreTypedefType = 0;
  894. $$ = newStruct($1->name);
  895. $$->level = NestLevel;
  896. $$->block = currBlockno;
  897. $$->tagsym = $1;
  898. //$$ = findSymWithBlock (StructTab, $1, currBlockno);
  899. //if (! $$ )
  900. // {
  901. // $$ = newStruct($1->name);
  902. // $$->level = NestLevel;
  903. // $$->tagsym = $1;
  904. // //addSym (StructTab, $$, $$->tag, $$->level, currBlockno, 0);
  905. // }
  906. }
  907. ;
  908. struct_declaration_list
  909. : struct_declaration
  910. | struct_declaration_list struct_declaration
  911. {
  912. symbol *sym = $2;
  913. /* go to the end of the chain */
  914. while (sym->next) sym = sym->next;
  915. sym->next = $1;
  916. $$ = $2;
  917. }
  918. ;
  919. struct_declaration
  920. : type_specifier_list struct_declarator_list ';'
  921. {
  922. /* add this type to all the symbols */
  923. symbol *sym;
  924. for ( sym = $2; sym != NULL; sym = sym->next )
  925. {
  926. sym_link *btype = copyLinkChain($1);
  927. pointerTypes(sym->type, btype);
  928. if (!sym->type)
  929. {
  930. sym->type = btype;
  931. sym->etype = getSpec(sym->type);
  932. }
  933. else
  934. addDecl (sym, 0, btype);
  935. /* make sure the type is complete and sane */
  936. checkTypeSanity(sym->etype, sym->name);
  937. }
  938. ignoreTypedefType = 0;
  939. $$ = $2;
  940. }
  941. ;
  942. struct_declarator_list
  943. : struct_declarator
  944. | struct_declarator_list ',' struct_declarator
  945. {
  946. $3->next = $1;
  947. $$ = $3;
  948. }
  949. ;
  950. struct_declarator
  951. : declarator
  952. | ':' constant_expr
  953. {
  954. unsigned int bitsize;
  955. $$ = newSymbol (genSymName(NestLevel), NestLevel);
  956. bitsize = (unsigned int) ulFromVal(constExprValue($2, TRUE));
  957. if (bitsize > (port->s.int_size * 8))
  958. {
  959. bitsize = port->s.int_size * 8;
  960. werror(E_BITFLD_SIZE, bitsize);
  961. }
  962. if (!bitsize)
  963. bitsize = BITVAR_PAD;
  964. $$->bitVar = bitsize;
  965. $$->bitUnnamed = 1;
  966. }
  967. | declarator ':' constant_expr
  968. {
  969. unsigned int bitsize;
  970. bitsize = (unsigned int) ulFromVal(constExprValue($3, TRUE));
  971. if (bitsize > (port->s.int_size * 8))
  972. {
  973. bitsize = port->s.int_size * 8;
  974. werror(E_BITFLD_SIZE, bitsize);
  975. }
  976. if (!bitsize)
  977. {
  978. $$ = newSymbol (genSymName(NestLevel), NestLevel);
  979. $$->bitVar = BITVAR_PAD;
  980. werror(W_BITFLD_NAMED);
  981. }
  982. else
  983. $1->bitVar = bitsize;
  984. }
  985. | { $$ = newSymbol ("", NestLevel); }
  986. ;
  987. enum_specifier
  988. : ENUM '{' enumerator_list '}'
  989. {
  990. $$ = newEnumType ($3);
  991. SPEC_SCLS(getSpec($$)) = 0;
  992. }
  993. | ENUM identifier '{' enumerator_list '}'
  994. {
  995. symbol *csym;
  996. sym_link *enumtype;
  997. csym = findSymWithLevel(enumTab, $2);
  998. if ((csym && csym->level == $2->level))
  999. {
  1000. werrorfl($2->fileDef, $2->lineDef, E_DUPLICATE_TYPEDEF, csym->name);
  1001. werrorfl(csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
  1002. }
  1003. enumtype = newEnumType ($4);
  1004. SPEC_SCLS(getSpec(enumtype)) = 0;
  1005. $2->type = enumtype;
  1006. /* add this to the enumerator table */
  1007. if (!csym)
  1008. addSym (enumTab, $2, $2->name, $2->level, $2->block, 0);
  1009. $$ = copyLinkChain(enumtype);
  1010. }
  1011. | ENUM identifier
  1012. {
  1013. symbol *csym;
  1014. /* check the enumerator table */
  1015. if ((csym = findSymWithLevel(enumTab, $2)))
  1016. $$ = copyLinkChain(csym->type);
  1017. else
  1018. {
  1019. $$ = newLink(SPECIFIER);
  1020. SPEC_NOUN($$) = V_INT;
  1021. }
  1022. }
  1023. ;
  1024. enumerator_list
  1025. : enumerator
  1026. | enumerator_list ','
  1027. | enumerator_list ',' enumerator
  1028. {
  1029. $3->next = $1;
  1030. $$ = $3;
  1031. }
  1032. ;
  1033. enumerator
  1034. : identifier opt_assign_expr
  1035. {
  1036. symbol *sym;
  1037. // check if the symbol at the same level already exists
  1038. if ((sym = findSymWithLevel (SymbolTab, $1)) && sym->level == $1->level)
  1039. {
  1040. werrorfl ($1->fileDef, $1->lineDef, E_DUPLICATE_MEMBER, "enum", $1->name);
  1041. werrorfl (sym->fileDef, sym->lineDef, E_PREVIOUS_DEF);
  1042. }
  1043. $1->type = copyLinkChain ($2->type);
  1044. $1->etype = getSpec ($1->type);
  1045. SPEC_ENUM ($1->etype) = 1;
  1046. $$ = $1;
  1047. // do this now, so we can use it for the next enums in the list
  1048. addSymChain (&$1);
  1049. }
  1050. ;
  1051. opt_assign_expr
  1052. : '=' constant_expr
  1053. {
  1054. value *val;
  1055. val = constExprValue($2, TRUE);
  1056. if (!IS_INT(val->type) && !IS_CHAR(val->type) && !IS_BOOL(val->type))
  1057. {
  1058. werror(E_ENUM_NON_INTEGER);
  1059. SNPRINTF(lbuff, sizeof(lbuff), "%d", (int) ulFromVal(val));
  1060. val = constVal(lbuff);
  1061. }
  1062. $$ = cenum = val;
  1063. }
  1064. | {
  1065. if (cenum)
  1066. {
  1067. SNPRINTF(lbuff, sizeof(lbuff), "%d", (int) ulFromVal(cenum)+1);
  1068. $$ = cenum = constVal(lbuff);
  1069. }
  1070. else
  1071. {
  1072. $$ = cenum = constCharVal(0);
  1073. }
  1074. }
  1075. ;
  1076. declarator
  1077. : declarator3 { $$ = $1; }
  1078. | pointer declarator3
  1079. {
  1080. addDecl ($2,0,reverseLink($1));
  1081. $$ = $2;
  1082. }
  1083. ;
  1084. declarator3
  1085. : declarator2_function_attributes { $$ = $1; }
  1086. | declarator2 { $$ = $1; }
  1087. ;
  1088. function_declarator
  1089. : declarator2_function_attributes { $$ = $1; }
  1090. | pointer declarator2_function_attributes
  1091. {
  1092. addDecl ($2,0,reverseLink($1));
  1093. $$ = $2;
  1094. }
  1095. ;
  1096. declarator2_function_attributes
  1097. : function_declarator2 { $$ = $1; }
  1098. | function_declarator2 function_attribute {
  1099. // copy the functionAttributes (not the args and hasVargs !!)
  1100. struct value *args;
  1101. unsigned hasVargs;
  1102. sym_link *funcType=$1->type;
  1103. while (funcType && !IS_FUNC(funcType))
  1104. funcType = funcType->next;
  1105. if (!funcType)
  1106. werror (E_FUNC_ATTR);
  1107. else
  1108. {
  1109. args=FUNC_ARGS(funcType);
  1110. hasVargs=FUNC_HASVARARGS(funcType);
  1111. memcpy (&funcType->funcAttrs, &$2->funcAttrs,
  1112. sizeof($2->funcAttrs));
  1113. FUNC_ARGS(funcType)=args;
  1114. FUNC_HASVARARGS(funcType)=hasVargs;
  1115. // just to be sure
  1116. memset (&$2->funcAttrs, 0,
  1117. sizeof($2->funcAttrs));
  1118. addDecl ($1,0,$2);
  1119. }
  1120. }
  1121. ;
  1122. declarator2
  1123. : identifier
  1124. | '(' declarator ')' { $$ = $2; }
  1125. | declarator3 '[' ']'
  1126. {
  1127. sym_link *p;
  1128. p = newLink (DECLARATOR);
  1129. DCL_TYPE(p) = ARRAY;
  1130. DCL_ELEM(p) = 0;
  1131. addDecl($1,0,p);
  1132. }
  1133. | declarator3 '[' constant_expr ']'
  1134. {
  1135. sym_link *p;
  1136. value *tval;
  1137. int size;
  1138. tval = constExprValue($3, TRUE);
  1139. /* if it is not a constant then Error */
  1140. p = newLink (DECLARATOR);
  1141. DCL_TYPE(p) = ARRAY;
  1142. if (!tval || (SPEC_SCLS(tval->etype) != S_LITERAL))
  1143. {
  1144. werror(E_CONST_EXPECTED);
  1145. /* Assume a single item array to limit the cascade */
  1146. /* of additional errors. */
  1147. size = 1;
  1148. }
  1149. else
  1150. {
  1151. if ((size = (int) ulFromVal(tval)) < 0)
  1152. {
  1153. werror(E_NEGATIVE_ARRAY_SIZE, $1->name);
  1154. size = 1;
  1155. }
  1156. }
  1157. DCL_ELEM(p) = size;
  1158. addDecl($1, 0, p);
  1159. }
  1160. ;
  1161. function_declarator2
  1162. : declarator2 '(' ')'
  1163. {
  1164. addDecl ($1, FUNCTION, NULL);
  1165. }
  1166. | declarator2 '('
  1167. {
  1168. NestLevel++;
  1169. STACK_PUSH(blockNum, currBlockno);
  1170. currBlockno = ++blockNo;
  1171. }
  1172. parameter_type_list ')'
  1173. {
  1174. sym_link *funcType;
  1175. addDecl ($1, FUNCTION, NULL);
  1176. funcType = $1->type;
  1177. while (funcType && !IS_FUNC(funcType))
  1178. funcType = funcType->next;
  1179. assert (funcType);
  1180. FUNC_HASVARARGS(funcType) = IS_VARG($4);
  1181. FUNC_ARGS(funcType) = reverseVal($4);
  1182. /* nest level was incremented to take care of the parms */
  1183. NestLevel--;
  1184. currBlockno = STACK_POP(blockNum);
  1185. // if this was a pointer (to a function)
  1186. if (!IS_FUNC($1->type))
  1187. cleanUpLevel(SymbolTab, NestLevel + 1);
  1188. $$ = $1;
  1189. }
  1190. | declarator2 '(' identifier_list ')'
  1191. {
  1192. werror(E_OLD_STYLE,$1->name);
  1193. /* assume it returns an int */
  1194. $1->type = $1->etype = newIntLink();
  1195. $$ = $1;
  1196. }
  1197. ;
  1198. pointer
  1199. : unqualified_pointer { $$ = $1;}
  1200. | unqualified_pointer type_specifier_list
  1201. {
  1202. $$ = $1;
  1203. if (IS_SPEC($2)) {
  1204. DCL_TSPEC($1) = $2;
  1205. DCL_PTR_CONST($1) = SPEC_CONST($2);
  1206. DCL_PTR_VOLATILE($1) = SPEC_VOLATILE($2);
  1207. DCL_PTR_RESTRICT($1) = SPEC_RESTRICT($2);
  1208. DCL_PTR_ADDRSPACE($1) = SPEC_ADDRSPACE($2);
  1209. }
  1210. else
  1211. werror (W_PTR_TYPE_INVALID);
  1212. }
  1213. | unqualified_pointer pointer
  1214. {
  1215. $$ = $1;
  1216. $$->next = $2;
  1217. DCL_TYPE($2)=port->unqualified_pointer;
  1218. }
  1219. | unqualified_pointer type_specifier_list pointer
  1220. {
  1221. $$ = $1;
  1222. if (IS_SPEC($2) && DCL_TYPE($3) == UPOINTER) {
  1223. DCL_PTR_CONST($1) = SPEC_CONST($2);
  1224. DCL_PTR_VOLATILE($1) = SPEC_VOLATILE($2);
  1225. DCL_PTR_RESTRICT($1) = SPEC_RESTRICT($2);
  1226. DCL_PTR_ADDRSPACE($1) = SPEC_ADDRSPACE($2);
  1227. switch (SPEC_SCLS($2)) {
  1228. case S_XDATA:
  1229. DCL_TYPE($3) = FPOINTER;
  1230. break;
  1231. case S_IDATA:
  1232. DCL_TYPE($3) = IPOINTER;
  1233. break;
  1234. case S_PDATA:
  1235. DCL_TYPE($3) = PPOINTER;
  1236. break;
  1237. case S_DATA:
  1238. DCL_TYPE($3) = POINTER;
  1239. break;
  1240. case S_CODE:
  1241. DCL_TYPE($3) = CPOINTER;
  1242. break;
  1243. case S_EEPROM:
  1244. DCL_TYPE($3) = EEPPOINTER;
  1245. break;
  1246. default:
  1247. // this could be just "constant"
  1248. // werror(W_PTR_TYPE_INVALID);
  1249. ;
  1250. }
  1251. }
  1252. else
  1253. werror (W_PTR_TYPE_INVALID);
  1254. $$->next = $3;
  1255. }
  1256. ;
  1257. unqualified_pointer
  1258. : '*'
  1259. {
  1260. $$ = newLink(DECLARATOR);
  1261. DCL_TYPE($$)=UPOINTER;
  1262. }
  1263. ;
  1264. type_specifier_list : type_specifier_list_ { $$ = finalizeSpec($1); };
  1265. type_specifier_list_
  1266. : type_specifier
  1267. //| type_specifier_list_ type_specifier { $$ = mergeSpec ($1,$2, "type_specifier_list"); }
  1268. | type_specifier_list_ type_specifier {
  1269. /* if the decl $2 is not a specifier */
  1270. /* find the spec and replace it */
  1271. $$ = mergeDeclSpec($1, $2, "type_specifier_list type_specifier skipped");
  1272. }
  1273. ;
  1274. identifier_list
  1275. : identifier
  1276. | identifier_list ',' identifier
  1277. {
  1278. $3->next = $1;
  1279. $$ = $3;
  1280. }
  1281. ;
  1282. parameter_type_list
  1283. : parameter_list
  1284. | parameter_list ',' VAR_ARGS { $1->vArgs = 1;}
  1285. ;
  1286. parameter_list
  1287. : parameter_declaration
  1288. | parameter_list ',' parameter_declaration
  1289. {
  1290. $3->next = $1;
  1291. $$ = $3;
  1292. }
  1293. ;
  1294. parameter_declaration
  1295. : declaration_specifiers declarator
  1296. {
  1297. symbol *loop;
  1298. if (IS_SPEC ($1) && !IS_VALID_PARAMETER_STORAGE_CLASS_SPEC ($1))
  1299. {
  1300. werror (E_STORAGE_CLASS_FOR_PARAMETER, $2->name);
  1301. }
  1302. pointerTypes ($2->type, $1);
  1303. addDecl ($2, 0, $1);
  1304. for (loop = $2; loop; loop->_isparm = 1, loop = loop->next)
  1305. ;
  1306. addSymChain (&$2);
  1307. $$ = symbolVal ($2);
  1308. ignoreTypedefType = 0;
  1309. }
  1310. | type_name
  1311. {
  1312. $$ = newValue ();
  1313. $$->type = $1;
  1314. $$->etype = getSpec ($$->type);
  1315. ignoreTypedefType = 0;
  1316. }
  1317. ;
  1318. type_name
  1319. : declaration_specifiers
  1320. {
  1321. if (IS_SPEC ($1) && !IS_VALID_PARAMETER_STORAGE_CLASS_SPEC ($1))
  1322. {
  1323. werror (E_STORAGE_CLASS_FOR_PARAMETER, "type name");
  1324. }
  1325. $$ = $1; ignoreTypedefType = 0;
  1326. }
  1327. | declaration_specifiers abstract_declarator
  1328. {
  1329. /* go to the end of the list */
  1330. sym_link *p;
  1331. if (IS_SPEC ($1) && !IS_VALID_PARAMETER_STORAGE_CLASS_SPEC ($1))
  1332. {
  1333. werror (E_STORAGE_CLASS_FOR_PARAMETER, "type name");
  1334. }
  1335. pointerTypes ($2,$1);
  1336. for (p = $2; p && p->next; p = p->next)
  1337. ;
  1338. if (!p)
  1339. {
  1340. werror(E_SYNTAX_ERROR, yytext);
  1341. }
  1342. else
  1343. {
  1344. p->next = $1;
  1345. }
  1346. $$ = $2;
  1347. ignoreTypedefType = 0;
  1348. }
  1349. ;
  1350. abstract_declarator
  1351. : pointer { $$ = reverseLink($1); }
  1352. | abstract_declarator2
  1353. | pointer abstract_declarator2 { $1 = reverseLink($1); $1->next = $2; $$ = $1;
  1354. if (IS_PTR($1) && IS_FUNC($2))
  1355. DCL_TYPE($1) = CPOINTER;
  1356. }
  1357. ;
  1358. abstract_declarator2
  1359. : '(' abstract_declarator ')' { $$ = $2; }
  1360. | '[' ']' {
  1361. $$ = newLink (DECLARATOR);
  1362. DCL_TYPE($$) = ARRAY;
  1363. DCL_ELEM($$) = 0;
  1364. }
  1365. | '[' constant_expr ']' {
  1366. value *val;
  1367. $$ = newLink (DECLARATOR);
  1368. DCL_TYPE($$) = ARRAY;
  1369. DCL_ELEM($$) = (int) ulFromVal(val = constExprValue($2,TRUE));
  1370. }
  1371. | abstract_declarator2 '[' ']' {
  1372. $$ = newLink (DECLARATOR);
  1373. DCL_TYPE($$) = ARRAY;
  1374. DCL_ELEM($$) = 0;
  1375. $$->next = $1;
  1376. }
  1377. | abstract_declarator2 '[' constant_expr ']'
  1378. {
  1379. value *val;
  1380. $$ = newLink (DECLARATOR);
  1381. DCL_TYPE($$) = ARRAY;
  1382. DCL_ELEM($$) = (int) ulFromVal(val = constExprValue($3,TRUE));
  1383. $$->next = $1;
  1384. }
  1385. | '(' ')' { $$ = NULL;}
  1386. | '(' parameter_type_list ')' { $$ = NULL;}
  1387. | abstract_declarator2 '(' ')' {
  1388. // $1 must be a pointer to a function
  1389. sym_link *p=newLink(DECLARATOR);
  1390. DCL_TYPE(p) = FUNCTION;
  1391. if (!$1) {
  1392. // ((void (code *) ()) 0) ()
  1393. $1=newLink(DECLARATOR);
  1394. DCL_TYPE($1)=CPOINTER;
  1395. $$ = $1;
  1396. }
  1397. $1->next=p;
  1398. }
  1399. | abstract_declarator2 '('
  1400. {
  1401. NestLevel++;
  1402. STACK_PUSH(blockNum, currBlockno);
  1403. currBlockno = ++blockNo;
  1404. }
  1405. parameter_type_list ')'
  1406. {
  1407. sym_link *p = newLink(DECLARATOR);
  1408. DCL_TYPE(p) = FUNCTION;
  1409. FUNC_HASVARARGS(p) = IS_VARG($4);
  1410. FUNC_ARGS(p) = reverseVal($4);
  1411. /* nest level was incremented to take care of the parms */
  1412. NestLevel--;
  1413. currBlockno = STACK_POP(blockNum);
  1414. if (!$1)
  1415. {
  1416. /* ((void (code *) (void)) 0) () */
  1417. $1 = newLink(DECLARATOR);
  1418. DCL_TYPE($1) = CPOINTER;
  1419. $$ = $1;
  1420. }
  1421. $1->next = p;
  1422. // disabled to fix bug 3190029
  1423. // // remove the symbol args (if any)
  1424. // cleanUpLevel(SymbolTab, NestLevel + 1);
  1425. }
  1426. ;
  1427. initializer
  1428. : assignment_expr { $$ = newiList(INIT_NODE,$1); }
  1429. | '{' initializer_list '}' { $$ = newiList(INIT_DEEP,revinit($2)); }
  1430. | '{' initializer_list ',' '}' { $$ = newiList(INIT_DEEP,revinit($2)); }
  1431. ;
  1432. initializer_list
  1433. : designation_opt initializer { $2->designation = $1; $$

Large files files are truncated, but you can click here to view the full file