Print this page
new smatch

Split Close
Expand all
Collapse all
          --- old/usr/src/tools/smatch/src/parse.c
          +++ new/usr/src/tools/smatch/src/parse.c
↓ open down ↓ 50 lines elided ↑ open up ↑
  51   51  static struct token *statement(struct token *token, struct statement **tree);
  52   52  static struct token *handle_attributes(struct token *token, struct decl_state *ctx, unsigned int keywords);
  53   53  
  54   54  typedef struct token *declarator_t(struct token *, struct decl_state *);
  55   55  static declarator_t
  56   56          struct_specifier, union_specifier, enum_specifier,
  57   57          attribute_specifier, typeof_specifier, parse_asm_declarator,
  58   58          typedef_specifier, inline_specifier, auto_specifier,
  59   59          register_specifier, static_specifier, extern_specifier,
  60   60          thread_specifier, const_qualifier, volatile_qualifier;
       61 +static declarator_t restrict_qualifier;
       62 +static declarator_t atomic_qualifier;
  61   63  
  62   64  static struct token *parse_if_statement(struct token *token, struct statement *stmt);
  63   65  static struct token *parse_return_statement(struct token *token, struct statement *stmt);
  64   66  static struct token *parse_loop_iterator(struct token *token, struct statement *stmt);
  65   67  static struct token *parse_default_statement(struct token *token, struct statement *stmt);
  66   68  static struct token *parse_case_statement(struct token *token, struct statement *stmt);
  67   69  static struct token *parse_switch_statement(struct token *token, struct statement *stmt);
  68   70  static struct token *parse_for_statement(struct token *token, struct statement *stmt);
  69   71  static struct token *parse_while_statement(struct token *token, struct statement *stmt);
  70   72  static struct token *parse_do_statement(struct token *token, struct statement *stmt);
