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