Chris Lattner | eb8a28f | 2006-08-10 18:43:39 +0000 | [diff] [blame] | 1 | //===--- Declaration.cpp - Declaration Parsing ----------------------------===// |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Declarations.h" |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | using namespace clang; |
| 18 | |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | // C99 6.7: Declarations. |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 23 | /// ParseTypeName |
| 24 | /// type-name: [C99 6.7.6] |
| 25 | /// specifier-qualifier-list abstract-declarator[opt] |
| 26 | void Parser::ParseTypeName() { |
| 27 | // Parse the common declaration-specifiers piece. |
| 28 | DeclSpec DS; |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 29 | ParseSpecifierQualifierList(DS); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 30 | |
| 31 | // Parse the abstract-declarator, if present. |
| 32 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 33 | ParseDeclarator(DeclaratorInfo); |
| 34 | } |
| 35 | |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame^] | 36 | /// ParseAttributes - Parse a non-empty attributes list. |
| 37 | /// |
| 38 | /// [GNU] attributes: |
| 39 | /// attribute |
| 40 | /// attributes attribute |
| 41 | /// |
| 42 | /// [GNU] attribute: |
| 43 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 44 | /// |
| 45 | /// [GNU] attribute-list: |
| 46 | /// attrib |
| 47 | /// attribute_list ',' attrib |
| 48 | /// |
| 49 | /// [GNU] attrib: |
| 50 | /// empty |
| 51 | /// any-word |
| 52 | /// any-word '(' identifier ')' |
| 53 | /// any-word '(' identifier ',' nonempty-expr-list ')' |
| 54 | /// any-word '(' expr-list ')' |
| 55 | /// |
| 56 | void Parser::ParseAttributes() { |
| 57 | assert(Tok.getKind() == tok::kw___attribute && "Not an attribute list!"); |
| 58 | ConsumeToken(); |
| 59 | |
| 60 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 61 | "attribute")) |
| 62 | return; |
| 63 | |
| 64 | // TODO: Parse the attributes. |
| 65 | SkipUntil(tok::r_paren, false); |
| 66 | } |
| 67 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 69 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 70 | /// declaration-specifiers, some number of declarators, and a semicolon. |
| 71 | /// 'Context' should be a Declarator::TheContext value. |
| 72 | void Parser::ParseDeclaration(unsigned Context) { |
| 73 | // Parse the common declaration-specifiers piece. |
| 74 | DeclSpec DS; |
| 75 | ParseDeclarationSpecifiers(DS); |
| 76 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 77 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 78 | // declaration-specifiers init-declarator-list[opt] ';' |
| 79 | if (Tok.getKind() == tok::semi) { |
| 80 | // TODO: emit error on 'int;' or 'const enum foo;'. |
| 81 | // if (!DS.isMissingDeclaratorOk()) Diag(...); |
| 82 | |
| 83 | ConsumeToken(); |
| 84 | return; |
| 85 | } |
| 86 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 87 | Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); |
| 88 | ParseDeclarator(DeclaratorInfo); |
| 89 | |
| 90 | ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
| 91 | } |
| 92 | |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 93 | /// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after |
| 94 | /// parsing 'declaration-specifiers declarator'. This method is split out this |
| 95 | /// way to handle the ambiguity between top-level function-definitions and |
| 96 | /// declarations. |
| 97 | /// |
| 98 | /// declaration: [C99 6.7] |
| 99 | /// declaration-specifiers init-declarator-list[opt] ';' [TODO] |
| 100 | /// [!C99] init-declarator-list ';' [TODO] |
| 101 | /// [OMP] threadprivate-directive [TODO] |
| 102 | /// |
| 103 | /// init-declarator-list: [C99 6.7] |
| 104 | /// init-declarator |
| 105 | /// init-declarator-list ',' init-declarator |
| 106 | /// init-declarator: [C99 6.7] |
| 107 | /// declarator |
| 108 | /// declarator '=' initializer |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 109 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 110 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 111 | /// |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 112 | void Parser::ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) { |
| 113 | // At this point, we know that it is not a function definition. Parse the |
| 114 | // rest of the init-declarator-list. |
| 115 | while (1) { |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 116 | // If a simple-asm-expr is present, parse it. |
| 117 | if (Tok.getKind() == tok::kw_asm) |
| 118 | ParseSimpleAsm(); |
| 119 | |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame^] | 120 | // If attributes are present, parse them. |
| 121 | if (Tok.getKind() == tok::kw___attribute) |
| 122 | ParseAttributes(); |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 124 | // Parse declarator '=' initializer. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 125 | ExprResult Init; |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 126 | if (Tok.getKind() == tok::equal) { |
| 127 | ConsumeToken(); |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 128 | Init = ParseInitializer(); |
| 129 | if (!Init.isInvalid) { |
| 130 | SkipUntil(tok::semi); |
| 131 | return; |
| 132 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 135 | // Inform the current actions module that we just parsed a declarator. |
| 136 | Actions.ParseDeclarator(Tok.getLocation(), CurScope, D, Init.Val); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 137 | |
| 138 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 139 | // error, bail out. |
| 140 | if (Tok.getKind() != tok::comma) |
| 141 | break; |
| 142 | |
| 143 | // Consume the comma. |
| 144 | ConsumeToken(); |
| 145 | |
| 146 | // Parse the next declarator. |
| 147 | D.clear(); |
| 148 | ParseDeclarator(D); |
| 149 | } |
| 150 | |
| 151 | if (Tok.getKind() == tok::semi) { |
| 152 | ConsumeToken(); |
| 153 | } else { |
| 154 | Diag(Tok, diag::err_parse_error); |
| 155 | // Skip to end of block or statement |
| 156 | SkipUntil(tok::r_brace, true); |
| 157 | if (Tok.getKind() == tok::semi) |
| 158 | ConsumeToken(); |
| 159 | } |
| 160 | } |
| 161 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 162 | /// ParseSpecifierQualifierList |
| 163 | /// specifier-qualifier-list: |
| 164 | /// type-specifier specifier-qualifier-list[opt] |
| 165 | /// type-qualifier specifier-qualifier-list[opt] |
| 166 | /// |
| 167 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { |
| 168 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 169 | /// parse declaration-specifiers and complain about extra stuff. |
| 170 | SourceLocation Loc = Tok.getLocation(); |
| 171 | ParseDeclarationSpecifiers(DS); |
| 172 | |
| 173 | // Validate declspec for type-name. |
| 174 | unsigned Specs = DS.getParsedSpecifiers(); |
| 175 | if (Specs == DeclSpec::PQ_None) |
| 176 | Diag(Tok, diag::err_typename_requires_specqual); |
| 177 | |
| 178 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 179 | Diag(Loc, diag::err_typename_invalid_storageclass); |
| 180 | // Remove storage class. |
| 181 | DS.StorageClassSpec = DeclSpec::SCS_unspecified; |
| 182 | DS.SCS_thread_specified = false; |
| 183 | } |
| 184 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
| 185 | Diag(Loc, diag::err_typename_invalid_functionspec); |
| 186 | DS.FS_inline_specified = false; |
| 187 | } |
| 188 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 189 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 190 | /// ParseDeclarationSpecifiers |
| 191 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 192 | /// storage-class-specifier declaration-specifiers[opt] |
| 193 | /// type-specifier declaration-specifiers[opt] |
| 194 | /// type-qualifier declaration-specifiers[opt] |
| 195 | /// [C99] function-specifier declaration-specifiers[opt] |
| 196 | /// [GNU] attributes declaration-specifiers[opt] [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 197 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 198 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 199 | /// 'typedef' |
| 200 | /// 'extern' |
| 201 | /// 'static' |
| 202 | /// 'auto' |
| 203 | /// 'register' |
| 204 | /// [GNU] '__thread' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 205 | /// type-specifier: [C99 6.7.2] |
| 206 | /// 'void' |
| 207 | /// 'char' |
| 208 | /// 'short' |
| 209 | /// 'int' |
| 210 | /// 'long' |
| 211 | /// 'float' |
| 212 | /// 'double' |
| 213 | /// 'signed' |
| 214 | /// 'unsigned' |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 215 | /// struct-or-union-specifier |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 216 | /// enum-specifier |
Chris Lattner | 3b4fdda3 | 2006-08-14 00:45:39 +0000 | [diff] [blame] | 217 | /// typedef-name |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 218 | /// [C99] '_Bool' |
| 219 | /// [C99] '_Complex' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 220 | /// [C99] '_Imaginary' // Removed in TC2? |
| 221 | /// [GNU] '_Decimal32' |
| 222 | /// [GNU] '_Decimal64' |
| 223 | /// [GNU] '_Decimal128' |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 224 | /// [GNU] typeof-specifier [TODO] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 225 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 226 | /// [OBJC] typedef-name objc-protocol-refs [TODO] |
| 227 | /// [OBJC] objc-protocol-refs [TODO] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 228 | /// type-qualifier: |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 229 | /// 'const' |
| 230 | /// 'volatile' |
| 231 | /// [C99] 'restrict' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 232 | /// function-specifier: [C99 6.7.4] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 233 | /// [C99] 'inline' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 234 | /// |
| 235 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) { |
| 236 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 237 | while (1) { |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 238 | int isInvalid = false; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 239 | const char *PrevSpec = 0; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 240 | switch (Tok.getKind()) { |
Chris Lattner | 3b4fdda3 | 2006-08-14 00:45:39 +0000 | [diff] [blame] | 241 | // typedef-name |
| 242 | case tok::identifier: |
| 243 | // This identifier can only be a typedef name if we haven't already seen |
| 244 | // a typename. This avoids the virtual method call on things like |
| 245 | // 'int foo'. |
| 246 | if (DS.TypeSpecType == DeclSpec::TST_unspecified && |
| 247 | Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) { |
| 248 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, PrevSpec); |
| 249 | break; |
| 250 | } |
| 251 | // FALL THROUGH. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 252 | default: |
| 253 | // If this is not a declaration specifier token, we're done reading decl |
| 254 | // specifiers. First verify that DeclSpec's are consistent. |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 255 | DS.Finish(StartLoc, Diags, getLang()); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 256 | return; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 257 | |
| 258 | // storage-class-specifier |
| 259 | case tok::kw_typedef: |
| 260 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec); |
| 261 | break; |
| 262 | case tok::kw_extern: |
| 263 | if (DS.SCS_thread_specified) |
| 264 | Diag(Tok, diag::ext_thread_before, "extern"); |
| 265 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec); |
| 266 | break; |
| 267 | case tok::kw_static: |
| 268 | if (DS.SCS_thread_specified) |
| 269 | Diag(Tok, diag::ext_thread_before, "static"); |
| 270 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec); |
| 271 | break; |
| 272 | case tok::kw_auto: |
| 273 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec); |
| 274 | break; |
| 275 | case tok::kw_register: |
| 276 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec); |
| 277 | break; |
| 278 | case tok::kw___thread: |
| 279 | if (DS.SCS_thread_specified) |
| 280 | isInvalid = 2, PrevSpec = "__thread"; |
| 281 | else |
| 282 | DS.SCS_thread_specified = true; |
| 283 | break; |
| 284 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 285 | // type-specifiers |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 286 | case tok::kw_short: |
| 287 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec); |
| 288 | break; |
| 289 | case tok::kw_long: |
| 290 | if (DS.TypeSpecWidth != DeclSpec::TSW_long) { |
| 291 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec); |
| 292 | } else { |
| 293 | DS.TypeSpecWidth = DeclSpec::TSW_unspecified; |
| 294 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec); |
| 295 | } |
| 296 | break; |
| 297 | case tok::kw_signed: |
| 298 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec); |
| 299 | break; |
| 300 | case tok::kw_unsigned: |
| 301 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec); |
| 302 | break; |
| 303 | case tok::kw__Complex: |
| 304 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec); |
| 305 | break; |
| 306 | case tok::kw__Imaginary: |
| 307 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec); |
| 308 | break; |
| 309 | case tok::kw_void: |
| 310 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec); |
| 311 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 312 | case tok::kw_char: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 313 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec); |
| 314 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 315 | case tok::kw_int: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 316 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec); |
| 317 | break; |
| 318 | case tok::kw_float: |
| 319 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec); |
| 320 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 321 | case tok::kw_double: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 322 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec); |
| 323 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 324 | case tok::kw__Bool: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 325 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec); |
| 326 | break; |
| 327 | case tok::kw__Decimal32: |
| 328 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec); |
| 329 | break; |
| 330 | case tok::kw__Decimal64: |
| 331 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec); |
| 332 | break; |
| 333 | case tok::kw__Decimal128: |
| 334 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 335 | break; |
| 336 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 337 | case tok::kw_struct: |
| 338 | case tok::kw_union: |
| 339 | ParseStructUnionSpecifier(DS); |
| 340 | continue; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 341 | case tok::kw_enum: |
| 342 | ParseEnumSpecifier(DS); |
| 343 | continue; |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 344 | |
| 345 | // type-qualifier |
| 346 | case tok::kw_const: |
| 347 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2; |
| 348 | break; |
| 349 | case tok::kw_volatile: |
| 350 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2; |
| 351 | break; |
| 352 | case tok::kw_restrict: |
| 353 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2; |
| 354 | break; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 355 | |
| 356 | // function-specifier |
| 357 | case tok::kw_inline: |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 358 | // 'inline inline' is ok. |
| 359 | DS.FS_inline_specified = true; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 360 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 361 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 362 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 363 | if (isInvalid) { |
| 364 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 365 | if (isInvalid == 1) // Error. |
| 366 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 367 | else // extwarn. |
| 368 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 369 | } |
| 370 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 374 | |
| 375 | /// ParseStructUnionSpecifier |
| 376 | /// struct-or-union-specifier: [C99 6.7.2.1] |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 377 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 378 | /// struct-or-union identifier |
| 379 | /// struct-or-union: |
| 380 | /// 'struct' |
| 381 | /// 'union' |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 382 | /// struct-contents: |
| 383 | /// struct-declaration-list |
| 384 | /// [EXT] empty |
| 385 | /// [GNU] "struct-declaration-list" without terminatoring ';' [TODO] |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 386 | /// struct-declaration-list: |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 387 | /// struct-declaration |
| 388 | /// struct-declaration-list struct-declaration |
| 389 | /// [OBC] '@' 'defs' '(' class-name ')' [TODO] |
| 390 | /// struct-declaration: |
| 391 | /// specifier-qualifier-list struct-declarator-list ';' |
| 392 | /// [GNU] __extension__ struct-declaration [TODO] |
| 393 | /// [GNU] specifier-qualifier-list ';' [TODO] |
| 394 | /// struct-declarator-list: |
| 395 | /// struct-declarator |
| 396 | /// struct-declarator-list ',' struct-declarator |
| 397 | /// struct-declarator: |
| 398 | /// declarator |
| 399 | /// declarator[opt] ':' constant-expression |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 400 | /// |
| 401 | void Parser::ParseStructUnionSpecifier(DeclSpec &DS) { |
| 402 | assert((Tok.getKind() == tok::kw_struct || |
| 403 | Tok.getKind() == tok::kw_union) && "Not a struct/union specifier"); |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 404 | SourceLocation Start = Tok.getLocation(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 405 | bool isUnion = Tok.getKind() == tok::kw_union; |
| 406 | ConsumeToken(); |
| 407 | |
| 408 | // Must have either 'struct name' or 'struct {...}'. |
| 409 | if (Tok.getKind() != tok::identifier && |
| 410 | Tok.getKind() != tok::l_brace) { |
| 411 | Diag(Tok, diag::err_expected_ident_lbrace); |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | if (Tok.getKind() == tok::identifier) |
| 416 | ConsumeToken(); |
| 417 | |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 418 | if (Tok.getKind() == tok::l_brace) { |
| 419 | SourceLocation LBraceLoc = Tok.getLocation(); |
| 420 | ConsumeBrace(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 421 | |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 422 | if (Tok.getKind() == tok::r_brace) |
| 423 | Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct"); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 424 | |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 425 | while (Tok.getKind() != tok::r_brace && |
| 426 | Tok.getKind() != tok::eof) { |
| 427 | // Each iteration of this loop reads one struct-declaration. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 428 | |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 429 | // Parse the common specifier-qualifiers-list piece. |
| 430 | DeclSpec DS; |
| 431 | SourceLocation SpecQualLoc = Tok.getLocation(); |
| 432 | ParseSpecifierQualifierList(DS); |
| 433 | // TODO: Does specifier-qualifier list correctly check that *something* is |
| 434 | // specified? |
| 435 | |
| 436 | Declarator DeclaratorInfo(DS, Declarator::MemberContext); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 437 | |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 438 | // If there are no declarators, issue a warning. |
| 439 | if (Tok.getKind() == tok::semi) { |
| 440 | Diag(SpecQualLoc, diag::w_no_declarators); |
| 441 | } else { |
| 442 | // Read struct-declarators until we find the semicolon. |
| 443 | while (1) { |
| 444 | /// struct-declarator: declarator |
| 445 | /// struct-declarator: declarator[opt] ':' constant-expression |
| 446 | if (Tok.getKind() != tok::colon) |
| 447 | ParseDeclarator(DeclaratorInfo); |
| 448 | |
| 449 | if (Tok.getKind() == tok::colon) { |
| 450 | ConsumeToken(); |
| 451 | ExprResult Res = ParseConstantExpression(); |
| 452 | if (Res.isInvalid) { |
| 453 | SkipUntil(tok::semi, true, true); |
| 454 | } else { |
| 455 | // Process it. |
| 456 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 457 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 458 | |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 459 | // TODO: install declarator. |
| 460 | |
| 461 | // If we don't have a comma, it is either the end of the list (a ';') |
| 462 | // or an error, bail out. |
| 463 | if (Tok.getKind() != tok::comma) |
| 464 | break; |
| 465 | |
| 466 | // Consume the comma. |
| 467 | ConsumeToken(); |
| 468 | |
| 469 | // Parse the next declarator. |
| 470 | DeclaratorInfo.clear(); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | if (Tok.getKind() == tok::semi) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 475 | ConsumeToken(); |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 476 | } else { |
| 477 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 478 | // Skip to end of block or statement |
| 479 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 482 | |
Chris Lattner | b8bbad7 | 2006-08-13 22:21:02 +0000 | [diff] [blame] | 483 | MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{",diag::err_expected_rbrace); |
| 484 | } |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 485 | |
| 486 | const char *PrevSpec = 0; |
| 487 | if (DS.SetTypeSpecType(isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct, |
| 488 | PrevSpec)) |
| 489 | Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 493 | /// ParseEnumSpecifier |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 494 | /// enum-specifier: [C99 6.7.2.2] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 495 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
| 496 | /// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}' |
Chris Lattner | 476c3ad | 2006-08-13 22:09:58 +0000 | [diff] [blame] | 497 | /// [GNU] 'enum' identifier[opt] '{' enumerator-list '}' attributes [TODO] |
| 498 | /// [GNU] 'enum' identifier[opt] '{' enumerator-list ',' '}' attributes [TODO] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 499 | /// 'enum' identifier |
| 500 | /// enumerator-list: |
| 501 | /// enumerator |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 502 | /// enumerator-list ',' enumerator |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 503 | /// enumerator: |
| 504 | /// enumeration-constant |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 505 | /// enumeration-constant '=' constant-expression |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 506 | /// enumeration-constant: |
| 507 | /// identifier |
| 508 | /// |
| 509 | void Parser::ParseEnumSpecifier(DeclSpec &DS) { |
| 510 | assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier"); |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 511 | SourceLocation Start = Tok.getLocation(); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 512 | ConsumeToken(); |
| 513 | |
| 514 | // Must have either 'enum name' or 'enum {...}'. |
| 515 | if (Tok.getKind() != tok::identifier && |
| 516 | Tok.getKind() != tok::l_brace) { |
| 517 | Diag(Tok, diag::err_expected_ident_lbrace); |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | if (Tok.getKind() == tok::identifier) |
| 522 | ConsumeToken(); |
| 523 | |
Chris Lattner | 0fb8b36 | 2006-08-14 01:30:12 +0000 | [diff] [blame] | 524 | if (Tok.getKind() == tok::l_brace) { |
| 525 | SourceLocation LBraceLoc = Tok.getLocation(); |
| 526 | ConsumeBrace(); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 527 | |
Chris Lattner | 0fb8b36 | 2006-08-14 01:30:12 +0000 | [diff] [blame] | 528 | if (Tok.getKind() == tok::r_brace) |
| 529 | Diag(Tok, diag::ext_empty_struct_union_enum, "enum"); |
| 530 | |
| 531 | // Parse the enumerator-list. |
| 532 | while (Tok.getKind() == tok::identifier) { |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 533 | ConsumeToken(); |
Chris Lattner | 0fb8b36 | 2006-08-14 01:30:12 +0000 | [diff] [blame] | 534 | |
| 535 | if (Tok.getKind() == tok::equal) { |
| 536 | ConsumeToken(); |
| 537 | ExprResult Res = ParseConstantExpression(); |
| 538 | if (Res.isInvalid) SkipUntil(tok::comma, true, false); |
| 539 | } |
| 540 | |
| 541 | if (Tok.getKind() != tok::comma) |
| 542 | break; |
| 543 | SourceLocation CommaLoc = Tok.getLocation(); |
| 544 | ConsumeToken(); |
| 545 | |
| 546 | if (Tok.getKind() != tok::identifier && !getLang().C99) |
| 547 | Diag(CommaLoc, diag::ext_c99_enumerator_list_comma); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Chris Lattner | 0fb8b36 | 2006-08-14 01:30:12 +0000 | [diff] [blame] | 550 | // Eat the }. |
| 551 | MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{", |
| 552 | diag::err_expected_rbrace); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 553 | } |
| 554 | // TODO: semantic analysis on the declspec for enums. |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 555 | |
| 556 | |
| 557 | const char *PrevSpec = 0; |
| 558 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, PrevSpec)) |
| 559 | Diag(Start, diag::err_invalid_decl_spec_combination, PrevSpec); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 563 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
| 564 | /// start of a specifier-qualifier-list. |
| 565 | bool Parser::isTypeSpecifierQualifier() const { |
| 566 | switch (Tok.getKind()) { |
| 567 | default: return false; |
| 568 | // type-specifiers |
| 569 | case tok::kw_short: |
| 570 | case tok::kw_long: |
| 571 | case tok::kw_signed: |
| 572 | case tok::kw_unsigned: |
| 573 | case tok::kw__Complex: |
| 574 | case tok::kw__Imaginary: |
| 575 | case tok::kw_void: |
| 576 | case tok::kw_char: |
| 577 | case tok::kw_int: |
| 578 | case tok::kw_float: |
| 579 | case tok::kw_double: |
| 580 | case tok::kw__Bool: |
| 581 | case tok::kw__Decimal32: |
| 582 | case tok::kw__Decimal64: |
| 583 | case tok::kw__Decimal128: |
| 584 | |
| 585 | // struct-or-union-specifier |
| 586 | case tok::kw_struct: |
| 587 | case tok::kw_union: |
| 588 | // enum-specifier |
| 589 | case tok::kw_enum: |
| 590 | |
| 591 | // type-qualifier |
| 592 | case tok::kw_const: |
| 593 | case tok::kw_volatile: |
| 594 | case tok::kw_restrict: |
| 595 | return true; |
| 596 | |
| 597 | // typedef-name |
| 598 | case tok::identifier: |
Chris Lattner | 3b4fdda3 | 2006-08-14 00:45:39 +0000 | [diff] [blame] | 599 | return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 600 | |
| 601 | // TODO: Attributes. |
| 602 | } |
| 603 | } |
| 604 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 605 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 606 | /// declaration specifier. |
| 607 | bool Parser::isDeclarationSpecifier() const { |
| 608 | switch (Tok.getKind()) { |
| 609 | default: return false; |
| 610 | // storage-class-specifier |
| 611 | case tok::kw_typedef: |
| 612 | case tok::kw_extern: |
| 613 | case tok::kw_static: |
| 614 | case tok::kw_auto: |
| 615 | case tok::kw_register: |
| 616 | case tok::kw___thread: |
| 617 | |
| 618 | // type-specifiers |
| 619 | case tok::kw_short: |
| 620 | case tok::kw_long: |
| 621 | case tok::kw_signed: |
| 622 | case tok::kw_unsigned: |
| 623 | case tok::kw__Complex: |
| 624 | case tok::kw__Imaginary: |
| 625 | case tok::kw_void: |
| 626 | case tok::kw_char: |
| 627 | case tok::kw_int: |
| 628 | case tok::kw_float: |
| 629 | case tok::kw_double: |
| 630 | case tok::kw__Bool: |
| 631 | case tok::kw__Decimal32: |
| 632 | case tok::kw__Decimal64: |
| 633 | case tok::kw__Decimal128: |
| 634 | |
| 635 | // struct-or-union-specifier |
| 636 | case tok::kw_struct: |
| 637 | case tok::kw_union: |
| 638 | // enum-specifier |
| 639 | case tok::kw_enum: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 640 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 641 | // type-qualifier |
| 642 | case tok::kw_const: |
| 643 | case tok::kw_volatile: |
| 644 | case tok::kw_restrict: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 645 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 646 | // function-specifier |
| 647 | case tok::kw_inline: |
| 648 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 649 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 650 | // typedef-name |
| 651 | case tok::identifier: |
Chris Lattner | 3b4fdda3 | 2006-08-14 00:45:39 +0000 | [diff] [blame] | 652 | return Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 653 | // TODO: Attributes. |
| 654 | } |
| 655 | } |
| 656 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 657 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 658 | /// ParseTypeQualifierListOpt |
| 659 | /// type-qualifier-list: [C99 6.7.5] |
| 660 | /// type-qualifier |
| 661 | /// [GNU] attributes [TODO] |
| 662 | /// type-qualifier-list type-qualifier |
| 663 | /// [GNU] type-qualifier-list attributes [TODO] |
| 664 | /// |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 665 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) { |
| 666 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 667 | while (1) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 668 | int isInvalid = false; |
| 669 | const char *PrevSpec = 0; |
| 670 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 671 | switch (Tok.getKind()) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 672 | default: |
| 673 | // If this is not a declaration specifier token, we're done reading decl |
| 674 | // specifiers. First verify that DeclSpec's are consistent. |
| 675 | DS.Finish(StartLoc, Diags, getLang()); |
| 676 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 677 | // TODO: attributes. |
| 678 | case tok::kw_const: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 679 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2; |
| 680 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 681 | case tok::kw_volatile: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 682 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2; |
| 683 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 684 | case tok::kw_restrict: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 685 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 686 | break; |
| 687 | } |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 688 | |
| 689 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 690 | if (isInvalid) { |
| 691 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 692 | if (isInvalid == 1) // Error. |
| 693 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 694 | else // extwarn. |
| 695 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 696 | } |
| 697 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 701 | |
| 702 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 703 | /// |
| 704 | void Parser::ParseDeclarator(Declarator &D) { |
| 705 | /// This implements the 'declarator' production in the C grammar, then checks |
| 706 | /// for well-formedness and issues diagnostics. |
| 707 | ParseDeclaratorInternal(D); |
| 708 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 709 | // TODO: validate D. |
Chris Lattner | bf320c8 | 2006-08-07 05:05:30 +0000 | [diff] [blame] | 710 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /// ParseDeclaratorInternal |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 714 | /// declarator: [C99 6.7.5] |
| 715 | /// pointer[opt] direct-declarator |
| 716 | /// |
| 717 | /// pointer: [C99 6.7.5] |
| 718 | /// '*' type-qualifier-list[opt] |
| 719 | /// '*' type-qualifier-list[opt] pointer |
| 720 | /// |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 721 | void Parser::ParseDeclaratorInternal(Declarator &D) { |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 722 | if (Tok.getKind() != tok::star) |
| 723 | return ParseDirectDeclarator(D); |
| 724 | |
| 725 | // Otherwise, '*' -> pointer. |
| 726 | SourceLocation Loc = Tok.getLocation(); |
| 727 | ConsumeToken(); // Eat the *. |
| 728 | DeclSpec DS; |
| 729 | ParseTypeQualifierListOpt(DS); |
| 730 | |
| 731 | // Recursively parse the declarator. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 732 | ParseDeclaratorInternal(D); |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 733 | |
| 734 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 735 | D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc)); |
| 736 | } |
| 737 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 738 | |
| 739 | /// ParseDirectDeclarator |
| 740 | /// direct-declarator: [C99 6.7.5] |
| 741 | /// identifier |
| 742 | /// '(' declarator ')' |
| 743 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 744 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 745 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 746 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 747 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 748 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 749 | /// direct-declarator '(' parameter-type-list ')' |
| 750 | /// direct-declarator '(' identifier-list[opt] ')' |
| 751 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 752 | /// parameter-type-list[opt] ')' |
| 753 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 754 | void Parser::ParseDirectDeclarator(Declarator &D) { |
| 755 | // Parse the first direct-declarator seen. |
| 756 | if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) { |
| 757 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 758 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 759 | ConsumeToken(); |
| 760 | } else if (Tok.getKind() == tok::l_paren) { |
| 761 | // direct-declarator: '(' declarator ')' |
| 762 | // direct-declarator: '(' attributes declarator ')' [TODO] |
| 763 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 764 | ParseParenDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 765 | } else if (D.mayOmitIdentifier()) { |
| 766 | // This could be something simple like "int" (in which case the declarator |
| 767 | // portion is empty), if an abstract-declarator is allowed. |
| 768 | D.SetIdentifier(0, Tok.getLocation()); |
| 769 | } else { |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 770 | // Expected identifier or '('. |
| 771 | Diag(Tok, diag::err_expected_ident_lparen); |
| 772 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | assert(D.isPastIdentifier() && |
| 776 | "Haven't past the location of the identifier yet?"); |
| 777 | |
| 778 | while (1) { |
| 779 | if (Tok.getKind() == tok::l_paren) { |
| 780 | ParseParenDeclarator(D); |
| 781 | } else if (Tok.getKind() == tok::l_square) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 782 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 783 | } else { |
| 784 | break; |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This may |
| 790 | /// either be before the identifier (in which case these are just grouping |
| 791 | /// parens for precedence) or it may be after the identifier, in which case |
| 792 | /// these are function arguments. |
| 793 | /// |
| 794 | /// This method also handles this portion of the grammar: |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 795 | /// parameter-type-list: [C99 6.7.5] |
| 796 | /// parameter-list |
| 797 | /// parameter-list ',' '...' |
| 798 | /// |
| 799 | /// parameter-list: [C99 6.7.5] |
| 800 | /// parameter-declaration |
| 801 | /// parameter-list ',' parameter-declaration |
| 802 | /// |
| 803 | /// parameter-declaration: [C99 6.7.5] |
| 804 | /// declaration-specifiers declarator |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 805 | /// [GNU] declaration-specifiers declarator attributes [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 806 | /// declaration-specifiers abstract-declarator[opt] |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 807 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 808 | /// |
| 809 | /// identifier-list: [C99 6.7.5] |
| 810 | /// identifier |
| 811 | /// identifier-list ',' identifier |
| 812 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 813 | void Parser::ParseParenDeclarator(Declarator &D) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 814 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 815 | ConsumeParen(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 816 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 817 | // If we haven't past the identifier yet (or where the identifier would be |
| 818 | // stored, if this is an abstract declarator), then this is probably just |
| 819 | // grouping parens. |
| 820 | if (!D.isPastIdentifier()) { |
| 821 | // Okay, this is probably a grouping paren. However, if this could be an |
| 822 | // abstract-declarator, then this could also be the start of function |
| 823 | // arguments (consider 'void()'). |
| 824 | bool isGrouping; |
| 825 | |
| 826 | if (!D.mayOmitIdentifier()) { |
| 827 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 828 | // paren, because we haven't seen the identifier yet. |
| 829 | isGrouping = true; |
| 830 | } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function. |
| 831 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 832 | |
| 833 | isGrouping = false; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 834 | } else { |
Chris Lattner | a350722 | 2006-08-07 00:33:37 +0000 | [diff] [blame] | 835 | // Otherwise, this is a grouping paren, e.g. 'int (*X)'. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 836 | isGrouping = true; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 837 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 838 | |
| 839 | // If this is a grouping paren, handle: |
| 840 | // direct-declarator: '(' declarator ')' |
| 841 | // direct-declarator: '(' attributes declarator ')' [TODO] |
| 842 | if (isGrouping) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 843 | ParseDeclaratorInternal(D); |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 844 | // Match the ')'. |
| 845 | MatchRHSPunctuation(tok::r_paren, StartLoc, "(", |
| 846 | diag::err_expected_rparen); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 847 | return; |
| 848 | } |
| 849 | |
| 850 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
Chris Lattner | a350722 | 2006-08-07 00:33:37 +0000 | [diff] [blame] | 851 | // argument list. Recognize that this declarator will never have an |
| 852 | // identifier (and remember where it would have been), then fall through to |
| 853 | // the handling of argument lists. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 854 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 857 | // Okay, this is the parameter list of a function definition, or it is an |
| 858 | // identifier list of a K&R-style function. |
| 859 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 860 | // TODO: enter function-declaration scope, limiting any declarators for |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 861 | // arguments to the function scope. |
| 862 | // NOTE: better to only create a scope if not '()' |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 863 | bool IsVariadic; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 864 | bool HasPrototype; |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 865 | bool IsEmpty = false; |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 866 | bool ErrorEmitted = false; |
| 867 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 868 | if (Tok.getKind() == tok::r_paren) { |
| 869 | // int() -> no prototype, no '...'. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 870 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 871 | HasPrototype = false; |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 872 | IsEmpty = true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 873 | } else if (Tok.getKind() == tok::identifier && |
Chris Lattner | 8a3e918 | 2006-08-14 15:44:00 +0000 | [diff] [blame] | 874 | !Actions.isTypedefName(*Tok.getIdentifierInfo(), CurScope)) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 875 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 876 | // normal declarators, not for abstract-declarators. |
| 877 | assert(D.isPastIdentifier() && "Identifier (if present) must be passed!"); |
| 878 | |
| 879 | // If there was no identifier specified, either we are in an |
| 880 | // abstract-declarator, or we are in a parameter declarator which was found |
| 881 | // to be abstract. In abstract-declarators, identifier lists are not valid, |
| 882 | // diagnose this. |
| 883 | if (!D.getIdentifier()) |
| 884 | Diag(Tok, diag::ext_ident_list_in_param); |
| 885 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 886 | // TODO: Remember token. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 887 | ConsumeToken(); |
| 888 | while (Tok.getKind() == tok::comma) { |
| 889 | // Eat the comma. |
| 890 | ConsumeToken(); |
| 891 | |
Chris Lattner | 0be454e | 2006-08-12 19:30:51 +0000 | [diff] [blame] | 892 | if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) { |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 893 | ErrorEmitted = true; |
| 894 | break; |
| 895 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 898 | // K&R 'prototype'. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 899 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 900 | HasPrototype = false; |
| 901 | } else { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 902 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 903 | bool ReadArg = false; |
| 904 | // Finally, a normal, non-empty parameter type list. |
| 905 | while (1) { |
| 906 | if (Tok.getKind() == tok::ellipsis) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 907 | IsVariadic = true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 908 | |
| 909 | // Check to see if this is "void(...)" which is not allowed. |
| 910 | if (!ReadArg) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 911 | // Otherwise, parse parameter type list. If it starts with an |
| 912 | // ellipsis, diagnose the malformed function. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 913 | Diag(Tok, diag::err_ellipsis_first_arg); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 914 | IsVariadic = false; // Treat this like 'void()'. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | // Consume the ellipsis. |
| 918 | ConsumeToken(); |
| 919 | break; |
| 920 | } |
| 921 | |
| 922 | ReadArg = true; |
| 923 | |
| 924 | // Parse the declaration-specifiers. |
| 925 | DeclSpec DS; |
| 926 | ParseDeclarationSpecifiers(DS); |
| 927 | |
| 928 | // Parse the declarator. This is "PrototypeContext", because we must |
| 929 | // accept either 'declarator' or 'abstract-declarator' here. |
| 930 | Declarator DeclaratorInfo(DS, Declarator::PrototypeContext); |
| 931 | ParseDeclarator(DeclaratorInfo); |
| 932 | |
| 933 | // TODO: do something with the declarator, if it is valid. |
| 934 | |
| 935 | // If the next token is a comma, consume it and keep reading arguments. |
| 936 | if (Tok.getKind() != tok::comma) break; |
| 937 | |
| 938 | // Consume the comma. |
| 939 | ConsumeToken(); |
| 940 | } |
| 941 | |
| 942 | HasPrototype = true; |
| 943 | } |
| 944 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 945 | // TODO: pop the scope. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 946 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 947 | // TODO: capture argument info. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 948 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 949 | // Remember that we parsed a function type, and remember the attributes. |
| 950 | D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic, |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 951 | IsEmpty, StartLoc)); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 952 | |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 953 | |
| 954 | // If we have the closing ')', eat it and we're done. |
| 955 | if (Tok.getKind() == tok::r_paren) { |
| 956 | ConsumeParen(); |
| 957 | } else { |
| 958 | // If an error happened earlier parsing something else in the proto, don't |
| 959 | // issue another error. |
| 960 | if (!ErrorEmitted) |
| 961 | Diag(Tok, diag::err_expected_rparen); |
| 962 | SkipUntil(tok::r_paren); |
| 963 | } |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 964 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 965 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 966 | |
| 967 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 968 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 969 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 970 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 971 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 972 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 973 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 974 | ConsumeBracket(); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 975 | |
| 976 | // If valid, this location is the position where we read the 'static' keyword. |
| 977 | SourceLocation StaticLoc; |
| 978 | if (Tok.getKind() == tok::kw_static) { |
| 979 | StaticLoc = Tok.getLocation(); |
| 980 | ConsumeToken(); |
| 981 | } |
| 982 | |
| 983 | // If there is a type-qualifier-list, read it now. |
| 984 | DeclSpec DS; |
| 985 | ParseTypeQualifierListOpt(DS); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 986 | |
| 987 | // If we haven't already read 'static', check to see if there is one after the |
| 988 | // type-qualifier-list. |
| 989 | if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) { |
| 990 | StaticLoc = Tok.getLocation(); |
| 991 | ConsumeToken(); |
| 992 | } |
| 993 | |
| 994 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 995 | bool isStar = false; |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 996 | ExprResult NumElements(false); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 997 | if (Tok.getKind() == tok::star) { |
| 998 | // Remember the '*' token, in case we have to un-get it. |
| 999 | LexerToken StarTok = Tok; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1000 | ConsumeToken(); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 1001 | |
| 1002 | // Check that the ']' token is present to avoid incorrectly parsing |
| 1003 | // expressions starting with '*' as [*]. |
| 1004 | if (Tok.getKind() == tok::r_square) { |
| 1005 | if (StaticLoc.isValid()) |
| 1006 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
| 1007 | StaticLoc = SourceLocation(); // Drop the static. |
| 1008 | isStar = true; |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 1009 | } else { |
| 1010 | // Otherwise, the * must have been some expression (such as '*ptr') that |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 1011 | // started an assignment-expr. We already consumed the token, but now we |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 1012 | // need to reparse it. This handles cases like 'X[*p + 4]' |
| 1013 | NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 1014 | } |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 1015 | } else if (Tok.getKind() != tok::r_square) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1016 | // Parse the assignment-expression now. |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 1017 | NumElements = ParseAssignmentExpression(); |
| 1018 | } |
| 1019 | |
| 1020 | // If there was an error parsing the assignment-expression, recover. |
| 1021 | if (NumElements.isInvalid) { |
| 1022 | // If the expression was invalid, skip it. |
| 1023 | SkipUntil(tok::r_square); |
| 1024 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 1027 | MatchRHSPunctuation(tok::r_square, StartLoc, "[", diag::err_expected_rsquare); |
| 1028 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1029 | // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if |
| 1030 | // it was not a constant expression. |
| 1031 | if (!getLang().C99) { |
| 1032 | // TODO: check C90 array constant exprness. |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1033 | if (isStar || StaticLoc.isValid() || |
| 1034 | 0/*TODO: NumElts is not a C90 constantexpr */) |
Chris Lattner | 8a39edc | 2006-08-06 18:33:32 +0000 | [diff] [blame] | 1035 | Diag(StartLoc, diag::ext_c99_array_usage); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1036 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 1037 | |
| 1038 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1039 | D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers, |
| 1040 | StaticLoc.isValid(), isStar, |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 1041 | NumElements.Val, StartLoc)); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |