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 | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame^] | 108 | /// storage-class-specifier declaration-specifiers[opt] |
| 109 | /// type-specifier declaration-specifiers[opt] |
| 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] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame^] | 132 | /// enum-specifier |
Chris Lattner | 8e90ef6 | 2006-08-05 03:30:45 +0000 | [diff] [blame] | 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 | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame^] | 141 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 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: |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame^] | 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] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame^] | 149 | /// [C99] 'inline' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 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 | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame^] | 162 | // FIXME: Handle struct/union 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: |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame^] | 245 | case tok::kw_enum: |
| 246 | ParseEnumSpecifier(DS); |
| 247 | continue; |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 248 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 249 | //case tok::identifier: |
| 250 | // TODO: handle typedef names. |
| 251 | |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 252 | // type-qualifier |
| 253 | case tok::kw_const: |
| 254 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2; |
| 255 | break; |
| 256 | case tok::kw_volatile: |
| 257 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2; |
| 258 | break; |
| 259 | case tok::kw_restrict: |
| 260 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2; |
| 261 | break; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 262 | |
| 263 | // function-specifier |
| 264 | case tok::kw_inline: |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 265 | // 'inline inline' is ok. |
| 266 | DS.FS_inline_specified = true; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 267 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 268 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 269 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 270 | if (isInvalid) { |
| 271 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 272 | if (isInvalid == 1) // Error. |
| 273 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 274 | else // extwarn. |
| 275 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 276 | } |
| 277 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame^] | 281 | /// ParseEnumSpecifier |
| 282 | /// enum-specifier: |
| 283 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
| 284 | /// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}' |
| 285 | /// 'enum' identifier |
| 286 | /// enumerator-list: |
| 287 | /// enumerator |
| 288 | /// enumerator-list , enumerator |
| 289 | /// enumerator: |
| 290 | /// enumeration-constant |
| 291 | /// enumeration-constant = constant-expression |
| 292 | /// enumeration-constant: |
| 293 | /// identifier |
| 294 | /// |
| 295 | void Parser::ParseEnumSpecifier(DeclSpec &DS) { |
| 296 | assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier"); |
| 297 | ConsumeToken(); |
| 298 | |
| 299 | // Must have either 'enum name' or 'enum {...}'. |
| 300 | if (Tok.getKind() != tok::identifier && |
| 301 | Tok.getKind() != tok::l_brace) { |
| 302 | Diag(Tok, diag::err_expected_ident_lbrace); |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | if (Tok.getKind() == tok::identifier) |
| 307 | ConsumeToken(); |
| 308 | |
| 309 | if (Tok.getKind() == tok::l_brace) { |
| 310 | SourceLocation LBraceLoc = Tok.getLocation(); |
| 311 | ConsumeBrace(); |
| 312 | |
| 313 | // Parse the enumerator-list. |
| 314 | while (Tok.getKind() == tok::identifier) { |
| 315 | ConsumeToken(); |
| 316 | |
| 317 | if (Tok.getKind() == tok::equal) { |
| 318 | ConsumeToken(); |
| 319 | ExprResult Res = ParseConstantExpression(); |
| 320 | if (Res.isInvalid) SkipUntil(tok::comma, true, false); |
| 321 | } |
| 322 | |
| 323 | if (Tok.getKind() != tok::comma) |
| 324 | break; |
| 325 | SourceLocation CommaLoc = Tok.getLocation(); |
| 326 | ConsumeToken(); |
| 327 | |
| 328 | if (Tok.getKind() != tok::identifier && !getLang().C99) |
| 329 | Diag(CommaLoc, diag::ext_c99_enumerator_list_comma); |
| 330 | } |
| 331 | |
| 332 | // Eat the }. |
| 333 | MatchRHSPunctuation(tok::r_brace, LBraceLoc, "{", |
| 334 | diag::err_expected_rbrace); |
| 335 | } |
| 336 | // TODO: semantic analysis on the declspec for enums. |
| 337 | } |
| 338 | |
| 339 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 340 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
| 341 | /// start of a specifier-qualifier-list. |
| 342 | bool Parser::isTypeSpecifierQualifier() const { |
| 343 | switch (Tok.getKind()) { |
| 344 | default: return false; |
| 345 | // type-specifiers |
| 346 | case tok::kw_short: |
| 347 | case tok::kw_long: |
| 348 | case tok::kw_signed: |
| 349 | case tok::kw_unsigned: |
| 350 | case tok::kw__Complex: |
| 351 | case tok::kw__Imaginary: |
| 352 | case tok::kw_void: |
| 353 | case tok::kw_char: |
| 354 | case tok::kw_int: |
| 355 | case tok::kw_float: |
| 356 | case tok::kw_double: |
| 357 | case tok::kw__Bool: |
| 358 | case tok::kw__Decimal32: |
| 359 | case tok::kw__Decimal64: |
| 360 | case tok::kw__Decimal128: |
| 361 | |
| 362 | // struct-or-union-specifier |
| 363 | case tok::kw_struct: |
| 364 | case tok::kw_union: |
| 365 | // enum-specifier |
| 366 | case tok::kw_enum: |
| 367 | |
| 368 | // type-qualifier |
| 369 | case tok::kw_const: |
| 370 | case tok::kw_volatile: |
| 371 | case tok::kw_restrict: |
| 372 | return true; |
| 373 | |
| 374 | // typedef-name |
| 375 | case tok::identifier: |
| 376 | // FIXME: if this is a typedef return true. |
| 377 | return false; |
| 378 | |
| 379 | // TODO: Attributes. |
| 380 | } |
| 381 | } |
| 382 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 383 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 384 | /// declaration specifier. |
| 385 | bool Parser::isDeclarationSpecifier() const { |
| 386 | switch (Tok.getKind()) { |
| 387 | default: return false; |
| 388 | // storage-class-specifier |
| 389 | case tok::kw_typedef: |
| 390 | case tok::kw_extern: |
| 391 | case tok::kw_static: |
| 392 | case tok::kw_auto: |
| 393 | case tok::kw_register: |
| 394 | case tok::kw___thread: |
| 395 | |
| 396 | // type-specifiers |
| 397 | case tok::kw_short: |
| 398 | case tok::kw_long: |
| 399 | case tok::kw_signed: |
| 400 | case tok::kw_unsigned: |
| 401 | case tok::kw__Complex: |
| 402 | case tok::kw__Imaginary: |
| 403 | case tok::kw_void: |
| 404 | case tok::kw_char: |
| 405 | case tok::kw_int: |
| 406 | case tok::kw_float: |
| 407 | case tok::kw_double: |
| 408 | case tok::kw__Bool: |
| 409 | case tok::kw__Decimal32: |
| 410 | case tok::kw__Decimal64: |
| 411 | case tok::kw__Decimal128: |
| 412 | |
| 413 | // struct-or-union-specifier |
| 414 | case tok::kw_struct: |
| 415 | case tok::kw_union: |
| 416 | // enum-specifier |
| 417 | case tok::kw_enum: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 418 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 419 | // type-qualifier |
| 420 | case tok::kw_const: |
| 421 | case tok::kw_volatile: |
| 422 | case tok::kw_restrict: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 423 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 424 | // function-specifier |
| 425 | case tok::kw_inline: |
| 426 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 427 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 428 | // typedef-name |
| 429 | case tok::identifier: |
| 430 | // FIXME: if this is a typedef return true. |
| 431 | return false; |
| 432 | // TODO: Attributes. |
| 433 | } |
| 434 | } |
| 435 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 436 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 437 | /// ParseTypeQualifierListOpt |
| 438 | /// type-qualifier-list: [C99 6.7.5] |
| 439 | /// type-qualifier |
| 440 | /// [GNU] attributes [TODO] |
| 441 | /// type-qualifier-list type-qualifier |
| 442 | /// [GNU] type-qualifier-list attributes [TODO] |
| 443 | /// |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 444 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) { |
| 445 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 446 | while (1) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 447 | int isInvalid = false; |
| 448 | const char *PrevSpec = 0; |
| 449 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 450 | switch (Tok.getKind()) { |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 451 | default: |
| 452 | // If this is not a declaration specifier token, we're done reading decl |
| 453 | // specifiers. First verify that DeclSpec's are consistent. |
| 454 | DS.Finish(StartLoc, Diags, getLang()); |
| 455 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 456 | // TODO: attributes. |
| 457 | case tok::kw_const: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 458 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2; |
| 459 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 460 | case tok::kw_volatile: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 461 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2; |
| 462 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 463 | case tok::kw_restrict: |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 464 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 465 | break; |
| 466 | } |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 467 | |
| 468 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 469 | if (isInvalid) { |
| 470 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 471 | if (isInvalid == 1) // Error. |
| 472 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 473 | else // extwarn. |
| 474 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 475 | } |
| 476 | ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 480 | |
| 481 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 482 | /// |
| 483 | void Parser::ParseDeclarator(Declarator &D) { |
| 484 | /// This implements the 'declarator' production in the C grammar, then checks |
| 485 | /// for well-formedness and issues diagnostics. |
| 486 | ParseDeclaratorInternal(D); |
| 487 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 488 | // TODO: validate D. |
Chris Lattner | bf320c8 | 2006-08-07 05:05:30 +0000 | [diff] [blame] | 489 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /// ParseDeclaratorInternal |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 493 | /// declarator: [C99 6.7.5] |
| 494 | /// pointer[opt] direct-declarator |
| 495 | /// |
| 496 | /// pointer: [C99 6.7.5] |
| 497 | /// '*' type-qualifier-list[opt] |
| 498 | /// '*' type-qualifier-list[opt] pointer |
| 499 | /// |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 500 | void Parser::ParseDeclaratorInternal(Declarator &D) { |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 501 | if (Tok.getKind() != tok::star) |
| 502 | return ParseDirectDeclarator(D); |
| 503 | |
| 504 | // Otherwise, '*' -> pointer. |
| 505 | SourceLocation Loc = Tok.getLocation(); |
| 506 | ConsumeToken(); // Eat the *. |
| 507 | DeclSpec DS; |
| 508 | ParseTypeQualifierListOpt(DS); |
| 509 | |
| 510 | // Recursively parse the declarator. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 511 | ParseDeclaratorInternal(D); |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 512 | |
| 513 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 514 | D.AddTypeInfo(DeclaratorTypeInfo::getPointer(DS.TypeQualifiers, Loc)); |
| 515 | } |
| 516 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 517 | |
| 518 | /// ParseDirectDeclarator |
| 519 | /// direct-declarator: [C99 6.7.5] |
| 520 | /// identifier |
| 521 | /// '(' declarator ')' |
| 522 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 523 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 524 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 525 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 526 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 527 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 528 | /// direct-declarator '(' parameter-type-list ')' |
| 529 | /// direct-declarator '(' identifier-list[opt] ')' |
| 530 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 531 | /// parameter-type-list[opt] ')' |
| 532 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 533 | void Parser::ParseDirectDeclarator(Declarator &D) { |
| 534 | // Parse the first direct-declarator seen. |
| 535 | if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) { |
| 536 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 537 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 538 | ConsumeToken(); |
| 539 | } else if (Tok.getKind() == tok::l_paren) { |
| 540 | // direct-declarator: '(' declarator ')' |
| 541 | // direct-declarator: '(' attributes declarator ')' [TODO] |
| 542 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 543 | ParseParenDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 544 | } else if (D.mayOmitIdentifier()) { |
| 545 | // This could be something simple like "int" (in which case the declarator |
| 546 | // portion is empty), if an abstract-declarator is allowed. |
| 547 | D.SetIdentifier(0, Tok.getLocation()); |
| 548 | } else { |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 549 | // Expected identifier or '('. |
| 550 | Diag(Tok, diag::err_expected_ident_lparen); |
| 551 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | assert(D.isPastIdentifier() && |
| 555 | "Haven't past the location of the identifier yet?"); |
| 556 | |
| 557 | while (1) { |
| 558 | if (Tok.getKind() == tok::l_paren) { |
| 559 | ParseParenDeclarator(D); |
| 560 | } else if (Tok.getKind() == tok::l_square) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 561 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 562 | } else { |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This may |
| 569 | /// either be before the identifier (in which case these are just grouping |
| 570 | /// parens for precedence) or it may be after the identifier, in which case |
| 571 | /// these are function arguments. |
| 572 | /// |
| 573 | /// This method also handles this portion of the grammar: |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 574 | /// parameter-type-list: [C99 6.7.5] |
| 575 | /// parameter-list |
| 576 | /// parameter-list ',' '...' |
| 577 | /// |
| 578 | /// parameter-list: [C99 6.7.5] |
| 579 | /// parameter-declaration |
| 580 | /// parameter-list ',' parameter-declaration |
| 581 | /// |
| 582 | /// parameter-declaration: [C99 6.7.5] |
| 583 | /// declaration-specifiers declarator |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 584 | /// [GNU] declaration-specifiers declarator attributes [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 585 | /// declaration-specifiers abstract-declarator[opt] |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 586 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes [TODO] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 587 | /// |
| 588 | /// identifier-list: [C99 6.7.5] |
| 589 | /// identifier |
| 590 | /// identifier-list ',' identifier |
| 591 | /// |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 592 | void Parser::ParseParenDeclarator(Declarator &D) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 593 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 594 | ConsumeParen(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 595 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 596 | // If we haven't past the identifier yet (or where the identifier would be |
| 597 | // stored, if this is an abstract declarator), then this is probably just |
| 598 | // grouping parens. |
| 599 | if (!D.isPastIdentifier()) { |
| 600 | // Okay, this is probably a grouping paren. However, if this could be an |
| 601 | // abstract-declarator, then this could also be the start of function |
| 602 | // arguments (consider 'void()'). |
| 603 | bool isGrouping; |
| 604 | |
| 605 | if (!D.mayOmitIdentifier()) { |
| 606 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 607 | // paren, because we haven't seen the identifier yet. |
| 608 | isGrouping = true; |
| 609 | } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function. |
| 610 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 611 | |
| 612 | isGrouping = false; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 613 | } else { |
Chris Lattner | a350722 | 2006-08-07 00:33:37 +0000 | [diff] [blame] | 614 | // Otherwise, this is a grouping paren, e.g. 'int (*X)'. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 615 | isGrouping = true; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 616 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 617 | |
| 618 | // If this is a grouping paren, handle: |
| 619 | // direct-declarator: '(' declarator ')' |
| 620 | // direct-declarator: '(' attributes declarator ')' [TODO] |
| 621 | if (isGrouping) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 622 | ParseDeclaratorInternal(D); |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 623 | // Match the ')'. |
| 624 | MatchRHSPunctuation(tok::r_paren, StartLoc, "(", |
| 625 | diag::err_expected_rparen); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 626 | return; |
| 627 | } |
| 628 | |
| 629 | // 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] | 630 | // argument list. Recognize that this declarator will never have an |
| 631 | // identifier (and remember where it would have been), then fall through to |
| 632 | // the handling of argument lists. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 633 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 636 | // Okay, this is the parameter list of a function definition, or it is an |
| 637 | // identifier list of a K&R-style function. |
| 638 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 639 | // TODO: enter function-declaration scope, limiting any declarators for |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 640 | // arguments to the function scope. |
| 641 | // NOTE: better to only create a scope if not '()' |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 642 | bool IsVariadic; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 643 | bool HasPrototype; |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 644 | bool IsEmpty = false; |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 645 | bool ErrorEmitted = false; |
| 646 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 647 | if (Tok.getKind() == tok::r_paren) { |
| 648 | // int() -> no prototype, no '...'. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 649 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 650 | HasPrototype = false; |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 651 | IsEmpty = true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 652 | } else if (Tok.getKind() == tok::identifier && |
Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame] | 653 | 1/*TODO: !isatypedefname(Tok.getIdentifierInfo())*/) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 654 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 655 | // normal declarators, not for abstract-declarators. |
| 656 | assert(D.isPastIdentifier() && "Identifier (if present) must be passed!"); |
| 657 | |
| 658 | // If there was no identifier specified, either we are in an |
| 659 | // abstract-declarator, or we are in a parameter declarator which was found |
| 660 | // to be abstract. In abstract-declarators, identifier lists are not valid, |
| 661 | // diagnose this. |
| 662 | if (!D.getIdentifier()) |
| 663 | Diag(Tok, diag::ext_ident_list_in_param); |
| 664 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 665 | // TODO: Remember token. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 666 | ConsumeToken(); |
| 667 | while (Tok.getKind() == tok::comma) { |
| 668 | // Eat the comma. |
| 669 | ConsumeToken(); |
| 670 | |
Chris Lattner | 0be454e | 2006-08-12 19:30:51 +0000 | [diff] [blame] | 671 | if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) { |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 672 | ErrorEmitted = true; |
| 673 | break; |
| 674 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 677 | // K&R 'prototype'. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 678 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 679 | HasPrototype = false; |
| 680 | } else { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 681 | IsVariadic = false; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 682 | bool ReadArg = false; |
| 683 | // Finally, a normal, non-empty parameter type list. |
| 684 | while (1) { |
| 685 | if (Tok.getKind() == tok::ellipsis) { |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 686 | IsVariadic = true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 687 | |
| 688 | // Check to see if this is "void(...)" which is not allowed. |
| 689 | if (!ReadArg) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 690 | // Otherwise, parse parameter type list. If it starts with an |
| 691 | // ellipsis, diagnose the malformed function. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 692 | Diag(Tok, diag::err_ellipsis_first_arg); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 693 | IsVariadic = false; // Treat this like 'void()'. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | // Consume the ellipsis. |
| 697 | ConsumeToken(); |
| 698 | break; |
| 699 | } |
| 700 | |
| 701 | ReadArg = true; |
| 702 | |
| 703 | // Parse the declaration-specifiers. |
| 704 | DeclSpec DS; |
| 705 | ParseDeclarationSpecifiers(DS); |
| 706 | |
| 707 | // Parse the declarator. This is "PrototypeContext", because we must |
| 708 | // accept either 'declarator' or 'abstract-declarator' here. |
| 709 | Declarator DeclaratorInfo(DS, Declarator::PrototypeContext); |
| 710 | ParseDeclarator(DeclaratorInfo); |
| 711 | |
| 712 | // TODO: do something with the declarator, if it is valid. |
| 713 | |
| 714 | // If the next token is a comma, consume it and keep reading arguments. |
| 715 | if (Tok.getKind() != tok::comma) break; |
| 716 | |
| 717 | // Consume the comma. |
| 718 | ConsumeToken(); |
| 719 | } |
| 720 | |
| 721 | HasPrototype = true; |
| 722 | } |
| 723 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 724 | // TODO: pop the scope. |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 725 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 726 | // TODO: capture argument info. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 727 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 728 | // Remember that we parsed a function type, and remember the attributes. |
| 729 | D.AddTypeInfo(DeclaratorTypeInfo::getFunction(HasPrototype, IsVariadic, |
Chris Lattner | fff824f | 2006-08-07 06:31:38 +0000 | [diff] [blame] | 730 | IsEmpty, StartLoc)); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 731 | |
Chris Lattner | 14776b9 | 2006-08-06 22:27:40 +0000 | [diff] [blame] | 732 | |
| 733 | // If we have the closing ')', eat it and we're done. |
| 734 | if (Tok.getKind() == tok::r_paren) { |
| 735 | ConsumeParen(); |
| 736 | } else { |
| 737 | // If an error happened earlier parsing something else in the proto, don't |
| 738 | // issue another error. |
| 739 | if (!ErrorEmitted) |
| 740 | Diag(Tok, diag::err_expected_rparen); |
| 741 | SkipUntil(tok::r_paren); |
| 742 | } |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 743 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 744 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 745 | |
| 746 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 747 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 748 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 749 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 750 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 751 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 752 | SourceLocation StartLoc = Tok.getLocation(); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 753 | ConsumeBracket(); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 754 | |
| 755 | // If valid, this location is the position where we read the 'static' keyword. |
| 756 | SourceLocation StaticLoc; |
| 757 | if (Tok.getKind() == tok::kw_static) { |
| 758 | StaticLoc = Tok.getLocation(); |
| 759 | ConsumeToken(); |
| 760 | } |
| 761 | |
| 762 | // If there is a type-qualifier-list, read it now. |
| 763 | DeclSpec DS; |
| 764 | ParseTypeQualifierListOpt(DS); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 765 | |
| 766 | // If we haven't already read 'static', check to see if there is one after the |
| 767 | // type-qualifier-list. |
| 768 | if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) { |
| 769 | StaticLoc = Tok.getLocation(); |
| 770 | ConsumeToken(); |
| 771 | } |
| 772 | |
| 773 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 774 | bool isStar = false; |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 775 | ExprResult NumElements(false); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 776 | if (Tok.getKind() == tok::star) { |
| 777 | // Remember the '*' token, in case we have to un-get it. |
| 778 | LexerToken StarTok = Tok; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 779 | ConsumeToken(); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 780 | |
| 781 | // Check that the ']' token is present to avoid incorrectly parsing |
| 782 | // expressions starting with '*' as [*]. |
| 783 | if (Tok.getKind() == tok::r_square) { |
| 784 | if (StaticLoc.isValid()) |
| 785 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
| 786 | StaticLoc = SourceLocation(); // Drop the static. |
| 787 | isStar = true; |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 788 | } else { |
| 789 | // Otherwise, the * must have been some expression (such as '*ptr') that |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 790 | // started an assignment-expr. We already consumed the token, but now we |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 791 | // need to reparse it. This handles cases like 'X[*p + 4]' |
| 792 | NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok); |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 793 | } |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 794 | } else if (Tok.getKind() != tok::r_square) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 795 | // Parse the assignment-expression now. |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 796 | NumElements = ParseAssignmentExpression(); |
| 797 | } |
| 798 | |
| 799 | // If there was an error parsing the assignment-expression, recover. |
| 800 | if (NumElements.isInvalid) { |
| 801 | // If the expression was invalid, skip it. |
| 802 | SkipUntil(tok::r_square); |
| 803 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Chris Lattner | 9fab3b9 | 2006-08-12 18:25:42 +0000 | [diff] [blame] | 806 | MatchRHSPunctuation(tok::r_square, StartLoc, "[", diag::err_expected_rsquare); |
| 807 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 808 | // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if |
| 809 | // it was not a constant expression. |
| 810 | if (!getLang().C99) { |
| 811 | // TODO: check C90 array constant exprness. |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 812 | if (isStar || StaticLoc.isValid() || 0/*FIXME: NumElts is constantexpr*/) |
Chris Lattner | 8a39edc | 2006-08-06 18:33:32 +0000 | [diff] [blame] | 813 | Diag(StartLoc, diag::ext_c99_array_usage); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 814 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 815 | |
| 816 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 817 | D.AddTypeInfo(DeclaratorTypeInfo::getArray(DS.TypeQualifiers, |
| 818 | StaticLoc.isValid(), isStar, |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 819 | NumElements.Val, StartLoc)); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 820 | } |
| 821 | |