↓ open down ↓ 2 lines elided ↑ open up ↑
  73   75  static struct token *parse_range_statement(struct token *token, struct statement *stmt);
  74   76  static struct token *parse_asm_statement(struct token *token, struct statement *stmt);
  75   77  static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list);
  76   78  static struct token *parse_static_assert(struct token *token, struct symbol_list **unused);
  77   79  
  78   80  typedef struct token *attr_t(struct token *, struct symbol *,
  79   81                               struct decl_state *);
  80   82  
  81   83  static attr_t
  82   84          attribute_packed, attribute_aligned, attribute_modifier,
       85 +        attribute_ext_visible,
  83   86          attribute_bitwise,
  84   87          attribute_address_space, attribute_context,
  85   88          attribute_designated_init,
  86   89          attribute_transparent_union, ignore_attribute,
  87   90          attribute_mode, attribute_force;
  88   91  
  89   92  typedef struct symbol *to_mode_t(struct symbol *);
  90   93  
  91   94  static to_mode_t
  92      -        to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode, to_word_mode;
       95 +        to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode;
       96 +static to_mode_t to_pointer_mode, to_word_mode;
  93   97  
  94   98  enum {
  95   99          Set_T = 1,
  96  100          Set_S = 2,
  97  101          Set_Char = 4,
  98  102          Set_Int = 8,
  99  103          Set_Double = 16,
 100  104          Set_Float = 32,
 101  105          Set_Signed = 64,
 102  106          Set_Unsigned = 128,
↓ open down ↓ 5 lines elided ↑ open up ↑
 108  112  };
 109  113  
 110  114  enum {
 111  115          CInt = 0, CSInt, CUInt, CReal, CChar, CSChar, CUChar,
 112  116  };
 113  117  
 114  118  enum {
 115  119          SNone = 0, STypedef, SAuto, SRegister, SExtern, SStatic, SForced, SMax,
 116  120  };
 117  121  
      122 +static void asm_modifier(struct token *token, unsigned long *mods, unsigned long mod)
      123 +{
      124 +        if (*mods & mod)
      125 +                warning(token->pos, "duplicated asm modifier");
      126 +        *mods |= mod;
      127 +}
      128 +
      129 +static void asm_modifier_volatile(struct token *token, unsigned long *mods)
      130 +{
      131 +        asm_modifier(token, mods, MOD_VOLATILE);
      132 +}
      133 +
      134 +static void asm_modifier_inline(struct token *token, unsigned long *mods)
      135 +{
      136 +        asm_modifier(token, mods, MOD_INLINE);
      137 +}
      138 +
 118  139  static struct symbol_op typedef_op = {
 119  140          .type = KW_MODIFIER,
 120  141          .declarator = typedef_specifier,
 121  142  };
 122  143  
 123  144  static struct symbol_op inline_op = {
 124  145          .type = KW_MODIFIER,
 125  146          .declarator = inline_specifier,
      147 +        .asm_modifier = asm_modifier_inline,
 126  148  };
 127  149  
 128  150  static declarator_t noreturn_specifier;
 129  151  static struct symbol_op noreturn_op = {
 130  152          .type = KW_MODIFIER,
 131  153          .declarator = noreturn_specifier,
 132  154  };
 133  155  
 134  156  static declarator_t alignas_specifier;
 135  157  static struct symbol_op alignas_op = {
↓ open down ↓ 27 lines elided ↑ open up ↑
 163  185  };
 164  186  
 165  187  static struct symbol_op const_op = {
 166  188          .type = KW_QUALIFIER,
 167  189          .declarator = const_qualifier,
 168  190  };
 169  191  
 170  192  static struct symbol_op volatile_op = {
 171  193          .type = KW_QUALIFIER,
 172  194          .declarator = volatile_qualifier,
      195 +        .asm_modifier = asm_modifier_volatile,
 173  196  };
 174  197  
 175  198  static struct symbol_op restrict_op = {
 176  199          .type = KW_QUALIFIER,
      200 +        .declarator = restrict_qualifier,
 177  201  };
 178  202  
      203 +static struct symbol_op atomic_op = {
      204 +        .type = KW_QUALIFIER,
      205 +        .declarator = atomic_qualifier,
      206 +};
      207 +
 179  208  static struct symbol_op typeof_op = {
 180  209          .type = KW_SPECIFIER,
 181  210          .declarator = typeof_specifier,
 182  211          .test = Set_Any,
 183  212          .set = Set_S|Set_T,
 184  213  };
 185  214  
 186  215  static struct symbol_op attribute_op = {
 187  216          .type = KW_ATTRIBUTE,
 188  217          .declarator = attribute_specifier,
↓ open down ↓ 39 lines elided ↑ open up ↑
 228  257          .set = Set_T|Set_Int,
 229  258  };
 230  259  
 231  260  static struct symbol_op double_op = {
 232  261          .type = KW_SPECIFIER,
 233  262          .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
 234  263          .set = Set_T|Set_Double,
 235  264          .class = CReal,
 236  265  };
 237  266  
 238      -/* FIXME: this is not even slightly right. */
 239      -static struct symbol_op complex_op = {
 240      -        .type = KW_SPECIFIER,
 241      -        .test = 0, //Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong,
 242      -        .set = 0, //Set_Double, //Set_T,Set_Double,
 243      -        .class = CReal,
 244      -};
 245      -
 246  267  static struct symbol_op float_op = {
 247  268          .type = KW_SPECIFIER | KW_SHORT,
 248  269          .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long,
 249  270          .set = Set_T|Set_Float,
 250  271          .class = CReal,
 251  272  };
 252  273  
 253  274  static struct symbol_op short_op = {
 254  275          .type = KW_SPECIFIER | KW_SHORT,
 255  276          .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short,
↓ open down ↓ 90 lines elided ↑ open up ↑
 346  367  };
 347  368  
 348  369  static struct symbol_op aligned_op = {
 349  370          .attribute = attribute_aligned,
 350  371  };
 351  372  
 352  373  static struct symbol_op attr_mod_op = {
 353  374          .attribute = attribute_modifier,
 354  375  };
 355  376  
      377 +static struct symbol_op ext_visible_op = {
      378 +        .attribute = attribute_ext_visible,
      379 +};
      380 +
 356  381  static struct symbol_op attr_bitwise_op = {
 357  382          .attribute = attribute_bitwise,
 358  383  };
 359  384  
 360  385  static struct symbol_op attr_force_op = {
 361  386          .attribute = attribute_force,
 362  387  };
 363  388  
 364  389  static struct symbol_op address_space_op = {
 365  390          .attribute = attribute_address_space,
↓ open down ↓ 37 lines elided ↑ open up ↑
 403  428  static struct symbol_op mode_DI_op = {
 404  429          .type = KW_MODE,
 405  430          .to_mode = to_DI_mode
 406  431  };
 407  432  
 408  433  static struct symbol_op mode_TI_op = {
 409  434          .type = KW_MODE,
 410  435          .to_mode = to_TI_mode
 411  436  };
 412  437  
      438 +static struct symbol_op mode_pointer_op = {
      439 +        .type = KW_MODE,
      440 +        .to_mode = to_pointer_mode
      441 +};
      442 +
 413  443  static struct symbol_op mode_word_op = {
 414  444          .type = KW_MODE,
 415  445          .to_mode = to_word_mode
 416  446  };
 417  447  
 418  448  /* Using NS_TYPEDEF will also make the keyword a reserved one */
 419  449  static struct init_keyword {
 420  450          const char *name;
 421  451          enum namespace ns;
 422  452          unsigned long modifiers;
 423  453          struct symbol_op *op;
 424  454          struct symbol *type;
 425  455  } keyword_table[] = {
 426  456          /* Type qualifiers */
 427  457          { "const",      NS_TYPEDEF, .op = &const_op },
 428  458          { "__const",    NS_TYPEDEF, .op = &const_op },
 429  459          { "__const__",  NS_TYPEDEF, .op = &const_op },
 430  460          { "volatile",   NS_TYPEDEF, .op = &volatile_op },
 431  461          { "__volatile",         NS_TYPEDEF, .op = &volatile_op },
 432  462          { "__volatile__",       NS_TYPEDEF, .op = &volatile_op },
      463 +        { "restrict",   NS_TYPEDEF, .op = &restrict_op},
      464 +        { "__restrict", NS_TYPEDEF, .op = &restrict_op},
      465 +        { "__restrict__",       NS_TYPEDEF, .op = &restrict_op},
      466 +        { "_Atomic",    NS_TYPEDEF, .op = &atomic_op},
 433  467  
 434  468          /* Typedef.. */
 435  469          { "typedef",    NS_TYPEDEF, .op = &typedef_op },
 436  470  
 437  471          /* Type specifiers */
 438  472          { "void",       NS_TYPEDEF, .type = &void_ctype, .op = &spec_op},
 439  473          { "char",       NS_TYPEDEF, .op = &char_op },
 440  474          { "short",      NS_TYPEDEF, .op = &short_op },
 441  475          { "int",        NS_TYPEDEF, .op = &int_op },
 442  476          { "long",       NS_TYPEDEF, .op = &long_op },
 443  477          { "float",      NS_TYPEDEF, .op = &float_op },
 444  478          { "double",     NS_TYPEDEF, .op = &double_op },
 445  479          { "signed",     NS_TYPEDEF, .op = &signed_op },
 446  480          { "__signed",   NS_TYPEDEF, .op = &signed_op },
 447  481          { "__signed__", NS_TYPEDEF, .op = &signed_op },
 448  482          { "unsigned",   NS_TYPEDEF, .op = &unsigned_op },
 449  483          { "__int128",   NS_TYPEDEF, .op = &int128_op },
 450  484          { "_Bool",      NS_TYPEDEF, .type = &bool_ctype, .op = &spec_op },
 451      -        { "_Complex",   NS_TYPEDEF, .op = &complex_op },
 452  485  
 453  486          /* Predeclared types */
 454  487          { "__builtin_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
 455  488          { "__builtin_ms_va_list", NS_TYPEDEF, .type = &ptr_ctype, .op = &spec_op },
 456  489          { "__int128_t", NS_TYPEDEF, .type = &lllong_ctype, .op = &spec_op },
 457  490          { "__uint128_t",NS_TYPEDEF, .type = &ulllong_ctype, .op = &spec_op },
      491 +        { "_Float32",   NS_TYPEDEF, .type = &float32_ctype, .op = &spec_op },
      492 +        { "_Float32x",  NS_TYPEDEF, .type = &float32x_ctype, .op = &spec_op },
      493 +        { "_Float64",   NS_TYPEDEF, .type = &float64_ctype, .op = &spec_op },
      494 +        { "_Float64x",  NS_TYPEDEF, .type = &float64x_ctype, .op = &spec_op },
      495 +        { "_Float128",  NS_TYPEDEF, .type = &float128_ctype, .op = &spec_op },
 458  496  
 459  497          /* Extended types */
 460  498          { "typeof",     NS_TYPEDEF, .op = &typeof_op },
 461  499          { "__typeof",   NS_TYPEDEF, .op = &typeof_op },
 462  500          { "__typeof__", NS_TYPEDEF, .op = &typeof_op },
 463  501  
 464  502          { "__attribute",   NS_TYPEDEF, .op = &attribute_op },
 465  503          { "__attribute__", NS_TYPEDEF, .op = &attribute_op },
 466  504  
 467  505          { "struct",     NS_TYPEDEF, .op = &struct_op },
↓ open down ↓ 1 lines elided ↑ open up ↑
 469  507          { "enum",       NS_TYPEDEF, .op = &enum_op },
 470  508  
 471  509          { "inline",     NS_TYPEDEF, .op = &inline_op },
 472  510          { "__inline",   NS_TYPEDEF, .op = &inline_op },
 473  511          { "__inline__", NS_TYPEDEF, .op = &inline_op },
 474  512  
 475  513          { "_Noreturn",  NS_TYPEDEF, .op = &noreturn_op },
 476  514  
 477  515          { "_Alignas",   NS_TYPEDEF, .op = &alignas_op },
 478  516  
 479      -        /* Ignored for now.. */
 480      -        { "restrict",   NS_TYPEDEF, .op = &restrict_op},
 481      -        { "__restrict", NS_TYPEDEF, .op = &restrict_op},
 482      -        { "__restrict__",       NS_TYPEDEF, .op = &restrict_op},
 483      -
 484  517          /* Static assertion */
 485  518          { "_Static_assert", NS_KEYWORD, .op = &static_assert_op },
 486  519  
 487  520          /* Storage class */
 488  521          { "auto",       NS_TYPEDEF, .op = &auto_op },
 489  522          { "register",   NS_TYPEDEF, .op = &register_op },
 490  523          { "static",     NS_TYPEDEF, .op = &static_op },
 491  524          { "extern",     NS_TYPEDEF, .op = &extern_op },
 492  525          { "__thread",   NS_TYPEDEF, .op = &thread_op },
 493  526          { "_Thread_local",      NS_TYPEDEF, .op = &thread_op },
↓ open down ↓ 21 lines elided ↑ open up ↑
 515  548          { "__packed__", NS_KEYWORD, .op = &packed_op },
 516  549          { "aligned",    NS_KEYWORD, .op = &aligned_op },
 517  550          { "__aligned__",NS_KEYWORD, .op = &aligned_op },
 518  551          { "nocast",     NS_KEYWORD,     MOD_NOCAST,     .op = &attr_mod_op },
 519  552          { "noderef",    NS_KEYWORD,     MOD_NODEREF,    .op = &attr_mod_op },
 520  553          { "safe",       NS_KEYWORD,     MOD_SAFE,       .op = &attr_mod_op },
 521  554          { "force",      NS_KEYWORD,     .op = &attr_force_op },
 522  555          { "bitwise",    NS_KEYWORD,     MOD_BITWISE,    .op = &attr_bitwise_op },
 523  556          { "__bitwise__",NS_KEYWORD,     MOD_BITWISE,    .op = &attr_bitwise_op },
 524  557          { "address_space",NS_KEYWORD,   .op = &address_space_op },
 525      -        { "mode",       NS_KEYWORD,     .op = &mode_op },
 526  558          { "context",    NS_KEYWORD,     .op = &context_op },
 527  559          { "designated_init",    NS_KEYWORD,     .op = &designated_init_op },
      560 +        { "__designated_init__",        NS_KEYWORD,     .op = &designated_init_op },
      561 +        { "transparent_union",  NS_KEYWORD,     .op = &transparent_union_op },
 528  562          { "__transparent_union__",      NS_KEYWORD,     .op = &transparent_union_op },
 529  563          { "noreturn",   NS_KEYWORD,     MOD_NORETURN,   .op = &attr_mod_op },
 530  564          { "__noreturn__",       NS_KEYWORD,     MOD_NORETURN,   .op = &attr_mod_op },
 531  565          { "pure",       NS_KEYWORD,     MOD_PURE,       .op = &attr_mod_op },
 532  566          {"__pure__",    NS_KEYWORD,     MOD_PURE,       .op = &attr_mod_op },
 533  567          {"const",       NS_KEYWORD,     MOD_PURE,       .op = &attr_mod_op },
 534  568          {"__const",     NS_KEYWORD,     MOD_PURE,       .op = &attr_mod_op },
 535  569          {"__const__",   NS_KEYWORD,     MOD_PURE,       .op = &attr_mod_op },
      570 +        {"externally_visible",  NS_KEYWORD,     .op = &ext_visible_op },
      571 +        {"__externally_visible__",      NS_KEYWORD,     .op = &ext_visible_op },
 536  572  
      573 +        { "mode",       NS_KEYWORD,     .op = &mode_op },
 537  574          { "__mode__",   NS_KEYWORD,     .op = &mode_op },
 538      -        { "QI",         NS_KEYWORD,     MOD_CHAR,       .op = &mode_QI_op },
 539      -        { "__QI__",     NS_KEYWORD,     MOD_CHAR,       .op = &mode_QI_op },
 540      -        { "HI",         NS_KEYWORD,     MOD_SHORT,      .op = &mode_HI_op },
 541      -        { "__HI__",     NS_KEYWORD,     MOD_SHORT,      .op = &mode_HI_op },
 542      -        { "SI",         NS_KEYWORD,                     .op = &mode_SI_op },
 543      -        { "__SI__",     NS_KEYWORD,                     .op = &mode_SI_op },
 544      -        { "DI",         NS_KEYWORD,     MOD_LONGLONG,   .op = &mode_DI_op },
 545      -        { "__DI__",     NS_KEYWORD,     MOD_LONGLONG,   .op = &mode_DI_op },
 546      -        { "TI",         NS_KEYWORD,     MOD_LONGLONGLONG,       .op = &mode_TI_op },
 547      -        { "__TI__",     NS_KEYWORD,     MOD_LONGLONGLONG,       .op = &mode_TI_op },
 548      -        { "word",       NS_KEYWORD,     MOD_LONG,       .op = &mode_word_op },
 549      -        { "__word__",   NS_KEYWORD,     MOD_LONG,       .op = &mode_word_op },
      575 +        { "QI",         NS_KEYWORD,     .op = &mode_QI_op },
      576 +        { "__QI__",     NS_KEYWORD,     .op = &mode_QI_op },
      577 +        { "HI",         NS_KEYWORD,     .op = &mode_HI_op },
      578 +        { "__HI__",     NS_KEYWORD,     .op = &mode_HI_op },
      579 +        { "SI",         NS_KEYWORD,     .op = &mode_SI_op },
      580 +        { "__SI__",     NS_KEYWORD,     .op = &mode_SI_op },
      581 +        { "DI",         NS_KEYWORD,     .op = &mode_DI_op },
      582 +        { "__DI__",     NS_KEYWORD,     .op = &mode_DI_op },
      583 +        { "TI",         NS_KEYWORD,     .op = &mode_TI_op },
      584 +        { "__TI__",     NS_KEYWORD,     .op = &mode_TI_op },
      585 +        { "byte",       NS_KEYWORD,     .op = &mode_QI_op },
      586 +        { "__byte__",   NS_KEYWORD,     .op = &mode_QI_op },
      587 +        { "pointer",    NS_KEYWORD,     .op = &mode_pointer_op },
      588 +        { "__pointer__",NS_KEYWORD,     .op = &mode_pointer_op },
      589 +        { "word",       NS_KEYWORD,     .op = &mode_word_op },
      590 +        { "__word__",   NS_KEYWORD,     .op = &mode_word_op },
 550  591  };
 551  592  
 552  593  
 553  594  static const char *ignored_attributes[] = {
 554  595  
 555  596  #define GCC_ATTR(x)             \
 556  597          STRINGIFY(x),           \
 557  598          STRINGIFY(__##x##__),
 558  599  
 559  600  #include "gcc-attr-list.h"
↓ open down ↓ 200 lines elided ↑ open up ↑
 760  801  static struct token *struct_specifier(struct token *token, struct decl_state *ctx)
 761  802  {
 762  803          return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration);
 763  804  }
 764  805  
 765  806  static struct token *union_specifier(struct token *token, struct decl_state *ctx)
 766  807  {
 767  808          return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration);
 768  809  }
 769  810  
 770      -
 771      -typedef struct {
 772      -        int x;
 773      -        unsigned long long y;
 774      -} Num;
 775      -
 776      -static void upper_boundary(Num *n, Num *v)
      811 +///
      812 +// safe right shift
      813 +//
      814 +// This allow to use a shift amount as big (or bigger)
      815 +// than the width of the value to be shifted, in which case
      816 +// the result is, of course, 0.
      817 +static unsigned long long rshift(unsigned long long val, unsigned int n)
 777  818  {
 778      -        if (n->x > v->x)
 779      -                return;
 780      -        if (n->x < v->x) {
 781      -                *n = *v;
 782      -                return;
 783      -        }
 784      -        if (n->y < v->y)
 785      -                n->y = v->y;
      819 +        if (n >= (sizeof(val) * 8))
      820 +                return 0;
      821 +        return val >> n;
 786  822  }
 787  823  
 788      -static void lower_boundary(Num *n, Num *v)
      824 +struct range {
      825 +        long long               neg;
      826 +        unsigned long long      pos;
      827 +};
      828 +
      829 +static void update_range(struct range *range, unsigned long long uval, struct symbol *vtype)
 789  830  {
 790      -        if (n->x < v->x)
 791      -                return;
 792      -        if (n->x > v->x) {
 793      -                *n = *v;
 794      -                return;
      831 +        long long sval = uval;
      832 +
      833 +        if (is_signed_type(vtype) && (sval < 0)) {
      834 +                if (sval < range->neg)
      835 +                        range->neg = sval;
      836 +        } else {
      837 +                if (uval > range->pos)
      838 +                        range->pos = uval;
 795  839          }
 796      -        if (n->y > v->y)
 797      -                n->y = v->y;
 798  840  }
 799  841  
 800      -static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
      842 +static int type_is_ok(struct symbol *type, struct range range)
 801  843  {
 802  844          int shift = type->bit_size;
 803  845          int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED;
 804  846  
 805  847          if (!is_unsigned)
 806  848                  shift--;
 807      -        if (upper->x == 0 && upper->y >> shift)
      849 +        if (rshift(range.pos, shift))
 808  850                  return 0;
 809      -        if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
      851 +        if (range.neg == 0)
 810  852                  return 1;
 811      -        return 0;
      853 +        if (is_unsigned)
      854 +                return 0;
      855 +        if (rshift(~range.neg, shift))
      856 +                return 0;
      857 +        return 1;
 812  858  }
 813  859  
 814      -static struct symbol *bigger_enum_type(struct symbol *s1, struct symbol *s2)
      860 +static struct range type_range(struct symbol *type)
 815  861  {
 816      -        if (s1->bit_size < s2->bit_size) {
 817      -                s1 = s2;
 818      -        } else if (s1->bit_size == s2->bit_size) {
 819      -                if (s2->ctype.modifiers & MOD_UNSIGNED)
 820      -                        s1 = s2;
      862 +        struct range range;
      863 +        unsigned int size = type->bit_size;
      864 +        unsigned long long max;
      865 +        long long min;
      866 +
      867 +        if (is_signed_type(type)) {
      868 +                min = sign_bit(size);
      869 +                max = min - 1;
      870 +        } else {
      871 +                min = 0;
      872 +                max = bits_mask(size);
 821  873          }
 822      -        if (s1->bit_size < bits_in_int)
 823      -                return &int_ctype;
 824      -        return s1;
      874 +
      875 +        range.pos = max;
      876 +        range.neg = min;
      877 +        return range;
 825  878  }
 826  879  
      880 +static int val_in_range(struct range *range, long long sval, struct symbol *vtype)
      881 +{
      882 +        unsigned long long uval = sval;
      883 +
      884 +        if (is_signed_type(vtype) && (sval < 0))
      885 +                return range->neg <= sval;
      886 +        else
      887 +                return uval <= range->pos;
      888 +}
      889 +
 827  890  static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
 828  891  {
      892 +        struct range irange = type_range(&int_ctype);
 829  893          struct symbol *sym;
 830  894  
 831  895          FOR_EACH_PTR(list, sym) {
 832  896                  struct expression *expr = sym->initializer;
 833  897                  struct symbol *ctype;
      898 +                long long val;
 834  899                  if (expr->type != EXPR_VALUE)
 835  900                          continue;
 836  901                  ctype = expr->ctype;
 837      -                if (ctype->bit_size == base_type->bit_size)
      902 +                val = get_expression_value(expr);
      903 +                if (is_int_type(ctype) && val_in_range(&irange, val, ctype)) {
      904 +                        expr->ctype = &int_ctype;
 838  905                          continue;
      906 +                }
      907 +                if (ctype->bit_size == base_type->bit_size) {
      908 +                        expr->ctype = base_type;
      909 +                        continue;
      910 +                }
 839  911                  cast_value(expr, base_type, expr, ctype);
      912 +                expr->ctype = base_type;
 840  913          } END_FOR_EACH_PTR(sym);
 841  914  }
 842  915  
 843  916  static struct token *parse_enum_declaration(struct token *token, struct symbol *parent)
 844  917  {
 845  918          unsigned long long lastval = 0;
 846  919          struct symbol *ctype = NULL, *base_type = NULL;
 847      -        Num upper = {-1, 0}, lower = {1, 0};
      920 +        struct range range = { };
      921 +        int mix_bitwise = 0;
 848  922  
 849  923          parent->examined = 1;
 850  924          parent->ctype.base_type = &int_ctype;
 851  925          while (token_type(token) == TOKEN_IDENT) {
 852  926                  struct expression *expr = NULL;
 853  927                  struct token *next = token->next;
 854  928                  struct symbol *sym;
 855  929  
 856  930                  if (match_op(next, '=')) {
 857  931                          next = constant_expression(next->next, &expr);
↓ open down ↓ 35 lines elided ↑ open up ↑
 893  967                          /*
 894  968                           * base_type rules:
 895  969                           *  - if all enums are of the same type, then
 896  970                           *    the base_type is that type (two first
 897  971                           *    cases)
 898  972                           *  - if enums are of different types, they
 899  973                           *    all have to be integer types, and the
 900  974                           *    base type is at least "int_ctype".
 901  975                           *  - otherwise the base_type is "bad_ctype".
 902  976                           */
 903      -                        if (!base_type) {
      977 +                        if (!base_type || ctype == &bad_ctype) {
 904  978                                  base_type = ctype;
 905  979                          } else if (ctype == base_type) {
 906  980                                  /* nothing */
 907  981                          } else if (is_int_type(base_type) && is_int_type(ctype)) {
 908      -                                base_type = bigger_enum_type(base_type, ctype);
 909      -                        } else
      982 +                                base_type = &int_ctype;
      983 +                        } else if (is_restricted_type(base_type) != is_restricted_type(ctype)) {
      984 +                                if (!mix_bitwise++) {
      985 +                                        warning(expr->pos, "mixed bitwiseness");
      986 +                                }
      987 +                        } else if (is_restricted_type(base_type) && base_type != ctype) {
      988 +                                sparse_error(expr->pos, "incompatible restricted type");
      989 +                                info(expr->pos, "   expected: %s", show_typename(base_type));
      990 +                                info(expr->pos, "        got: %s", show_typename(ctype));
 910  991                                  base_type = &bad_ctype;
      992 +                        } else if (base_type != &bad_ctype) {
      993 +                                sparse_error(token->pos, "bad enum definition");
      994 +                                base_type = &bad_ctype;
      995 +                        }
 911  996                          parent->ctype.base_type = base_type;
 912  997                  }
 913  998                  if (is_int_type(base_type)) {
 914      -                        Num v = {.y = lastval};
 915      -                        if (ctype->ctype.modifiers & MOD_UNSIGNED)
 916      -                                v.x = 0;
 917      -                        else if ((long long)lastval >= 0)
 918      -                                v.x = 0;
 919      -                        else
 920      -                                v.x = -1;
 921      -                        upper_boundary(&upper, &v);
 922      -                        lower_boundary(&lower, &v);
      999 +                        update_range(&range, lastval, ctype);
 923 1000                  }
 924 1001                  token = next;
 925 1002  
 926 1003                  sym->endpos = token->pos;
 927 1004  
 928 1005                  if (!match_op(token, ','))
 929 1006                          break;
 930 1007                  token = token->next;
 931 1008          }
 932 1009          if (!base_type) {
 933      -                sparse_error(token->pos, "bad enum definition");
     1010 +                sparse_error(token->pos, "empty enum definition");
 934 1011                  base_type = &bad_ctype;
 935 1012          }
 936 1013          else if (!is_int_type(base_type))
 937      -                base_type = base_type;
 938      -        else if (type_is_ok(base_type, &upper, &lower))
 939      -                base_type = base_type;
 940      -        else if (type_is_ok(&int_ctype, &upper, &lower))
 941      -                base_type = &int_ctype;
 942      -        else if (type_is_ok(&uint_ctype, &upper, &lower))
     1014 +                ;
     1015 +        else if (type_is_ok(&uint_ctype, range))
 943 1016                  base_type = &uint_ctype;
 944      -        else if (type_is_ok(&long_ctype, &upper, &lower))
 945      -                base_type = &long_ctype;
 946      -        else if (type_is_ok(&ulong_ctype, &upper, &lower))
     1017 +        else if (type_is_ok(&int_ctype, range))
     1018 +                base_type = &int_ctype;
     1019 +        else if (type_is_ok(&ulong_ctype, range))
 947 1020                  base_type = &ulong_ctype;
 948      -        else if (type_is_ok(&llong_ctype, &upper, &lower))
 949      -                base_type = &llong_ctype;
 950      -        else if (type_is_ok(&ullong_ctype, &upper, &lower))
     1021 +        else if (type_is_ok(&long_ctype, range))
     1022 +                base_type = &long_ctype;
     1023 +        else if (type_is_ok(&ullong_ctype, range))
 951 1024                  base_type = &ullong_ctype;
     1025 +        else if (type_is_ok(&llong_ctype, range))
     1026 +                base_type = &llong_ctype;
 952 1027          else
 953 1028                  base_type = &bad_ctype;
 954 1029          parent->ctype.base_type = base_type;
 955 1030          parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED);
 956 1031          parent->examined = 0;
 957 1032  
     1033 +        if (mix_bitwise)
     1034 +                return token;
 958 1035          cast_enum_list(parent->symbol_list, base_type);
 959 1036  
 960 1037          return token;
 961 1038  }
 962 1039  
 963 1040  static struct token *enum_specifier(struct token *token, struct decl_state *ctx)
 964 1041  {
 965 1042          struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration);
 966 1043          struct ctype *ctype = &ctx->ctype.base_type->ctype;
 967 1044  
↓ open down ↓ 68 lines elided ↑ open up ↑
1036 1113                  warning(*pos, "duplicate %s", modifier_string(qual));
1037 1114          ctx->modifiers |= qual;
1038 1115  }
1039 1116  
1040 1117  static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx)
1041 1118  {
1042 1119          apply_qualifier(&token->pos, &ctx->ctype, attr->ctype.modifiers);
1043 1120          return token;
1044 1121  }
1045 1122  
     1123 +static struct token *attribute_ext_visible(struct token *token, struct symbol *attr, struct decl_state *ctx)
     1124 +{
     1125 +        ctx->is_ext_visible = 1;
     1126 +        return token;
     1127 +}
     1128 +
1046 1129  static struct token *attribute_bitwise(struct token *token, struct symbol *attr, struct decl_state *ctx)
1047 1130  {
1048 1131          if (Wbitwise)
1049 1132                  attribute_modifier(token, attr, ctx);
1050 1133          return token;
1051 1134  }
1052 1135  
     1136 +static struct ident *numerical_address_space(int asn)
     1137 +{
     1138 +        char buff[32];
     1139 +
     1140 +        if (!asn)
     1141 +                return NULL;
     1142 +        sprintf(buff, "<asn:%d>", asn);
     1143 +        return built_in_ident(buff);
     1144 +}
     1145 +
1053 1146  static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx)
1054 1147  {
1055 1148          struct expression *expr = NULL;
1056      -        int as;
     1149 +        struct ident *as = NULL;
     1150 +        struct token *next;
     1151 +
1057 1152          token = expect(token, '(', "after address_space attribute");
1058      -        token = conditional_expression(token, &expr);
1059      -        if (expr) {
1060      -                as = const_expression_value(expr);
1061      -                if (Waddress_space && as)
1062      -                        ctx->ctype.as = as;
     1153 +        switch (token_type(token)) {
     1154 +        case TOKEN_NUMBER:
     1155 +                next = primary_expression(token, &expr);
     1156 +                if (expr->type != EXPR_VALUE)
     1157 +                        goto invalid;
     1158 +                as = numerical_address_space(expr->value);
     1159 +                break;
     1160 +        case TOKEN_IDENT:
     1161 +                next = token->next;
     1162 +                as = token->ident;
     1163 +                break;
     1164 +        default:
     1165 +                next = token->next;
     1166 +        invalid:
     1167 +                as = NULL;
     1168 +                warning(token->pos, "invalid address space name");
1063 1169          }
1064      -        token = expect(token, ')', "after address_space attribute");
     1170 +
     1171 +        if (Waddress_space && as) {
     1172 +                if (ctx->ctype.as)
     1173 +                        sparse_error(token->pos,
     1174 +                                     "multiple address space given: %s & %s",
     1175 +                                     show_as(ctx->ctype.as), show_as(as));
     1176 +                ctx->ctype.as = as;
     1177 +        }
     1178 +        token = expect(next, ')', "after address_space attribute");
1065 1179          return token;
1066 1180  }
1067 1181  
1068 1182  static struct symbol *to_QI_mode(struct symbol *ctype)
1069 1183  {
1070 1184          if (ctype->ctype.base_type != &int_type)
1071 1185                  return NULL;
1072 1186          if (ctype == &char_ctype)
1073 1187                  return ctype;
1074 1188          return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype
↓ open down ↓ 25 lines elided ↑ open up ↑
1100 1214  }
1101 1215  
1102 1216  static struct symbol *to_TI_mode(struct symbol *ctype)
1103 1217  {
1104 1218          if (ctype->ctype.base_type != &int_type)
1105 1219                  return NULL;
1106 1220          return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulllong_ctype
1107 1221                                                       : &slllong_ctype;
1108 1222  }
1109 1223  
     1224 +static struct symbol *to_pointer_mode(struct symbol *ctype)
     1225 +{
     1226 +        if (ctype->ctype.base_type != &int_type)
     1227 +                return NULL;
     1228 +        return ctype->ctype.modifiers & MOD_UNSIGNED ? uintptr_ctype
     1229 +                                                     :  intptr_ctype;
     1230 +}
     1231 +
1110 1232  static struct symbol *to_word_mode(struct symbol *ctype)
1111 1233  {
1112 1234          if (ctype->ctype.base_type != &int_type)
1113 1235                  return NULL;
1114 1236          return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype
1115 1237                                                       : &slong_ctype;
1116 1238  }
1117 1239  
1118 1240  static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx)
1119 1241  {
1120 1242          token = expect(token, '(', "after mode attribute");
1121 1243          if (token_type(token) == TOKEN_IDENT) {
1122 1244                  struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD);
1123 1245                  if (mode && mode->op->type == KW_MODE)
1124 1246                          ctx->mode = mode->op;
1125 1247                  else
1126      -                        sparse_error(token->pos, "unknown mode attribute %s\n", show_ident(token->ident));
     1248 +                        sparse_error(token->pos, "unknown mode attribute %s", show_ident(token->ident));
1127 1249                  token = token->next;
1128 1250          } else
1129 1251                  sparse_error(token->pos, "expect attribute mode symbol\n");
1130 1252          token = expect(token, ')', "after mode attribute");
1131 1253          return token;
1132 1254  }
1133 1255  
1134 1256  static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx)
1135 1257  {
1136 1258          struct context *context = alloc_context();
1137 1259          struct expression *args[3];
1138      -        int argc = 0;
     1260 +        int idx = 0;
1139 1261  
1140 1262          token = expect(token, '(', "after context attribute");
1141      -        while (!match_op(token, ')')) {
1142      -                struct expression *expr = NULL;
1143      -                token = conditional_expression(token, &expr);
1144      -                if (!expr)
1145      -                        break;
1146      -                if (argc < 3)
1147      -                        args[argc++] = expr;
1148      -                if (!match_op(token, ','))
1149      -                        break;
     1263 +        token = conditional_expression(token, &args[0]);
     1264 +        token = expect(token, ',', "after context 1st argument");
     1265 +        token = conditional_expression(token, &args[1]);
     1266 +        if (match_op(token, ',')) {
1150 1267                  token = token->next;
1151      -        }
1152      -
1153      -        switch(argc) {
1154      -        case 0:
1155      -                sparse_error(token->pos, "expected context input/output values");
1156      -                break;
1157      -        case 1:
1158      -                context->in = get_expression_value(args[0]);
1159      -                break;
1160      -        case 2:
1161      -                context->in = get_expression_value(args[0]);
1162      -                context->out = get_expression_value(args[1]);
1163      -                break;
1164      -        case 3:
     1268 +                token = conditional_expression(token, &args[2]);
     1269 +                token = expect(token, ')', "after context 3rd argument");
1165 1270                  context->context = args[0];
1166      -                context->in = get_expression_value(args[1]);
1167      -                context->out = get_expression_value(args[2]);
1168      -                break;
     1271 +                idx++;
     1272 +        } else {
     1273 +                token = expect(token, ')', "after context 2nd argument");
1169 1274          }
1170      -
1171      -        if (argc)
1172      -                add_ptr_list(&ctx->ctype.contexts, context);
1173      -
1174      -        token = expect(token, ')', "after context attribute");
     1275 +        context->in =  get_expression_value(args[idx++]);
     1276 +        context->out = get_expression_value(args[idx++]);
     1277 +        add_ptr_list(&ctx->ctype.contexts, context);
1175 1278          return token;
1176 1279  }
1177 1280  
1178 1281  static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx)
1179 1282  {
1180 1283          if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT)
1181 1284                  ctx->ctype.base_type->designated_init = 1;
1182 1285          else
1183 1286                  warning(token->pos, "attribute designated_init applied to non-structure type");
1184 1287          return token;
↓ open down ↓ 9 lines elided ↑ open up ↑
1194 1297          else
1195 1298                  warning(token->pos, "attribute __transparent_union__ applied to non-union type");
1196 1299          return token;
1197 1300  }
1198 1301  
1199 1302  static struct token *recover_unknown_attribute(struct token *token)
1200 1303  {
1201 1304          struct expression *expr = NULL;
1202 1305  
1203 1306          if (Wunknown_attribute)
1204      -                warning(token->pos, "attribute '%s': unknown attribute", show_ident(token->ident));
     1307 +                warning(token->pos, "unknown attribute '%s'", show_ident(token->ident));
1205 1308          token = token->next;
1206 1309          if (match_op(token, '('))
1207 1310                  token = parens_expression(token, &expr, "in attribute");
1208 1311          return token;
1209 1312  }
1210 1313  
1211 1314  static struct token *attribute_specifier(struct token *token, struct decl_state *ctx)
1212 1315  {
1213 1316          token = expect(token, '(', "after attribute");
1214 1317          token = expect(token, '(', "after attribute");
↓ open down ↓ 38 lines elided ↑ open up ↑
1253 1356  static unsigned long storage_modifiers(struct decl_state *ctx)
1254 1357  {
1255 1358          static unsigned long mod[SMax] =
1256 1359          {
1257 1360                  [SAuto] = MOD_AUTO,
1258 1361                  [SExtern] = MOD_EXTERN,
1259 1362                  [SStatic] = MOD_STATIC,
1260 1363                  [SRegister] = MOD_REGISTER
1261 1364          };
1262 1365          return mod[ctx->storage_class] | (ctx->is_inline ? MOD_INLINE : 0)
1263      -                | (ctx->is_tls ? MOD_TLS : 0);
     1366 +                | (ctx->is_tls ? MOD_TLS : 0)
     1367 +                | (ctx->is_ext_visible ? MOD_EXT_VISIBLE : 0);
1264 1368  }
1265 1369  
1266 1370  static void set_storage_class(struct position *pos, struct decl_state *ctx, int class)
1267 1371  {
1268 1372          /* __thread can be used alone, or with extern or static */
1269 1373          if (ctx->is_tls && (class != SStatic && class != SExtern)) {
1270 1374                  sparse_error(*pos, "__thread can only be used alone, or with "
1271 1375                                  "extern or static");
1272 1376                  return;
1273 1377          }
↓ open down ↓ 110 lines elided ↑ open up ↑
1384 1488          apply_qualifier(&next->pos, &ctx->ctype, MOD_CONST);
1385 1489          return next;
1386 1490  }
1387 1491  
1388 1492  static struct token *volatile_qualifier(struct token *next, struct decl_state *ctx)
1389 1493  {
1390 1494          apply_qualifier(&next->pos, &ctx->ctype, MOD_VOLATILE);
1391 1495          return next;
1392 1496  }
1393 1497  
     1498 +static struct token *restrict_qualifier(struct token *next, struct decl_state *ctx)
     1499 +{
     1500 +        apply_qualifier(&next->pos, &ctx->ctype, MOD_RESTRICT);
     1501 +        return next;
     1502 +}
     1503 +
     1504 +static struct token *atomic_qualifier(struct token *next, struct decl_state *ctx)
     1505 +{
     1506 +        apply_qualifier(&next->pos, &ctx->ctype, MOD_ATOMIC);
     1507 +        return next;
     1508 +}
     1509 +
1394 1510  static void apply_ctype(struct position pos, struct ctype *thistype, struct ctype *ctype)
1395 1511  {
1396 1512          unsigned long mod = thistype->modifiers;
1397 1513  
1398 1514          if (mod)
1399 1515                  apply_qualifier(&pos, ctype, mod);
1400 1516  
1401 1517          /* Context */
1402 1518          concat_ptr_list((struct ptr_list *)thistype->contexts,
1403 1519                          (struct ptr_list **)&ctype->contexts);
↓ open down ↓ 294 lines elided ↑ open up ↑
1698 1814                          warning(token->pos,
1699 1815                                  "identifier list not in definition");
1700 1816                  return K_R;
1701 1817          }
1702 1818  
1703 1819          if (token_type(next) != TOKEN_SPECIAL)
1704 1820                  return Bad_Func;
1705 1821  
1706 1822          if (next->special == ')') {
1707 1823                  /* don't complain about those */
1708      -                if (!n || match_op(next->next, ';'))
1709 1824                  if (!n || match_op(next->next, ';') || match_op(next->next, ','))
1710 1825                          return Empty;
1711 1826                  if (Wstrict_prototypes)
1712 1827                          warning(next->pos,
1713 1828                                  "non-ANSI function declaration of function '%s'",
1714 1829                                  show_ident(*n));
1715 1830                  return Empty;
1716 1831          }
1717 1832  
1718 1833          if (next->special == SPECIAL_ELLIPSIS) {
↓ open down ↓ 55 lines elided ↑ open up ↑
1774 1889  static struct token *pointer(struct token *token, struct decl_state *ctx)
1775 1890  {
1776 1891          while (match_op(token,'*')) {
1777 1892                  struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR);
1778 1893                  ptr->ctype.modifiers = ctx->ctype.modifiers;
1779 1894                  ptr->ctype.base_type = ctx->ctype.base_type;
1780 1895                  ptr->ctype.as = ctx->ctype.as;
1781 1896                  ptr->ctype.contexts = ctx->ctype.contexts;
1782 1897                  ctx->ctype.modifiers = 0;
1783 1898                  ctx->ctype.base_type = ptr;
1784      -                ctx->ctype.as = 0;
     1899 +                ctx->ctype.as = NULL;
1785 1900                  ctx->ctype.contexts = NULL;
1786 1901                  ctx->ctype.alignment = 0;
1787 1902  
1788 1903                  token = handle_qualifiers(token->next, ctx);
1789 1904                  ctx->ctype.base_type->endpos = token->pos;
1790 1905          }
1791 1906          return token;
1792 1907  }
1793 1908  
1794 1909  static struct token *declarator(struct token *token, struct decl_state *ctx)
↓ open down ↓ 162 lines elided ↑ open up ↑
1957 2072          if (match_ident(token, &_Pragma_ident))
1958 2073                  return parse_underscore_Pragma(token);
1959 2074  
1960 2075          token = parse_expression(token, tree);
1961 2076          return expect(token, ';', "at end of statement");
1962 2077  }
1963 2078  
1964 2079  static struct token *parse_asm_operands(struct token *token, struct statement *stmt,
1965 2080          struct expression_list **inout)
1966 2081  {
1967      -        struct expression *expr;
1968      -
1969 2082          /* Allow empty operands */
1970 2083          if (match_op(token->next, ':') || match_op(token->next, ')'))
1971 2084                  return token->next;
1972 2085          do {
1973      -                struct ident *ident = NULL;
     2086 +                struct expression *op = alloc_expression(token->pos, EXPR_ASM_OPERAND);
1974 2087                  if (match_op(token->next, '[') &&
1975 2088                      token_type(token->next->next) == TOKEN_IDENT &&
1976 2089                      match_op(token->next->next->next, ']')) {
1977      -                        ident = token->next->next->ident;
     2090 +                        op->name = token->next->next->ident;
1978 2091                          token = token->next->next->next;
1979 2092                  }
1980      -                add_expression(inout, (struct expression *)ident); /* UGGLEE!!! */
1981      -                token = primary_expression(token->next, &expr);
1982      -                add_expression(inout, expr);
1983      -                token = parens_expression(token, &expr, "in asm parameter");
1984      -                add_expression(inout, expr);
     2093 +                token = primary_expression(token->next, &op->constraint);
     2094 +                token = parens_expression(token, &op->expr, "in asm parameter");
     2095 +                add_expression(inout, op);
1985 2096          } while (match_op(token, ','));
1986 2097          return token;
1987 2098  }
1988 2099  
1989 2100  static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt,
1990 2101          struct expression_list **clobbers)
1991 2102  {
1992 2103          struct expression *expr;
1993 2104  
1994 2105          do {
↓ open down ↓ 15 lines elided ↑ open up ↑
2010 2121                          return token;
2011 2122                  label = label_symbol(token);
2012 2123                  add_symbol(labels, label);
2013 2124                  token = token->next;
2014 2125          } while (match_op(token, ','));
2015 2126          return token;
2016 2127  }
2017 2128  
2018 2129  static struct token *parse_asm_statement(struct token *token, struct statement *stmt)
2019 2130  {
2020      -        int is_goto = 0;
     2131 +        unsigned long mods = 0;
2021 2132  
2022 2133          token = token->next;
2023 2134          stmt->type = STMT_ASM;
2024      -        if (match_idents(token, &__volatile___ident, &__volatile_ident, &volatile_ident, NULL)) {
     2135 +        while (token_type(token) == TOKEN_IDENT) {
     2136 +                struct symbol *s = lookup_keyword(token->ident, NS_TYPEDEF);
     2137 +                if (s && s->op  && s->op->asm_modifier)
     2138 +                        s->op->asm_modifier(token, &mods);
     2139 +                else if (token->ident == &goto_ident)
     2140 +                        asm_modifier(token, &mods, MOD_ASM_GOTO);
2025 2141                  token = token->next;
2026 2142          }
2027      -        if (token_type(token) == TOKEN_IDENT && token->ident == &goto_ident) {
2028      -                is_goto = 1;
2029      -                token = token->next;
2030      -        }
2031 2143          token = expect(token, '(', "after asm");
2032 2144          token = parse_expression(token, &stmt->asm_string);
2033 2145          if (match_op(token, ':'))
2034 2146                  token = parse_asm_operands(token, stmt, &stmt->asm_outputs);
2035 2147          if (match_op(token, ':'))
2036 2148                  token = parse_asm_operands(token, stmt, &stmt->asm_inputs);
2037 2149          if (match_op(token, ':'))
2038 2150                  token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers);
2039      -        if (is_goto && match_op(token, ':'))
     2151 +        if (match_op(token, ':') && (mods & MOD_ASM_GOTO))
2040 2152                  token = parse_asm_labels(token, stmt, &stmt->asm_labels);
2041 2153          token = expect(token, ')', "after asm");
2042 2154          return expect(token, ';', "at end of asm-statement");
2043 2155  }
2044 2156  
2045 2157  static struct token *parse_asm_declarator(struct token *token, struct decl_state *ctx)
2046 2158  {
2047 2159          struct expression *expr;
2048 2160          token = expect(token, '(', "after asm");
2049 2161          token = parse_expression(token->next, &expr);
↓ open down ↓ 72 lines elided ↑ open up ↑
2122 2234  }
2123 2235  
2124 2236  static struct statement *start_function(struct symbol *sym)
2125 2237  {
2126 2238          struct symbol *ret;
2127 2239          struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND);
2128 2240  
2129 2241          start_function_scope(sym->pos);
2130 2242          ret = alloc_symbol(sym->pos, SYM_NODE);
2131 2243          ret->ctype = sym->ctype.base_type->ctype;
2132      -        ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_CONST | MOD_VOLATILE | MOD_TLS | MOD_INLINE | MOD_ADDRESSABLE | MOD_NOCAST | MOD_NODEREF | MOD_ACCESSED | MOD_TOPLEVEL);
     2244 +        ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_QUALIFIER | MOD_TLS | MOD_ACCESS | MOD_NOCAST | MOD_NODEREF);
