Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 16 | #include "clang/Parse/Scope.h" |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 17 | #include "ExtensionRAIIObject.h" |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 18 | #include "AstGuard.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 29 | /// |
| 30 | /// Called type-id in C++. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 31 | Parser::TypeTy *Parser::ParseTypeName() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | // Parse the common declaration-specifiers piece. |
| 33 | DeclSpec DS; |
| 34 | ParseSpecifierQualifierList(DS); |
| 35 | |
| 36 | // Parse the abstract-declarator, if present. |
| 37 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 38 | ParseDeclarator(DeclaratorInfo); |
| 39 | |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 40 | return Actions.ActOnTypeName(CurScope, DeclaratorInfo).get(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /// ParseAttributes - Parse a non-empty attributes list. |
| 44 | /// |
| 45 | /// [GNU] attributes: |
| 46 | /// attribute |
| 47 | /// attributes attribute |
| 48 | /// |
| 49 | /// [GNU] attribute: |
| 50 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 51 | /// |
| 52 | /// [GNU] attribute-list: |
| 53 | /// attrib |
| 54 | /// attribute_list ',' attrib |
| 55 | /// |
| 56 | /// [GNU] attrib: |
| 57 | /// empty |
| 58 | /// attrib-name |
| 59 | /// attrib-name '(' identifier ')' |
| 60 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 61 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 62 | /// |
| 63 | /// [GNU] attrib-name: |
| 64 | /// identifier |
| 65 | /// typespec |
| 66 | /// typequal |
| 67 | /// storageclass |
| 68 | /// |
| 69 | /// FIXME: The GCC grammar/code for this construct implies we need two |
| 70 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 71 | /// which is followed by a comma or close parenthesis, then the arguments |
| 72 | /// start with that identifier; otherwise they are an expression list." |
| 73 | /// |
| 74 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 75 | /// any attributes that don't work (based on my limited testing). Most |
| 76 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 77 | /// a pressing need to implement the 2 token lookahead. |
| 78 | |
| 79 | AttributeList *Parser::ParseAttributes() { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 80 | assert(Tok.is(tok::kw___attribute) && "Not an attribute list!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | |
| 82 | AttributeList *CurrAttr = 0; |
| 83 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 84 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | ConsumeToken(); |
| 86 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 87 | "attribute")) { |
| 88 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 89 | return CurrAttr; |
| 90 | } |
| 91 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 92 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 93 | return CurrAttr; |
| 94 | } |
| 95 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 96 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 97 | Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 99 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 101 | ConsumeToken(); |
| 102 | continue; |
| 103 | } |
| 104 | // we have an identifier or declaration specifier (const, int, etc.) |
| 105 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 106 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 107 | |
| 108 | // check if we have a "paramterized" attribute |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 109 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | ConsumeParen(); // ignore the left paren loc for now |
| 111 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 112 | if (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | IdentifierInfo *ParmName = Tok.getIdentifierInfo(); |
| 114 | SourceLocation ParmLoc = ConsumeToken(); |
| 115 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 116 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | // __attribute__(( mode(byte) )) |
| 118 | ConsumeParen(); // ignore the right paren loc for now |
| 119 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 120 | ParmName, ParmLoc, 0, 0, CurrAttr); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 121 | } else if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | ConsumeToken(); |
| 123 | // __attribute__(( format(printf, 1, 2) )) |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 124 | ExprVector ArgExprs(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | bool ArgExprsOk = true; |
| 126 | |
| 127 | // now parse the non-empty comma separated list of expressions |
| 128 | while (1) { |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 129 | OwningExprResult ArgExpr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 130 | if (ArgExpr.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | ArgExprsOk = false; |
| 132 | SkipUntil(tok::r_paren); |
| 133 | break; |
| 134 | } else { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 135 | ArgExprs.push_back(ArgExpr.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 137 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 138 | break; |
| 139 | ConsumeToken(); // Eat the comma, move to the next argument |
| 140 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 141 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 142 | ConsumeParen(); // ignore the right paren loc for now |
| 143 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName, |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 144 | ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | } else { // not an identifier |
| 148 | // parse a possibly empty comma separated list of expressions |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 149 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | // __attribute__(( nonnull() )) |
| 151 | ConsumeParen(); // ignore the right paren loc for now |
| 152 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 153 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 154 | } else { |
| 155 | // __attribute__(( aligned(16) )) |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 156 | ExprVector ArgExprs(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | bool ArgExprsOk = true; |
| 158 | |
| 159 | // now parse the list of expressions |
| 160 | while (1) { |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 161 | OwningExprResult ArgExpr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 162 | if (ArgExpr.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 163 | ArgExprsOk = false; |
| 164 | SkipUntil(tok::r_paren); |
| 165 | break; |
| 166 | } else { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 167 | ArgExprs.push_back(ArgExpr.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 169 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | break; |
| 171 | ConsumeToken(); // Eat the comma, move to the next argument |
| 172 | } |
| 173 | // Match the ')'. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 174 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | ConsumeParen(); // ignore the right paren loc for now |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 176 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, |
| 177 | SourceLocation(), ArgExprs.take(), ArgExprs.size(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | CurrAttr); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } else { |
| 183 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 184 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 185 | } |
| 186 | } |
| 187 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 188 | SkipUntil(tok::r_paren, false); |
| 189 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 190 | SkipUntil(tok::r_paren, false); |
| 191 | } |
| 192 | return CurrAttr; |
| 193 | } |
| 194 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 195 | /// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this |
| 196 | /// routine is called to skip/ignore tokens that comprise the MS declspec. |
| 197 | void Parser::FuzzyParseMicrosoftDeclSpec() { |
| 198 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
| 199 | ConsumeToken(); |
| 200 | if (Tok.is(tok::l_paren)) { |
| 201 | unsigned short savedParenCount = ParenCount; |
| 202 | do { |
| 203 | ConsumeAnyToken(); |
| 204 | } while (ParenCount > savedParenCount && Tok.isNot(tok::eof)); |
| 205 | } |
| 206 | return; |
| 207 | } |
| 208 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 210 | /// declaration-specifiers, some number of declarators, and a semicolon. |
| 211 | /// 'Context' should be a Declarator::TheContext value. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 212 | /// |
| 213 | /// declaration: [C99 6.7] |
| 214 | /// block-declaration -> |
| 215 | /// simple-declaration |
| 216 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 217 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 218 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 219 | /// [C++] using-directive |
| 220 | /// [C++] using-declaration [TODO] |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 221 | /// others... [FIXME] |
| 222 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 223 | Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) { |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 224 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 225 | case tok::kw_export: |
| 226 | case tok::kw_template: |
| 227 | return ParseTemplateDeclaration(Context); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 228 | case tok::kw_namespace: |
| 229 | return ParseNamespace(Context); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 230 | case tok::kw_using: |
| 231 | return ParseUsingDirectiveOrDeclaration(Context); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 232 | default: |
| 233 | return ParseSimpleDeclaration(Context); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 238 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 239 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 240 | /// [OMP] threadprivate-directive [TODO] |
| 241 | Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 242 | // Parse the common declaration-specifiers piece. |
| 243 | DeclSpec DS; |
| 244 | ParseDeclarationSpecifiers(DS); |
| 245 | |
| 246 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 247 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 248 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 249 | ConsumeToken(); |
| 250 | return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
| 251 | } |
| 252 | |
| 253 | Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); |
| 254 | ParseDeclarator(DeclaratorInfo); |
| 255 | |
| 256 | return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
| 257 | } |
| 258 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 259 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | /// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after |
| 261 | /// parsing 'declaration-specifiers declarator'. This method is split out this |
| 262 | /// way to handle the ambiguity between top-level function-definitions and |
| 263 | /// declarations. |
| 264 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 265 | /// init-declarator-list: [C99 6.7] |
| 266 | /// init-declarator |
| 267 | /// init-declarator-list ',' init-declarator |
| 268 | /// init-declarator: [C99 6.7] |
| 269 | /// declarator |
| 270 | /// declarator '=' initializer |
| 271 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 272 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 273 | /// [C++] declarator initializer[opt] |
| 274 | /// |
| 275 | /// [C++] initializer: |
| 276 | /// [C++] '=' initializer-clause |
| 277 | /// [C++] '(' expression-list ')' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 278 | /// |
| 279 | Parser::DeclTy *Parser:: |
| 280 | ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) { |
| 281 | |
| 282 | // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so |
| 283 | // that they can be chained properly if the actions want this. |
| 284 | Parser::DeclTy *LastDeclInGroup = 0; |
| 285 | |
| 286 | // At this point, we know that it is not a function definition. Parse the |
| 287 | // rest of the init-declarator-list. |
| 288 | while (1) { |
| 289 | // If a simple-asm-expr is present, parse it. |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 290 | if (Tok.is(tok::kw_asm)) { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 291 | OwningExprResult AsmLabel(ParseSimpleAsm()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 292 | if (AsmLabel.isInvalid()) { |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 293 | SkipUntil(tok::semi); |
| 294 | return 0; |
| 295 | } |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 296 | |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 297 | D.setAsmLabel(AsmLabel.release()); |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 298 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 299 | |
| 300 | // If attributes are present, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 301 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 302 | D.AddAttributes(ParseAttributes()); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 303 | |
| 304 | // Inform the current actions module that we just parsed this declarator. |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 305 | LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 306 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | // Parse declarator '=' initializer. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 308 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | ConsumeToken(); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 310 | OwningExprResult Init(ParseInitializer()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 311 | if (Init.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 312 | SkipUntil(tok::semi); |
| 313 | return 0; |
| 314 | } |
Sebastian Redl | f512e82 | 2009-01-18 18:03:53 +0000 | [diff] [blame] | 315 | Actions.AddInitializerToDecl(LastDeclInGroup, move_arg(Init)); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 316 | } else if (Tok.is(tok::l_paren)) { |
| 317 | // Parse C++ direct initializer: '(' expression-list ')' |
| 318 | SourceLocation LParenLoc = ConsumeParen(); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 319 | ExprVector Exprs(Actions); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 320 | CommaLocsTy CommaLocs; |
| 321 | |
| 322 | bool InvalidExpr = false; |
| 323 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 324 | SkipUntil(tok::r_paren); |
| 325 | InvalidExpr = true; |
| 326 | } |
| 327 | // Match the ')'. |
| 328 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 329 | |
| 330 | if (!InvalidExpr) { |
| 331 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 332 | "Unexpected number of commas!"); |
| 333 | Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc, |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 334 | Exprs.take(), Exprs.size(), |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 335 | &CommaLocs[0], RParenLoc); |
| 336 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 337 | } else { |
| 338 | Actions.ActOnUninitializedDecl(LastDeclInGroup); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 341 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 342 | // error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 343 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 344 | break; |
| 345 | |
| 346 | // Consume the comma. |
| 347 | ConsumeToken(); |
| 348 | |
| 349 | // Parse the next declarator. |
| 350 | D.clear(); |
Chris Lattner | aab740a | 2008-10-20 04:57:38 +0000 | [diff] [blame] | 351 | |
| 352 | // Accept attributes in an init-declarator. In the first declarator in a |
| 353 | // declaration, these would be part of the declspec. In subsequent |
| 354 | // declarators, they become part of the declarator itself, so that they |
| 355 | // don't apply to declarators after *this* one. Examples: |
| 356 | // short __attribute__((common)) var; -> declspec |
| 357 | // short var __attribute__((common)); -> declarator |
| 358 | // short x, __attribute__((common)) var; -> declarator |
| 359 | if (Tok.is(tok::kw___attribute)) |
| 360 | D.AddAttributes(ParseAttributes()); |
| 361 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 362 | ParseDeclarator(D); |
| 363 | } |
| 364 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 365 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 366 | ConsumeToken(); |
Fariborz Jahanian | 41f2b32 | 2009-01-17 00:00:40 +0000 | [diff] [blame] | 367 | // for(is key; in keys) is error. |
| 368 | if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) { |
| 369 | Diag(Tok, diag::err_parse_error); |
| 370 | return 0; |
| 371 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 372 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 373 | } |
Fariborz Jahanian | bdd15f7 | 2008-01-04 23:23:46 +0000 | [diff] [blame] | 374 | // If this is an ObjC2 for-each loop, this is a successful declarator |
| 375 | // parse. The syntax for these looks like: |
| 376 | // 'for' '(' declaration 'in' expr ')' statement |
Fariborz Jahanian | 335a2d4 | 2008-01-04 23:04:08 +0000 | [diff] [blame] | 377 | if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) { |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 378 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 379 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 380 | Diag(Tok, diag::err_parse_error); |
| 381 | // Skip to end of block or statement |
Chris Lattner | ed44238 | 2007-08-21 18:36:18 +0000 | [diff] [blame] | 382 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 383 | if (Tok.is(tok::semi)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 384 | ConsumeToken(); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | /// ParseSpecifierQualifierList |
| 389 | /// specifier-qualifier-list: |
| 390 | /// type-specifier specifier-qualifier-list[opt] |
| 391 | /// type-qualifier specifier-qualifier-list[opt] |
| 392 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 393 | /// |
| 394 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { |
| 395 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 396 | /// parse declaration-specifiers and complain about extra stuff. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 397 | ParseDeclarationSpecifiers(DS); |
| 398 | |
| 399 | // Validate declspec for type-name. |
| 400 | unsigned Specs = DS.getParsedSpecifiers(); |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 401 | if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 402 | Diag(Tok, diag::err_typename_requires_specqual); |
| 403 | |
| 404 | // Issue diagnostic and remove storage class if present. |
| 405 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 406 | if (DS.getStorageClassSpecLoc().isValid()) |
| 407 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 408 | else |
| 409 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 410 | DS.ClearStorageClassSpecs(); |
| 411 | } |
| 412 | |
| 413 | // Issue diagnostic and remove function specfier if present. |
| 414 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 415 | if (DS.isInlineSpecified()) |
| 416 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 417 | if (DS.isVirtualSpecified()) |
| 418 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 419 | if (DS.isExplicitSpecified()) |
| 420 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 421 | DS.ClearFunctionSpecs(); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /// ParseDeclarationSpecifiers |
| 426 | /// declaration-specifiers: [C99 6.7] |
| 427 | /// storage-class-specifier declaration-specifiers[opt] |
| 428 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 429 | /// [C99] function-specifier declaration-specifiers[opt] |
| 430 | /// [GNU] attributes declaration-specifiers[opt] |
| 431 | /// |
| 432 | /// storage-class-specifier: [C99 6.7.1] |
| 433 | /// 'typedef' |
| 434 | /// 'extern' |
| 435 | /// 'static' |
| 436 | /// 'auto' |
| 437 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 438 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 439 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 440 | /// function-specifier: [C99 6.7.4] |
| 441 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 442 | /// [C++] 'virtual' |
| 443 | /// [C++] 'explicit' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 444 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 445 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 446 | TemplateParameterLists *TemplateParams){ |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 447 | DS.SetRangeStart(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 448 | while (1) { |
| 449 | int isInvalid = false; |
| 450 | const char *PrevSpec = 0; |
| 451 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 452 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 453 | switch (Tok.getKind()) { |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 454 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 455 | DoneWithDeclSpec: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 456 | // If this is not a declaration specifier token, we're done reading decl |
| 457 | // specifiers. First verify that DeclSpec's are consistent. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 458 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 459 | return; |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 460 | |
| 461 | case tok::coloncolon: // ::foo::bar |
| 462 | // Annotate C++ scope specifiers. If we get one, loop. |
| 463 | if (TryAnnotateCXXScopeToken()) |
| 464 | continue; |
| 465 | goto DoneWithDeclSpec; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 466 | |
| 467 | case tok::annot_cxxscope: { |
| 468 | if (DS.hasTypeSpecifier()) |
| 469 | goto DoneWithDeclSpec; |
| 470 | |
| 471 | // We are looking for a qualified typename. |
| 472 | if (NextToken().isNot(tok::identifier)) |
| 473 | goto DoneWithDeclSpec; |
| 474 | |
| 475 | CXXScopeSpec SS; |
| 476 | SS.setScopeRep(Tok.getAnnotationValue()); |
| 477 | SS.setRange(Tok.getAnnotationRange()); |
| 478 | |
| 479 | // If the next token is the name of the class type that the C++ scope |
| 480 | // denotes, followed by a '(', then this is a constructor declaration. |
| 481 | // We're done with the decl-specifiers. |
| 482 | if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(), |
| 483 | CurScope, &SS) && |
| 484 | GetLookAheadToken(2).is(tok::l_paren)) |
| 485 | goto DoneWithDeclSpec; |
| 486 | |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame^] | 487 | Token Next = NextToken(); |
| 488 | TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 489 | Next.getLocation(), CurScope, &SS); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 490 | if (TypeRep == 0) |
| 491 | goto DoneWithDeclSpec; |
| 492 | |
| 493 | ConsumeToken(); // The C++ scope. |
| 494 | |
| 495 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
| 496 | TypeRep); |
| 497 | if (isInvalid) |
| 498 | break; |
| 499 | |
| 500 | DS.SetRangeEnd(Tok.getLocation()); |
| 501 | ConsumeToken(); // The typename. |
| 502 | |
| 503 | continue; |
| 504 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 505 | |
| 506 | case tok::annot_typename: { |
| 507 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
| 508 | Tok.getAnnotationValue()); |
| 509 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 510 | ConsumeToken(); // The typename |
| 511 | |
| 512 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 513 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 514 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 515 | // just a normal reference to a typedef name. |
| 516 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 517 | continue; |
| 518 | |
| 519 | SourceLocation EndProtoLoc; |
| 520 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
| 521 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
| 522 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
| 523 | |
| 524 | DS.SetRangeEnd(EndProtoLoc); |
| 525 | continue; |
| 526 | } |
| 527 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 528 | // typedef-name |
| 529 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 530 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 531 | // so handle it as such. This is important for ctor parsing. |
Chris Lattner | 837acd0 | 2009-01-21 19:19:26 +0000 | [diff] [blame] | 532 | if (getLang().CPlusPlus && TryAnnotateCXXScopeToken()) |
| 533 | continue; |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 534 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 535 | // This identifier can only be a typedef name if we haven't already seen |
| 536 | // a type-specifier. Without this check we misparse: |
| 537 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 538 | if (DS.hasTypeSpecifier()) |
| 539 | goto DoneWithDeclSpec; |
| 540 | |
| 541 | // It has to be available as a typedef too! |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame^] | 542 | TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 543 | Tok.getLocation(), CurScope); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 544 | if (TypeRep == 0) |
| 545 | goto DoneWithDeclSpec; |
| 546 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 547 | // C++: If the identifier is actually the name of the class type |
| 548 | // being defined and the next token is a '(', then this is a |
| 549 | // constructor declaration. We're done with the decl-specifiers |
| 550 | // and will treat this token as an identifier. |
| 551 | if (getLang().CPlusPlus && |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 552 | CurScope->isClassScope() && |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 553 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) && |
| 554 | NextToken().getKind() == tok::l_paren) |
| 555 | goto DoneWithDeclSpec; |
| 556 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 557 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
| 558 | TypeRep); |
| 559 | if (isInvalid) |
| 560 | break; |
| 561 | |
| 562 | DS.SetRangeEnd(Tok.getLocation()); |
| 563 | ConsumeToken(); // The identifier |
| 564 | |
| 565 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 566 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 567 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 568 | // just a normal reference to a typedef name. |
| 569 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 570 | continue; |
| 571 | |
| 572 | SourceLocation EndProtoLoc; |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 573 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 574 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 575 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 576 | |
| 577 | DS.SetRangeEnd(EndProtoLoc); |
| 578 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 579 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 580 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 581 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 582 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 583 | // GNU attributes support. |
| 584 | case tok::kw___attribute: |
| 585 | DS.AddAttributes(ParseAttributes()); |
| 586 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 587 | |
| 588 | // Microsoft declspec support. |
| 589 | case tok::kw___declspec: |
| 590 | if (!PP.getLangOptions().Microsoft) |
| 591 | goto DoneWithDeclSpec; |
| 592 | FuzzyParseMicrosoftDeclSpec(); |
| 593 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 594 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 595 | // Microsoft single token adornments. |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 596 | case tok::kw___forceinline: |
| 597 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 598 | case tok::kw___cdecl: |
| 599 | case tok::kw___stdcall: |
| 600 | case tok::kw___fastcall: |
| 601 | if (!PP.getLangOptions().Microsoft) |
| 602 | goto DoneWithDeclSpec; |
| 603 | // Just ignore it. |
| 604 | break; |
| 605 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 606 | // storage-class-specifier |
| 607 | case tok::kw_typedef: |
| 608 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec); |
| 609 | break; |
| 610 | case tok::kw_extern: |
| 611 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 612 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 613 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec); |
| 614 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 615 | case tok::kw___private_extern__: |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 616 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, |
| 617 | PrevSpec); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 618 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 619 | case tok::kw_static: |
| 620 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 621 | Diag(Tok, diag::ext_thread_before) << "static"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 622 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec); |
| 623 | break; |
| 624 | case tok::kw_auto: |
| 625 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec); |
| 626 | break; |
| 627 | case tok::kw_register: |
| 628 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec); |
| 629 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 630 | case tok::kw_mutable: |
| 631 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec); |
| 632 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 633 | case tok::kw___thread: |
| 634 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2; |
| 635 | break; |
| 636 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 637 | continue; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 638 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 639 | // function-specifier |
| 640 | case tok::kw_inline: |
| 641 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec); |
| 642 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 643 | case tok::kw_virtual: |
| 644 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec); |
| 645 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 646 | case tok::kw_explicit: |
| 647 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec); |
| 648 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 649 | |
| 650 | // type-specifier |
| 651 | case tok::kw_short: |
| 652 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
| 653 | break; |
| 654 | case tok::kw_long: |
| 655 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
| 656 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
| 657 | else |
| 658 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec); |
| 659 | break; |
| 660 | case tok::kw_signed: |
| 661 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
| 662 | break; |
| 663 | case tok::kw_unsigned: |
| 664 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
| 665 | break; |
| 666 | case tok::kw__Complex: |
| 667 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec); |
| 668 | break; |
| 669 | case tok::kw__Imaginary: |
| 670 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec); |
| 671 | break; |
| 672 | case tok::kw_void: |
| 673 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
| 674 | break; |
| 675 | case tok::kw_char: |
| 676 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
| 677 | break; |
| 678 | case tok::kw_int: |
| 679 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
| 680 | break; |
| 681 | case tok::kw_float: |
| 682 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
| 683 | break; |
| 684 | case tok::kw_double: |
| 685 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
| 686 | break; |
| 687 | case tok::kw_wchar_t: |
| 688 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec); |
| 689 | break; |
| 690 | case tok::kw_bool: |
| 691 | case tok::kw__Bool: |
| 692 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
| 693 | break; |
| 694 | case tok::kw__Decimal32: |
| 695 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec); |
| 696 | break; |
| 697 | case tok::kw__Decimal64: |
| 698 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec); |
| 699 | break; |
| 700 | case tok::kw__Decimal128: |
| 701 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec); |
| 702 | break; |
| 703 | |
| 704 | // class-specifier: |
| 705 | case tok::kw_class: |
| 706 | case tok::kw_struct: |
| 707 | case tok::kw_union: |
| 708 | ParseClassSpecifier(DS, TemplateParams); |
| 709 | continue; |
| 710 | |
| 711 | // enum-specifier: |
| 712 | case tok::kw_enum: |
| 713 | ParseEnumSpecifier(DS); |
| 714 | continue; |
| 715 | |
| 716 | // cv-qualifier: |
| 717 | case tok::kw_const: |
| 718 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2; |
| 719 | break; |
| 720 | case tok::kw_volatile: |
| 721 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 722 | getLang())*2; |
| 723 | break; |
| 724 | case tok::kw_restrict: |
| 725 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 726 | getLang())*2; |
| 727 | break; |
| 728 | |
| 729 | // GNU typeof support. |
| 730 | case tok::kw_typeof: |
| 731 | ParseTypeofSpecifier(DS); |
| 732 | continue; |
| 733 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 734 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 735 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 736 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 737 | // but we support it. |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 738 | if (DS.hasTypeSpecifier() || !getLang().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 739 | goto DoneWithDeclSpec; |
| 740 | |
| 741 | { |
| 742 | SourceLocation EndProtoLoc; |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 743 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 744 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 745 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 746 | DS.SetRangeEnd(EndProtoLoc); |
| 747 | |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 748 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 749 | << SourceRange(Loc, EndProtoLoc); |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 750 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 751 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 752 | continue; |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 753 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 754 | } |
| 755 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 756 | if (isInvalid) { |
| 757 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 758 | // Pick between error or extwarn. |
| 759 | unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination |
| 760 | : diag::ext_duplicate_declspec; |
| 761 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 762 | } |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 763 | DS.SetRangeEnd(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 764 | ConsumeToken(); |
| 765 | } |
| 766 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 767 | |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 768 | /// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 769 | /// primarily follow the C++ grammar with additions for C99 and GNU, |
| 770 | /// which together subsume the C grammar. Note that the C++ |
| 771 | /// type-specifier also includes the C type-qualifier (for const, |
| 772 | /// volatile, and C99 restrict). Returns true if a type-specifier was |
| 773 | /// found (and parsed), false otherwise. |
| 774 | /// |
| 775 | /// type-specifier: [C++ 7.1.5] |
| 776 | /// simple-type-specifier |
| 777 | /// class-specifier |
| 778 | /// enum-specifier |
| 779 | /// elaborated-type-specifier [TODO] |
| 780 | /// cv-qualifier |
| 781 | /// |
| 782 | /// cv-qualifier: [C++ 7.1.5.1] |
| 783 | /// 'const' |
| 784 | /// 'volatile' |
| 785 | /// [C99] 'restrict' |
| 786 | /// |
| 787 | /// simple-type-specifier: [ C++ 7.1.5.2] |
| 788 | /// '::'[opt] nested-name-specifier[opt] type-name [TODO] |
| 789 | /// '::'[opt] nested-name-specifier 'template' template-id [TODO] |
| 790 | /// 'char' |
| 791 | /// 'wchar_t' |
| 792 | /// 'bool' |
| 793 | /// 'short' |
| 794 | /// 'int' |
| 795 | /// 'long' |
| 796 | /// 'signed' |
| 797 | /// 'unsigned' |
| 798 | /// 'float' |
| 799 | /// 'double' |
| 800 | /// 'void' |
| 801 | /// [C99] '_Bool' |
| 802 | /// [C99] '_Complex' |
| 803 | /// [C99] '_Imaginary' // Removed in TC2? |
| 804 | /// [GNU] '_Decimal32' |
| 805 | /// [GNU] '_Decimal64' |
| 806 | /// [GNU] '_Decimal128' |
| 807 | /// [GNU] typeof-specifier |
| 808 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
| 809 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 810 | bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid, |
| 811 | const char *&PrevSpec, |
| 812 | TemplateParameterLists *TemplateParams){ |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 813 | SourceLocation Loc = Tok.getLocation(); |
| 814 | |
| 815 | switch (Tok.getKind()) { |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 816 | case tok::identifier: // foo::bar |
| 817 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 818 | // recurse to handle whatever we get. |
| 819 | if (TryAnnotateTypeOrScopeToken()) |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 820 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams); |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 821 | // Otherwise, not a type specifier. |
| 822 | return false; |
| 823 | case tok::coloncolon: // ::foo::bar |
| 824 | if (NextToken().is(tok::kw_new) || // ::new |
| 825 | NextToken().is(tok::kw_delete)) // ::delete |
| 826 | return false; |
| 827 | |
| 828 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 829 | // recurse to handle whatever we get. |
| 830 | if (TryAnnotateTypeOrScopeToken()) |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 831 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams); |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 832 | // Otherwise, not a type specifier. |
| 833 | return false; |
| 834 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 835 | // simple-type-specifier: |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 836 | case tok::annot_typename: { |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 837 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 838 | Tok.getAnnotationValue()); |
| 839 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 840 | ConsumeToken(); // The typename |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 841 | |
| 842 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 843 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 844 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 845 | // just a normal reference to a typedef name. |
| 846 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 847 | return true; |
| 848 | |
| 849 | SourceLocation EndProtoLoc; |
| 850 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
| 851 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
| 852 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
| 853 | |
| 854 | DS.SetRangeEnd(EndProtoLoc); |
| 855 | return true; |
| 856 | } |
| 857 | |
| 858 | case tok::kw_short: |
| 859 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
| 860 | break; |
| 861 | case tok::kw_long: |
| 862 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
| 863 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
| 864 | else |
| 865 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec); |
| 866 | break; |
| 867 | case tok::kw_signed: |
| 868 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
| 869 | break; |
| 870 | case tok::kw_unsigned: |
| 871 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
| 872 | break; |
| 873 | case tok::kw__Complex: |
| 874 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec); |
| 875 | break; |
| 876 | case tok::kw__Imaginary: |
| 877 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec); |
| 878 | break; |
| 879 | case tok::kw_void: |
| 880 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
| 881 | break; |
| 882 | case tok::kw_char: |
| 883 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
| 884 | break; |
| 885 | case tok::kw_int: |
| 886 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
| 887 | break; |
| 888 | case tok::kw_float: |
| 889 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
| 890 | break; |
| 891 | case tok::kw_double: |
| 892 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
| 893 | break; |
| 894 | case tok::kw_wchar_t: |
| 895 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec); |
| 896 | break; |
| 897 | case tok::kw_bool: |
| 898 | case tok::kw__Bool: |
| 899 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
| 900 | break; |
| 901 | case tok::kw__Decimal32: |
| 902 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec); |
| 903 | break; |
| 904 | case tok::kw__Decimal64: |
| 905 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec); |
| 906 | break; |
| 907 | case tok::kw__Decimal128: |
| 908 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec); |
| 909 | break; |
| 910 | |
| 911 | // class-specifier: |
| 912 | case tok::kw_class: |
| 913 | case tok::kw_struct: |
| 914 | case tok::kw_union: |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 915 | ParseClassSpecifier(DS, TemplateParams); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 916 | return true; |
| 917 | |
| 918 | // enum-specifier: |
| 919 | case tok::kw_enum: |
| 920 | ParseEnumSpecifier(DS); |
| 921 | return true; |
| 922 | |
| 923 | // cv-qualifier: |
| 924 | case tok::kw_const: |
| 925 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 926 | getLang())*2; |
| 927 | break; |
| 928 | case tok::kw_volatile: |
| 929 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 930 | getLang())*2; |
| 931 | break; |
| 932 | case tok::kw_restrict: |
| 933 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 934 | getLang())*2; |
| 935 | break; |
| 936 | |
| 937 | // GNU typeof support. |
| 938 | case tok::kw_typeof: |
| 939 | ParseTypeofSpecifier(DS); |
| 940 | return true; |
| 941 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 942 | case tok::kw___cdecl: |
| 943 | case tok::kw___stdcall: |
| 944 | case tok::kw___fastcall: |
Chris Lattner | 837acd0 | 2009-01-21 19:19:26 +0000 | [diff] [blame] | 945 | if (!PP.getLangOptions().Microsoft) return false; |
| 946 | ConsumeToken(); |
| 947 | return true; |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 948 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 949 | default: |
| 950 | // Not a type-specifier; do nothing. |
| 951 | return false; |
| 952 | } |
| 953 | |
| 954 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 955 | if (isInvalid) { |
| 956 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 957 | // Pick between error or extwarn. |
| 958 | unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination |
| 959 | : diag::ext_duplicate_declspec; |
| 960 | Diag(Tok, DiagID) << PrevSpec; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 961 | } |
| 962 | DS.SetRangeEnd(Tok.getLocation()); |
| 963 | ConsumeToken(); // whatever we parsed above. |
| 964 | return true; |
| 965 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 966 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 967 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 968 | /// semicolon. |
| 969 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 970 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 971 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 972 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 973 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 974 | /// struct-declarator-list: |
| 975 | /// struct-declarator |
| 976 | /// struct-declarator-list ',' struct-declarator |
| 977 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 978 | /// struct-declarator: |
| 979 | /// declarator |
| 980 | /// [GNU] declarator attributes[opt] |
| 981 | /// declarator[opt] ':' constant-expression |
| 982 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 983 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 984 | void Parser:: |
| 985 | ParseStructDeclaration(DeclSpec &DS, |
| 986 | llvm::SmallVectorImpl<FieldDeclarator> &Fields) { |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 987 | if (Tok.is(tok::kw___extension__)) { |
| 988 | // __extension__ silences extension warnings in the subexpression. |
| 989 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 990 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 991 | return ParseStructDeclaration(DS, Fields); |
| 992 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 993 | |
| 994 | // Parse the common specifier-qualifiers-list piece. |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 995 | SourceLocation DSStart = Tok.getLocation(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 996 | ParseSpecifierQualifierList(DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 997 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 998 | // If there are no declarators, this is a free-standing declaration |
| 999 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1000 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 1001 | Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1002 | return; |
| 1003 | } |
| 1004 | |
| 1005 | // Read struct-declarators until we find the semicolon. |
Chris Lattner | ebe457c | 2008-04-10 16:37:40 +0000 | [diff] [blame] | 1006 | Fields.push_back(FieldDeclarator(DS)); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1007 | while (1) { |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1008 | FieldDeclarator &DeclaratorInfo = Fields.back(); |
| 1009 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1010 | /// struct-declarator: declarator |
| 1011 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1012 | if (Tok.isNot(tok::colon)) |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1013 | ParseDeclarator(DeclaratorInfo.D); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1014 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1015 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1016 | ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1017 | OwningExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1018 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1019 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 1020 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1021 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | // If attributes exist after the declarator, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1025 | if (Tok.is(tok::kw___attribute)) |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1026 | DeclaratorInfo.D.AddAttributes(ParseAttributes()); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1027 | |
| 1028 | // If we don't have a comma, it is either the end of the list (a ';') |
| 1029 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1030 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1031 | return; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1032 | |
| 1033 | // Consume the comma. |
| 1034 | ConsumeToken(); |
| 1035 | |
| 1036 | // Parse the next declarator. |
Chris Lattner | ebe457c | 2008-04-10 16:37:40 +0000 | [diff] [blame] | 1037 | Fields.push_back(FieldDeclarator(DS)); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1038 | |
| 1039 | // Attributes are only allowed on the second declarator. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1040 | if (Tok.is(tok::kw___attribute)) |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1041 | Fields.back().D.AddAttributes(ParseAttributes()); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1042 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | /// ParseStructUnionBody |
| 1046 | /// struct-contents: |
| 1047 | /// struct-declaration-list |
| 1048 | /// [EXT] empty |
| 1049 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 1050 | /// struct-declaration-list: |
| 1051 | /// struct-declaration |
| 1052 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1053 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1054 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1055 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
| 1056 | unsigned TagType, DeclTy *TagDecl) { |
| 1057 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 1058 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 1059 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1060 | Actions.ActOnTagStartDefinition(CurScope, TagDecl); |
| 1061 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1062 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 1063 | // C++. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1064 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1065 | Diag(Tok, diag::ext_empty_struct_union_enum) |
| 1066 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1067 | |
| 1068 | llvm::SmallVector<DeclTy*, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1069 | llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators; |
| 1070 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1071 | // While we still have something to read, read the declarations in the struct. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1072 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1073 | // Each iteration of this loop reads one struct-declaration. |
| 1074 | |
| 1075 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1076 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1077 | Diag(Tok, diag::ext_extra_struct_semi); |
| 1078 | ConsumeToken(); |
| 1079 | continue; |
| 1080 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1081 | |
| 1082 | // Parse all the comma separated declarators. |
| 1083 | DeclSpec DS; |
| 1084 | FieldDeclarators.clear(); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1085 | if (!Tok.is(tok::at)) { |
| 1086 | ParseStructDeclaration(DS, FieldDeclarators); |
| 1087 | |
| 1088 | // Convert them all to fields. |
| 1089 | for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) { |
| 1090 | FieldDeclarator &FD = FieldDeclarators[i]; |
| 1091 | // Install the declarator into the current TagDecl. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1092 | DeclTy *Field = Actions.ActOnField(CurScope, TagDecl, |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1093 | DS.getSourceRange().getBegin(), |
| 1094 | FD.D, FD.BitfieldSize); |
| 1095 | FieldDecls.push_back(Field); |
| 1096 | } |
| 1097 | } else { // Handle @defs |
| 1098 | ConsumeToken(); |
| 1099 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 1100 | Diag(Tok, diag::err_unexpected_at); |
| 1101 | SkipUntil(tok::semi, true, true); |
| 1102 | continue; |
| 1103 | } |
| 1104 | ConsumeToken(); |
| 1105 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 1106 | if (!Tok.is(tok::identifier)) { |
| 1107 | Diag(Tok, diag::err_expected_ident); |
| 1108 | SkipUntil(tok::semi, true, true); |
| 1109 | continue; |
| 1110 | } |
| 1111 | llvm::SmallVector<DeclTy*, 16> Fields; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1112 | Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(), |
| 1113 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1114 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 1115 | ConsumeToken(); |
| 1116 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
| 1117 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1118 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1119 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1120 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1121 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1122 | Diag(Tok, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1123 | break; |
| 1124 | } else { |
| 1125 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 1126 | // Skip to end of block or statement |
| 1127 | SkipUntil(tok::r_brace, true, true); |
| 1128 | } |
| 1129 | } |
| 1130 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1131 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1132 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1133 | AttributeList *AttrList = 0; |
| 1134 | // If attributes exist after struct contents, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1135 | if (Tok.is(tok::kw___attribute)) |
Daniel Dunbar | 5e592d8 | 2008-10-03 16:42:10 +0000 | [diff] [blame] | 1136 | AttrList = ParseAttributes(); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 1137 | |
| 1138 | Actions.ActOnFields(CurScope, |
| 1139 | RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(), |
| 1140 | LBraceLoc, RBraceLoc, |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1141 | AttrList); |
| 1142 | StructScope.Exit(); |
| 1143 | Actions.ActOnTagFinishDefinition(CurScope, TagDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | |
| 1147 | /// ParseEnumSpecifier |
| 1148 | /// enum-specifier: [C99 6.7.2.2] |
| 1149 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1150 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1151 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 1152 | /// '}' attributes[opt] |
| 1153 | /// 'enum' identifier |
| 1154 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1155 | /// |
| 1156 | /// [C++] elaborated-type-specifier: |
| 1157 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 1158 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1159 | void Parser::ParseEnumSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1160 | assert(Tok.is(tok::kw_enum) && "Not an enum specifier"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1161 | SourceLocation StartLoc = ConsumeToken(); |
| 1162 | |
| 1163 | // Parse the tag portion of this. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1164 | |
| 1165 | AttributeList *Attr = 0; |
| 1166 | // If attributes exist after tag, parse them. |
| 1167 | if (Tok.is(tok::kw___attribute)) |
| 1168 | Attr = ParseAttributes(); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1169 | |
| 1170 | CXXScopeSpec SS; |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 1171 | if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1172 | if (Tok.isNot(tok::identifier)) { |
| 1173 | Diag(Tok, diag::err_expected_ident); |
| 1174 | if (Tok.isNot(tok::l_brace)) { |
| 1175 | // Has no name and is not a definition. |
| 1176 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 1177 | SkipUntil(tok::comma, true); |
| 1178 | return; |
| 1179 | } |
| 1180 | } |
| 1181 | } |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1182 | |
| 1183 | // Must have either 'enum name' or 'enum {...}'. |
| 1184 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) { |
| 1185 | Diag(Tok, diag::err_expected_ident_lbrace); |
| 1186 | |
| 1187 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 1188 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1189 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | // If an identifier is present, consume and remember it. |
| 1193 | IdentifierInfo *Name = 0; |
| 1194 | SourceLocation NameLoc; |
| 1195 | if (Tok.is(tok::identifier)) { |
| 1196 | Name = Tok.getIdentifierInfo(); |
| 1197 | NameLoc = ConsumeToken(); |
| 1198 | } |
| 1199 | |
| 1200 | // There are three options here. If we have 'enum foo;', then this is a |
| 1201 | // forward declaration. If we have 'enum foo {...' then this is a |
| 1202 | // definition. Otherwise we have something like 'enum foo xyz', a reference. |
| 1203 | // |
| 1204 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 1205 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 1206 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 1207 | // |
| 1208 | Action::TagKind TK; |
| 1209 | if (Tok.is(tok::l_brace)) |
| 1210 | TK = Action::TK_Definition; |
| 1211 | else if (Tok.is(tok::semi)) |
| 1212 | TK = Action::TK_Declaration; |
| 1213 | else |
| 1214 | TK = Action::TK_Reference; |
| 1215 | DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1216 | SS, Name, NameLoc, Attr, |
| 1217 | Action::MultiTemplateParamsArg(Actions)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1219 | if (Tok.is(tok::l_brace)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1220 | ParseEnumBody(StartLoc, TagDecl); |
| 1221 | |
| 1222 | // TODO: semantic analysis on the declspec for enums. |
| 1223 | const char *PrevSpec = 0; |
| 1224 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl)) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1225 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 1229 | /// enumerator-list: |
| 1230 | /// enumerator |
| 1231 | /// enumerator-list ',' enumerator |
| 1232 | /// enumerator: |
| 1233 | /// enumeration-constant |
| 1234 | /// enumeration-constant '=' constant-expression |
| 1235 | /// enumeration-constant: |
| 1236 | /// identifier |
| 1237 | /// |
| 1238 | void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1239 | // Enter the scope of the enum body and start the definition. |
| 1240 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1241 | Actions.ActOnTagStartDefinition(CurScope, EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1242 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1243 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 1244 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 1245 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1246 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1247 | Diag(Tok, diag::ext_empty_struct_union_enum) << "enum"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1248 | |
| 1249 | llvm::SmallVector<DeclTy*, 32> EnumConstantDecls; |
| 1250 | |
| 1251 | DeclTy *LastEnumConstDecl = 0; |
| 1252 | |
| 1253 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1254 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1255 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 1256 | SourceLocation IdentLoc = ConsumeToken(); |
| 1257 | |
| 1258 | SourceLocation EqualLoc; |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1259 | OwningExprResult AssignedVal(Actions); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1260 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1261 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1262 | AssignedVal = ParseConstantExpression(); |
| 1263 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1264 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | // Install the enumerator constant into EnumDecl. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1268 | DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1269 | LastEnumConstDecl, |
| 1270 | IdentLoc, Ident, |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1271 | EqualLoc, |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1272 | AssignedVal.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1273 | EnumConstantDecls.push_back(EnumConstDecl); |
| 1274 | LastEnumConstDecl = EnumConstDecl; |
| 1275 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1276 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1277 | break; |
| 1278 | SourceLocation CommaLoc = ConsumeToken(); |
| 1279 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1280 | if (Tok.isNot(tok::identifier) && !getLang().C99) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1281 | Diag(CommaLoc, diag::ext_c99_enumerator_list_comma); |
| 1282 | } |
| 1283 | |
| 1284 | // Eat the }. |
| 1285 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 1286 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1287 | Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0], |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1288 | EnumConstantDecls.size()); |
| 1289 | |
| 1290 | DeclTy *AttrList = 0; |
| 1291 | // If attributes exist after the identifier list, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1292 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1293 | AttrList = ParseAttributes(); // FIXME: where do they do? |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1294 | |
| 1295 | EnumScope.Exit(); |
| 1296 | Actions.ActOnTagFinishDefinition(CurScope, EnumDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 1300 | /// start of a type-qualifier-list. |
| 1301 | bool Parser::isTypeQualifier() const { |
| 1302 | switch (Tok.getKind()) { |
| 1303 | default: return false; |
| 1304 | // type-qualifier |
| 1305 | case tok::kw_const: |
| 1306 | case tok::kw_volatile: |
| 1307 | case tok::kw_restrict: |
| 1308 | return true; |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1313 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1314 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1315 | switch (Tok.getKind()) { |
| 1316 | default: return false; |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1317 | |
| 1318 | case tok::identifier: // foo::bar |
| 1319 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1320 | // recurse to handle whatever we get. |
| 1321 | if (TryAnnotateTypeOrScopeToken()) |
| 1322 | return isTypeSpecifierQualifier(); |
| 1323 | // Otherwise, not a type specifier. |
| 1324 | return false; |
| 1325 | case tok::coloncolon: // ::foo::bar |
| 1326 | if (NextToken().is(tok::kw_new) || // ::new |
| 1327 | NextToken().is(tok::kw_delete)) // ::delete |
| 1328 | return false; |
| 1329 | |
| 1330 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1331 | // recurse to handle whatever we get. |
| 1332 | if (TryAnnotateTypeOrScopeToken()) |
| 1333 | return isTypeSpecifierQualifier(); |
| 1334 | // Otherwise, not a type specifier. |
| 1335 | return false; |
| 1336 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1337 | // GNU attributes support. |
| 1338 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1339 | // GNU typeof support. |
| 1340 | case tok::kw_typeof: |
| 1341 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1342 | // type-specifiers |
| 1343 | case tok::kw_short: |
| 1344 | case tok::kw_long: |
| 1345 | case tok::kw_signed: |
| 1346 | case tok::kw_unsigned: |
| 1347 | case tok::kw__Complex: |
| 1348 | case tok::kw__Imaginary: |
| 1349 | case tok::kw_void: |
| 1350 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1351 | case tok::kw_wchar_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1352 | case tok::kw_int: |
| 1353 | case tok::kw_float: |
| 1354 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 1355 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1356 | case tok::kw__Bool: |
| 1357 | case tok::kw__Decimal32: |
| 1358 | case tok::kw__Decimal64: |
| 1359 | case tok::kw__Decimal128: |
| 1360 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 1361 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 1362 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1363 | case tok::kw_struct: |
| 1364 | case tok::kw_union: |
| 1365 | // enum-specifier |
| 1366 | case tok::kw_enum: |
| 1367 | |
| 1368 | // type-qualifier |
| 1369 | case tok::kw_const: |
| 1370 | case tok::kw_volatile: |
| 1371 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1372 | |
| 1373 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 1374 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1375 | return true; |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 1376 | |
| 1377 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 1378 | case tok::less: |
| 1379 | return getLang().ObjC1; |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1380 | |
| 1381 | case tok::kw___cdecl: |
| 1382 | case tok::kw___stdcall: |
| 1383 | case tok::kw___fastcall: |
| 1384 | return PP.getLangOptions().Microsoft; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 1389 | /// declaration specifier. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1390 | bool Parser::isDeclarationSpecifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1391 | switch (Tok.getKind()) { |
| 1392 | default: return false; |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1393 | |
| 1394 | case tok::identifier: // foo::bar |
| 1395 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1396 | // recurse to handle whatever we get. |
| 1397 | if (TryAnnotateTypeOrScopeToken()) |
| 1398 | return isDeclarationSpecifier(); |
| 1399 | // Otherwise, not a declaration specifier. |
| 1400 | return false; |
| 1401 | case tok::coloncolon: // ::foo::bar |
| 1402 | if (NextToken().is(tok::kw_new) || // ::new |
| 1403 | NextToken().is(tok::kw_delete)) // ::delete |
| 1404 | return false; |
| 1405 | |
| 1406 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1407 | // recurse to handle whatever we get. |
| 1408 | if (TryAnnotateTypeOrScopeToken()) |
| 1409 | return isDeclarationSpecifier(); |
| 1410 | // Otherwise, not a declaration specifier. |
| 1411 | return false; |
| 1412 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1413 | // storage-class-specifier |
| 1414 | case tok::kw_typedef: |
| 1415 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 1416 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1417 | case tok::kw_static: |
| 1418 | case tok::kw_auto: |
| 1419 | case tok::kw_register: |
| 1420 | case tok::kw___thread: |
| 1421 | |
| 1422 | // type-specifiers |
| 1423 | case tok::kw_short: |
| 1424 | case tok::kw_long: |
| 1425 | case tok::kw_signed: |
| 1426 | case tok::kw_unsigned: |
| 1427 | case tok::kw__Complex: |
| 1428 | case tok::kw__Imaginary: |
| 1429 | case tok::kw_void: |
| 1430 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1431 | case tok::kw_wchar_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1432 | case tok::kw_int: |
| 1433 | case tok::kw_float: |
| 1434 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 1435 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1436 | case tok::kw__Bool: |
| 1437 | case tok::kw__Decimal32: |
| 1438 | case tok::kw__Decimal64: |
| 1439 | case tok::kw__Decimal128: |
| 1440 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 1441 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 1442 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1443 | case tok::kw_struct: |
| 1444 | case tok::kw_union: |
| 1445 | // enum-specifier |
| 1446 | case tok::kw_enum: |
| 1447 | |
| 1448 | // type-qualifier |
| 1449 | case tok::kw_const: |
| 1450 | case tok::kw_volatile: |
| 1451 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1452 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1453 | // function-specifier |
| 1454 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1455 | case tok::kw_virtual: |
| 1456 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 1457 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1458 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 1459 | case tok::annot_typename: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1460 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 1461 | // GNU typeof support. |
| 1462 | case tok::kw_typeof: |
| 1463 | |
| 1464 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 1465 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1466 | return true; |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 1467 | |
| 1468 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 1469 | case tok::less: |
| 1470 | return getLang().ObjC1; |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1471 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 1472 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1473 | case tok::kw___cdecl: |
| 1474 | case tok::kw___stdcall: |
| 1475 | case tok::kw___fastcall: |
| 1476 | return PP.getLangOptions().Microsoft; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | |
| 1481 | /// ParseTypeQualifierListOpt |
| 1482 | /// type-qualifier-list: [C99 6.7.5] |
| 1483 | /// type-qualifier |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1484 | /// [GNU] attributes [ only if AttributesAllowed=true ] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1485 | /// type-qualifier-list type-qualifier |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1486 | /// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1487 | /// |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1488 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1489 | while (1) { |
| 1490 | int isInvalid = false; |
| 1491 | const char *PrevSpec = 0; |
| 1492 | SourceLocation Loc = Tok.getLocation(); |
| 1493 | |
| 1494 | switch (Tok.getKind()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1495 | case tok::kw_const: |
| 1496 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 1497 | getLang())*2; |
| 1498 | break; |
| 1499 | case tok::kw_volatile: |
| 1500 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 1501 | getLang())*2; |
| 1502 | break; |
| 1503 | case tok::kw_restrict: |
| 1504 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 1505 | getLang())*2; |
| 1506 | break; |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 1507 | case tok::kw___ptr64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1508 | case tok::kw___cdecl: |
| 1509 | case tok::kw___stdcall: |
| 1510 | case tok::kw___fastcall: |
| 1511 | if (!PP.getLangOptions().Microsoft) |
| 1512 | goto DoneWithTypeQuals; |
| 1513 | // Just ignore it. |
| 1514 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1515 | case tok::kw___attribute: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1516 | if (AttributesAllowed) { |
| 1517 | DS.AddAttributes(ParseAttributes()); |
| 1518 | continue; // do *not* consume the next token! |
| 1519 | } |
| 1520 | // otherwise, FALL THROUGH! |
| 1521 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1522 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1523 | // If this is not a type-qualifier token, we're done reading type |
| 1524 | // qualifiers. First verify that DeclSpec's are consistent. |
| 1525 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
| 1526 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1527 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 1528 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1529 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 1530 | if (isInvalid) { |
| 1531 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1532 | // Pick between error or extwarn. |
| 1533 | unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination |
| 1534 | : diag::ext_duplicate_declspec; |
| 1535 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1536 | } |
| 1537 | ConsumeToken(); |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | |
| 1542 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 1543 | /// |
| 1544 | void Parser::ParseDeclarator(Declarator &D) { |
| 1545 | /// This implements the 'declarator' production in the C grammar, then checks |
| 1546 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1547 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1550 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 1551 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 1552 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1553 | /// ptr-operator production. |
| 1554 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1555 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 1556 | /// [C] pointer[opt] direct-declarator |
| 1557 | /// [C++] direct-declarator |
| 1558 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1559 | /// |
| 1560 | /// pointer: [C99 6.7.5] |
| 1561 | /// '*' type-qualifier-list[opt] |
| 1562 | /// '*' type-qualifier-list[opt] pointer |
| 1563 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1564 | /// ptr-operator: |
| 1565 | /// '*' cv-qualifier-seq[opt] |
| 1566 | /// '&' |
| 1567 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1568 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1569 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 1570 | DirectDeclParseFunction DirectDeclParser) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1571 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1572 | // C++ member pointers start with a '::' or a nested-name. |
| 1573 | // Member pointers get special handling, since there's no place for the |
| 1574 | // scope spec in the generic path below. |
| 1575 | if ((Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 1576 | Tok.is(tok::annot_cxxscope)) && getLang().CPlusPlus) { |
| 1577 | CXXScopeSpec SS; |
| 1578 | if (ParseOptionalCXXScopeSpecifier(SS)) { |
| 1579 | if(Tok.isNot(tok::star)) { |
| 1580 | // The scope spec really belongs to the direct-declarator. |
| 1581 | D.getCXXScopeSpec() = SS; |
| 1582 | if (DirectDeclParser) |
| 1583 | (this->*DirectDeclParser)(D); |
| 1584 | return; |
| 1585 | } |
| 1586 | |
| 1587 | SourceLocation Loc = ConsumeToken(); |
| 1588 | DeclSpec DS; |
| 1589 | ParseTypeQualifierListOpt(DS); |
| 1590 | |
| 1591 | // Recurse to parse whatever is left. |
| 1592 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 1593 | |
| 1594 | // Sema will have to catch (syntactically invalid) pointers into global |
| 1595 | // scope. It has to catch pointers into namespace scope anyway. |
| 1596 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
| 1597 | Loc,DS.TakeAttributes())); |
| 1598 | return; |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1603 | // Not a pointer, C++ reference, or block. |
| 1604 | if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) && |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1605 | (Kind != tok::caret || !getLang().Blocks)) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1606 | if (DirectDeclParser) |
| 1607 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1608 | return; |
| 1609 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1610 | |
Steve Naroff | 4ef1c99 | 2008-08-28 10:07:06 +0000 | [diff] [blame] | 1611 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1612 | SourceLocation Loc = ConsumeToken(); // Eat the * or &. |
| 1613 | |
Steve Naroff | 4ef1c99 | 2008-08-28 10:07:06 +0000 | [diff] [blame] | 1614 | if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 1615 | // Is a pointer. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1616 | DeclSpec DS; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1617 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1618 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1619 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1620 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1621 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1622 | if (Kind == tok::star) |
| 1623 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1624 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
| 1625 | DS.TakeAttributes())); |
| 1626 | else |
| 1627 | // Remember that we parsed a Block type, and remember the type-quals. |
| 1628 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
| 1629 | Loc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1630 | } else { |
| 1631 | // Is a reference |
| 1632 | DeclSpec DS; |
| 1633 | |
| 1634 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 1635 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 1636 | // template type argument, in which case the cv-qualifiers are ignored. |
| 1637 | // |
| 1638 | // [GNU] Retricted references are allowed. |
| 1639 | // [GNU] Attributes on references are allowed. |
| 1640 | ParseTypeQualifierListOpt(DS); |
| 1641 | |
| 1642 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 1643 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 1644 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1645 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1646 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 1647 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1648 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1652 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1653 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1654 | if (D.getNumTypeObjects() > 0) { |
| 1655 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 1656 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 1657 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 1658 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 1659 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 1660 | << II; |
| 1661 | else |
| 1662 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 1663 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1664 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1665 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1666 | // can go ahead and build the (technically ill-formed) |
| 1667 | // declarator: reference collapsing will take care of it. |
| 1668 | } |
| 1669 | } |
| 1670 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1671 | // Remember that we parsed a reference type. It doesn't have type-quals. |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 1672 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
| 1673 | DS.TakeAttributes())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | /// ParseDirectDeclarator |
| 1678 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1679 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1680 | /// '(' declarator ')' |
| 1681 | /// [GNU] '(' attributes declarator ')' |
| 1682 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1683 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1684 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1685 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1686 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1687 | /// direct-declarator '(' parameter-type-list ')' |
| 1688 | /// direct-declarator '(' identifier-list[opt] ')' |
| 1689 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 1690 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1691 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 1692 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1693 | /// [C++] declarator-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1694 | /// |
| 1695 | /// declarator-id: [C++ 8] |
| 1696 | /// id-expression |
| 1697 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 1698 | /// |
| 1699 | /// id-expression: [C++ 5.1] |
| 1700 | /// unqualified-id |
| 1701 | /// qualified-id [TODO] |
| 1702 | /// |
| 1703 | /// unqualified-id: [C++ 5.1] |
| 1704 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1705 | /// operator-function-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1706 | /// conversion-function-id [TODO] |
| 1707 | /// '~' class-name |
| 1708 | /// template-id [TODO] |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1709 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1710 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1711 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1712 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1713 | if (getLang().CPlusPlus) { |
| 1714 | if (D.mayHaveIdentifier()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1715 | // ParseDeclaratorInternal might already have parsed the scope. |
| 1716 | bool afterCXXScope = D.getCXXScopeSpec().isSet() || |
| 1717 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1718 | if (afterCXXScope) { |
| 1719 | // Change the declaration context for name lookup, until this function |
| 1720 | // is exited (and the declarator has been parsed). |
| 1721 | DeclScopeObj.EnterDeclaratorScope(); |
| 1722 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1723 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1724 | if (Tok.is(tok::identifier)) { |
| 1725 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 1726 | |
| 1727 | // If this identifier is followed by a '<', we may have a template-id. |
| 1728 | DeclTy *Template; |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1729 | if (NextToken().is(tok::less) && |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 1730 | (Template = Actions.isTemplateName(*Tok.getIdentifierInfo(), |
| 1731 | CurScope))) { |
| 1732 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1733 | AnnotateTemplateIdToken(Template, 0); |
| 1734 | // FIXME: Set the declarator to a template-id. How? I don't |
| 1735 | // know... for now, just use the identifier. |
| 1736 | D.SetIdentifier(II, Tok.getLocation()); |
| 1737 | } |
| 1738 | // If this identifier is the name of the current class, it's a |
| 1739 | // constructor name. |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1740 | else if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope)) |
Steve Naroff | b43a50f | 2009-01-28 19:39:02 +0000 | [diff] [blame] | 1741 | D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(), |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame^] | 1742 | Tok.getLocation(), CurScope), |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1743 | Tok.getLocation()); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 1744 | // This is a normal identifier. |
| 1745 | else |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1746 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 1747 | ConsumeToken(); |
| 1748 | goto PastIdentifier; |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1749 | } else if (Tok.is(tok::kw_operator)) { |
| 1750 | SourceLocation OperatorLoc = Tok.getLocation(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1751 | |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1752 | // First try the name of an overloaded operator |
| 1753 | if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) { |
| 1754 | D.setOverloadedOperator(Op, OperatorLoc); |
| 1755 | } else { |
| 1756 | // This must be a conversion function (C++ [class.conv.fct]). |
| 1757 | if (TypeTy *ConvType = ParseConversionFunctionId()) |
| 1758 | D.setConversionFunction(ConvType, OperatorLoc); |
| 1759 | else |
| 1760 | D.SetIdentifier(0, Tok.getLocation()); |
| 1761 | } |
| 1762 | goto PastIdentifier; |
| 1763 | } else if (Tok.is(tok::tilde)) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1764 | // This should be a C++ destructor. |
| 1765 | SourceLocation TildeLoc = ConsumeToken(); |
| 1766 | if (Tok.is(tok::identifier)) { |
| 1767 | if (TypeTy *Type = ParseClassName()) |
| 1768 | D.setDestructor(Type, TildeLoc); |
| 1769 | else |
| 1770 | D.SetIdentifier(0, TildeLoc); |
| 1771 | } else { |
| 1772 | Diag(Tok, diag::err_expected_class_name); |
| 1773 | D.SetIdentifier(0, TildeLoc); |
| 1774 | } |
| 1775 | goto PastIdentifier; |
| 1776 | } |
| 1777 | |
| 1778 | // If we reached this point, token is not identifier and not '~'. |
| 1779 | |
| 1780 | if (afterCXXScope) { |
| 1781 | Diag(Tok, diag::err_expected_unqualified_id); |
| 1782 | D.SetIdentifier(0, Tok.getLocation()); |
| 1783 | D.setInvalidType(true); |
| 1784 | goto PastIdentifier; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1785 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1786 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | // If we reached this point, we are either in C/ObjC or the token didn't |
| 1790 | // satisfy any of the C++-specific checks. |
| 1791 | |
| 1792 | if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
| 1793 | assert(!getLang().CPlusPlus && |
| 1794 | "There's a C++-specific check for tok::identifier above"); |
| 1795 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 1796 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 1797 | ConsumeToken(); |
| 1798 | } else if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1799 | // direct-declarator: '(' declarator ')' |
| 1800 | // direct-declarator: '(' attributes declarator ')' |
| 1801 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 1802 | ParseParenDeclarator(D); |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1803 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1804 | // This could be something simple like "int" (in which case the declarator |
| 1805 | // portion is empty), if an abstract-declarator is allowed. |
| 1806 | D.SetIdentifier(0, Tok.getLocation()); |
| 1807 | } else { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1808 | if (getLang().CPlusPlus) |
| 1809 | Diag(Tok, diag::err_expected_unqualified_id); |
| 1810 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1811 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1812 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 1813 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1816 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1817 | assert(D.isPastIdentifier() && |
| 1818 | "Haven't past the location of the identifier yet?"); |
| 1819 | |
| 1820 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1821 | if (Tok.is(tok::l_paren)) { |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1822 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 1823 | // In such a case, check if we actually have a function declarator; if it |
| 1824 | // is not, the declarator has been fully parsed. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1825 | if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 1826 | // When not in file scope, warn for ambiguous function declarators, just |
| 1827 | // in case the author intended it as a variable definition. |
| 1828 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 1829 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 1830 | break; |
| 1831 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1832 | ParseFunctionDeclarator(ConsumeParen(), D); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1833 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1834 | ParseBracketDeclarator(D); |
| 1835 | } else { |
| 1836 | break; |
| 1837 | } |
| 1838 | } |
| 1839 | } |
| 1840 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1841 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 1842 | /// only called before the identifier, so these are most likely just grouping |
| 1843 | /// parens for precedence. If we find that these are actually function |
| 1844 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 1845 | /// |
| 1846 | /// direct-declarator: |
| 1847 | /// '(' declarator ')' |
| 1848 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1849 | /// direct-declarator '(' parameter-type-list ')' |
| 1850 | /// direct-declarator '(' identifier-list[opt] ')' |
| 1851 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 1852 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1853 | /// |
| 1854 | void Parser::ParseParenDeclarator(Declarator &D) { |
| 1855 | SourceLocation StartLoc = ConsumeParen(); |
| 1856 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
| 1857 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1858 | // Eat any attributes before we look at whether this is a grouping or function |
| 1859 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 1860 | // the type being built up, for example: |
| 1861 | // int (__attribute__(()) *x)(long y) |
| 1862 | // If this ends up not being a grouping paren, the attribute applies to the |
| 1863 | // first argument, for example: |
| 1864 | // int (__attribute__(()) int x) |
| 1865 | // In either case, we need to eat any attributes to be able to determine what |
| 1866 | // sort of paren this is. |
| 1867 | // |
| 1868 | AttributeList *AttrList = 0; |
| 1869 | bool RequiresArg = false; |
| 1870 | if (Tok.is(tok::kw___attribute)) { |
| 1871 | AttrList = ParseAttributes(); |
| 1872 | |
| 1873 | // We require that the argument list (if this is a non-grouping paren) be |
| 1874 | // present even if the attribute list was empty. |
| 1875 | RequiresArg = true; |
| 1876 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1877 | // Eat any Microsoft extensions. |
Douglas Gregor | 5a2f5d3 | 2009-01-10 00:48:18 +0000 | [diff] [blame] | 1878 | while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
| 1879 | (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft) |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1880 | ConsumeToken(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1881 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1882 | // If we haven't past the identifier yet (or where the identifier would be |
| 1883 | // stored, if this is an abstract declarator), then this is probably just |
| 1884 | // grouping parens. However, if this could be an abstract-declarator, then |
| 1885 | // this could also be the start of function arguments (consider 'void()'). |
| 1886 | bool isGrouping; |
| 1887 | |
| 1888 | if (!D.mayOmitIdentifier()) { |
| 1889 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 1890 | // paren, because we haven't seen the identifier yet. |
| 1891 | isGrouping = true; |
| 1892 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Argyrios Kyrtzidis | e25d270 | 2008-10-06 00:07:55 +0000 | [diff] [blame] | 1893 | (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1894 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 1895 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 1896 | // considered to be a type, not a K&R identifier-list. |
| 1897 | isGrouping = false; |
| 1898 | } else { |
| 1899 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 1900 | isGrouping = true; |
| 1901 | } |
| 1902 | |
| 1903 | // If this is a grouping paren, handle: |
| 1904 | // direct-declarator: '(' declarator ')' |
| 1905 | // direct-declarator: '(' attributes declarator ')' |
| 1906 | if (isGrouping) { |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 1907 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1908 | D.setGroupingParens(true); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1909 | if (AttrList) |
| 1910 | D.AddAttributes(AttrList); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1911 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1912 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1913 | // Match the ')'. |
| 1914 | MatchRHSPunctuation(tok::r_paren, StartLoc); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 1915 | |
| 1916 | D.setGroupingParens(hadGroupingParens); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1917 | return; |
| 1918 | } |
| 1919 | |
| 1920 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 1921 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1922 | // identifier (and remember where it would have been), then call into |
| 1923 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1924 | D.SetIdentifier(0, Tok.getLocation()); |
| 1925 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1926 | ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 1930 | /// declarator D up to a paren, which indicates that we are parsing function |
| 1931 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1932 | /// |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1933 | /// If AttrList is non-null, then the caller parsed those arguments immediately |
| 1934 | /// after the open paren - they should be considered to be the first argument of |
| 1935 | /// a parameter. If RequiresArg is true, then the first argument of the |
| 1936 | /// function is required to be present and required to not be an identifier |
| 1937 | /// list. |
| 1938 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1939 | /// This method also handles this portion of the grammar: |
| 1940 | /// parameter-type-list: [C99 6.7.5] |
| 1941 | /// parameter-list |
| 1942 | /// parameter-list ',' '...' |
| 1943 | /// |
| 1944 | /// parameter-list: [C99 6.7.5] |
| 1945 | /// parameter-declaration |
| 1946 | /// parameter-list ',' parameter-declaration |
| 1947 | /// |
| 1948 | /// parameter-declaration: [C99 6.7.5] |
| 1949 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1950 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1951 | /// [GNU] declaration-specifiers declarator attributes |
| 1952 | /// declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1953 | /// [C++] declaration-specifiers abstract-declarator[opt] |
| 1954 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1955 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 1956 | /// |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1957 | /// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]" |
| 1958 | /// and "exception-specification[opt]"(TODO). |
| 1959 | /// |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1960 | void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D, |
| 1961 | AttributeList *AttrList, |
| 1962 | bool RequiresArg) { |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1963 | // lparen is already consumed! |
| 1964 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1965 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1966 | // This parameter list may be empty. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1967 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1968 | if (RequiresArg) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1969 | Diag(Tok, diag::err_argument_required_after_attribute); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1970 | delete AttrList; |
| 1971 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1972 | |
| 1973 | ConsumeParen(); // Eat the closing ')'. |
| 1974 | |
| 1975 | // cv-qualifier-seq[opt]. |
| 1976 | DeclSpec DS; |
| 1977 | if (getLang().CPlusPlus) { |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1978 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 1979 | |
| 1980 | // Parse exception-specification[opt]. |
| 1981 | if (Tok.is(tok::kw_throw)) |
| 1982 | ParseExceptionSpecification(); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1985 | // Remember that we parsed a function type, and remember the attributes. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1986 | // int() -> no prototype, no '...'. |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1987 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus, |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1988 | /*variadic*/ false, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1989 | /*arglist*/ 0, 0, |
| 1990 | DS.getTypeQualifiers(), |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 1991 | LParenLoc, D)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1992 | return; |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | // Alternatively, this parameter list may be an identifier list form for a |
| 1996 | // K&R-style function: void foo(a,b,c) |
Steve Naroff | 2d081c4 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 1997 | if (!getLang().CPlusPlus && Tok.is(tok::identifier)) { |
Steve Naroff | f64ef62 | 2009-01-30 14:23:32 +0000 | [diff] [blame] | 1998 | if (!TryAnnotateTypeOrScopeToken()) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1999 | // K&R identifier lists can't have typedefs as identifiers, per |
| 2000 | // C99 6.7.5.3p11. |
Steve Naroff | 2d081c4 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 2001 | if (RequiresArg) { |
| 2002 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 2003 | delete AttrList; |
| 2004 | } |
Steve Naroff | 2d081c4 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 2005 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 2006 | // normal declarators, not for abstract-declarators. |
| 2007 | return ParseFunctionDeclaratorIdentifierList(LParenLoc, D); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2008 | } |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | // Finally, a normal, non-empty parameter type list. |
| 2012 | |
| 2013 | // Build up an array of information about the parsed arguments. |
| 2014 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2015 | |
| 2016 | // Enter function-declaration scope, limiting any declarators to the |
| 2017 | // function prototype scope, including parameter declarators. |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2018 | ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2019 | |
| 2020 | bool IsVariadic = false; |
| 2021 | while (1) { |
| 2022 | if (Tok.is(tok::ellipsis)) { |
| 2023 | IsVariadic = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2024 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2025 | // Check to see if this is "void(...)" which is not allowed. |
Argyrios Kyrtzidis | e25d270 | 2008-10-06 00:07:55 +0000 | [diff] [blame] | 2026 | if (!getLang().CPlusPlus && ParamInfo.empty()) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2027 | // Otherwise, parse parameter type list. If it starts with an |
| 2028 | // ellipsis, diagnose the malformed function. |
| 2029 | Diag(Tok, diag::err_ellipsis_first_arg); |
| 2030 | IsVariadic = false; // Treat this like 'void()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2031 | } |
Chris Lattner | e0e713b | 2008-01-31 06:10:07 +0000 | [diff] [blame] | 2032 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2033 | ConsumeToken(); // Consume the ellipsis. |
| 2034 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2037 | SourceLocation DSStart = Tok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2038 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2039 | // Parse the declaration-specifiers. |
| 2040 | DeclSpec DS; |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2041 | |
| 2042 | // If the caller parsed attributes for the first argument, add them now. |
| 2043 | if (AttrList) { |
| 2044 | DS.AddAttributes(AttrList); |
| 2045 | AttrList = 0; // Only apply the attributes to the first parameter. |
| 2046 | } |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2047 | ParseDeclarationSpecifiers(DS); |
| 2048 | |
| 2049 | // Parse the declarator. This is "PrototypeContext", because we must |
| 2050 | // accept either 'declarator' or 'abstract-declarator' here. |
| 2051 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 2052 | ParseDeclarator(ParmDecl); |
| 2053 | |
| 2054 | // Parse GNU attributes, if present. |
| 2055 | if (Tok.is(tok::kw___attribute)) |
| 2056 | ParmDecl.AddAttributes(ParseAttributes()); |
| 2057 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2058 | // Remember this parsed parameter in ParamInfo. |
| 2059 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
| 2060 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2061 | // DefArgToks is used when the parsing of default arguments needs |
| 2062 | // to be delayed. |
| 2063 | CachedTokens *DefArgToks = 0; |
| 2064 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2065 | // If no parameter was specified, verify that *something* was specified, |
| 2066 | // otherwise we have a missing type and identifier. |
| 2067 | if (DS.getParsedSpecifiers() == DeclSpec::PQ_None && |
| 2068 | ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) { |
| 2069 | // Completely missing, emit error. |
| 2070 | Diag(DSStart, diag::err_missing_param); |
| 2071 | } else { |
| 2072 | // Otherwise, we have something. Add it and let semantic analysis try |
| 2073 | // to grok it and add the result to the ParamInfo we are building. |
| 2074 | |
| 2075 | // Inform the actions module about the parameter declarator, so it gets |
| 2076 | // added to the current scope. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2077 | DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl); |
| 2078 | |
| 2079 | // Parse the default argument, if any. We parse the default |
| 2080 | // arguments in all dialects; the semantic analysis in |
| 2081 | // ActOnParamDefaultArgument will reject the default argument in |
| 2082 | // C. |
| 2083 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2084 | SourceLocation EqualLoc = Tok.getLocation(); |
| 2085 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2086 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2087 | if (D.getContext() == Declarator::MemberContext) { |
| 2088 | // If we're inside a class definition, cache the tokens |
| 2089 | // corresponding to the default argument. We'll actually parse |
| 2090 | // them when we see the end of the class definition. |
| 2091 | // FIXME: Templates will require something similar. |
| 2092 | // FIXME: Can we use a smart pointer for Toks? |
| 2093 | DefArgToks = new CachedTokens; |
| 2094 | |
| 2095 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
| 2096 | tok::semi, false)) { |
| 2097 | delete DefArgToks; |
| 2098 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2099 | Actions.ActOnParamDefaultArgumentError(Param); |
| 2100 | } else |
| 2101 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2102 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2103 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2104 | ConsumeToken(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2105 | |
| 2106 | OwningExprResult DefArgResult(ParseAssignmentExpression()); |
| 2107 | if (DefArgResult.isInvalid()) { |
| 2108 | Actions.ActOnParamDefaultArgumentError(Param); |
| 2109 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 2110 | } else { |
| 2111 | // Inform the actions module about the default argument |
| 2112 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
| 2113 | DefArgResult.release()); |
| 2114 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2115 | } |
| 2116 | } |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2117 | |
| 2118 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2119 | ParmDecl.getIdentifierLoc(), Param, |
| 2120 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | // If the next token is a comma, consume it and keep reading arguments. |
| 2124 | if (Tok.isNot(tok::comma)) break; |
| 2125 | |
| 2126 | // Consume the comma. |
| 2127 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2130 | // Leave prototype scope. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 2131 | PrototypeScope.Exit(); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2132 | |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2133 | // If we have the closing ')', eat it. |
| 2134 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 2135 | |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2136 | DeclSpec DS; |
| 2137 | if (getLang().CPlusPlus) { |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2138 | // Parse cv-qualifier-seq[opt]. |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 2139 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2140 | |
| 2141 | // Parse exception-specification[opt]. |
| 2142 | if (Tok.is(tok::kw_throw)) |
| 2143 | ParseExceptionSpecification(); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2146 | // Remember that we parsed a function type, and remember the attributes. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2147 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic, |
| 2148 | &ParamInfo[0], ParamInfo.size(), |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2149 | DS.getTypeQualifiers(), |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 2150 | LParenLoc, D)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2153 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 2154 | /// we found a K&R-style identifier list instead of a type argument list. The |
| 2155 | /// current token is known to be the first identifier in the list. |
| 2156 | /// |
| 2157 | /// identifier-list: [C99 6.7.5] |
| 2158 | /// identifier |
| 2159 | /// identifier-list ',' identifier |
| 2160 | /// |
| 2161 | void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc, |
| 2162 | Declarator &D) { |
| 2163 | // Build up an array of information about the parsed arguments. |
| 2164 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
| 2165 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 2166 | |
| 2167 | // If there was no identifier specified for the declarator, either we are in |
| 2168 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 2169 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 2170 | // diagnose this. |
| 2171 | if (!D.getIdentifier()) |
| 2172 | Diag(Tok, diag::ext_ident_list_in_param); |
| 2173 | |
| 2174 | // Tok is known to be the first identifier in the list. Remember this |
| 2175 | // identifier in ParamInfo. |
Chris Lattner | 3825c2e | 2008-04-06 06:50:56 +0000 | [diff] [blame] | 2176 | ParamsSoFar.insert(Tok.getIdentifierInfo()); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2177 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(), |
| 2178 | Tok.getLocation(), 0)); |
| 2179 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2180 | ConsumeToken(); // eat the first identifier. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2181 | |
| 2182 | while (Tok.is(tok::comma)) { |
| 2183 | // Eat the comma. |
| 2184 | ConsumeToken(); |
| 2185 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2186 | // If this isn't an identifier, report the error and skip until ')'. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2187 | if (Tok.isNot(tok::identifier)) { |
| 2188 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2189 | SkipUntil(tok::r_paren); |
| 2190 | return; |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2191 | } |
Chris Lattner | aaf9ddb | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 2192 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2193 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
Chris Lattner | aaf9ddb | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 2194 | |
| 2195 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame^] | 2196 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope)) |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 2197 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2198 | |
| 2199 | // Verify that the argument identifier has not already been mentioned. |
| 2200 | if (!ParamsSoFar.insert(ParmII)) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 2201 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2202 | } else { |
| 2203 | // Remember this identifier in ParamInfo. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2204 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 2205 | Tok.getLocation(), 0)); |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2206 | } |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2207 | |
| 2208 | // Eat the identifier. |
| 2209 | ConsumeToken(); |
| 2210 | } |
| 2211 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2212 | // Remember that we parsed a function type, and remember the attributes. This |
| 2213 | // function type is always a K&R style function type, which is not varargs and |
| 2214 | // has no prototype. |
| 2215 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false, |
| 2216 | &ParamInfo[0], ParamInfo.size(), |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 2217 | /*TypeQuals*/0, LParenLoc, D)); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2218 | |
| 2219 | // If we have the closing ')', eat it and we're done. |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2220 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2221 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2222 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2223 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 2224 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 2225 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 2226 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 2227 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 2228 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 2229 | SourceLocation StartLoc = ConsumeBracket(); |
| 2230 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2231 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 2232 | // This code does a fast path to handle some of the most obvious cases. |
| 2233 | if (Tok.getKind() == tok::r_square) { |
| 2234 | MatchRHSPunctuation(tok::r_square, StartLoc); |
| 2235 | // Remember that we parsed the empty array type. |
| 2236 | OwningExprResult NumElements(Actions); |
| 2237 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc)); |
| 2238 | return; |
| 2239 | } else if (Tok.getKind() == tok::numeric_constant && |
| 2240 | GetLookAheadToken(1).is(tok::r_square)) { |
| 2241 | // [4] is very common. Parse the numeric constant expression. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2242 | OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok)); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2243 | ConsumeToken(); |
| 2244 | |
| 2245 | MatchRHSPunctuation(tok::r_square, StartLoc); |
| 2246 | |
| 2247 | // If there was an error parsing the assignment-expression, recover. |
| 2248 | if (ExprRes.isInvalid()) |
| 2249 | ExprRes.release(); // Deallocate expr, just use []. |
| 2250 | |
| 2251 | // Remember that we parsed a array type, and remember its features. |
| 2252 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
| 2253 | ExprRes.release(), StartLoc)); |
| 2254 | return; |
| 2255 | } |
| 2256 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2257 | // If valid, this location is the position where we read the 'static' keyword. |
| 2258 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2259 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2260 | StaticLoc = ConsumeToken(); |
| 2261 | |
| 2262 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 2263 | // Type qualifiers in an array subscript are a C99 feature. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2264 | DeclSpec DS; |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 2265 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2266 | |
| 2267 | // If we haven't already read 'static', check to see if there is one after the |
| 2268 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2269 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2270 | StaticLoc = ConsumeToken(); |
| 2271 | |
| 2272 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 2273 | bool isStar = false; |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 2274 | OwningExprResult NumElements(Actions); |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 2275 | |
| 2276 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 2277 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 2278 | // the the token after the star is a ']'. Since stars in arrays are |
| 2279 | // infrequent, use of lookahead is not costly here. |
| 2280 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 2281 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2282 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 2283 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 2284 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 2285 | StaticLoc = SourceLocation(); // Drop the static. |
| 2286 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 2287 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2288 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2289 | // Note, in C89, this production uses the constant-expr production instead |
| 2290 | // of assignment-expr. The only difference is that assignment-expr allows |
| 2291 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 2292 | // are not i-c-e's, so we don't need to distinguish between the two here. |
| 2293 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2294 | // Parse the assignment-expression now. |
| 2295 | NumElements = ParseAssignmentExpression(); |
| 2296 | } |
| 2297 | |
| 2298 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2299 | if (NumElements.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2300 | // If the expression was invalid, skip it. |
| 2301 | SkipUntil(tok::r_square); |
| 2302 | return; |
| 2303 | } |
| 2304 | |
| 2305 | MatchRHSPunctuation(tok::r_square, StartLoc); |
| 2306 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2307 | // Remember that we parsed a array type, and remember its features. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2308 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
| 2309 | StaticLoc.isValid(), isStar, |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2310 | NumElements.release(), StartLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2313 | /// [GNU] typeof-specifier: |
| 2314 | /// typeof ( expressions ) |
| 2315 | /// typeof ( type-name ) |
| 2316 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2317 | /// |
| 2318 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2319 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 2320 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2321 | SourceLocation StartLoc = ConsumeToken(); |
| 2322 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2323 | if (Tok.isNot(tok::l_paren)) { |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2324 | if (!getLang().CPlusPlus) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2325 | Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII; |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2326 | return; |
| 2327 | } |
| 2328 | |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 2329 | OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2330 | if (Result.isInvalid()) |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2331 | return; |
| 2332 | |
| 2333 | const char *PrevSpec = 0; |
| 2334 | // Check for duplicate type specifiers. |
| 2335 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2336 | Result.release())) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2337 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2338 | |
| 2339 | // FIXME: Not accurate, the range gets one token more than it should. |
| 2340 | DS.SetRangeEnd(Tok.getLocation()); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 2341 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2342 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2343 | |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2344 | SourceLocation LParenLoc = ConsumeParen(), RParenLoc; |
| 2345 | |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 2346 | if (isTypeIdInParens()) { |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2347 | TypeTy *Ty = ParseTypeName(); |
| 2348 | |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 2349 | assert(Ty && "Parser::ParseTypeofSpecifier(): missing type"); |
| 2350 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2351 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 2352 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 2353 | return; |
| 2354 | } |
| 2355 | RParenLoc = ConsumeParen(); |
| 2356 | const char *PrevSpec = 0; |
| 2357 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 2358 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty)) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2359 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2360 | } else { // we have an expression. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 2361 | OwningExprResult Result(ParseExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2362 | |
| 2363 | if (Result.isInvalid() || Tok.isNot(tok::r_paren)) { |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 2364 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 2365 | return; |
| 2366 | } |
| 2367 | RParenLoc = ConsumeParen(); |
| 2368 | const char *PrevSpec = 0; |
| 2369 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 2370 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2371 | Result.release())) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2372 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2373 | } |
Argyrios Kyrtzidis | 0919f9e | 2008-08-16 10:21:33 +0000 | [diff] [blame] | 2374 | DS.SetRangeEnd(RParenLoc); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 2377 | |