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