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