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