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