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