2133 2245          ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER);
2134 2246          bind_symbol(ret, &return_ident, NS_ITERATOR);
2135 2247          stmt->ret = ret;
2136 2248          fn_local_symbol(ret);
2137 2249  
2138 2250          // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__
2139 2251          current_fn = sym;
2140 2252  
2141 2253          return stmt;
2142 2254  }
↓ open down ↓ 222 lines elided ↑ open up ↑
2365 2477                  token = token->next;
2366 2478          } else {
2367 2479                  sparse_error(token->pos, "Expected identifier or goto expression");
2368 2480          }
2369 2481          return expect(token, ';', "at end of statement");
2370 2482  }
2371 2483  
2372 2484  static struct token *parse_context_statement(struct token *token, struct statement *stmt)
2373 2485  {
2374 2486          stmt->type = STMT_CONTEXT;
2375      -        token = parse_expression(token->next, &stmt->expression);
2376      -        if (stmt->expression->type == EXPR_PREOP
2377      -            && stmt->expression->op == '('
2378      -            && stmt->expression->unop->type == EXPR_COMMA) {
2379      -                struct expression *expr;
2380      -                expr = stmt->expression->unop;
2381      -                stmt->context = expr->left;
2382      -                stmt->expression = expr->right;
     2487 +        token = token->next;
     2488 +        token = expect(token, '(', "after __context__ statement");
     2489 +        token = assignment_expression(token, &stmt->expression);
     2490 +        if (!stmt->expression)
     2491 +                unexpected(token, "expression expected after '('");
     2492 +        if (match_op(token, ',')) {
     2493 +                token = token->next;
     2494 +                stmt->context = stmt->expression;
     2495 +                token = assignment_expression(token, &stmt->expression);
     2496 +                if (!stmt->expression)
     2497 +                        unexpected(token, "expression expected after ','");
2383 2498          }
     2499 +        token = expect(token, ')', "at end of __context__ statement");
2384 2500          return expect(token, ';', "at end of statement");
2385 2501  }
2386 2502  
2387 2503  static struct token *parse_range_statement(struct token *token, struct statement *stmt)
2388 2504  {
2389 2505          stmt->type = STMT_RANGE;
2390      -        token = assignment_expression(token->next, &stmt->range_expression);
     2506 +        token = token->next;
     2507 +        token = expect(token, '(', "after __range__ statement");
     2508 +        token = assignment_expression(token, &stmt->range_expression);
2391 2509          token = expect(token, ',', "after range expression");
2392 2510          token = assignment_expression(token, &stmt->range_low);
2393 2511          token = expect(token, ',', "after low range");
2394 2512          token = assignment_expression(token, &stmt->range_high);
     2513 +        token = expect(token, ')', "after range statement");
2395 2514          return expect(token, ';', "after range statement");
2396 2515  }
2397 2516  
2398 2517  static struct token *statement(struct token *token, struct statement **tree)
2399 2518  {
2400 2519          struct statement *stmt = alloc_statement(token->pos, STMT_NONE);
2401 2520  
2402 2521          *tree = stmt;
2403 2522          if (token_type(token) == TOKEN_IDENT) {
2404 2523                  struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD);
2405 2524                  if (s && s->op->statement)
2406 2525                          return s->op->statement(token, stmt);
2407 2526  
2408 2527                  if (match_op(token->next, ':')) {
2409 2528                          struct symbol *s = label_symbol(token);
     2529 +                        token = skip_attributes(token->next->next);
     2530 +                        if (s->stmt) {
     2531 +                                sparse_error(stmt->pos, "label '%s' redefined", show_ident(s->ident));
     2532 +                                // skip the label to avoid multiple definitions
     2533 +                                return statement(token, tree);
     2534 +                        }
2410 2535                          stmt->type = STMT_LABEL;
2411 2536                          stmt->label_identifier = s;
2412      -                        if (s->stmt)
2413      -                                sparse_error(stmt->pos, "label '%s' redefined", show_ident(token->ident));
2414 2537                          s->stmt = stmt;
2415      -                        token = skip_attributes(token->next->next);
2416 2538                          return statement(token, &stmt->label_statement);
2417 2539                  }
2418 2540          }
2419 2541  
2420 2542          if (match_op(token, '{')) {
2421 2543                  stmt->type = STMT_COMPOUND;
2422 2544                  start_symbol_scope(stmt->pos);
2423 2545                  token = compound_statement(token->next, stmt);
2424 2546                  end_symbol_scope();
2425 2547                  
↓ open down ↓ 264 lines elided ↑ open up ↑
2690 2812          if (decl->ctype.modifiers & MOD_INLINE) {
2691 2813                  function_symbol_list = &decl->inline_symbol_list;
2692 2814                  p = &base_type->inline_stmt;
2693 2815          } else {
2694 2816                  function_symbol_list = &decl->symbol_list;
2695 2817                  p = &base_type->stmt;
2696 2818          }
2697 2819          function_computed_target_list = NULL;
2698 2820          function_computed_goto_list = NULL;
2699 2821  
2700      -        if (decl->ctype.modifiers & MOD_EXTERN &&
2701      -                !(decl->ctype.modifiers & MOD_INLINE) &&
2702      -                Wexternal_function_has_definition)
2703      -                warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
2704      -
     2822 +        if ((decl->ctype.modifiers & (MOD_EXTERN|MOD_INLINE)) == MOD_EXTERN) {
     2823 +                if (Wexternal_function_has_definition)
     2824 +                        warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident));
     2825 +        }
