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; |
| 29 | SourceLocation Loc = Tok.getLocation(); |
| 30 | ParseDeclarationSpecifiers(DS); |
| 31 | |
| 32 | // Validate declspec for type-name. |
| 33 | unsigned Specs = DS.getParsedSpecifiers(); |
| 34 | if (Specs == DeclSpec::PQ_None) |
| 35 | Diag(Tok, diag::err_typename_requires_specqual); |
| 36 | |
| 37 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 38 | Diag(Loc, diag::err_typename_invalid_storageclass); |
| 39 | // Remove storage class. |
| 40 | DS.StorageClassSpec = DeclSpec::SCS_unspecified; |
| 41 | DS.SCS_thread_specified = false; |
| 42 | } |
| 43 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
| 44 | Diag(Loc, diag::err_typename_invalid_functionspec); |
| 45 | DS.FS_inline_specified = false; |
| 46 | } |
| 47 | |
| 48 | // Parse the abstract-declarator, if present. |
| 49 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 50 | ParseDeclarator(DeclaratorInfo); |
| 51 | } |
| 52 | |
| 53 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 54 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 55 | /// declaration-specifiers, some number of declarators, and a semicolon. |
| 56 | /// 'Context' should be a Declarator::TheContext value. |
| 57 | void Parser::ParseDeclaration(unsigned Context) { |
| 58 | // Parse the common declaration-specifiers piece. |
| 59 | DeclSpec DS; |
| 60 | ParseDeclarationSpecifiers(DS); |
| 61 | |
| 62 | Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); |
| 63 | ParseDeclarator(DeclaratorInfo); |
| 64 | |
| 65 | ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
| 66 | } |
| 67 | |
| 68 | void Parser::ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) { |
| 69 | // At this point, we know that it is not a function definition. Parse the |
| 70 | // rest of the init-declarator-list. |
| 71 | while (1) { |
| 72 | // must be: decl-spec[opt] declarator init-declarator-list |
| 73 | // Parse declarator '=' initializer. |
| 74 | if (Tok.getKind() == tok::equal) { |
| 75 | ConsumeToken(); |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 76 | ParseInitializer(); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 79 | // TODO: install declarator. |
| 80 | |
| 81 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 82 | // error, bail out. |
| 83 | if (Tok.getKind() != tok::comma) |
| 84 | break; |
| 85 | |
| 86 | // Consume the comma. |
| 87 | ConsumeToken(); |
| 88 | |
| 89 | // Parse the next declarator. |
| 90 | D.clear(); |
| 91 | ParseDeclarator(D); |
| 92 | } |
| 93 | |
| 94 | if (Tok.getKind() == tok::semi) { |
| 95 | ConsumeToken(); |
| 96 | } else { |
| 97 | Diag(Tok, diag::err_parse_error); |
| 98 | // Skip to end of block or statement |
| 99 | SkipUntil(tok::r_brace, true); |
| 100 | if (Tok.getKind() == tok::semi) |
| 101 | ConsumeToken(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 106 | /// ParseDeclarationSpecifiers |
| 107 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 108 | /// storage-class-specifier declaration-specifiers [opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 109 | /// type-specifier declaration-specifiers [opt] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 110 | /// type-qualifier declaration-specifiers [opt] |
| 111 | /// [C99] function-specifier declaration-specifiers [opt] |
| 112 | /// [GNU] attributes declaration-specifiers [opt] [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 113 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 114 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 115 | /// 'typedef' |
| 116 | /// 'extern' |
| 117 | /// 'static' |
| 118 | /// 'auto' |
| 119 | /// 'register' |
| 120 | /// [GNU] '__thread' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 121 | /// type-specifier: [C99 6.7.2] |
| 122 | /// 'void' |
| 123 | /// 'char' |
| 124 | /// 'short' |
| 125 | /// 'int' |
| 126 | /// 'long' |
| 127 | /// 'float' |
| 128 | /// 'double' |
| 129 | /// 'signed' |
| 130 | /// 'unsigned' |
Chris Lattner | 8e90ef6 | 2006-08-05 03:30:45 +0000 | [diff] [blame] | 131 | /// struct-or-union-specifier [TODO] |
| 132 | /// enum-specifier [TODO] |
| 133 | /// typedef-name [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 134 | /// [C99] '_Bool' |
| 135 | /// [C99] '_Complex' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 136 | /// [C99] '_Imaginary' // Removed in TC2? |
| 137 | /// [GNU] '_Decimal32' |
| 138 | /// [GNU] '_Decimal64' |
| 139 | /// [GNU] '_Decimal128' |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 140 | /// [GNU] typeof-specifier [TODO] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 141 | /// [OBJC] class-name objc-protocol-refs [opt] [TODO] |
| 142 | /// [OBJC] typedef-name objc-protocol-refs [TODO] |
| 143 | /// [OBJC] objc-protocol-refs [TODO] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 144 | /// type-qualifier: |
| 145 | /// const |
| 146 | /// volatile |
| 147 | /// [C99] restrict |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 148 | /// function-specifier: [C99 6.7.4] |
| 149 | /// [C99] inline |
| 150 | /// |
| 151 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) { |
| 152 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 153 | while (1) { |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 154 | int isInvalid = false; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 155 | const char *PrevSpec = 0; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 156 | switch (Tok.getKind()) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 157 | default: |
| 158 | // If this is not a declaration specifier token, we're done reading decl |
| 159 | // specifiers. First verify that DeclSpec's are consistent. |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 160 | DS.Finish(StartLoc, Diags, getLang()); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 161 | return; |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 162 | // FIXME: Handle struct/union/enum tags. |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 163 | |
| 164 | // storage-class-specifier |
| 165 | case tok::kw_typedef: |
| 166 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec); |
| 167 | break; |
| 168 | case tok::kw_extern: |
| 169 | if (DS.SCS_thread_specified) |
| 170 | Diag(Tok, diag::ext_thread_before, "extern"); |
| 171 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec); |
| 172 | break; |
| 173 | case tok::kw_static: |
| 174 | if (DS.SCS_thread_specified) |
| 175 | Diag(Tok, diag::ext_thread_before, "static"); |
| 176 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec); |
| 177 | break; |
| 178 | case tok::kw_auto: |
| 179 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec); |
| 180 | break; |
| 181 | case tok::kw_register: |
| 182 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec); |
| 183 | break; |
| 184 | case tok::kw___thread: |
| 185 | if (DS.SCS_thread_specified) |
| 186 | isInvalid = 2, PrevSpec = "__thread"; |
| 187 | else |
| 188 | DS.SCS_thread_specified = true; |
| 189 | break; |
| 190 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 191 | // type-specifiers |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 192 | case tok::kw_short: |
| 193 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec); |
| 194 | break; |
| 195 | case tok::kw_long: |
| 196 | if (DS.TypeSpecWidth != DeclSpec::TSW_long) { |
| 197 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec); |
| 198 | } else { |
| 199 | DS.TypeSpecWidth = DeclSpec::TSW_unspecified; |
| 200 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec); |
| 201 | } |
| 202 | break; |
| 203 | case tok::kw_signed: |
| 204 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec); |
| 205 | break; |
| 206 | case tok::kw_unsigned: |
| 207 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec); |
| 208 | break; |
| 209 | case tok::kw__Complex: |
| 210 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec); |
| 211 | break; |
| 212 | case tok::kw__Imaginary: |
| 213 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec); |
| 214 | break; |
| 215 | case tok::kw_void: |
| 216 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec); |
| 217 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 218 | case tok::kw_char: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 219 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec); |
| 220 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 221 | case tok::kw_int: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 222 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec); |
| 223 | break; |
| 224 | case tok::kw_float: |
| 225 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec); |
| 226 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 227 | case tok::kw_double: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 228 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec); |
| 229 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 230 | case tok::kw__Bool: |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 231 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec); |
| 232 | break; |
| 233 | case tok::kw__Decimal32: |
| 234 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec); |
| 235 | break; |
| 236 | case tok::kw__Decimal64: |
| 237 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec); |
| 238 | break; |
| 239 | case tok::kw__Decimal128: |
| 240 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 241 | break; |
| 242 | |
| 243 | //case tok::kw_struct: |
| 244 | //case tok::kw_union: |
| 245 | //case tok::kw_enum: |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 246 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 247 | //case tok::identifier: |
| 248 | // TODO: handle typedef names. |
| 249 | |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 250 | // type-qualifier |
| 251 | case tok::kw_const: |
| 252 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2; |
| 253 | break; |
| 254 | case tok::kw_volatile: |
| 255 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2; |
| 256 | break; |
| 257 | case tok::kw_restrict: |
| 258 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2; |
| 259 | break; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 260 | |
| 261 | // function-specifier |
| 262 | case tok::kw_inline: |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 263 | // 'inline inline' is ok. |
| 264 | DS.FS_inline_specified = true; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 265 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 266 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 267 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 268 | if (isInvalid) { |
| 269 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 270 | if (isInvalid == 1) // Error. |
| 271 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 272 | else // extwarn. |
| 273 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 274 | } |
| 275 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 279 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
| 280 | /// start of a specifier-qualifier-list. |
| 281 | bool Parser::isTypeSpecifierQualifier() const { |
| 282 | switch (Tok.getKind()) { |
| 283 | default: return false; |
| 284 | // type-specifiers |
| 285 | case tok::kw_short: |
| 286 | case tok::kw_long: |
| 287 | case tok::kw_signed: |
| 288 | case tok::kw_unsigned: |
| 289 | case tok::kw__Complex: |
| 290 | case tok::kw__Imaginary: |
| 291 | case tok::kw_void: |
| 292 | case tok::kw_char: |
| 293 | case tok::kw_int: |
| 294 | case tok::kw_float: |
| 295 | case tok::kw_double: |
| 296 | case tok::kw__Bool: |
| 297 | case tok::kw__Decimal32: |
| 298 | case tok::kw__Decimal64: |
| 299 | case tok::kw__Decimal128: |
| 300 | |
| 301 | // struct-or-union-specifier |
| 302 | case tok::kw_struct: |
| 303 | case tok::kw_union: |
| 304 | // enum-specifier |
| 305 | case tok::kw_enum: |
| 306 | |
| 307 | // type-qualifier |
| 308 | case tok::kw_const: |
| 309 | case tok::kw_volatile: |
| 310 | case tok::kw_restrict: |
| 311 | return true; |
| 312 | |
| 313 | // typedef-name |
| 314 | case tok::identifier: |
| 315 | // FIXME: if this is a typedef return true. |
| 316 | return false; |
| 317 | |
| 318 | // TODO: Attributes. |
| 319 | } |
| 320 | } |
| 321 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 322 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 323 | /// declaration specifier. |
| 324 | bool Parser::isDeclarationSpecifier() const { |
| 325 | switch (Tok.getKind()) { |
| 326 | default: return false; |
| 327 | // storage-class-specifier |
| 328 | case tok::kw_typedef: |
| 329 | case tok::kw_extern: |
| 330 | case tok::kw_static: |
| 331 | case tok::kw_auto: |
| 332 | case tok::kw_register: |
| 333 | case tok::kw___thread: |
| 334 | |
| 335 | // type-specifiers |
| 336 | case tok::kw_short: |
| 337 | case tok::kw_long: |
| 338 | case tok::kw_signed: |
| 339 | case tok::kw_unsigned: |
| 340 | case tok::kw__Complex: |
| 341 | case tok::kw__Imaginary: |
| 342 | case tok::kw_void: |
| 343 | case tok::kw_char: |
| 344 | case tok::kw_int: |
| 345 | case tok::kw_float: |
| 346 | case tok::kw_double: |
| 347 | case tok::kw__Bool: |
| 348 | case tok::kw__Decimal32: |
| 349 | case tok::kw__Decimal64: |
| 350 | case tok::kw__Decimal128: |
| 351 | |
| 352 | // struct-or-union-specifier |
| 353 | case tok::kw_struct: |
| 354 | case tok::kw_union: |
| 355 | // enum-specifier |
| 356 | case tok::kw_enum: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 357 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 358 | // type-qualifier |
| 359 | case tok::kw_const: |
| 360 | case tok::kw_volatile: |
| 361 | case tok::kw_restrict: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 362 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 363 | // function-specifier |
| 364 | case tok::kw_inline: |
| 365 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 366 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 367 | // typedef-name |
| 368 | case tok::identifier: |
| 369 | // FIXME: if this is a typedef return true. |
| 370 | return false; |
| 371 | // TODO: Attributes. |
| 372 | } |
| 373 | } |
| 374 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 375 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 376 | /// ParseTypeQualifierListOpt |
| 377 | /// type-qualifier-list: [C99 6.7.5] |
| 378 | /// type-qualifier |
| 379 | /// [GNU] attributes [TODO] |
| 380 | /// type-qualifier-list type-qualifier |
| 381 | /// [GNU] type-qualifier-list attributes [TODO] |
| 382 | /// |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 383 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) { |
| 384 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 385 | while (1) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 386 | int isInvalid = false; |
| 387 | const char *PrevSpec = 0; |
| 388 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 389 | switch (Tok.getKind()) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 390 | default: |
| 391 | // If this is not a declaration specifier token, we're done reading decl |
| 392 | // specifiers. First verify that DeclSpec's are consistent. |
| 393 | DS.Finish(StartLoc, Diags, getLang()); |
| 394 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 395 | // TODO: attributes. |
| 396 | case tok::kw_const: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 397 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2; |
| 398 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 399 | case tok::kw_volatile: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 400 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2; |
| 401 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 402 | case tok::kw_restrict: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 403 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 404 | break; |
| 405 | } |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 406 | |
| 407 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 408 | if (isInvalid) { |
| 409 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 410 | if (isInvalid == 1) // Error. |
| 411 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 412 | else // extwarn. |
| 413 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 414 | } |
| 415 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 419 | |
| 420 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 421 | /// |
| 422 | void Parser::ParseDeclarator(Declarator &D) { |
| 423 | /// This implements the 'declarator' production in the C grammar, then checks |
| 424 | /// for well-formedness and issues diagnostics. |
| 425 | ParseDeclaratorInternal(D); |
| 426 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 427 | // TODO: validate D. |
Chris Lattner | bf320c8 | 2006-08-07 05:05:30 +0000 | [diff] [blame] | 428 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /// ParseDeclaratorInternal |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 432 | /// declarator: [C99 6.7.5] |
| 433 | /// pointer[opt] direct-declarator |
| 434 | /// |
| 435 | /// pointer: [C99 6.7.5] |
| 436 | /// '*' type-qualifier-list[opt] |
| 437 | /// '*' type-qualifier-list[opt] pointer |
| 438 | /// |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 439 | void Parser::ParseDeclaratorInternal(Declarator &D) { |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 440 | if (Tok.getKind() != tok::star) |
| 441 | return ParseDirectDeclarator(D); |
| 442 | |
| 443 | // Otherwise, '*' -> pointer. |
| 444 | SourceLocation Loc = Tok.getLocation(); |
| 445 | ConsumeToken(); // Eat the *. |
| 446 | DeclSpec DS; |
| 447 | ParseTypeQualifierListOpt(DS); |
| 448 | |
| 449 | // Recursively parse the declarator. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 450 | ParseDeclaratorInternal(D); |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 451 | |
| 452 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 453 | D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc)); |
| 454 | } |
| 455 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 456 | |
| 457 | /// ParseDirectDeclarator |
| 458 | /// direct-declarator: [C99 6.7.5] |
| 459 | /// identifier |
| 460 | /// '(' declarator ')' |
| 461 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 462 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 463 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 464 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 465 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 466 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 467 | /// direct-declarator '(' parameter-type-list ')' |
| 468 | /// direct-declarator '(' identifier-list[opt] ')' |
| 469 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 470 | /// parameter-type-list[opt] ')' |
| 471 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 472 | void Parser::ParseDirectDeclarator(Declarator &D) { |
| 473 | // Parse the first direct-declarator seen. |
| 474 | if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) { |
| 475 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 476 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 477 | ConsumeToken(); |
| 478 | } else if (Tok.getKind() == tok::l_paren) { |
| 479 | // direct-declarator: '(' declarator ')' |
| 480 | // direct-declarator: '(' attributes declarator ')' [TODO] |
| 481 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 482 | ParseParenDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 483 | } else if (D.mayOmitIdentifier()) { |
| 484 | // This could be something simple like "int" (in which case the declarator |
| 485 | // portion is empty), if an abstract-declarator is allowed. |
| 486 | D.SetIdentifier(0, Tok.getLocation()); |
| 487 | } else { |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 488 | // Expected identifier or '('. |
| 489 | Diag(Tok, diag::err_expected_ident_lparen); |
| 490 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | assert(D.isPastIdentifier() && |
| 494 | "Haven't past the location of the identifier yet?"); |
| 495 | |
| 496 | while (1) { |
| 497 | if (Tok.getKind() == tok::l_paren) { |
| 498 | ParseParenDeclarator(D); |
| 499 | } else if (Tok.getKind() == tok::l_square) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 500 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 501 | } else { |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This may |
| 508 | /// either be before the identifier (in which case these are just grouping |
| 509 | /// parens for precedence) or it may be after the identifier, in which case |
| 510 | /// these are function arguments. |
| 511 | /// |
| 512 | /// This method also handles this portion of the grammar: |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 513 | /// parameter-type-list: [C99 6.7.5] |
| 514 | /// parameter-list |
| 515 | /// parameter-list ',' '...' |
| 516 | /// |
| 517 | /// parameter-list: [C99 6.7.5] |
| 518 | /// parameter-declaration |
| 519 | /// parameter-list ',' parameter-declaration |
| 520 | /// |
| 521 | /// parameter-declaration: [C99 6.7.5] |
| 522 | /// declaration-specifiers declarator |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 523 | /// [GNU] declaration-specifiers declarator attributes [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 524 | /// declaration-specifiers abstract-declarator[opt] |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 525 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 526 | /// |
| 527 | /// identifier-list: [C99 6.7.5] |
| 528 | /// identifier |
| 529 | /// identifier-list ',' identifier |
| 530 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 531 | void Parser::ParseParenDeclarator(Declarator &D) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 532 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 533 | ConsumeParen(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 534 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 535 | // If we haven't past the identifier yet (or where the identifier would be |
| 536 | // stored, if this is an abstract declarator), then this is probably just |
| 537 | // grouping parens. |
| 538 | if (!D.isPastIdentifier()) { |
| 539 | // Okay, this is probably a grouping paren. However, if this could be an |
| 540 | // abstract-declarator, then this could also be the start of function |
| 541 | // arguments (consider 'void()'). |
| 542 | bool isGrouping; |
| 543 | |
| 544 | if (!D.mayOmitIdentifier()) { |
| 545 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 546 | // paren, because we haven't seen the identifier yet. |
| 547 | isGrouping = true; |
| 548 | } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function. |
| 549 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 550 | |
| 551 | isGrouping = false; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 552 | } else { |
Chris Lattner | a350722 | 2006-08-07 00:33:37 +0000 | [diff] [blame] | 553 | // Otherwise, this is a grouping paren, e.g. 'int (*X)'. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 554 | isGrouping = true; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 555 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 556 | |
| 557 | // If this is a grouping paren, handle: |
| 558 | // direct-declarator: '(' declarator ')' |
| 559 | // direct-declarator: '(' attributes declarator ')' [TODO] |
| 560 | if (isGrouping) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 561 | ParseDeclaratorInternal(D); |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 562 | // Match the ')'. |
| 563 | MatchRHSPunctuation(tok::r_paren, StartLoc, "(", |
| 564 | diag::err_expected_rparen); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 565 | return; |
| 566 | } |
| 567 | |
| 568 | // 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] | 569 | // argument list. Recognize that this declarator will never have an |
| 570 | // identifier (and remember where it would have been), then fall through to |
| 571 | // the handling of argument lists. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 572 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 575 | // Okay, this is the parameter list of a function definition, or it is an |
| 576 | // identifier list of a K&R-style function. |
| 577 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 578 | // TODO: enter function-declaration scope, limiting any declarators for |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 579 | // arguments to the function scope. |
| 580 | // NOTE: better to only create a scope if not '()' |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 581 | bool IsVariadic; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 582 | bool HasPrototype; |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 583 | bool IsEmpty = false; |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 584 | bool ErrorEmitted = false; |
| 585 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 586 | if (Tok.getKind() == tok::r_paren) { |
| 587 | // int() -> no prototype, no '...'. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 588 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 589 | HasPrototype = false; |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 590 | IsEmpty = true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 591 | } else if (Tok.getKind() == tok::identifier && |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 592 | 1/*TODO: !isatypedefname(Tok.getIdentifierInfo())*/) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 593 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 594 | // normal declarators, not for abstract-declarators. |
| 595 | assert(D.isPastIdentifier() && "Identifier (if present) must be passed!"); |
| 596 | |
| 597 | // If there was no identifier specified, either we are in an |
| 598 | // abstract-declarator, or we are in a parameter declarator which was found |
| 599 | // to be abstract. In abstract-declarators, identifier lists are not valid, |
| 600 | // diagnose this. |
| 601 | if (!D.getIdentifier()) |
| 602 | Diag(Tok, diag::ext_ident_list_in_param); |
| 603 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 604 | // TODO: Remember token. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 605 | ConsumeToken(); |
| 606 | while (Tok.getKind() == tok::comma) { |
| 607 | // Eat the comma. |
| 608 | ConsumeToken(); |
| 609 | |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 610 | if (Tok.getKind() != tok::identifier) { |
| 611 | // If not identifier, diagnose the error. |
| 612 | Diag(Tok, diag::err_expected_ident); |
| 613 | ErrorEmitted = true; |
| 614 | break; |
| 615 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 616 | |
| 617 | // Eat the id. |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 618 | // TODO: remember it! |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 619 | ConsumeToken(); |
| 620 | } |
| 621 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 622 | // K&R 'prototype'. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 623 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 624 | HasPrototype = false; |
| 625 | } else { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 626 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 627 | bool ReadArg = false; |
| 628 | // Finally, a normal, non-empty parameter type list. |
| 629 | while (1) { |
| 630 | if (Tok.getKind() == tok::ellipsis) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 631 | IsVariadic = true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 632 | |
| 633 | // Check to see if this is "void(...)" which is not allowed. |
| 634 | if (!ReadArg) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 635 | // Otherwise, parse parameter type list. If it starts with an |
| 636 | // ellipsis, diagnose the malformed function. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 637 | Diag(Tok, diag::err_ellipsis_first_arg); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 638 | IsVariadic = false; // Treat this like 'void()'. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | // Consume the ellipsis. |
| 642 | ConsumeToken(); |
| 643 | break; |
| 644 | } |
| 645 | |
| 646 | ReadArg = true; |
| 647 | |
| 648 | // Parse the declaration-specifiers. |
| 649 | DeclSpec DS; |
| 650 | ParseDeclarationSpecifiers(DS); |
| 651 | |
| 652 | // Parse the declarator. This is "PrototypeContext", because we must |
| 653 | // accept either 'declarator' or 'abstract-declarator' here. |
| 654 | Declarator DeclaratorInfo(DS, Declarator::PrototypeContext); |
| 655 | ParseDeclarator(DeclaratorInfo); |
| 656 | |
| 657 | // TODO: do something with the declarator, if it is valid. |
| 658 | |
| 659 | // If the next token is a comma, consume it and keep reading arguments. |
| 660 | if (Tok.getKind() != tok::comma) break; |
| 661 | |
| 662 | // Consume the comma. |
| 663 | ConsumeToken(); |
| 664 | } |
| 665 | |
| 666 | HasPrototype = true; |
| 667 | } |
| 668 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 669 | // TODO: pop the scope. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 670 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 671 | // TODO: capture argument info. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 672 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 673 | // Remember that we parsed a function type, and remember the attributes. |
| 674 | D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic, |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 675 | IsEmpty, StartLoc)); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 676 | |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 677 | |
| 678 | // If we have the closing ')', eat it and we're done. |
| 679 | if (Tok.getKind() == tok::r_paren) { |
| 680 | ConsumeParen(); |
| 681 | } else { |
| 682 | // If an error happened earlier parsing something else in the proto, don't |
| 683 | // issue another error. |
| 684 | if (!ErrorEmitted) |
| 685 | Diag(Tok, diag::err_expected_rparen); |
| 686 | SkipUntil(tok::r_paren); |
| 687 | } |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 688 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 689 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 690 | |
| 691 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 692 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 693 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 694 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 695 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 696 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 697 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 698 | ConsumeBracket(); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 699 | |
| 700 | // If valid, this location is the position where we read the 'static' keyword. |
| 701 | SourceLocation StaticLoc; |
| 702 | if (Tok.getKind() == tok::kw_static) { |
| 703 | StaticLoc = Tok.getLocation(); |
| 704 | ConsumeToken(); |
| 705 | } |
| 706 | |
| 707 | // If there is a type-qualifier-list, read it now. |
| 708 | DeclSpec DS; |
| 709 | ParseTypeQualifierListOpt(DS); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 710 | |
| 711 | // If we haven't already read 'static', check to see if there is one after the |
| 712 | // type-qualifier-list. |
| 713 | if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) { |
| 714 | StaticLoc = Tok.getLocation(); |
| 715 | ConsumeToken(); |
| 716 | } |
| 717 | |
| 718 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 719 | bool isStar = false; |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame^] | 720 | ExprResult NumElements(false); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 721 | if (Tok.getKind() == tok::star) { |
| 722 | // Remember the '*' token, in case we have to un-get it. |
| 723 | LexerToken StarTok = Tok; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 724 | ConsumeToken(); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 725 | |
| 726 | // Check that the ']' token is present to avoid incorrectly parsing |
| 727 | // expressions starting with '*' as [*]. |
| 728 | if (Tok.getKind() == tok::r_square) { |
| 729 | if (StaticLoc.isValid()) |
| 730 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
| 731 | StaticLoc = SourceLocation(); // Drop the static. |
| 732 | isStar = true; |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 733 | } else { |
| 734 | // Otherwise, the * must have been some expression (such as '*ptr') that |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 735 | // started an assignment-expr. We already consumed the token, but now we |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame^] | 736 | // need to reparse it. This handles cases like 'X[*p + 4]' |
| 737 | NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 738 | } |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 739 | } else if (Tok.getKind() != tok::r_square) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 740 | // Parse the assignment-expression now. |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame^] | 741 | NumElements = ParseAssignmentExpression(); |
| 742 | } |
| 743 | |
| 744 | // If there was an error parsing the assignment-expression, recover. |
| 745 | if (NumElements.isInvalid) { |
| 746 | // If the expression was invalid, skip it. |
| 747 | SkipUntil(tok::r_square); |
| 748 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 751 | MatchRHSPunctuation(tok::r_square, StartLoc, "[", diag::err_expected_rsquare); |
| 752 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 753 | // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if |
| 754 | // it was not a constant expression. |
| 755 | if (!getLang().C99) { |
| 756 | // TODO: check C90 array constant exprness. |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame^] | 757 | if (isStar || StaticLoc.isValid() || 0/*FIXME: NumElts is constantexpr*/) |
Chris Lattner | 8a39edc | 2006-08-06 18:33:32 +0000 | [diff] [blame] | 758 | Diag(StartLoc, diag::ext_c99_array_usage); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 759 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 760 | |
| 761 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 762 | D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers, |
| 763 | StaticLoc.isValid(), isStar, |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame^] | 764 | NumElements.Val, StartLoc)); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 765 | } |
| 766 | |