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