2705 2826          if (!(decl->ctype.modifiers & MOD_STATIC))
2706 2827                  decl->ctype.modifiers |= MOD_EXTERN;
2707 2828  
2708 2829          stmt = start_function(decl);
2709 2830  
2710 2831          *p = stmt;
2711 2832          FOR_EACH_PTR (base_type->arguments, arg) {
2712 2833                  declare_argument(arg, base_type);
2713 2834          } END_FOR_EACH_PTR(arg);
2714 2835  
↓ open down ↓ 55 lines elided ↑ open up ↑
2770 2891  
2771 2892                  /* This is quadratic in the number of arguments. We _really_ don't care */
2772 2893                  FOR_EACH_PTR(argtypes, type) {
2773 2894                          if (type->ident == arg->ident)
2774 2895                                  goto match;
2775 2896                  } END_FOR_EACH_PTR(type);
2776 2897                  if (Wimplicit_int) {
2777 2898                          sparse_error(arg->pos, "missing type declaration for parameter '%s'",
2778 2899                                  show_ident(arg->ident));
2779 2900                  }
2780      -                continue;
     2901 +                type = alloc_symbol(arg->pos, SYM_NODE);
     2902 +                type->ident = arg->ident;
     2903 +                type->ctype.base_type = &int_ctype;
