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