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