2781 2904  match:
2782 2905                  type->used = 1;
2783 2906                  /* "char" and "short" promote to "int" */
2784 2907                  promote_k_r_types(type);
2785 2908  
2786 2909                  arg->ctype = type->ctype;
2787 2910          } END_FOR_EACH_PTR(arg);
2788 2911  
2789 2912          FOR_EACH_PTR(argtypes, arg) {
2790 2913                  if (!arg->used)
↓ open down ↓ 152 lines elided ↑ open up ↑
2943 3066  
2944 3067                          if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) {
2945 3068                                  add_symbol(list, decl);
2946 3069                                  fn_local_symbol(decl);
2947 3070                          }
2948 3071                  }
2949 3072                  check_declaration(decl);
2950 3073                  if (decl->same_symbol) {
2951 3074                          decl->definition = decl->same_symbol->definition;
2952 3075                          decl->op = decl->same_symbol->op;
     3076 +                        if (is_typedef) {
     3077 +                                // TODO: handle -std=c89 --pedantic
     3078 +                                check_duplicates(decl);
     3079 +                        }
2953 3080                  }
2954 3081  
2955 3082                  if (!match_op(token, ','))
2956 3083                          break;
2957 3084  
2958 3085                  token = token->next;
2959 3086                  ident = NULL;
2960 3087                  decl = alloc_symbol(token->pos, SYM_NODE);
2961 3088                  ctx.ctype = saved;
2962 3089                  token = handle_attributes(token, &ctx, KW_ATTRIBUTE);
↓ open down ↓ 25 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX