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