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