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