Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 17 | #include "clang/Parse/Scope.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallSet.h" |
| 19 | using namespace clang; |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // C99 6.7: Declarations. |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | /// ParseTypeName |
| 26 | /// type-name: [C99 6.7.6] |
| 27 | /// specifier-qualifier-list abstract-declarator[opt] |
| 28 | Parser::TypeTy *Parser::ParseTypeName() { |
| 29 | // Parse the common declaration-specifiers piece. |
| 30 | DeclSpec DS; |
| 31 | ParseSpecifierQualifierList(DS); |
| 32 | |
| 33 | // Parse the abstract-declarator, if present. |
| 34 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 35 | ParseDeclarator(DeclaratorInfo); |
| 36 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 37 | return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /// ParseAttributes - Parse a non-empty attributes list. |
| 41 | /// |
| 42 | /// [GNU] attributes: |
| 43 | /// attribute |
| 44 | /// attributes attribute |
| 45 | /// |
| 46 | /// [GNU] attribute: |
| 47 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 48 | /// |
| 49 | /// [GNU] attribute-list: |
| 50 | /// attrib |
| 51 | /// attribute_list ',' attrib |
| 52 | /// |
| 53 | /// [GNU] attrib: |
| 54 | /// empty |
| 55 | /// attrib-name |
| 56 | /// attrib-name '(' identifier ')' |
| 57 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 58 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 59 | /// |
| 60 | /// [GNU] attrib-name: |
| 61 | /// identifier |
| 62 | /// typespec |
| 63 | /// typequal |
| 64 | /// storageclass |
| 65 | /// |
| 66 | /// FIXME: The GCC grammar/code for this construct implies we need two |
| 67 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 68 | /// which is followed by a comma or close parenthesis, then the arguments |
| 69 | /// start with that identifier; otherwise they are an expression list." |
| 70 | /// |
| 71 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 72 | /// any attributes that don't work (based on my limited testing). Most |
| 73 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 74 | /// a pressing need to implement the 2 token lookahead. |
| 75 | |
| 76 | AttributeList *Parser::ParseAttributes() { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 77 | assert(Tok.is(tok::kw___attribute) && "Not an attribute list!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | |
| 79 | AttributeList *CurrAttr = 0; |
| 80 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 81 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | ConsumeToken(); |
| 83 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 84 | "attribute")) { |
| 85 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 86 | return CurrAttr; |
| 87 | } |
| 88 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 89 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 90 | return CurrAttr; |
| 91 | } |
| 92 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 93 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 94 | Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 96 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 98 | ConsumeToken(); |
| 99 | continue; |
| 100 | } |
| 101 | // we have an identifier or declaration specifier (const, int, etc.) |
| 102 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 103 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 104 | |
| 105 | // check if we have a "paramterized" attribute |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 106 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | ConsumeParen(); // ignore the left paren loc for now |
| 108 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 109 | if (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | IdentifierInfo *ParmName = Tok.getIdentifierInfo(); |
| 111 | SourceLocation ParmLoc = ConsumeToken(); |
| 112 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 113 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | // __attribute__(( mode(byte) )) |
| 115 | ConsumeParen(); // ignore the right paren loc for now |
| 116 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 117 | ParmName, ParmLoc, 0, 0, CurrAttr); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 118 | } else if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | ConsumeToken(); |
| 120 | // __attribute__(( format(printf, 1, 2) )) |
| 121 | llvm::SmallVector<ExprTy*, 8> ArgExprs; |
| 122 | bool ArgExprsOk = true; |
| 123 | |
| 124 | // now parse the non-empty comma separated list of expressions |
| 125 | while (1) { |
| 126 | ExprResult ArgExpr = ParseAssignmentExpression(); |
| 127 | if (ArgExpr.isInvalid) { |
| 128 | ArgExprsOk = false; |
| 129 | SkipUntil(tok::r_paren); |
| 130 | break; |
| 131 | } else { |
| 132 | ArgExprs.push_back(ArgExpr.Val); |
| 133 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 134 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | break; |
| 136 | ConsumeToken(); // Eat the comma, move to the next argument |
| 137 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 138 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 139 | ConsumeParen(); // ignore the right paren loc for now |
| 140 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName, |
| 141 | ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr); |
| 142 | } |
| 143 | } |
| 144 | } else { // not an identifier |
| 145 | // parse a possibly empty comma separated list of expressions |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 146 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 147 | // __attribute__(( nonnull() )) |
| 148 | ConsumeParen(); // ignore the right paren loc for now |
| 149 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 150 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 151 | } else { |
| 152 | // __attribute__(( aligned(16) )) |
| 153 | llvm::SmallVector<ExprTy*, 8> ArgExprs; |
| 154 | bool ArgExprsOk = true; |
| 155 | |
| 156 | // now parse the list of expressions |
| 157 | while (1) { |
| 158 | ExprResult ArgExpr = ParseAssignmentExpression(); |
| 159 | if (ArgExpr.isInvalid) { |
| 160 | ArgExprsOk = false; |
| 161 | SkipUntil(tok::r_paren); |
| 162 | break; |
| 163 | } else { |
| 164 | ArgExprs.push_back(ArgExpr.Val); |
| 165 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 166 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | break; |
| 168 | ConsumeToken(); // Eat the comma, move to the next argument |
| 169 | } |
| 170 | // Match the ')'. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 171 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | ConsumeParen(); // ignore the right paren loc for now |
| 173 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, |
| 174 | SourceLocation(), &ArgExprs[0], ArgExprs.size(), |
| 175 | CurrAttr); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } else { |
| 180 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 181 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 182 | } |
| 183 | } |
| 184 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 185 | SkipUntil(tok::r_paren, false); |
| 186 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 187 | SkipUntil(tok::r_paren, false); |
| 188 | } |
| 189 | return CurrAttr; |
| 190 | } |
| 191 | |
| 192 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 193 | /// declaration-specifiers, some number of declarators, and a semicolon. |
| 194 | /// 'Context' should be a Declarator::TheContext value. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 195 | /// |
| 196 | /// declaration: [C99 6.7] |
| 197 | /// block-declaration -> |
| 198 | /// simple-declaration |
| 199 | /// others [FIXME] |
| 200 | /// [C++] namespace-definition |
| 201 | /// others... [FIXME] |
| 202 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 203 | Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) { |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 204 | switch (Tok.getKind()) { |
| 205 | case tok::kw_namespace: |
| 206 | return ParseNamespace(Context); |
| 207 | default: |
| 208 | return ParseSimpleDeclaration(Context); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 213 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 214 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 215 | /// [OMP] threadprivate-directive [TODO] |
| 216 | Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 217 | // Parse the common declaration-specifiers piece. |
| 218 | DeclSpec DS; |
| 219 | ParseDeclarationSpecifiers(DS); |
| 220 | |
| 221 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 222 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 223 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | ConsumeToken(); |
| 225 | return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
| 226 | } |
| 227 | |
| 228 | Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); |
| 229 | ParseDeclarator(DeclaratorInfo); |
| 230 | |
| 231 | return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
| 232 | } |
| 233 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 234 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | /// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after |
| 236 | /// parsing 'declaration-specifiers declarator'. This method is split out this |
| 237 | /// way to handle the ambiguity between top-level function-definitions and |
| 238 | /// declarations. |
| 239 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 240 | /// init-declarator-list: [C99 6.7] |
| 241 | /// init-declarator |
| 242 | /// init-declarator-list ',' init-declarator |
| 243 | /// init-declarator: [C99 6.7] |
| 244 | /// declarator |
| 245 | /// declarator '=' initializer |
| 246 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 247 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
| 248 | /// |
| 249 | Parser::DeclTy *Parser:: |
| 250 | ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) { |
| 251 | |
| 252 | // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so |
| 253 | // that they can be chained properly if the actions want this. |
| 254 | Parser::DeclTy *LastDeclInGroup = 0; |
| 255 | |
| 256 | // At this point, we know that it is not a function definition. Parse the |
| 257 | // rest of the init-declarator-list. |
| 258 | while (1) { |
| 259 | // If a simple-asm-expr is present, parse it. |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 260 | if (Tok.is(tok::kw_asm)) { |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 261 | ExprResult AsmLabel = ParseSimpleAsm(); |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 262 | if (AsmLabel.isInvalid) { |
| 263 | SkipUntil(tok::semi); |
| 264 | return 0; |
| 265 | } |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 266 | |
| 267 | D.setAsmLabel(AsmLabel.Val); |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 268 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 269 | |
| 270 | // If attributes are present, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 271 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 272 | D.AddAttributes(ParseAttributes()); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 273 | |
| 274 | // Inform the current actions module that we just parsed this declarator. |
| 275 | // FIXME: pass asm & attributes. |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 276 | LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 277 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 278 | // Parse declarator '=' initializer. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 279 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | ConsumeToken(); |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 281 | ExprResult Init = ParseInitializer(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 282 | if (Init.isInvalid) { |
| 283 | SkipUntil(tok::semi); |
| 284 | return 0; |
| 285 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 286 | Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 289 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 290 | // error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 291 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 292 | break; |
| 293 | |
| 294 | // Consume the comma. |
| 295 | ConsumeToken(); |
| 296 | |
| 297 | // Parse the next declarator. |
| 298 | D.clear(); |
| 299 | ParseDeclarator(D); |
| 300 | } |
| 301 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 302 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 303 | ConsumeToken(); |
| 304 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 305 | } |
Fariborz Jahanian | bdd15f7 | 2008-01-04 23:23:46 +0000 | [diff] [blame] | 306 | // If this is an ObjC2 for-each loop, this is a successful declarator |
| 307 | // parse. The syntax for these looks like: |
| 308 | // 'for' '(' declaration 'in' expr ')' statement |
Fariborz Jahanian | 335a2d4 | 2008-01-04 23:04:08 +0000 | [diff] [blame] | 309 | if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) { |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 310 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 311 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 312 | Diag(Tok, diag::err_parse_error); |
| 313 | // Skip to end of block or statement |
Chris Lattner | ed44238 | 2007-08-21 18:36:18 +0000 | [diff] [blame] | 314 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 315 | if (Tok.is(tok::semi)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 316 | ConsumeToken(); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /// ParseSpecifierQualifierList |
| 321 | /// specifier-qualifier-list: |
| 322 | /// type-specifier specifier-qualifier-list[opt] |
| 323 | /// type-qualifier specifier-qualifier-list[opt] |
| 324 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 325 | /// |
| 326 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { |
| 327 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 328 | /// parse declaration-specifiers and complain about extra stuff. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 329 | ParseDeclarationSpecifiers(DS); |
| 330 | |
| 331 | // Validate declspec for type-name. |
| 332 | unsigned Specs = DS.getParsedSpecifiers(); |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 333 | if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 334 | Diag(Tok, diag::err_typename_requires_specqual); |
| 335 | |
| 336 | // Issue diagnostic and remove storage class if present. |
| 337 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 338 | if (DS.getStorageClassSpecLoc().isValid()) |
| 339 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 340 | else |
| 341 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 342 | DS.ClearStorageClassSpecs(); |
| 343 | } |
| 344 | |
| 345 | // Issue diagnostic and remove function specfier if present. |
| 346 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
| 347 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 348 | DS.ClearFunctionSpecs(); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /// ParseDeclarationSpecifiers |
| 353 | /// declaration-specifiers: [C99 6.7] |
| 354 | /// storage-class-specifier declaration-specifiers[opt] |
| 355 | /// type-specifier declaration-specifiers[opt] |
| 356 | /// type-qualifier declaration-specifiers[opt] |
| 357 | /// [C99] function-specifier declaration-specifiers[opt] |
| 358 | /// [GNU] attributes declaration-specifiers[opt] |
| 359 | /// |
| 360 | /// storage-class-specifier: [C99 6.7.1] |
| 361 | /// 'typedef' |
| 362 | /// 'extern' |
| 363 | /// 'static' |
| 364 | /// 'auto' |
| 365 | /// 'register' |
| 366 | /// [GNU] '__thread' |
| 367 | /// type-specifier: [C99 6.7.2] |
| 368 | /// 'void' |
| 369 | /// 'char' |
| 370 | /// 'short' |
| 371 | /// 'int' |
| 372 | /// 'long' |
| 373 | /// 'float' |
| 374 | /// 'double' |
| 375 | /// 'signed' |
| 376 | /// 'unsigned' |
| 377 | /// struct-or-union-specifier |
| 378 | /// enum-specifier |
| 379 | /// typedef-name |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 380 | /// [C++] 'wchar_t' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 381 | /// [C++] 'bool' |
| 382 | /// [C99] '_Bool' |
| 383 | /// [C99] '_Complex' |
| 384 | /// [C99] '_Imaginary' // Removed in TC2? |
| 385 | /// [GNU] '_Decimal32' |
| 386 | /// [GNU] '_Decimal64' |
| 387 | /// [GNU] '_Decimal128' |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 388 | /// [GNU] typeof-specifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 389 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 390 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 391 | /// type-qualifier: |
| 392 | /// 'const' |
| 393 | /// 'volatile' |
| 394 | /// [C99] 'restrict' |
| 395 | /// function-specifier: [C99 6.7.4] |
| 396 | /// [C99] 'inline' |
| 397 | /// |
| 398 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) { |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 399 | DS.SetRangeStart(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 400 | while (1) { |
| 401 | int isInvalid = false; |
| 402 | const char *PrevSpec = 0; |
| 403 | SourceLocation Loc = Tok.getLocation(); |
| 404 | |
| 405 | switch (Tok.getKind()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 406 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 407 | DoneWithDeclSpec: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 408 | // If this is not a declaration specifier token, we're done reading decl |
| 409 | // specifiers. First verify that DeclSpec's are consistent. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 410 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 411 | return; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 412 | |
| 413 | // typedef-name |
| 414 | case tok::identifier: { |
| 415 | // This identifier can only be a typedef name if we haven't already seen |
| 416 | // a type-specifier. Without this check we misparse: |
| 417 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 418 | if (DS.hasTypeSpecifier()) |
| 419 | goto DoneWithDeclSpec; |
| 420 | |
| 421 | // It has to be available as a typedef too! |
Argyrios Kyrtzidis | 39caa08 | 2008-08-01 10:35:27 +0000 | [diff] [blame] | 422 | TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 423 | if (TypeRep == 0) |
| 424 | goto DoneWithDeclSpec; |
| 425 | |
| 426 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
| 427 | TypeRep); |
| 428 | if (isInvalid) |
| 429 | break; |
| 430 | |
| 431 | DS.SetRangeEnd(Tok.getLocation()); |
| 432 | ConsumeToken(); // The identifier |
| 433 | |
| 434 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 435 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 436 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 437 | // just a normal reference to a typedef name. |
| 438 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 439 | continue; |
| 440 | |
| 441 | SourceLocation EndProtoLoc; |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 442 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 443 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 444 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 445 | |
| 446 | DS.SetRangeEnd(EndProtoLoc); |
| 447 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 448 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 449 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 450 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 451 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 452 | // GNU attributes support. |
| 453 | case tok::kw___attribute: |
| 454 | DS.AddAttributes(ParseAttributes()); |
| 455 | continue; |
| 456 | |
| 457 | // storage-class-specifier |
| 458 | case tok::kw_typedef: |
| 459 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec); |
| 460 | break; |
| 461 | case tok::kw_extern: |
| 462 | if (DS.isThreadSpecified()) |
| 463 | Diag(Tok, diag::ext_thread_before, "extern"); |
| 464 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec); |
| 465 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 466 | case tok::kw___private_extern__: |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 467 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, |
| 468 | PrevSpec); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 469 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 470 | case tok::kw_static: |
| 471 | if (DS.isThreadSpecified()) |
| 472 | Diag(Tok, diag::ext_thread_before, "static"); |
| 473 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec); |
| 474 | break; |
| 475 | case tok::kw_auto: |
| 476 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec); |
| 477 | break; |
| 478 | case tok::kw_register: |
| 479 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec); |
| 480 | break; |
| 481 | case tok::kw___thread: |
| 482 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2; |
| 483 | break; |
| 484 | |
| 485 | // type-specifiers |
| 486 | case tok::kw_short: |
| 487 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
| 488 | break; |
| 489 | case tok::kw_long: |
| 490 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
| 491 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
| 492 | else |
| 493 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec); |
| 494 | break; |
| 495 | case tok::kw_signed: |
| 496 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
| 497 | break; |
| 498 | case tok::kw_unsigned: |
| 499 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
| 500 | break; |
| 501 | case tok::kw__Complex: |
| 502 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec); |
| 503 | break; |
| 504 | case tok::kw__Imaginary: |
| 505 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec); |
| 506 | break; |
| 507 | case tok::kw_void: |
| 508 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
| 509 | break; |
| 510 | case tok::kw_char: |
| 511 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
| 512 | break; |
| 513 | case tok::kw_int: |
| 514 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
| 515 | break; |
| 516 | case tok::kw_float: |
| 517 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
| 518 | break; |
| 519 | case tok::kw_double: |
| 520 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
| 521 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 522 | case tok::kw_wchar_t: // [C++ 2.11p1] |
| 523 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec); |
| 524 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 525 | case tok::kw_bool: // [C++ 2.11p1] |
| 526 | case tok::kw__Bool: |
| 527 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
| 528 | break; |
| 529 | case tok::kw__Decimal32: |
| 530 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec); |
| 531 | break; |
| 532 | case tok::kw__Decimal64: |
| 533 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec); |
| 534 | break; |
| 535 | case tok::kw__Decimal128: |
| 536 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec); |
| 537 | break; |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 538 | |
| 539 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 540 | case tok::kw_struct: |
| 541 | case tok::kw_union: |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 542 | ParseClassSpecifier(DS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 543 | continue; |
| 544 | case tok::kw_enum: |
| 545 | ParseEnumSpecifier(DS); |
| 546 | continue; |
| 547 | |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 548 | // GNU typeof support. |
| 549 | case tok::kw_typeof: |
| 550 | ParseTypeofSpecifier(DS); |
| 551 | continue; |
| 552 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 553 | // type-qualifier |
| 554 | case tok::kw_const: |
| 555 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 556 | getLang())*2; |
| 557 | break; |
| 558 | case tok::kw_volatile: |
| 559 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 560 | getLang())*2; |
| 561 | break; |
| 562 | case tok::kw_restrict: |
| 563 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 564 | getLang())*2; |
| 565 | break; |
| 566 | |
| 567 | // function-specifier |
| 568 | case tok::kw_inline: |
| 569 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec); |
| 570 | break; |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 571 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 572 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 573 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 574 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 575 | // but we support it. |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 576 | if (DS.hasTypeSpecifier() || !getLang().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 577 | goto DoneWithDeclSpec; |
| 578 | |
| 579 | { |
| 580 | SourceLocation EndProtoLoc; |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 581 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 582 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 583 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 584 | DS.SetRangeEnd(EndProtoLoc); |
| 585 | |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 586 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id, |
| 587 | SourceRange(Loc, EndProtoLoc)); |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 588 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 589 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 590 | continue; |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 591 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 592 | } |
| 593 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 594 | if (isInvalid) { |
| 595 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 596 | if (isInvalid == 1) // Error. |
| 597 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 598 | else // extwarn. |
| 599 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 600 | } |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 601 | DS.SetRangeEnd(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 602 | ConsumeToken(); |
| 603 | } |
| 604 | } |
| 605 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 606 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 607 | /// semicolon. |
| 608 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 609 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 610 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 611 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 612 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 613 | /// struct-declarator-list: |
| 614 | /// struct-declarator |
| 615 | /// struct-declarator-list ',' struct-declarator |
| 616 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 617 | /// struct-declarator: |
| 618 | /// declarator |
| 619 | /// [GNU] declarator attributes[opt] |
| 620 | /// declarator[opt] ':' constant-expression |
| 621 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 622 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 623 | void Parser:: |
| 624 | ParseStructDeclaration(DeclSpec &DS, |
| 625 | llvm::SmallVectorImpl<FieldDeclarator> &Fields) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 626 | // FIXME: When __extension__ is specified, disable extension diagnostics. |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 627 | while (Tok.is(tok::kw___extension__)) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 628 | ConsumeToken(); |
| 629 | |
| 630 | // Parse the common specifier-qualifiers-list piece. |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 631 | SourceLocation DSStart = Tok.getLocation(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 632 | ParseSpecifierQualifierList(DS); |
| 633 | // TODO: Does specifier-qualifier list correctly check that *something* is |
| 634 | // specified? |
| 635 | |
| 636 | // If there are no declarators, issue a warning. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 637 | if (Tok.is(tok::semi)) { |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 638 | Diag(DSStart, diag::w_no_declarators); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 639 | return; |
| 640 | } |
| 641 | |
| 642 | // Read struct-declarators until we find the semicolon. |
Chris Lattner | ebe457c | 2008-04-10 16:37:40 +0000 | [diff] [blame] | 643 | Fields.push_back(FieldDeclarator(DS)); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 644 | while (1) { |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 645 | FieldDeclarator &DeclaratorInfo = Fields.back(); |
| 646 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 647 | /// struct-declarator: declarator |
| 648 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 649 | if (Tok.isNot(tok::colon)) |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 650 | ParseDeclarator(DeclaratorInfo.D); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 651 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 652 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 653 | ConsumeToken(); |
| 654 | ExprResult Res = ParseConstantExpression(); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 655 | if (Res.isInvalid) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 656 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 657 | else |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 658 | DeclaratorInfo.BitfieldSize = Res.Val; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | // If attributes exist after the declarator, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 662 | if (Tok.is(tok::kw___attribute)) |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 663 | DeclaratorInfo.D.AddAttributes(ParseAttributes()); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 664 | |
| 665 | // If we don't have a comma, it is either the end of the list (a ';') |
| 666 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 667 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 668 | return; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 669 | |
| 670 | // Consume the comma. |
| 671 | ConsumeToken(); |
| 672 | |
| 673 | // Parse the next declarator. |
Chris Lattner | ebe457c | 2008-04-10 16:37:40 +0000 | [diff] [blame] | 674 | Fields.push_back(FieldDeclarator(DS)); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 675 | |
| 676 | // Attributes are only allowed on the second declarator. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 677 | if (Tok.is(tok::kw___attribute)) |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 678 | Fields.back().D.AddAttributes(ParseAttributes()); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 679 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /// ParseStructUnionBody |
| 683 | /// struct-contents: |
| 684 | /// struct-declaration-list |
| 685 | /// [EXT] empty |
| 686 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 687 | /// struct-declaration-list: |
| 688 | /// struct-declaration |
| 689 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 690 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 691 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 692 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
| 693 | unsigned TagType, DeclTy *TagDecl) { |
| 694 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 695 | |
| 696 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 697 | // C++. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 698 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 699 | Diag(Tok, diag::ext_empty_struct_union_enum, |
| 700 | DeclSpec::getSpecifierName((DeclSpec::TST)TagType)); |
| 701 | |
| 702 | llvm::SmallVector<DeclTy*, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 703 | llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators; |
| 704 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 705 | // While we still have something to read, read the declarations in the struct. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 706 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 707 | // Each iteration of this loop reads one struct-declaration. |
| 708 | |
| 709 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 710 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 711 | Diag(Tok, diag::ext_extra_struct_semi); |
| 712 | ConsumeToken(); |
| 713 | continue; |
| 714 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 715 | |
| 716 | // Parse all the comma separated declarators. |
| 717 | DeclSpec DS; |
| 718 | FieldDeclarators.clear(); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 719 | if (!Tok.is(tok::at)) { |
| 720 | ParseStructDeclaration(DS, FieldDeclarators); |
| 721 | |
| 722 | // Convert them all to fields. |
| 723 | for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) { |
| 724 | FieldDeclarator &FD = FieldDeclarators[i]; |
| 725 | // Install the declarator into the current TagDecl. |
| 726 | DeclTy *Field = Actions.ActOnField(CurScope, |
| 727 | DS.getSourceRange().getBegin(), |
| 728 | FD.D, FD.BitfieldSize); |
| 729 | FieldDecls.push_back(Field); |
| 730 | } |
| 731 | } else { // Handle @defs |
| 732 | ConsumeToken(); |
| 733 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 734 | Diag(Tok, diag::err_unexpected_at); |
| 735 | SkipUntil(tok::semi, true, true); |
| 736 | continue; |
| 737 | } |
| 738 | ConsumeToken(); |
| 739 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 740 | if (!Tok.is(tok::identifier)) { |
| 741 | Diag(Tok, diag::err_expected_ident); |
| 742 | SkipUntil(tok::semi, true, true); |
| 743 | continue; |
| 744 | } |
| 745 | llvm::SmallVector<DeclTy*, 16> Fields; |
| 746 | Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(), |
| 747 | Fields); |
| 748 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 749 | ConsumeToken(); |
| 750 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
| 751 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 752 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 753 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 754 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 755 | } else if (Tok.is(tok::r_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 756 | Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list); |
| 757 | break; |
| 758 | } else { |
| 759 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 760 | // Skip to end of block or statement |
| 761 | SkipUntil(tok::r_brace, true, true); |
| 762 | } |
| 763 | } |
| 764 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 765 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 766 | |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 767 | Actions.ActOnFields(CurScope, |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 768 | RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(), |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 769 | LBraceLoc, RBraceLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 770 | |
| 771 | AttributeList *AttrList = 0; |
| 772 | // If attributes exist after struct contents, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 773 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 774 | AttrList = ParseAttributes(); // FIXME: where should I put them? |
| 775 | } |
| 776 | |
| 777 | |
| 778 | /// ParseEnumSpecifier |
| 779 | /// enum-specifier: [C99 6.7.2.2] |
| 780 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
| 781 | /// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}' |
| 782 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 783 | /// '}' attributes[opt] |
| 784 | /// 'enum' identifier |
| 785 | /// [GNU] 'enum' attributes[opt] identifier |
| 786 | void Parser::ParseEnumSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 787 | assert(Tok.is(tok::kw_enum) && "Not an enum specifier"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 788 | SourceLocation StartLoc = ConsumeToken(); |
| 789 | |
| 790 | // Parse the tag portion of this. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 791 | |
| 792 | AttributeList *Attr = 0; |
| 793 | // If attributes exist after tag, parse them. |
| 794 | if (Tok.is(tok::kw___attribute)) |
| 795 | Attr = ParseAttributes(); |
| 796 | |
| 797 | // Must have either 'enum name' or 'enum {...}'. |
| 798 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) { |
| 799 | Diag(Tok, diag::err_expected_ident_lbrace); |
| 800 | |
| 801 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 802 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 803 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | // If an identifier is present, consume and remember it. |
| 807 | IdentifierInfo *Name = 0; |
| 808 | SourceLocation NameLoc; |
| 809 | if (Tok.is(tok::identifier)) { |
| 810 | Name = Tok.getIdentifierInfo(); |
| 811 | NameLoc = ConsumeToken(); |
| 812 | } |
| 813 | |
| 814 | // There are three options here. If we have 'enum foo;', then this is a |
| 815 | // forward declaration. If we have 'enum foo {...' then this is a |
| 816 | // definition. Otherwise we have something like 'enum foo xyz', a reference. |
| 817 | // |
| 818 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 819 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 820 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 821 | // |
| 822 | Action::TagKind TK; |
| 823 | if (Tok.is(tok::l_brace)) |
| 824 | TK = Action::TK_Definition; |
| 825 | else if (Tok.is(tok::semi)) |
| 826 | TK = Action::TK_Declaration; |
| 827 | else |
| 828 | TK = Action::TK_Reference; |
| 829 | DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc, |
| 830 | Name, NameLoc, Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 831 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 832 | if (Tok.is(tok::l_brace)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 833 | ParseEnumBody(StartLoc, TagDecl); |
| 834 | |
| 835 | // TODO: semantic analysis on the declspec for enums. |
| 836 | const char *PrevSpec = 0; |
| 837 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl)) |
| 838 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 839 | } |
| 840 | |
| 841 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 842 | /// enumerator-list: |
| 843 | /// enumerator |
| 844 | /// enumerator-list ',' enumerator |
| 845 | /// enumerator: |
| 846 | /// enumeration-constant |
| 847 | /// enumeration-constant '=' constant-expression |
| 848 | /// enumeration-constant: |
| 849 | /// identifier |
| 850 | /// |
| 851 | void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) { |
| 852 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 853 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 854 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 855 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 856 | Diag(Tok, diag::ext_empty_struct_union_enum, "enum"); |
| 857 | |
| 858 | llvm::SmallVector<DeclTy*, 32> EnumConstantDecls; |
| 859 | |
| 860 | DeclTy *LastEnumConstDecl = 0; |
| 861 | |
| 862 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 863 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 864 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 865 | SourceLocation IdentLoc = ConsumeToken(); |
| 866 | |
| 867 | SourceLocation EqualLoc; |
| 868 | ExprTy *AssignedVal = 0; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 869 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 870 | EqualLoc = ConsumeToken(); |
| 871 | ExprResult Res = ParseConstantExpression(); |
| 872 | if (Res.isInvalid) |
| 873 | SkipUntil(tok::comma, tok::r_brace, true, true); |
| 874 | else |
| 875 | AssignedVal = Res.Val; |
| 876 | } |
| 877 | |
| 878 | // Install the enumerator constant into EnumDecl. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 879 | DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 880 | LastEnumConstDecl, |
| 881 | IdentLoc, Ident, |
| 882 | EqualLoc, AssignedVal); |
| 883 | EnumConstantDecls.push_back(EnumConstDecl); |
| 884 | LastEnumConstDecl = EnumConstDecl; |
| 885 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 886 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 887 | break; |
| 888 | SourceLocation CommaLoc = ConsumeToken(); |
| 889 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 890 | if (Tok.isNot(tok::identifier) && !getLang().C99) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 891 | Diag(CommaLoc, diag::ext_c99_enumerator_list_comma); |
| 892 | } |
| 893 | |
| 894 | // Eat the }. |
| 895 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 896 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 897 | Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0], |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 898 | EnumConstantDecls.size()); |
| 899 | |
| 900 | DeclTy *AttrList = 0; |
| 901 | // If attributes exist after the identifier list, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 902 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 903 | AttrList = ParseAttributes(); // FIXME: where do they do? |
| 904 | } |
| 905 | |
| 906 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 907 | /// start of a type-qualifier-list. |
| 908 | bool Parser::isTypeQualifier() const { |
| 909 | switch (Tok.getKind()) { |
| 910 | default: return false; |
| 911 | // type-qualifier |
| 912 | case tok::kw_const: |
| 913 | case tok::kw_volatile: |
| 914 | case tok::kw_restrict: |
| 915 | return true; |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 920 | /// start of a specifier-qualifier-list. |
| 921 | bool Parser::isTypeSpecifierQualifier() const { |
| 922 | switch (Tok.getKind()) { |
| 923 | default: return false; |
| 924 | // GNU attributes support. |
| 925 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 926 | // GNU typeof support. |
| 927 | case tok::kw_typeof: |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 928 | // GNU bizarre protocol extension. FIXME: make an extension? |
| 929 | case tok::less: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 930 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 931 | // type-specifiers |
| 932 | case tok::kw_short: |
| 933 | case tok::kw_long: |
| 934 | case tok::kw_signed: |
| 935 | case tok::kw_unsigned: |
| 936 | case tok::kw__Complex: |
| 937 | case tok::kw__Imaginary: |
| 938 | case tok::kw_void: |
| 939 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 940 | case tok::kw_wchar_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 941 | case tok::kw_int: |
| 942 | case tok::kw_float: |
| 943 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 944 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 945 | case tok::kw__Bool: |
| 946 | case tok::kw__Decimal32: |
| 947 | case tok::kw__Decimal64: |
| 948 | case tok::kw__Decimal128: |
| 949 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 950 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 951 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 952 | case tok::kw_struct: |
| 953 | case tok::kw_union: |
| 954 | // enum-specifier |
| 955 | case tok::kw_enum: |
| 956 | |
| 957 | // type-qualifier |
| 958 | case tok::kw_const: |
| 959 | case tok::kw_volatile: |
| 960 | case tok::kw_restrict: |
| 961 | return true; |
| 962 | |
| 963 | // typedef-name |
| 964 | case tok::identifier: |
| 965 | return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
| 969 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 970 | /// declaration specifier. |
| 971 | bool Parser::isDeclarationSpecifier() const { |
| 972 | switch (Tok.getKind()) { |
| 973 | default: return false; |
| 974 | // storage-class-specifier |
| 975 | case tok::kw_typedef: |
| 976 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 977 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 978 | case tok::kw_static: |
| 979 | case tok::kw_auto: |
| 980 | case tok::kw_register: |
| 981 | case tok::kw___thread: |
| 982 | |
| 983 | // type-specifiers |
| 984 | case tok::kw_short: |
| 985 | case tok::kw_long: |
| 986 | case tok::kw_signed: |
| 987 | case tok::kw_unsigned: |
| 988 | case tok::kw__Complex: |
| 989 | case tok::kw__Imaginary: |
| 990 | case tok::kw_void: |
| 991 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 992 | case tok::kw_wchar_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 993 | case tok::kw_int: |
| 994 | case tok::kw_float: |
| 995 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 996 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 997 | case tok::kw__Bool: |
| 998 | case tok::kw__Decimal32: |
| 999 | case tok::kw__Decimal64: |
| 1000 | case tok::kw__Decimal128: |
| 1001 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 1002 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 1003 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1004 | case tok::kw_struct: |
| 1005 | case tok::kw_union: |
| 1006 | // enum-specifier |
| 1007 | case tok::kw_enum: |
| 1008 | |
| 1009 | // type-qualifier |
| 1010 | case tok::kw_const: |
| 1011 | case tok::kw_volatile: |
| 1012 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1013 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1014 | // function-specifier |
| 1015 | case tok::kw_inline: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 1017 | // GNU typeof support. |
| 1018 | case tok::kw_typeof: |
| 1019 | |
| 1020 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 1021 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1022 | return true; |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 1023 | |
| 1024 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 1025 | case tok::less: |
| 1026 | return getLang().ObjC1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1027 | |
| 1028 | // typedef-name |
| 1029 | case tok::identifier: |
| 1030 | return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | |
| 1035 | /// ParseTypeQualifierListOpt |
| 1036 | /// type-qualifier-list: [C99 6.7.5] |
| 1037 | /// type-qualifier |
| 1038 | /// [GNU] attributes |
| 1039 | /// type-qualifier-list type-qualifier |
| 1040 | /// [GNU] type-qualifier-list attributes |
| 1041 | /// |
| 1042 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) { |
| 1043 | while (1) { |
| 1044 | int isInvalid = false; |
| 1045 | const char *PrevSpec = 0; |
| 1046 | SourceLocation Loc = Tok.getLocation(); |
| 1047 | |
| 1048 | switch (Tok.getKind()) { |
| 1049 | default: |
| 1050 | // If this is not a type-qualifier token, we're done reading type |
| 1051 | // qualifiers. First verify that DeclSpec's are consistent. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 1052 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1053 | return; |
| 1054 | case tok::kw_const: |
| 1055 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 1056 | getLang())*2; |
| 1057 | break; |
| 1058 | case tok::kw_volatile: |
| 1059 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 1060 | getLang())*2; |
| 1061 | break; |
| 1062 | case tok::kw_restrict: |
| 1063 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 1064 | getLang())*2; |
| 1065 | break; |
| 1066 | case tok::kw___attribute: |
| 1067 | DS.AddAttributes(ParseAttributes()); |
| 1068 | continue; // do *not* consume the next token! |
| 1069 | } |
| 1070 | |
| 1071 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 1072 | if (isInvalid) { |
| 1073 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 1074 | if (isInvalid == 1) // Error. |
| 1075 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 1076 | else // extwarn. |
| 1077 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 1078 | } |
| 1079 | ConsumeToken(); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 1085 | /// |
| 1086 | void Parser::ParseDeclarator(Declarator &D) { |
| 1087 | /// This implements the 'declarator' production in the C grammar, then checks |
| 1088 | /// for well-formedness and issues diagnostics. |
| 1089 | ParseDeclaratorInternal(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /// ParseDeclaratorInternal |
| 1093 | /// declarator: [C99 6.7.5] |
| 1094 | /// pointer[opt] direct-declarator |
| 1095 | /// [C++] '&' declarator [C++ 8p4, dcl.decl] |
| 1096 | /// [GNU] '&' restrict[opt] attributes[opt] declarator |
| 1097 | /// |
| 1098 | /// pointer: [C99 6.7.5] |
| 1099 | /// '*' type-qualifier-list[opt] |
| 1100 | /// '*' type-qualifier-list[opt] pointer |
| 1101 | /// |
| 1102 | void Parser::ParseDeclaratorInternal(Declarator &D) { |
| 1103 | tok::TokenKind Kind = Tok.getKind(); |
| 1104 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1105 | // Not a pointer, C++ reference, or block. |
| 1106 | if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) && |
| 1107 | (Kind != tok::caret || !getLang().Blocks)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1108 | return ParseDirectDeclarator(D); |
| 1109 | |
Steve Naroff | 4ef1c99 | 2008-08-28 10:07:06 +0000 | [diff] [blame] | 1110 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1111 | SourceLocation Loc = ConsumeToken(); // Eat the * or &. |
| 1112 | |
Steve Naroff | 4ef1c99 | 2008-08-28 10:07:06 +0000 | [diff] [blame] | 1113 | if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 1114 | // Is a pointer. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1115 | DeclSpec DS; |
| 1116 | |
| 1117 | ParseTypeQualifierListOpt(DS); |
| 1118 | |
| 1119 | // Recursively parse the declarator. |
| 1120 | ParseDeclaratorInternal(D); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1121 | if (Kind == tok::star) |
| 1122 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1123 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
| 1124 | DS.TakeAttributes())); |
| 1125 | else |
| 1126 | // Remember that we parsed a Block type, and remember the type-quals. |
| 1127 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
| 1128 | Loc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1129 | } else { |
| 1130 | // Is a reference |
| 1131 | DeclSpec DS; |
| 1132 | |
| 1133 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 1134 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 1135 | // template type argument, in which case the cv-qualifiers are ignored. |
| 1136 | // |
| 1137 | // [GNU] Retricted references are allowed. |
| 1138 | // [GNU] Attributes on references are allowed. |
| 1139 | ParseTypeQualifierListOpt(DS); |
| 1140 | |
| 1141 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 1142 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 1143 | Diag(DS.getConstSpecLoc(), |
| 1144 | diag::err_invalid_reference_qualifier_application, |
| 1145 | "const"); |
| 1146 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 1147 | Diag(DS.getVolatileSpecLoc(), |
| 1148 | diag::err_invalid_reference_qualifier_application, |
| 1149 | "volatile"); |
| 1150 | } |
| 1151 | |
| 1152 | // Recursively parse the declarator. |
| 1153 | ParseDeclaratorInternal(D); |
| 1154 | |
| 1155 | // Remember that we parsed a reference type. It doesn't have type-quals. |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 1156 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
| 1157 | DS.TakeAttributes())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | /// ParseDirectDeclarator |
| 1162 | /// direct-declarator: [C99 6.7.5] |
| 1163 | /// identifier |
| 1164 | /// '(' declarator ')' |
| 1165 | /// [GNU] '(' attributes declarator ')' |
| 1166 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1167 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1168 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1169 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1170 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1171 | /// direct-declarator '(' parameter-type-list ')' |
| 1172 | /// direct-declarator '(' identifier-list[opt] ')' |
| 1173 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 1174 | /// parameter-type-list[opt] ')' |
| 1175 | /// |
| 1176 | void Parser::ParseDirectDeclarator(Declarator &D) { |
| 1177 | // Parse the first direct-declarator seen. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1178 | if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1179 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 1180 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 1181 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1182 | } else if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1183 | // direct-declarator: '(' declarator ')' |
| 1184 | // direct-declarator: '(' attributes declarator ')' |
| 1185 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 1186 | ParseParenDeclarator(D); |
| 1187 | } else if (D.mayOmitIdentifier()) { |
| 1188 | // This could be something simple like "int" (in which case the declarator |
| 1189 | // portion is empty), if an abstract-declarator is allowed. |
| 1190 | D.SetIdentifier(0, Tok.getLocation()); |
| 1191 | } else { |
| 1192 | // Expected identifier or '('. |
| 1193 | Diag(Tok, diag::err_expected_ident_lparen); |
| 1194 | D.SetIdentifier(0, Tok.getLocation()); |
| 1195 | } |
| 1196 | |
| 1197 | assert(D.isPastIdentifier() && |
| 1198 | "Haven't past the location of the identifier yet?"); |
| 1199 | |
| 1200 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1201 | if (Tok.is(tok::l_paren)) { |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1202 | ParseFunctionDeclarator(ConsumeParen(), D); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1203 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1204 | ParseBracketDeclarator(D); |
| 1205 | } else { |
| 1206 | break; |
| 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1211 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 1212 | /// only called before the identifier, so these are most likely just grouping |
| 1213 | /// parens for precedence. If we find that these are actually function |
| 1214 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 1215 | /// |
| 1216 | /// direct-declarator: |
| 1217 | /// '(' declarator ')' |
| 1218 | /// [GNU] '(' attributes declarator ')' |
| 1219 | /// |
| 1220 | void Parser::ParseParenDeclarator(Declarator &D) { |
| 1221 | SourceLocation StartLoc = ConsumeParen(); |
| 1222 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
| 1223 | |
| 1224 | // If we haven't past the identifier yet (or where the identifier would be |
| 1225 | // stored, if this is an abstract declarator), then this is probably just |
| 1226 | // grouping parens. However, if this could be an abstract-declarator, then |
| 1227 | // this could also be the start of function arguments (consider 'void()'). |
| 1228 | bool isGrouping; |
| 1229 | |
| 1230 | if (!D.mayOmitIdentifier()) { |
| 1231 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 1232 | // paren, because we haven't seen the identifier yet. |
| 1233 | isGrouping = true; |
| 1234 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
| 1235 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 1236 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 1237 | // considered to be a type, not a K&R identifier-list. |
| 1238 | isGrouping = false; |
| 1239 | } else { |
| 1240 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 1241 | isGrouping = true; |
| 1242 | } |
| 1243 | |
| 1244 | // If this is a grouping paren, handle: |
| 1245 | // direct-declarator: '(' declarator ')' |
| 1246 | // direct-declarator: '(' attributes declarator ')' |
| 1247 | if (isGrouping) { |
| 1248 | if (Tok.is(tok::kw___attribute)) |
| 1249 | D.AddAttributes(ParseAttributes()); |
| 1250 | |
| 1251 | ParseDeclaratorInternal(D); |
| 1252 | // Match the ')'. |
| 1253 | MatchRHSPunctuation(tok::r_paren, StartLoc); |
| 1254 | return; |
| 1255 | } |
| 1256 | |
| 1257 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 1258 | // argument list. Recognize that this declarator will never have an |
| 1259 | // identifier (and remember where it would have been), then fall through to |
| 1260 | // the handling of argument lists. |
| 1261 | D.SetIdentifier(0, Tok.getLocation()); |
| 1262 | |
| 1263 | ParseFunctionDeclarator(StartLoc, D); |
| 1264 | } |
| 1265 | |
| 1266 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 1267 | /// declarator D up to a paren, which indicates that we are parsing function |
| 1268 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1269 | /// |
| 1270 | /// This method also handles this portion of the grammar: |
| 1271 | /// parameter-type-list: [C99 6.7.5] |
| 1272 | /// parameter-list |
| 1273 | /// parameter-list ',' '...' |
| 1274 | /// |
| 1275 | /// parameter-list: [C99 6.7.5] |
| 1276 | /// parameter-declaration |
| 1277 | /// parameter-list ',' parameter-declaration |
| 1278 | /// |
| 1279 | /// parameter-declaration: [C99 6.7.5] |
| 1280 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1281 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1282 | /// [GNU] declaration-specifiers declarator attributes |
| 1283 | /// declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1284 | /// [C++] declaration-specifiers abstract-declarator[opt] |
| 1285 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1286 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 1287 | /// |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1288 | void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) { |
| 1289 | // lparen is already consumed! |
| 1290 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1291 | |
| 1292 | // Okay, this is the parameter list of a function definition, or it is an |
| 1293 | // identifier list of a K&R-style function. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1294 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1295 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1296 | // Remember that we parsed a function type, and remember the attributes. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1297 | // int() -> no prototype, no '...'. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1298 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false, |
| 1299 | /*variadic*/ false, |
| 1300 | /*arglist*/ 0, 0, LParenLoc)); |
| 1301 | |
| 1302 | ConsumeParen(); // Eat the closing ')'. |
| 1303 | return; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1304 | } else if (Tok.is(tok::identifier) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1305 | // K&R identifier lists can't have typedefs as identifiers, per |
| 1306 | // C99 6.7.5.3p11. |
| 1307 | !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) { |
| 1308 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 1309 | // normal declarators, not for abstract-declarators. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1310 | return ParseFunctionDeclaratorIdentifierList(LParenLoc, D); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | // Finally, a normal, non-empty parameter type list. |
| 1314 | |
| 1315 | // Build up an array of information about the parsed arguments. |
| 1316 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1317 | |
| 1318 | // Enter function-declaration scope, limiting any declarators to the |
| 1319 | // function prototype scope, including parameter declarators. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1320 | EnterScope(Scope::FnScope|Scope::DeclScope); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1321 | |
| 1322 | bool IsVariadic = false; |
| 1323 | while (1) { |
| 1324 | if (Tok.is(tok::ellipsis)) { |
| 1325 | IsVariadic = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1326 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1327 | // Check to see if this is "void(...)" which is not allowed. |
| 1328 | if (ParamInfo.empty()) { |
| 1329 | // Otherwise, parse parameter type list. If it starts with an |
| 1330 | // ellipsis, diagnose the malformed function. |
| 1331 | Diag(Tok, diag::err_ellipsis_first_arg); |
| 1332 | IsVariadic = false; // Treat this like 'void()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1333 | } |
Chris Lattner | e0e713b | 2008-01-31 06:10:07 +0000 | [diff] [blame] | 1334 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1335 | ConsumeToken(); // Consume the ellipsis. |
| 1336 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1339 | SourceLocation DSStart = Tok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1340 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1341 | // Parse the declaration-specifiers. |
| 1342 | DeclSpec DS; |
| 1343 | ParseDeclarationSpecifiers(DS); |
| 1344 | |
| 1345 | // Parse the declarator. This is "PrototypeContext", because we must |
| 1346 | // accept either 'declarator' or 'abstract-declarator' here. |
| 1347 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 1348 | ParseDeclarator(ParmDecl); |
| 1349 | |
| 1350 | // Parse GNU attributes, if present. |
| 1351 | if (Tok.is(tok::kw___attribute)) |
| 1352 | ParmDecl.AddAttributes(ParseAttributes()); |
| 1353 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1354 | // Remember this parsed parameter in ParamInfo. |
| 1355 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
| 1356 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1357 | // If no parameter was specified, verify that *something* was specified, |
| 1358 | // otherwise we have a missing type and identifier. |
| 1359 | if (DS.getParsedSpecifiers() == DeclSpec::PQ_None && |
| 1360 | ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) { |
| 1361 | // Completely missing, emit error. |
| 1362 | Diag(DSStart, diag::err_missing_param); |
| 1363 | } else { |
| 1364 | // Otherwise, we have something. Add it and let semantic analysis try |
| 1365 | // to grok it and add the result to the ParamInfo we are building. |
| 1366 | |
| 1367 | // Inform the actions module about the parameter declarator, so it gets |
| 1368 | // added to the current scope. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1369 | DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl); |
| 1370 | |
| 1371 | // Parse the default argument, if any. We parse the default |
| 1372 | // arguments in all dialects; the semantic analysis in |
| 1373 | // ActOnParamDefaultArgument will reject the default argument in |
| 1374 | // C. |
| 1375 | if (Tok.is(tok::equal)) { |
| 1376 | SourceLocation EqualLoc = Tok.getLocation(); |
| 1377 | |
| 1378 | // Consume the '='. |
| 1379 | ConsumeToken(); |
| 1380 | |
| 1381 | // Parse the default argument |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1382 | ExprResult DefArgResult = ParseAssignmentExpression(); |
| 1383 | if (DefArgResult.isInvalid) { |
| 1384 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 1385 | } else { |
| 1386 | // Inform the actions module about the default argument |
| 1387 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val); |
| 1388 | } |
| 1389 | } |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1390 | |
| 1391 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1392 | ParmDecl.getIdentifierLoc(), Param)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | // If the next token is a comma, consume it and keep reading arguments. |
| 1396 | if (Tok.isNot(tok::comma)) break; |
| 1397 | |
| 1398 | // Consume the comma. |
| 1399 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1402 | // Leave prototype scope. |
| 1403 | ExitScope(); |
| 1404 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1405 | // Remember that we parsed a function type, and remember the attributes. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1406 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic, |
| 1407 | &ParamInfo[0], ParamInfo.size(), |
| 1408 | LParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1409 | |
| 1410 | // If we have the closing ')', eat it and we're done. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1411 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1414 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 1415 | /// we found a K&R-style identifier list instead of a type argument list. The |
| 1416 | /// current token is known to be the first identifier in the list. |
| 1417 | /// |
| 1418 | /// identifier-list: [C99 6.7.5] |
| 1419 | /// identifier |
| 1420 | /// identifier-list ',' identifier |
| 1421 | /// |
| 1422 | void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc, |
| 1423 | Declarator &D) { |
| 1424 | // Build up an array of information about the parsed arguments. |
| 1425 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
| 1426 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 1427 | |
| 1428 | // If there was no identifier specified for the declarator, either we are in |
| 1429 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 1430 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 1431 | // diagnose this. |
| 1432 | if (!D.getIdentifier()) |
| 1433 | Diag(Tok, diag::ext_ident_list_in_param); |
| 1434 | |
| 1435 | // Tok is known to be the first identifier in the list. Remember this |
| 1436 | // identifier in ParamInfo. |
Chris Lattner | 3825c2e | 2008-04-06 06:50:56 +0000 | [diff] [blame] | 1437 | ParamsSoFar.insert(Tok.getIdentifierInfo()); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1438 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(), |
| 1439 | Tok.getLocation(), 0)); |
| 1440 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1441 | ConsumeToken(); // eat the first identifier. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1442 | |
| 1443 | while (Tok.is(tok::comma)) { |
| 1444 | // Eat the comma. |
| 1445 | ConsumeToken(); |
| 1446 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1447 | // If this isn't an identifier, report the error and skip until ')'. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1448 | if (Tok.isNot(tok::identifier)) { |
| 1449 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1450 | SkipUntil(tok::r_paren); |
| 1451 | return; |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1452 | } |
Chris Lattner | aaf9ddb | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 1453 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1454 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
Chris Lattner | aaf9ddb | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 1455 | |
| 1456 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 1457 | if (Actions.isTypeName(*ParmII, CurScope)) |
| 1458 | Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName()); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1459 | |
| 1460 | // Verify that the argument identifier has not already been mentioned. |
| 1461 | if (!ParamsSoFar.insert(ParmII)) { |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1462 | Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName()); |
| 1463 | } else { |
| 1464 | // Remember this identifier in ParamInfo. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1465 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 1466 | Tok.getLocation(), 0)); |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1467 | } |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1468 | |
| 1469 | // Eat the identifier. |
| 1470 | ConsumeToken(); |
| 1471 | } |
| 1472 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1473 | // Remember that we parsed a function type, and remember the attributes. This |
| 1474 | // function type is always a K&R style function type, which is not varargs and |
| 1475 | // has no prototype. |
| 1476 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false, |
| 1477 | &ParamInfo[0], ParamInfo.size(), |
| 1478 | LParenLoc)); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1479 | |
| 1480 | // If we have the closing ')', eat it and we're done. |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1481 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1482 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1483 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1484 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1485 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1486 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1487 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1488 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1489 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 1490 | SourceLocation StartLoc = ConsumeBracket(); |
| 1491 | |
| 1492 | // If valid, this location is the position where we read the 'static' keyword. |
| 1493 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1494 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1495 | StaticLoc = ConsumeToken(); |
| 1496 | |
| 1497 | // If there is a type-qualifier-list, read it now. |
| 1498 | DeclSpec DS; |
| 1499 | ParseTypeQualifierListOpt(DS); |
| 1500 | |
| 1501 | // If we haven't already read 'static', check to see if there is one after the |
| 1502 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1503 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1504 | StaticLoc = ConsumeToken(); |
| 1505 | |
| 1506 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 1507 | bool isStar = false; |
| 1508 | ExprResult NumElements(false); |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 1509 | |
| 1510 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 1511 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 1512 | // the the token after the star is a ']'. Since stars in arrays are |
| 1513 | // infrequent, use of lookahead is not costly here. |
| 1514 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 1515 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1516 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 1517 | if (StaticLoc.isValid()) |
| 1518 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
| 1519 | StaticLoc = SourceLocation(); // Drop the static. |
| 1520 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1521 | } else if (Tok.isNot(tok::r_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1522 | // Parse the assignment-expression now. |
| 1523 | NumElements = ParseAssignmentExpression(); |
| 1524 | } |
| 1525 | |
| 1526 | // If there was an error parsing the assignment-expression, recover. |
| 1527 | if (NumElements.isInvalid) { |
| 1528 | // If the expression was invalid, skip it. |
| 1529 | SkipUntil(tok::r_square); |
| 1530 | return; |
| 1531 | } |
| 1532 | |
| 1533 | MatchRHSPunctuation(tok::r_square, StartLoc); |
| 1534 | |
| 1535 | // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if |
| 1536 | // it was not a constant expression. |
| 1537 | if (!getLang().C99) { |
| 1538 | // TODO: check C90 array constant exprness. |
| 1539 | if (isStar || StaticLoc.isValid() || |
| 1540 | 0/*TODO: NumElts is not a C90 constantexpr */) |
| 1541 | Diag(StartLoc, diag::ext_c99_array_usage); |
| 1542 | } |
| 1543 | |
| 1544 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1545 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
| 1546 | StaticLoc.isValid(), isStar, |
| 1547 | NumElements.Val, StartLoc)); |
| 1548 | } |
| 1549 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 1550 | /// [GNU] typeof-specifier: |
| 1551 | /// typeof ( expressions ) |
| 1552 | /// typeof ( type-name ) |
| 1553 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1554 | /// |
| 1555 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1556 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1557 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1558 | SourceLocation StartLoc = ConsumeToken(); |
| 1559 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1560 | if (Tok.isNot(tok::l_paren)) { |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 1561 | if (!getLang().CPlusPlus) { |
| 1562 | Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName()); |
| 1563 | return; |
| 1564 | } |
| 1565 | |
| 1566 | ExprResult Result = ParseCastExpression(true/*isUnaryExpression*/); |
| 1567 | if (Result.isInvalid) |
| 1568 | return; |
| 1569 | |
| 1570 | const char *PrevSpec = 0; |
| 1571 | // Check for duplicate type specifiers. |
| 1572 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
| 1573 | Result.Val)) |
| 1574 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 1575 | |
| 1576 | // FIXME: Not accurate, the range gets one token more than it should. |
| 1577 | DS.SetRangeEnd(Tok.getLocation()); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1578 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1579 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 1580 | |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1581 | SourceLocation LParenLoc = ConsumeParen(), RParenLoc; |
| 1582 | |
| 1583 | if (isTypeSpecifierQualifier()) { |
| 1584 | TypeTy *Ty = ParseTypeName(); |
| 1585 | |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1586 | assert(Ty && "Parser::ParseTypeofSpecifier(): missing type"); |
| 1587 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1588 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1589 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1590 | return; |
| 1591 | } |
| 1592 | RParenLoc = ConsumeParen(); |
| 1593 | const char *PrevSpec = 0; |
| 1594 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 1595 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty)) |
| 1596 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1597 | } else { // we have an expression. |
| 1598 | ExprResult Result = ParseExpression(); |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1599 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1600 | if (Result.isInvalid || Tok.isNot(tok::r_paren)) { |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1601 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1602 | return; |
| 1603 | } |
| 1604 | RParenLoc = ConsumeParen(); |
| 1605 | const char *PrevSpec = 0; |
| 1606 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 1607 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
| 1608 | Result.Val)) |
| 1609 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1610 | } |
Argyrios Kyrtzidis | 0919f9e | 2008-08-16 10:21:33 +0000 | [diff] [blame] | 1611 | DS.SetRangeEnd(RParenLoc); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1614 | |