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