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" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Scope.h" |
| 17 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 18 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 19 | #include "RAIIObjectsForParser.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallSet.h" |
| 21 | using namespace clang; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // C99 6.7: Declarations. |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | /// ParseTypeName |
| 28 | /// type-name: [C99 6.7.6] |
| 29 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 30 | /// |
| 31 | /// Called type-id in C++. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 32 | TypeResult Parser::ParseTypeName(SourceRange *Range) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | // Parse the common declaration-specifiers piece. |
| 34 | DeclSpec DS; |
| 35 | ParseSpecifierQualifierList(DS); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 36 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | // Parse the abstract-declarator, if present. |
| 38 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 39 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 40 | if (Range) |
| 41 | *Range = DeclaratorInfo.getSourceRange(); |
| 42 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 43 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 44 | return true; |
| 45 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 46 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 49 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | /// |
| 51 | /// [GNU] attributes: |
| 52 | /// attribute |
| 53 | /// attributes attribute |
| 54 | /// |
| 55 | /// [GNU] attribute: |
| 56 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 57 | /// |
| 58 | /// [GNU] attribute-list: |
| 59 | /// attrib |
| 60 | /// attribute_list ',' attrib |
| 61 | /// |
| 62 | /// [GNU] attrib: |
| 63 | /// empty |
| 64 | /// attrib-name |
| 65 | /// attrib-name '(' identifier ')' |
| 66 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 67 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 68 | /// |
| 69 | /// [GNU] attrib-name: |
| 70 | /// identifier |
| 71 | /// typespec |
| 72 | /// typequal |
| 73 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 77 | /// which is followed by a comma or close parenthesis, then the arguments |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | /// start with that identifier; otherwise they are an expression list." |
| 79 | /// |
| 80 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 81 | /// any attributes that don't work (based on my limited testing). Most |
| 82 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 83 | /// a pressing need to implement the 2 token lookahead. |
| 84 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 85 | AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) { |
| 86 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | AttributeList *CurrAttr = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 90 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | ConsumeToken(); |
| 92 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 93 | "attribute")) { |
| 94 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 95 | return CurrAttr; |
| 96 | } |
| 97 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 98 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 99 | return CurrAttr; |
| 100 | } |
| 101 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 102 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 103 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
| 105 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 107 | ConsumeToken(); |
| 108 | continue; |
| 109 | } |
| 110 | // we have an identifier or declaration specifier (const, int, etc.) |
| 111 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 112 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Douglas Gregor | ec1afbf | 2010-03-16 19:09:18 +0000 | [diff] [blame] | 114 | // check if we have a "parameterized" attribute |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 115 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | ConsumeParen(); // ignore the left paren loc for now |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 118 | if (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | IdentifierInfo *ParmName = Tok.getIdentifierInfo(); |
| 120 | SourceLocation ParmLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
| 122 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | // __attribute__(( mode(byte) )) |
| 124 | ConsumeParen(); // ignore the right paren loc for now |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 125 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | ParmName, ParmLoc, 0, 0, CurrAttr); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 127 | } else if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 128 | ConsumeToken(); |
| 129 | // __attribute__(( format(printf, 1, 2) )) |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 130 | ExprVector ArgExprs(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | bool ArgExprsOk = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 133 | // now parse the non-empty comma separated list of expressions |
| 134 | while (1) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 135 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 136 | if (ArgExpr.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 137 | ArgExprsOk = false; |
| 138 | SkipUntil(tok::r_paren); |
| 139 | break; |
| 140 | } else { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 141 | ArgExprs.push_back(ArgExpr.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 143 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | break; |
| 145 | ConsumeToken(); // Eat the comma, move to the next argument |
| 146 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 147 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | ConsumeParen(); // ignore the right paren loc for now |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 149 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, |
| 150 | AttrNameLoc, ParmName, ParmLoc, |
| 151 | ArgExprs.take(), ArgExprs.size(), |
| 152 | CurrAttr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } else { // not an identifier |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 156 | switch (Tok.getKind()) { |
| 157 | case tok::r_paren: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 158 | // parse a possibly empty comma separated list of expressions |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | // __attribute__(( nonnull() )) |
| 160 | ConsumeParen(); // ignore the right paren loc for now |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 161 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | 0, SourceLocation(), 0, 0, CurrAttr); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 163 | break; |
| 164 | case tok::kw_char: |
| 165 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 166 | case tok::kw_char16_t: |
| 167 | case tok::kw_char32_t: |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 168 | case tok::kw_bool: |
| 169 | case tok::kw_short: |
| 170 | case tok::kw_int: |
| 171 | case tok::kw_long: |
| 172 | case tok::kw_signed: |
| 173 | case tok::kw_unsigned: |
| 174 | case tok::kw_float: |
| 175 | case tok::kw_double: |
| 176 | case tok::kw_void: |
| 177 | case tok::kw_typeof: |
Fariborz Jahanian | 1b72fa7 | 2010-08-17 23:19:16 +0000 | [diff] [blame] | 178 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 179 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 180 | if (CurrAttr->getKind() == AttributeList::AT_IBOutletCollection) |
| 181 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 182 | // If it's a builtin type name, eat it and expect a rparen |
| 183 | // __attribute__(( vec_type_hint(char) )) |
| 184 | ConsumeToken(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 185 | if (Tok.is(tok::r_paren)) |
| 186 | ConsumeParen(); |
| 187 | break; |
| 188 | default: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | // __attribute__(( aligned(16) )) |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 190 | ExprVector ArgExprs(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 191 | bool ArgExprsOk = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 193 | // now parse the list of expressions |
| 194 | while (1) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 195 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 196 | if (ArgExpr.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 197 | ArgExprsOk = false; |
| 198 | SkipUntil(tok::r_paren); |
| 199 | break; |
| 200 | } else { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 201 | ArgExprs.push_back(ArgExpr.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 203 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 204 | break; |
| 205 | ConsumeToken(); // Eat the comma, move to the next argument |
| 206 | } |
| 207 | // Match the ')'. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 208 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | ConsumeParen(); // ignore the right paren loc for now |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 210 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 211 | AttrNameLoc, 0, SourceLocation(), ArgExprs.take(), |
| 212 | ArgExprs.size(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | CurrAttr); |
| 214 | } |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 215 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } else { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 219 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 220 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 221 | } |
| 222 | } |
| 223 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 225 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 226 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 227 | SkipUntil(tok::r_paren, false); |
| 228 | } |
| 229 | if (EndLoc) |
| 230 | *EndLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 231 | } |
| 232 | return CurrAttr; |
| 233 | } |
| 234 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 235 | /// ParseMicrosoftDeclSpec - Parse an __declspec construct |
| 236 | /// |
| 237 | /// [MS] decl-specifier: |
| 238 | /// __declspec ( extended-decl-modifier-seq ) |
| 239 | /// |
| 240 | /// [MS] extended-decl-modifier-seq: |
| 241 | /// extended-decl-modifier[opt] |
| 242 | /// extended-decl-modifier extended-decl-modifier-seq |
| 243 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 244 | AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 245 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 246 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 247 | ConsumeToken(); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 248 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 249 | "declspec")) { |
| 250 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 251 | return CurrAttr; |
| 252 | } |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 253 | while (Tok.getIdentifierInfo()) { |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 254 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 255 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 256 | if (Tok.is(tok::l_paren)) { |
| 257 | ConsumeParen(); |
| 258 | // FIXME: This doesn't parse __declspec(property(get=get_func_name)) |
| 259 | // correctly. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 260 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 261 | if (!ArgExpr.isInvalid()) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 262 | Expr *ExprList = ArgExpr.take(); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 263 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 264 | SourceLocation(), &ExprList, 1, |
| 265 | CurrAttr, true); |
| 266 | } |
| 267 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 268 | SkipUntil(tok::r_paren, false); |
| 269 | } else { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 270 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 271 | 0, SourceLocation(), 0, 0, CurrAttr, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 275 | SkipUntil(tok::r_paren, false); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 276 | return CurrAttr; |
| 277 | } |
| 278 | |
| 279 | AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) { |
| 280 | // Treat these like attributes |
| 281 | // FIXME: Allow Sema to distinguish between these and real attributes! |
| 282 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 283 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
| 284 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 285 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 286 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 287 | if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) |
| 288 | // FIXME: Support these properly! |
| 289 | continue; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 290 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 291 | SourceLocation(), 0, 0, CurrAttr, true); |
| 292 | } |
| 293 | return CurrAttr; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 296 | AttributeList* Parser::ParseBorlandTypeAttributes(AttributeList *CurrAttr) { |
| 297 | // Treat these like attributes |
| 298 | while (Tok.is(tok::kw___pascal)) { |
| 299 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 300 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 301 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 302 | SourceLocation(), 0, 0, CurrAttr, true); |
| 303 | } |
| 304 | return CurrAttr; |
| 305 | } |
| 306 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 308 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 309 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 310 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 311 | /// |
| 312 | /// declaration: [C99 6.7] |
| 313 | /// block-declaration -> |
| 314 | /// simple-declaration |
| 315 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 316 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 317 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 318 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 319 | /// [C++] using-declaration |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 320 | /// [C++0x] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 321 | /// others... [FIXME] |
| 322 | /// |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 323 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 324 | SourceLocation &DeclEnd, |
| 325 | CXX0XAttributeList Attr) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 326 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
| 327 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 328 | Decl *SingleDecl = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 329 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 330 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 331 | case tok::kw_export: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 332 | if (Attr.HasAttr) |
| 333 | Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) |
| 334 | << Attr.Range; |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 335 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 336 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 337 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 338 | // Could be the start of an inline namespace. Allowed as an ext in C++03. |
| 339 | if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 340 | if (Attr.HasAttr) |
| 341 | Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) |
| 342 | << Attr.Range; |
| 343 | SourceLocation InlineLoc = ConsumeToken(); |
| 344 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 345 | break; |
| 346 | } |
| 347 | return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 348 | case tok::kw_namespace: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 349 | if (Attr.HasAttr) |
| 350 | Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) |
| 351 | << Attr.Range; |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 352 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 353 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 354 | case tok::kw_using: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 355 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 356 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 357 | case tok::kw_static_assert: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 358 | if (Attr.HasAttr) |
| 359 | Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) |
| 360 | << Attr.Range; |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 361 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 362 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 363 | default: |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 364 | return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 365 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 366 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 367 | // This routine returns a DeclGroup, if the thing we parsed only contains a |
| 368 | // single decl, convert it now. |
| 369 | return Actions.ConvertDeclToDeclGroup(SingleDecl); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 373 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 374 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 375 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 376 | /// |
| 377 | /// If RequireSemi is false, this does not check for a ';' at the end of the |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 378 | /// declaration. If it is true, it checks for and eats it. |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 379 | Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 380 | SourceLocation &DeclEnd, |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 381 | AttributeList *Attr, |
| 382 | bool RequireSemi) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 384 | ParsingDeclSpec DS(*this); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 385 | if (Attr) |
| 386 | DS.AddAttributes(Attr); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 387 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
| 388 | getDeclSpecContextFromDeclaratorContext(Context)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 390 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 391 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 392 | if (Tok.is(tok::semi)) { |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 393 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 394 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 395 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 396 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 397 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 398 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 400 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 401 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 403 | /// ParseDeclGroup - Having concluded that this is either a function |
| 404 | /// definition or a group of object declarations, actually parse the |
| 405 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 406 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 407 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 408 | bool AllowFunctionDefinitions, |
| 409 | SourceLocation *DeclEnd) { |
| 410 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 411 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 412 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 413 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 414 | // Bail out if the first declarator didn't seem well-formed. |
| 415 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
| 416 | // Skip until ; or }. |
| 417 | SkipUntil(tok::r_brace, true, true); |
| 418 | if (Tok.is(tok::semi)) |
| 419 | ConsumeToken(); |
| 420 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 423 | // Check to see if we have a function *definition* which must have a body. |
| 424 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 425 | // Look at the next token to make sure that this isn't a function |
| 426 | // declaration. We have to check this because __attribute__ might be the |
| 427 | // start of a function definition in GCC-extended K&R C. |
| 428 | !isDeclarationAfterDeclarator()) { |
| 429 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 430 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 431 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 432 | Diag(Tok, diag::err_function_declared_typedef); |
| 433 | |
| 434 | // Recover by treating the 'typedef' as spurious. |
| 435 | DS.ClearStorageClassSpecs(); |
| 436 | } |
| 437 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 438 | Decl *TheDecl = ParseFunctionDefinition(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 439 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | if (isDeclarationSpecifier()) { |
| 443 | // If there is an invalid declaration specifier right after the function |
| 444 | // prototype, then we must be in a missing semicolon case where this isn't |
| 445 | // actually a body. Just fall through into the code that handles it as a |
| 446 | // prototype, and let the top-level code handle the erroneous declspec |
| 447 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 448 | } else { |
| 449 | Diag(Tok, diag::err_expected_fn_body); |
| 450 | SkipUntil(tok::semi); |
| 451 | return DeclGroupPtrTy(); |
| 452 | } |
| 453 | } |
| 454 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 455 | llvm::SmallVector<Decl *, 8> DeclsInGroup; |
| 456 | Decl *FirstDecl = ParseDeclarationAfterDeclarator(D); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 457 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 458 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 459 | DeclsInGroup.push_back(FirstDecl); |
| 460 | |
| 461 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 462 | // error, bail out. |
| 463 | while (Tok.is(tok::comma)) { |
| 464 | // Consume the comma. |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 465 | ConsumeToken(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 466 | |
| 467 | // Parse the next declarator. |
| 468 | D.clear(); |
| 469 | |
| 470 | // Accept attributes in an init-declarator. In the first declarator in a |
| 471 | // declaration, these would be part of the declspec. In subsequent |
| 472 | // declarators, they become part of the declarator itself, so that they |
| 473 | // don't apply to declarators after *this* one. Examples: |
| 474 | // short __attribute__((common)) var; -> declspec |
| 475 | // short var __attribute__((common)); -> declarator |
| 476 | // short x, __attribute__((common)) var; -> declarator |
| 477 | if (Tok.is(tok::kw___attribute)) { |
| 478 | SourceLocation Loc; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 479 | AttributeList *AttrList = ParseGNUAttributes(&Loc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 480 | D.AddAttributes(AttrList, Loc); |
| 481 | } |
| 482 | |
| 483 | ParseDeclarator(D); |
| 484 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 485 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 486 | D.complete(ThisDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 487 | if (ThisDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 488 | DeclsInGroup.push_back(ThisDecl); |
| 489 | } |
| 490 | |
| 491 | if (DeclEnd) |
| 492 | *DeclEnd = Tok.getLocation(); |
| 493 | |
| 494 | if (Context != Declarator::ForContext && |
| 495 | ExpectAndConsume(tok::semi, |
| 496 | Context == Declarator::FileContext |
| 497 | ? diag::err_invalid_token_after_toplevel_declarator |
| 498 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 499 | // Okay, there was no semicolon and one was expected. If we see a |
| 500 | // declaration specifier, just assume it was missing and continue parsing. |
| 501 | // Otherwise things are very confused and we skip to recover. |
| 502 | if (!isDeclarationSpecifier()) { |
| 503 | SkipUntil(tok::r_brace, true, true); |
| 504 | if (Tok.is(tok::semi)) |
| 505 | ConsumeToken(); |
| 506 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 509 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 510 | DeclsInGroup.data(), |
| 511 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 514 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 515 | /// declarator'. This method parses the remainder of the declaration |
| 516 | /// (including any attributes or initializer, among other things) and |
| 517 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 518 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 519 | /// init-declarator: [C99 6.7] |
| 520 | /// declarator |
| 521 | /// declarator '=' initializer |
| 522 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 523 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 524 | /// [C++] declarator initializer[opt] |
| 525 | /// |
| 526 | /// [C++] initializer: |
| 527 | /// [C++] '=' initializer-clause |
| 528 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 529 | /// [C++0x] '=' 'default' [TODO] |
| 530 | /// [C++0x] '=' 'delete' |
| 531 | /// |
| 532 | /// According to the standard grammar, =default and =delete are function |
| 533 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 534 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 535 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 536 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 537 | // If a simple-asm-expr is present, parse it. |
| 538 | if (Tok.is(tok::kw_asm)) { |
| 539 | SourceLocation Loc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 540 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 541 | if (AsmLabel.isInvalid()) { |
| 542 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 543 | return 0; |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 544 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 546 | D.setAsmLabel(AsmLabel.release()); |
| 547 | D.SetRangeEnd(Loc); |
| 548 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 550 | // If attributes are present, parse them. |
| 551 | if (Tok.is(tok::kw___attribute)) { |
| 552 | SourceLocation Loc; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 553 | AttributeList *AttrList = ParseGNUAttributes(&Loc); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 554 | D.AddAttributes(AttrList, Loc); |
| 555 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 556 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 557 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 558 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 559 | switch (TemplateInfo.Kind) { |
| 560 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 561 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 562 | break; |
| 563 | |
| 564 | case ParsedTemplateInfo::Template: |
| 565 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 566 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 567 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 568 | TemplateInfo.TemplateParams->data(), |
| 569 | TemplateInfo.TemplateParams->size()), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 570 | D); |
| 571 | break; |
| 572 | |
| 573 | case ParsedTemplateInfo::ExplicitInstantiation: { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 574 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 575 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 576 | TemplateInfo.ExternLoc, |
| 577 | TemplateInfo.TemplateLoc, |
| 578 | D); |
| 579 | if (ThisRes.isInvalid()) { |
| 580 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 581 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | ThisDecl = ThisRes.get(); |
| 585 | break; |
| 586 | } |
| 587 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 588 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 589 | // Parse declarator '=' initializer. |
| 590 | if (Tok.is(tok::equal)) { |
| 591 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame^] | 592 | if (Tok.is(tok::kw_delete)) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 593 | SourceLocation DelLoc = ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame^] | 594 | |
| 595 | if (!getLang().CPlusPlus0x) |
| 596 | Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension); |
| 597 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 598 | Actions.SetDeclDeleted(ThisDecl, DelLoc); |
| 599 | } else { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 600 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
| 601 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 602 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 603 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 604 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 605 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 606 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 607 | ConsumeCodeCompletionToken(); |
| 608 | SkipUntil(tok::comma, true, true); |
| 609 | return ThisDecl; |
| 610 | } |
| 611 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 612 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 613 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 614 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 615 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 616 | ExitScope(); |
| 617 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 618 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 619 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 620 | SkipUntil(tok::comma, true, true); |
| 621 | Actions.ActOnInitializerError(ThisDecl); |
| 622 | } else |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 623 | Actions.AddInitializerToDecl(ThisDecl, Init.take()); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 624 | } |
| 625 | } else if (Tok.is(tok::l_paren)) { |
| 626 | // Parse C++ direct initializer: '(' expression-list ')' |
| 627 | SourceLocation LParenLoc = ConsumeParen(); |
| 628 | ExprVector Exprs(Actions); |
| 629 | CommaLocsTy CommaLocs; |
| 630 | |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 631 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
| 632 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 633 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 636 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 637 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 638 | |
| 639 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 640 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 641 | ExitScope(); |
| 642 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 643 | } else { |
| 644 | // Match the ')'. |
| 645 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 646 | |
| 647 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 648 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 649 | |
| 650 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 651 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 652 | ExitScope(); |
| 653 | } |
| 654 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 655 | Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc, |
| 656 | move_arg(Exprs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 657 | RParenLoc); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 658 | } |
| 659 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | bool TypeContainsUndeducedAuto = |
Anders Carlsson | 6a75cd9 | 2009-07-11 00:34:39 +0000 | [diff] [blame] | 661 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 662 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | return ThisDecl; |
| 666 | } |
| 667 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 668 | /// ParseSpecifierQualifierList |
| 669 | /// specifier-qualifier-list: |
| 670 | /// type-specifier specifier-qualifier-list[opt] |
| 671 | /// type-qualifier specifier-qualifier-list[opt] |
| 672 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 673 | /// |
| 674 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { |
| 675 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 676 | /// parse declaration-specifiers and complain about extra stuff. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 677 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 679 | // Validate declspec for type-name. |
| 680 | unsigned Specs = DS.getParsedSpecifiers(); |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 681 | if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 682 | !DS.getAttributes()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 683 | Diag(Tok, diag::err_typename_requires_specqual); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 685 | // Issue diagnostic and remove storage class if present. |
| 686 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 687 | if (DS.getStorageClassSpecLoc().isValid()) |
| 688 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 689 | else |
| 690 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 691 | DS.ClearStorageClassSpecs(); |
| 692 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 694 | // Issue diagnostic and remove function specfier if present. |
| 695 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 696 | if (DS.isInlineSpecified()) |
| 697 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 698 | if (DS.isVirtualSpecified()) |
| 699 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 700 | if (DS.isExplicitSpecified()) |
| 701 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 702 | DS.ClearFunctionSpecs(); |
| 703 | } |
| 704 | } |
| 705 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 706 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 707 | /// specified token is valid after the identifier in a declarator which |
| 708 | /// immediately follows the declspec. For example, these things are valid: |
| 709 | /// |
| 710 | /// int x [ 4]; // direct-declarator |
| 711 | /// int x ( int y); // direct-declarator |
| 712 | /// int(int x ) // direct-declarator |
| 713 | /// int x ; // simple-declaration |
| 714 | /// int x = 17; // init-declarator-list |
| 715 | /// int x , y; // init-declarator-list |
| 716 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 717 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 718 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 719 | /// |
| 720 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 721 | /// ')' happens to be valid anyway). |
| 722 | /// int (x) |
| 723 | /// |
| 724 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 725 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 726 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 727 | 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] | 728 | } |
| 729 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 730 | |
| 731 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 732 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 733 | /// the declspec has no type specifier. In this case, the declspec is either |
| 734 | /// malformed or is "implicit int" (in K&R and C89). |
| 735 | /// |
| 736 | /// This method handles diagnosing this prettily and returns false if the |
| 737 | /// declspec is done being processed. If it recovers and thinks there may be |
| 738 | /// other pieces of declspec after it, it returns true. |
| 739 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 740 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 741 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 742 | AccessSpecifier AS) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 743 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 745 | SourceLocation Loc = Tok.getLocation(); |
| 746 | // If we see an identifier that is not a type name, we normally would |
| 747 | // parse it as the identifer being declared. However, when a typename |
| 748 | // is typo'd or the definition is not included, this will incorrectly |
| 749 | // parse the typename as the identifier name and fall over misparsing |
| 750 | // later parts of the diagnostic. |
| 751 | // |
| 752 | // As such, we try to do some look-ahead in cases where this would |
| 753 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 754 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 755 | // an identifier with implicit int, we'd get a parse error because the |
| 756 | // next token is obviously invalid for a type. Parse these as a case |
| 757 | // with an invalid type specifier. |
| 758 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 759 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 760 | // Since we know that this either implicit int (which is rare) or an |
| 761 | // error, we'd do lookahead to try to do better recovery. |
| 762 | if (isValidAfterIdentifierInDeclarator(NextToken())) { |
| 763 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 764 | // we just avoid eating the identifier, so it will be parsed as the |
| 765 | // identifier in the declarator. |
| 766 | return false; |
| 767 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 769 | // Otherwise, if we don't consume this token, we are going to emit an |
| 770 | // error anyway. Try to recover from various common problems. Check |
| 771 | // to see if this was a reference to a tag name without a tag specified. |
| 772 | // 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] | 773 | // |
| 774 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 775 | if (SS == 0) { |
| 776 | const char *TagName = 0; |
| 777 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 779 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 780 | default: break; |
| 781 | case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break; |
| 782 | case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break; |
| 783 | case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break; |
| 784 | case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break; |
| 785 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 786 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 787 | if (TagName) { |
| 788 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
John McCall | 23e907a | 2010-02-14 01:03:10 +0000 | [diff] [blame] | 789 | << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 790 | << FixItHint::CreateInsertion(Tok.getLocation(),TagName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 791 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 792 | // Parse this as a tag as if the missing tag were present. |
| 793 | if (TagKind == tok::kw_enum) |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 794 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 795 | else |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 796 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 797 | return true; |
| 798 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 799 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 801 | // This is almost certainly an invalid type name. Let the action emit a |
| 802 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 803 | ParsedType T; |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 804 | if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc, |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 805 | getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 806 | // The action emitted a diagnostic, so we don't have to. |
| 807 | if (T) { |
| 808 | // The action has suggested that the type T could be used. Set that as |
| 809 | // the type in the declaration specifiers, consume the would-be type |
| 810 | // name token, and we're done. |
| 811 | const char *PrevSpec; |
| 812 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 813 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 814 | DS.SetRangeEnd(Tok.getLocation()); |
| 815 | ConsumeToken(); |
| 816 | |
| 817 | // There may be other declaration specifiers after this. |
| 818 | return true; |
| 819 | } |
| 820 | |
| 821 | // Fall through; the action had no suggestion for us. |
| 822 | } else { |
| 823 | // The action did not emit a diagnostic, so emit one now. |
| 824 | SourceRange R; |
| 825 | if (SS) R = SS->getRange(); |
| 826 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 827 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 828 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 829 | // Mark this as an error. |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 830 | const char *PrevSpec; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 831 | unsigned DiagID; |
| 832 | DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 833 | DS.SetRangeEnd(Tok.getLocation()); |
| 834 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 836 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 837 | // avoid rippling error messages on subsequent uses of the same type, |
| 838 | // could be useful if #include was forgotten. |
| 839 | return false; |
| 840 | } |
| 841 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 842 | /// \brief Determine the declaration specifier context from the declarator |
| 843 | /// context. |
| 844 | /// |
| 845 | /// \param Context the declarator context, which is one of the |
| 846 | /// Declarator::TheContext enumerator values. |
| 847 | Parser::DeclSpecContext |
| 848 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 849 | if (Context == Declarator::MemberContext) |
| 850 | return DSC_class; |
| 851 | if (Context == Declarator::FileContext) |
| 852 | return DSC_top_level; |
| 853 | return DSC_normal; |
| 854 | } |
| 855 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 856 | /// ParseDeclarationSpecifiers |
| 857 | /// declaration-specifiers: [C99 6.7] |
| 858 | /// storage-class-specifier declaration-specifiers[opt] |
| 859 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 860 | /// [C99] function-specifier declaration-specifiers[opt] |
| 861 | /// [GNU] attributes declaration-specifiers[opt] |
| 862 | /// |
| 863 | /// storage-class-specifier: [C99 6.7.1] |
| 864 | /// 'typedef' |
| 865 | /// 'extern' |
| 866 | /// 'static' |
| 867 | /// 'auto' |
| 868 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 869 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 870 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 871 | /// function-specifier: [C99 6.7.4] |
| 872 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 873 | /// [C++] 'virtual' |
| 874 | /// [C++] 'explicit' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 875 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 876 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 877 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 878 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 879 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 880 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 881 | AccessSpecifier AS, |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 882 | DeclSpecContext DSContext) { |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 883 | DS.SetRangeStart(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 884 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 885 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 886 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 887 | unsigned DiagID = 0; |
| 888 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 889 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 890 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 891 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 893 | DoneWithDeclSpec: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 894 | // If this is not a declaration specifier token, we're done reading decl |
| 895 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 896 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 897 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 899 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 900 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 901 | if (DS.hasTypeSpecifier()) { |
| 902 | bool AllowNonIdentifiers |
| 903 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 904 | Scope::BlockScope | |
| 905 | Scope::TemplateParamScope | |
| 906 | Scope::FunctionPrototypeScope | |
| 907 | Scope::AtCatchScope)) == 0; |
| 908 | bool AllowNestedNameSpecifiers |
| 909 | = DSContext == DSC_top_level || |
| 910 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 911 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 912 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
| 913 | AllowNonIdentifiers, |
| 914 | AllowNestedNameSpecifiers); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 915 | ConsumeCodeCompletionToken(); |
| 916 | return; |
| 917 | } |
| 918 | |
| 919 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 920 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
| 921 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 922 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 923 | CCC = Sema::PCC_Class; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 924 | else if (ObjCImpDecl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 925 | CCC = Sema::PCC_ObjCImplementation; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 926 | |
| 927 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
| 928 | ConsumeCodeCompletionToken(); |
| 929 | return; |
| 930 | } |
| 931 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 932 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 933 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 934 | if (TryAnnotateCXXScopeToken(true)) { |
| 935 | if (!DS.hasTypeSpecifier()) |
| 936 | DS.SetTypeSpecError(); |
| 937 | goto DoneWithDeclSpec; |
| 938 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 939 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 940 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 941 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 942 | |
| 943 | case tok::annot_cxxscope: { |
| 944 | if (DS.hasTypeSpecifier()) |
| 945 | goto DoneWithDeclSpec; |
| 946 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 947 | CXXScopeSpec SS; |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 948 | SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue()); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 949 | SS.setRange(Tok.getAnnotationRange()); |
| 950 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 951 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 952 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 953 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 954 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 955 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 956 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 957 | |
| 958 | // C++ [class.qual]p2: |
| 959 | // In a lookup in which the constructor is an acceptable lookup |
| 960 | // result and the nested-name-specifier nominates a class C: |
| 961 | // |
| 962 | // - if the name specified after the |
| 963 | // nested-name-specifier, when looked up in C, is the |
| 964 | // injected-class-name of C (Clause 9), or |
| 965 | // |
| 966 | // - if the name specified after the nested-name-specifier |
| 967 | // is the same as the identifier or the |
| 968 | // simple-template-id's template-name in the last |
| 969 | // component of the nested-name-specifier, |
| 970 | // |
| 971 | // the name is instead considered to name the constructor of |
| 972 | // class C. |
| 973 | // |
| 974 | // Thus, if the template-name is actually the constructor |
| 975 | // name, then the code is ill-formed; this interpretation is |
| 976 | // reinforced by the NAD status of core issue 635. |
| 977 | TemplateIdAnnotation *TemplateId |
| 978 | = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 979 | if ((DSContext == DSC_top_level || |
| 980 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 981 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 982 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 983 | if (isConstructorDeclarator()) { |
| 984 | // The user meant this to be an out-of-line constructor |
| 985 | // definition, but template arguments are not allowed |
| 986 | // there. Just allow this as a constructor; we'll |
| 987 | // complain about it later. |
| 988 | goto DoneWithDeclSpec; |
| 989 | } |
| 990 | |
| 991 | // The user meant this to name a type, but it actually names |
| 992 | // a constructor with some extraneous template |
| 993 | // arguments. Complain, then parse it as a type as the user |
| 994 | // intended. |
| 995 | Diag(TemplateId->TemplateNameLoc, |
| 996 | diag::err_out_of_line_template_id_names_constructor) |
| 997 | << TemplateId->Name; |
| 998 | } |
| 999 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1000 | DS.getTypeSpecScope() = SS; |
| 1001 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1003 | "ParseOptionalCXXScopeSpecifier not working"); |
| 1004 | AnnotateTemplateIdTokenAsType(&SS); |
| 1005 | continue; |
| 1006 | } |
| 1007 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1008 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1009 | DS.getTypeSpecScope() = SS; |
| 1010 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1011 | if (Tok.getAnnotationValue()) { |
| 1012 | ParsedType T = getTypeAnnotation(Tok); |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1013 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1014 | PrevSpec, DiagID, T); |
| 1015 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1016 | else |
| 1017 | DS.SetTypeSpecError(); |
| 1018 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1019 | ConsumeToken(); // The typename |
| 1020 | } |
| 1021 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1022 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1023 | goto DoneWithDeclSpec; |
| 1024 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1025 | // If we're in a context where the identifier could be a class name, |
| 1026 | // check whether this is a constructor declaration. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1027 | if ((DSContext == DSC_top_level || |
| 1028 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1029 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1030 | &SS)) { |
| 1031 | if (isConstructorDeclarator()) |
| 1032 | goto DoneWithDeclSpec; |
| 1033 | |
| 1034 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 1035 | // of the class is qualified in a context where it could name |
| 1036 | // a constructor, its a constructor name. However, we've |
| 1037 | // looked at the declarator, and the user probably meant this |
| 1038 | // to be a type. Complain that it isn't supposed to be treated |
| 1039 | // as a type, then proceed to parse it as a type. |
| 1040 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 1041 | << Next.getIdentifierInfo(); |
| 1042 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1043 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1044 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 1045 | Next.getLocation(), |
| 1046 | getCurScope(), &SS); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1047 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1048 | // If the referenced identifier is not a type, then this declspec is |
| 1049 | // erroneous: We already checked about that it has no type specifier, and |
| 1050 | // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1052 | if (TypeRep == 0) { |
| 1053 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1054 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1055 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1056 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1058 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1059 | ConsumeToken(); // The C++ scope. |
| 1060 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1061 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1062 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1063 | if (isInvalid) |
| 1064 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1065 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1066 | DS.SetRangeEnd(Tok.getLocation()); |
| 1067 | ConsumeToken(); // The typename. |
| 1068 | |
| 1069 | continue; |
| 1070 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1072 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1073 | if (Tok.getAnnotationValue()) { |
| 1074 | ParsedType T = getTypeAnnotation(Tok); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1075 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1076 | DiagID, T); |
| 1077 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1078 | DS.SetTypeSpecError(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1079 | |
| 1080 | if (isInvalid) |
| 1081 | break; |
| 1082 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1083 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1084 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1085 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1086 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1087 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 1088 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 1089 | // just a normal reference to a typedef name. |
| 1090 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 1091 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1093 | SourceLocation LAngleLoc, EndProtoLoc; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1094 | llvm::SmallVector<Decl *, 8> ProtocolDecl; |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1095 | llvm::SmallVector<SourceLocation, 8> ProtocolLocs; |
| 1096 | ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false, |
| 1097 | LAngleLoc, EndProtoLoc); |
| 1098 | DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(), |
| 1099 | ProtocolLocs.data(), LAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1101 | DS.SetRangeEnd(EndProtoLoc); |
| 1102 | continue; |
| 1103 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1105 | // typedef-name |
| 1106 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1107 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 1108 | // so handle it as such. This is important for ctor parsing. |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1109 | if (getLang().CPlusPlus) { |
| 1110 | if (TryAnnotateCXXScopeToken(true)) { |
| 1111 | if (!DS.hasTypeSpecifier()) |
| 1112 | DS.SetTypeSpecError(); |
| 1113 | goto DoneWithDeclSpec; |
| 1114 | } |
| 1115 | if (!Tok.is(tok::identifier)) |
| 1116 | continue; |
| 1117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1118 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1119 | // This identifier can only be a typedef name if we haven't already seen |
| 1120 | // a type-specifier. Without this check we misparse: |
| 1121 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 1122 | if (DS.hasTypeSpecifier()) |
| 1123 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1124 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1125 | // Check for need to substitute AltiVec keyword tokens. |
| 1126 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 1127 | break; |
| 1128 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1129 | // It has to be available as a typedef too! |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1130 | ParsedType TypeRep = |
| 1131 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 1132 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1133 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1134 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 1135 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1136 | if (!TypeRep) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1137 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1138 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1139 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1140 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1141 | // If we're in a context where the identifier could be a class name, |
| 1142 | // check whether this is a constructor declaration. |
| 1143 | if (getLang().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1144 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1145 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1146 | goto DoneWithDeclSpec; |
| 1147 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1148 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1149 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1150 | if (isInvalid) |
| 1151 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1152 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1153 | DS.SetRangeEnd(Tok.getLocation()); |
| 1154 | ConsumeToken(); // The identifier |
| 1155 | |
| 1156 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1157 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 1158 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 1159 | // just a normal reference to a typedef name. |
| 1160 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 1161 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1163 | SourceLocation LAngleLoc, EndProtoLoc; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1164 | llvm::SmallVector<Decl *, 8> ProtocolDecl; |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1165 | llvm::SmallVector<SourceLocation, 8> ProtocolLocs; |
| 1166 | ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false, |
| 1167 | LAngleLoc, EndProtoLoc); |
| 1168 | DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(), |
| 1169 | ProtocolLocs.data(), LAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1171 | DS.SetRangeEnd(EndProtoLoc); |
| 1172 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 1173 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 1174 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 1175 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1176 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1177 | |
| 1178 | // type-name |
| 1179 | case tok::annot_template_id: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | TemplateIdAnnotation *TemplateId |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1181 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1182 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1183 | // This template-id does not refer to a type name, so we're |
| 1184 | // done with the type-specifiers. |
| 1185 | goto DoneWithDeclSpec; |
| 1186 | } |
| 1187 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1188 | // If we're in a context where the template-id could be a |
| 1189 | // constructor name or specialization, check whether this is a |
| 1190 | // constructor declaration. |
| 1191 | if (getLang().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1192 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1193 | isConstructorDeclarator()) |
| 1194 | goto DoneWithDeclSpec; |
| 1195 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1196 | // Turn the template-id annotation token into a type annotation |
| 1197 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1198 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1199 | continue; |
| 1200 | } |
| 1201 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1202 | // GNU attributes support. |
| 1203 | case tok::kw___attribute: |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1204 | DS.AddAttributes(ParseGNUAttributes()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1205 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 1206 | |
| 1207 | // Microsoft declspec support. |
| 1208 | case tok::kw___declspec: |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 1209 | DS.AddAttributes(ParseMicrosoftDeclSpec()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 1210 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1211 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1212 | // Microsoft single token adornments. |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 1213 | case tok::kw___forceinline: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1214 | // FIXME: Add handling here! |
| 1215 | break; |
| 1216 | |
| 1217 | case tok::kw___ptr64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 1218 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1219 | case tok::kw___cdecl: |
| 1220 | case tok::kw___stdcall: |
| 1221 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 1222 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1223 | DS.AddAttributes(ParseMicrosoftTypeAttributes()); |
| 1224 | continue; |
| 1225 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1226 | // Borland single token adornments. |
| 1227 | case tok::kw___pascal: |
| 1228 | DS.AddAttributes(ParseBorlandTypeAttributes()); |
| 1229 | continue; |
| 1230 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1231 | // storage-class-specifier |
| 1232 | case tok::kw_typedef: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1233 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec, |
| 1234 | DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1235 | break; |
| 1236 | case tok::kw_extern: |
| 1237 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1238 | Diag(Tok, diag::ext_thread_before) << "extern"; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1239 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec, |
| 1240 | DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1241 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 1242 | case tok::kw___private_extern__: |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1243 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1244 | PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 1245 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1246 | case tok::kw_static: |
| 1247 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1248 | Diag(Tok, diag::ext_thread_before) << "static"; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1249 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec, |
| 1250 | DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1251 | break; |
| 1252 | case tok::kw_auto: |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 1253 | if (getLang().CPlusPlus0x) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1254 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 1255 | DiagID); |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 1256 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1257 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec, |
| 1258 | DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1259 | break; |
| 1260 | case tok::kw_register: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1261 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec, |
| 1262 | DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1263 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1264 | case tok::kw_mutable: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1265 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec, |
| 1266 | DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1267 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1268 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1269 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1270 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1271 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1272 | // function-specifier |
| 1273 | case tok::kw_inline: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1274 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1275 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1276 | case tok::kw_virtual: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1277 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1278 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1279 | case tok::kw_explicit: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1280 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1281 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1282 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1283 | // friend |
| 1284 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1285 | if (DSContext == DSC_class) |
| 1286 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 1287 | else { |
| 1288 | PrevSpec = ""; // not actually used by the diagnostic |
| 1289 | DiagID = diag::err_friend_invalid_in_context; |
| 1290 | isInvalid = true; |
| 1291 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1292 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1293 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1294 | // constexpr |
| 1295 | case tok::kw_constexpr: |
| 1296 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 1297 | break; |
| 1298 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1299 | // type-specifier |
| 1300 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1301 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 1302 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1303 | break; |
| 1304 | case tok::kw_long: |
| 1305 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1306 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 1307 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1308 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1309 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 1310 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1311 | break; |
| 1312 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1313 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 1314 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1315 | break; |
| 1316 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1317 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 1318 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1319 | break; |
| 1320 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1321 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 1322 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1323 | break; |
| 1324 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1325 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 1326 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1327 | break; |
| 1328 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1329 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 1330 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1331 | break; |
| 1332 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1333 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 1334 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1335 | break; |
| 1336 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1337 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 1338 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1339 | break; |
| 1340 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1341 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 1342 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1343 | break; |
| 1344 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1345 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 1346 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1347 | break; |
| 1348 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1349 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 1350 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1351 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1352 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1353 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 1354 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1355 | break; |
| 1356 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1357 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 1358 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1359 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1360 | case tok::kw_bool: |
| 1361 | case tok::kw__Bool: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1362 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 1363 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1364 | break; |
| 1365 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1366 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 1367 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1368 | break; |
| 1369 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1370 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 1371 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1372 | break; |
| 1373 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1374 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 1375 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1376 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1377 | case tok::kw___vector: |
| 1378 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 1379 | break; |
| 1380 | case tok::kw___pixel: |
| 1381 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 1382 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1383 | |
| 1384 | // class-specifier: |
| 1385 | case tok::kw_class: |
| 1386 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1387 | case tok::kw_union: { |
| 1388 | tok::TokenKind Kind = Tok.getKind(); |
| 1389 | ConsumeToken(); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1390 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1391 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1392 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1393 | |
| 1394 | // enum-specifier: |
| 1395 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1396 | ConsumeToken(); |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 1397 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1398 | continue; |
| 1399 | |
| 1400 | // cv-qualifier: |
| 1401 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1402 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
| 1403 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1404 | break; |
| 1405 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1406 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
| 1407 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1408 | break; |
| 1409 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1410 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
| 1411 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1412 | break; |
| 1413 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1414 | // C++ typename-specifier: |
| 1415 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1416 | if (TryAnnotateTypeOrScopeToken()) { |
| 1417 | DS.SetTypeSpecError(); |
| 1418 | goto DoneWithDeclSpec; |
| 1419 | } |
| 1420 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1421 | continue; |
| 1422 | break; |
| 1423 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1424 | // GNU typeof support. |
| 1425 | case tok::kw_typeof: |
| 1426 | ParseTypeofSpecifier(DS); |
| 1427 | continue; |
| 1428 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1429 | case tok::kw_decltype: |
| 1430 | ParseDecltypeSpecifier(DS); |
| 1431 | continue; |
| 1432 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 1433 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1434 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1435 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 1436 | // but we support it. |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1437 | if (DS.hasTypeSpecifier() || !getLang().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1438 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1440 | { |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1441 | SourceLocation LAngleLoc, EndProtoLoc; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1442 | llvm::SmallVector<Decl *, 8> ProtocolDecl; |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1443 | llvm::SmallVector<SourceLocation, 8> ProtocolLocs; |
| 1444 | ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false, |
| 1445 | LAngleLoc, EndProtoLoc); |
| 1446 | DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(), |
| 1447 | ProtocolLocs.data(), LAngleLoc); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1448 | DS.SetRangeEnd(EndProtoLoc); |
| 1449 | |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1450 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1451 | << FixItHint::CreateInsertion(Loc, "id") |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1452 | << SourceRange(Loc, EndProtoLoc); |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 1453 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 1454 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 1455 | continue; |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 1456 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1457 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1458 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1459 | if (isInvalid) { |
| 1460 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1461 | assert(DiagID); |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 1462 | |
| 1463 | if (DiagID == diag::ext_duplicate_declspec) |
| 1464 | Diag(Tok, DiagID) |
| 1465 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1466 | else |
| 1467 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1468 | } |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 1469 | DS.SetRangeEnd(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1470 | ConsumeToken(); |
| 1471 | } |
| 1472 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1473 | |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 1474 | /// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1475 | /// primarily follow the C++ grammar with additions for C99 and GNU, |
| 1476 | /// which together subsume the C grammar. Note that the C++ |
| 1477 | /// type-specifier also includes the C type-qualifier (for const, |
| 1478 | /// volatile, and C99 restrict). Returns true if a type-specifier was |
| 1479 | /// found (and parsed), false otherwise. |
| 1480 | /// |
| 1481 | /// type-specifier: [C++ 7.1.5] |
| 1482 | /// simple-type-specifier |
| 1483 | /// class-specifier |
| 1484 | /// enum-specifier |
| 1485 | /// elaborated-type-specifier [TODO] |
| 1486 | /// cv-qualifier |
| 1487 | /// |
| 1488 | /// cv-qualifier: [C++ 7.1.5.1] |
| 1489 | /// 'const' |
| 1490 | /// 'volatile' |
| 1491 | /// [C99] 'restrict' |
| 1492 | /// |
| 1493 | /// simple-type-specifier: [ C++ 7.1.5.2] |
| 1494 | /// '::'[opt] nested-name-specifier[opt] type-name [TODO] |
| 1495 | /// '::'[opt] nested-name-specifier 'template' template-id [TODO] |
| 1496 | /// 'char' |
| 1497 | /// 'wchar_t' |
| 1498 | /// 'bool' |
| 1499 | /// 'short' |
| 1500 | /// 'int' |
| 1501 | /// 'long' |
| 1502 | /// 'signed' |
| 1503 | /// 'unsigned' |
| 1504 | /// 'float' |
| 1505 | /// 'double' |
| 1506 | /// 'void' |
| 1507 | /// [C99] '_Bool' |
| 1508 | /// [C99] '_Complex' |
| 1509 | /// [C99] '_Imaginary' // Removed in TC2? |
| 1510 | /// [GNU] '_Decimal32' |
| 1511 | /// [GNU] '_Decimal64' |
| 1512 | /// [GNU] '_Decimal128' |
| 1513 | /// [GNU] typeof-specifier |
| 1514 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
| 1515 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1516 | /// [C++0x] 'decltype' ( expression ) |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1517 | /// [AltiVec] '__vector' |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1518 | bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid, |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 1519 | const char *&PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1520 | unsigned &DiagID, |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 1521 | const ParsedTemplateInfo &TemplateInfo, |
| 1522 | bool SuppressDeclarations) { |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1523 | SourceLocation Loc = Tok.getLocation(); |
| 1524 | |
| 1525 | switch (Tok.getKind()) { |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1526 | case tok::identifier: // foo::bar |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 1527 | // If we already have a type specifier, this identifier is not a type. |
| 1528 | if (DS.getTypeSpecType() != DeclSpec::TST_unspecified || |
| 1529 | DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified || |
| 1530 | DS.getTypeSpecSign() != DeclSpec::TSS_unspecified) |
| 1531 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1532 | // Check for need to substitute AltiVec keyword tokens. |
| 1533 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 1534 | break; |
| 1535 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1536 | case tok::kw_typename: // typename foo::bar |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1537 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1538 | // recurse to handle whatever we get. |
| 1539 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1540 | return true; |
| 1541 | if (Tok.is(tok::identifier)) |
| 1542 | return false; |
| 1543 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, |
| 1544 | TemplateInfo, SuppressDeclarations); |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1545 | case tok::coloncolon: // ::foo::bar |
| 1546 | if (NextToken().is(tok::kw_new) || // ::new |
| 1547 | NextToken().is(tok::kw_delete)) // ::delete |
| 1548 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | |
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()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1553 | return true; |
| 1554 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, |
| 1555 | TemplateInfo, SuppressDeclarations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1557 | // simple-type-specifier: |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 1558 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1559 | if (ParsedType T = getTypeAnnotation(Tok)) { |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1560 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1561 | DiagID, T); |
| 1562 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1563 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1564 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1565 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1566 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1567 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1568 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 1569 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 1570 | // just a normal reference to a typedef name. |
| 1571 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 1572 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1574 | SourceLocation LAngleLoc, EndProtoLoc; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1575 | llvm::SmallVector<Decl *, 8> ProtocolDecl; |
Argyrios Kyrtzidis | 71b0add | 2009-09-29 19:41:44 +0000 | [diff] [blame] | 1576 | llvm::SmallVector<SourceLocation, 8> ProtocolLocs; |
| 1577 | ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false, |
| 1578 | LAngleLoc, EndProtoLoc); |
| 1579 | DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(), |
| 1580 | ProtocolLocs.data(), LAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1581 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1582 | DS.SetRangeEnd(EndProtoLoc); |
| 1583 | return true; |
| 1584 | } |
| 1585 | |
| 1586 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1587 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1588 | break; |
| 1589 | case tok::kw_long: |
| 1590 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1591 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 1592 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1593 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1594 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 1595 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1596 | break; |
| 1597 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1598 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1599 | break; |
| 1600 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1601 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 1602 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1603 | break; |
| 1604 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1605 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 1606 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1607 | break; |
| 1608 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1609 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 1610 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1611 | break; |
| 1612 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1613 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1614 | break; |
| 1615 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1616 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1617 | break; |
| 1618 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1619 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1620 | break; |
| 1621 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1622 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1623 | break; |
| 1624 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1625 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1626 | break; |
| 1627 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1628 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1629 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1630 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1631 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1632 | break; |
| 1633 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1634 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1635 | break; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1636 | case tok::kw_bool: |
| 1637 | case tok::kw__Bool: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1638 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1639 | break; |
| 1640 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1641 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 1642 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1643 | break; |
| 1644 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1645 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 1646 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1647 | break; |
| 1648 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1649 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 1650 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1651 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1652 | case tok::kw___vector: |
| 1653 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 1654 | break; |
| 1655 | case tok::kw___pixel: |
| 1656 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 1657 | break; |
| 1658 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1659 | // class-specifier: |
| 1660 | case tok::kw_class: |
| 1661 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1662 | case tok::kw_union: { |
| 1663 | tok::TokenKind Kind = Tok.getKind(); |
| 1664 | ConsumeToken(); |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 1665 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none, |
| 1666 | SuppressDeclarations); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1667 | return true; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1668 | } |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1669 | |
| 1670 | // enum-specifier: |
| 1671 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1672 | ConsumeToken(); |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 1673 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1674 | return true; |
| 1675 | |
| 1676 | // cv-qualifier: |
| 1677 | case tok::kw_const: |
| 1678 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1679 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1680 | break; |
| 1681 | case tok::kw_volatile: |
| 1682 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1683 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1684 | break; |
| 1685 | case tok::kw_restrict: |
| 1686 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1687 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1688 | break; |
| 1689 | |
| 1690 | // GNU typeof support. |
| 1691 | case tok::kw_typeof: |
| 1692 | ParseTypeofSpecifier(DS); |
| 1693 | return true; |
| 1694 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1695 | // C++0x decltype support. |
| 1696 | case tok::kw_decltype: |
| 1697 | ParseDecltypeSpecifier(DS); |
| 1698 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 1700 | // C++0x auto support. |
| 1701 | case tok::kw_auto: |
| 1702 | if (!getLang().CPlusPlus0x) |
| 1703 | return false; |
| 1704 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1705 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID); |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 1706 | break; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1707 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1708 | case tok::kw___ptr64: |
| 1709 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1710 | case tok::kw___cdecl: |
| 1711 | case tok::kw___stdcall: |
| 1712 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 1713 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1714 | DS.AddAttributes(ParseMicrosoftTypeAttributes()); |
Chris Lattner | 837acd0 | 2009-01-21 19:19:26 +0000 | [diff] [blame] | 1715 | return true; |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1716 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1717 | case tok::kw___pascal: |
| 1718 | DS.AddAttributes(ParseBorlandTypeAttributes()); |
| 1719 | return true; |
| 1720 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1721 | default: |
| 1722 | // Not a type-specifier; do nothing. |
| 1723 | return false; |
| 1724 | } |
| 1725 | |
| 1726 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 1727 | if (isInvalid) { |
| 1728 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1729 | // Pick between error or extwarn. |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1730 | Diag(Tok, DiagID) << PrevSpec; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1731 | } |
| 1732 | DS.SetRangeEnd(Tok.getLocation()); |
| 1733 | ConsumeToken(); // whatever we parsed above. |
| 1734 | return true; |
| 1735 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1736 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1737 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 1738 | /// semicolon. |
| 1739 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1740 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1741 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1742 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1743 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1744 | /// struct-declarator-list: |
| 1745 | /// struct-declarator |
| 1746 | /// struct-declarator-list ',' struct-declarator |
| 1747 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 1748 | /// struct-declarator: |
| 1749 | /// declarator |
| 1750 | /// [GNU] declarator attributes[opt] |
| 1751 | /// declarator[opt] ':' constant-expression |
| 1752 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 1753 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1754 | void Parser:: |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1755 | ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) { |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 1756 | if (Tok.is(tok::kw___extension__)) { |
| 1757 | // __extension__ silences extension warnings in the subexpression. |
| 1758 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1759 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 1760 | return ParseStructDeclaration(DS, Fields); |
| 1761 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1762 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1763 | // Parse the common specifier-qualifiers-list piece. |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 1764 | SourceLocation DSStart = Tok.getLocation(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1765 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1766 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 1767 | // If there are no declarators, this is a free-standing declaration |
| 1768 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1769 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1770 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1771 | return; |
| 1772 | } |
| 1773 | |
| 1774 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1775 | bool FirstDeclarator = true; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1776 | while (1) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1777 | ParsingDeclRAIIObject PD(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1778 | FieldDeclarator DeclaratorInfo(DS); |
| 1779 | |
| 1780 | // Attributes are only allowed here on successive declarators. |
| 1781 | if (!FirstDeclarator && Tok.is(tok::kw___attribute)) { |
| 1782 | SourceLocation Loc; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1783 | AttributeList *AttrList = ParseGNUAttributes(&Loc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1784 | DeclaratorInfo.D.AddAttributes(AttrList, Loc); |
| 1785 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1787 | /// struct-declarator: declarator |
| 1788 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 1789 | if (Tok.isNot(tok::colon)) { |
| 1790 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 1791 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1792 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 1793 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1795 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1796 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1797 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1798 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1799 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 1800 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1801 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1802 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1803 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1804 | // If attributes exist after the declarator, parse them. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1805 | if (Tok.is(tok::kw___attribute)) { |
| 1806 | SourceLocation Loc; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1807 | AttributeList *AttrList = ParseGNUAttributes(&Loc); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1808 | DeclaratorInfo.D.AddAttributes(AttrList, Loc); |
| 1809 | } |
| 1810 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1811 | // We're done with this declarator; invoke the callback. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1812 | Decl *D = Fields.invoke(DeclaratorInfo); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1813 | PD.complete(D); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1814 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1815 | // If we don't have a comma, it is either the end of the list (a ';') |
| 1816 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1817 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1818 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1819 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1820 | // Consume the comma. |
| 1821 | ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1822 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1823 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1824 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | /// ParseStructUnionBody |
| 1828 | /// struct-contents: |
| 1829 | /// struct-declaration-list |
| 1830 | /// [EXT] empty |
| 1831 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 1832 | /// struct-declaration-list: |
| 1833 | /// struct-declaration |
| 1834 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1835 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1836 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1837 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1838 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1839 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 1840 | "parsing struct/union body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1842 | SourceLocation LBraceLoc = ConsumeBrace(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1843 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 1844 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1845 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1846 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1847 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 1848 | // C++. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1849 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Douglas Gregor | 0333296 | 2010-07-29 14:29:34 +0000 | [diff] [blame] | 1850 | Diag(Tok, diag::ext_empty_struct_union) |
| 1851 | << (TagType == TST_union); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1852 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1853 | llvm::SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1854 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1855 | // 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] | 1856 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1857 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1858 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1859 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1860 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1861 | Diag(Tok, diag::ext_extra_struct_semi) |
Douglas Gregor | f13ca06 | 2010-06-16 23:08:59 +0000 | [diff] [blame] | 1862 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1863 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1864 | ConsumeToken(); |
| 1865 | continue; |
| 1866 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1867 | |
| 1868 | // Parse all the comma separated declarators. |
| 1869 | DeclSpec DS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1871 | if (!Tok.is(tok::at)) { |
| 1872 | struct CFieldCallback : FieldCallback { |
| 1873 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1874 | Decl *TagDecl; |
| 1875 | llvm::SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1876 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1877 | CFieldCallback(Parser &P, Decl *TagDecl, |
| 1878 | llvm::SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1879 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 1880 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1881 | virtual Decl *invoke(FieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1882 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1883 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 1884 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 1885 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1886 | FieldDecls.push_back(Field); |
| 1887 | return Field; |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 1888 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 1889 | } Callback(*this, TagDecl, FieldDecls); |
| 1890 | |
| 1891 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1892 | } else { // Handle @defs |
| 1893 | ConsumeToken(); |
| 1894 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 1895 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 1896 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1897 | continue; |
| 1898 | } |
| 1899 | ConsumeToken(); |
| 1900 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 1901 | if (!Tok.is(tok::identifier)) { |
| 1902 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 1903 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1904 | continue; |
| 1905 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1906 | llvm::SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1907 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1908 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1909 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 1910 | ConsumeToken(); |
| 1911 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1912 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1913 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1914 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1915 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1916 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 1917 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1918 | break; |
| 1919 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 1920 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 1921 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1922 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 1923 | // If we stopped at a ';', eat it. |
| 1924 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1925 | } |
| 1926 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1927 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1928 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1930 | llvm::OwningPtr<AttributeList> AttrList; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1931 | // If attributes exist after struct contents, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1932 | if (Tok.is(tok::kw___attribute)) |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1933 | AttrList.reset(ParseGNUAttributes()); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 1934 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1935 | Actions.ActOnFields(getCurScope(), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1936 | RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(), |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 1937 | LBraceLoc, RBraceLoc, |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1938 | AttrList.get()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1939 | StructScope.Exit(); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1940 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | |
| 1944 | /// ParseEnumSpecifier |
| 1945 | /// enum-specifier: [C99 6.7.2.2] |
| 1946 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1947 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1948 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 1949 | /// '}' attributes[opt] |
| 1950 | /// 'enum' identifier |
| 1951 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1952 | /// |
| 1953 | /// [C++] elaborated-type-specifier: |
| 1954 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 1955 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1956 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 1957 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1958 | AccessSpecifier AS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1959 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1960 | if (Tok.is(tok::code_completion)) { |
| 1961 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1962 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 1963 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1966 | llvm::OwningPtr<AttributeList> Attr; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1967 | // If attributes exist after tag, parse them. |
| 1968 | if (Tok.is(tok::kw___attribute)) |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 1969 | Attr.reset(ParseGNUAttributes()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1970 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 1971 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1972 | if (getLang().CPlusPlus) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1973 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1974 | return; |
| 1975 | |
| 1976 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1977 | Diag(Tok, diag::err_expected_ident); |
| 1978 | if (Tok.isNot(tok::l_brace)) { |
| 1979 | // Has no name and is not a definition. |
| 1980 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 1981 | SkipUntil(tok::comma, true); |
| 1982 | return; |
| 1983 | } |
| 1984 | } |
| 1985 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1987 | // Must have either 'enum name' or 'enum {...}'. |
| 1988 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) { |
| 1989 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1991 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 1992 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1993 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1994 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1996 | // If an identifier is present, consume and remember it. |
| 1997 | IdentifierInfo *Name = 0; |
| 1998 | SourceLocation NameLoc; |
| 1999 | if (Tok.is(tok::identifier)) { |
| 2000 | Name = Tok.getIdentifierInfo(); |
| 2001 | NameLoc = ConsumeToken(); |
| 2002 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2004 | // There are three options here. If we have 'enum foo;', then this is a |
| 2005 | // forward declaration. If we have 'enum foo {...' then this is a |
| 2006 | // definition. Otherwise we have something like 'enum foo xyz', a reference. |
| 2007 | // |
| 2008 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 2009 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 2010 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 2011 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2012 | Sema::TagUseKind TUK; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2013 | if (Tok.is(tok::l_brace)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2014 | TUK = Sema::TUK_Definition; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2015 | else if (Tok.is(tok::semi)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2016 | TUK = Sema::TUK_Declaration; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2017 | else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2018 | TUK = Sema::TUK_Reference; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 2019 | |
| 2020 | // enums cannot be templates, although they can be referenced from a |
| 2021 | // template. |
| 2022 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2023 | TUK != Sema::TUK_Reference) { |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 2024 | Diag(Tok, diag::err_enum_template); |
| 2025 | |
| 2026 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2027 | SkipUntil(tok::comma, true); |
| 2028 | return; |
| 2029 | } |
| 2030 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2031 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2032 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2033 | SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc; |
| 2034 | const char *PrevSpec = 0; |
| 2035 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2036 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
| 2037 | StartLoc, SS, Name, NameLoc, Attr.get(), |
| 2038 | AS, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2039 | MultiTemplateParamsArg(Actions), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2040 | Owned, IsDependent); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2041 | if (IsDependent) { |
| 2042 | // This enum has a dependent nested-name-specifier. Handle it as a |
| 2043 | // dependent tag. |
| 2044 | if (!Name) { |
| 2045 | DS.SetTypeSpecError(); |
| 2046 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 2047 | return; |
| 2048 | } |
| 2049 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2050 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2051 | TUK, SS, Name, StartLoc, |
| 2052 | NameLoc); |
| 2053 | if (Type.isInvalid()) { |
| 2054 | DS.SetTypeSpecError(); |
| 2055 | return; |
| 2056 | } |
| 2057 | |
| 2058 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2059 | Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2060 | Diag(StartLoc, DiagID) << PrevSpec; |
| 2061 | |
| 2062 | return; |
| 2063 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2064 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2065 | if (!TagDecl) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2066 | // The action failed to produce an enumeration tag. If this is a |
| 2067 | // definition, consume the entire definition. |
| 2068 | if (Tok.is(tok::l_brace)) { |
| 2069 | ConsumeBrace(); |
| 2070 | SkipUntil(tok::r_brace); |
| 2071 | } |
| 2072 | |
| 2073 | DS.SetTypeSpecError(); |
| 2074 | return; |
| 2075 | } |
| 2076 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2077 | if (Tok.is(tok::l_brace)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2078 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2080 | // FIXME: The DeclSpec should keep the locations of both the keyword |
| 2081 | // and the name (if there is one). |
Douglas Gregor | b988f9c | 2010-01-25 16:33:23 +0000 | [diff] [blame] | 2082 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2083 | TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2084 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 2088 | /// enumerator-list: |
| 2089 | /// enumerator |
| 2090 | /// enumerator-list ',' enumerator |
| 2091 | /// enumerator: |
| 2092 | /// enumeration-constant |
| 2093 | /// enumeration-constant '=' constant-expression |
| 2094 | /// enumeration-constant: |
| 2095 | /// identifier |
| 2096 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2097 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 2098 | // Enter the scope of the enum body and start the definition. |
| 2099 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2100 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 2101 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2102 | SourceLocation LBraceLoc = ConsumeBrace(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 2104 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2105 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 2106 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2107 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2108 | llvm::SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2109 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2110 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2111 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2112 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2113 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2114 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 2115 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2116 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2117 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2118 | ExprResult AssignedVal; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2119 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2120 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2121 | AssignedVal = ParseConstantExpression(); |
| 2122 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2123 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2124 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2125 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2126 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2127 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 2128 | LastEnumConstDecl, |
| 2129 | IdentLoc, Ident, |
| 2130 | EqualLoc, |
| 2131 | AssignedVal.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2132 | EnumConstantDecls.push_back(EnumConstDecl); |
| 2133 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2134 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 2135 | if (Tok.is(tok::identifier)) { |
| 2136 | // We're missing a comma between enumerators. |
| 2137 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 2138 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
| 2139 | << FixItHint::CreateInsertion(Loc, ", "); |
| 2140 | continue; |
| 2141 | } |
| 2142 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2143 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2144 | break; |
| 2145 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | |
| 2147 | if (Tok.isNot(tok::identifier) && |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2148 | !(getLang().C99 || getLang().CPlusPlus0x)) |
| 2149 | Diag(CommaLoc, diag::ext_enumerator_list_comma) |
| 2150 | << getLang().CPlusPlus |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2151 | << FixItHint::CreateRemoval(CommaLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2152 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2153 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2154 | // Eat the }. |
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 2155 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2156 | |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 2157 | llvm::OwningPtr<AttributeList> Attr; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2158 | // If attributes exist after the identifier list, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2159 | if (Tok.is(tok::kw___attribute)) |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 2160 | Attr.reset(ParseGNUAttributes()); // FIXME: where do they do? |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2161 | |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 2162 | Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl, |
| 2163 | EnumConstantDecls.data(), EnumConstantDecls.size(), |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2164 | getCurScope(), Attr.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2166 | EnumScope.Exit(); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2167 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 2171 | /// start of a type-qualifier-list. |
| 2172 | bool Parser::isTypeQualifier() const { |
| 2173 | switch (Tok.getKind()) { |
| 2174 | default: return false; |
| 2175 | // type-qualifier |
| 2176 | case tok::kw_const: |
| 2177 | case tok::kw_volatile: |
| 2178 | case tok::kw_restrict: |
| 2179 | return true; |
| 2180 | } |
| 2181 | } |
| 2182 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 2183 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 2184 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 2185 | /// specifier or if we're not sure. |
| 2186 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 2187 | switch (Tok.getKind()) { |
| 2188 | default: return false; |
| 2189 | // type-specifiers |
| 2190 | case tok::kw_short: |
| 2191 | case tok::kw_long: |
| 2192 | case tok::kw_signed: |
| 2193 | case tok::kw_unsigned: |
| 2194 | case tok::kw__Complex: |
| 2195 | case tok::kw__Imaginary: |
| 2196 | case tok::kw_void: |
| 2197 | case tok::kw_char: |
| 2198 | case tok::kw_wchar_t: |
| 2199 | case tok::kw_char16_t: |
| 2200 | case tok::kw_char32_t: |
| 2201 | case tok::kw_int: |
| 2202 | case tok::kw_float: |
| 2203 | case tok::kw_double: |
| 2204 | case tok::kw_bool: |
| 2205 | case tok::kw__Bool: |
| 2206 | case tok::kw__Decimal32: |
| 2207 | case tok::kw__Decimal64: |
| 2208 | case tok::kw__Decimal128: |
| 2209 | case tok::kw___vector: |
| 2210 | |
| 2211 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 2212 | case tok::kw_class: |
| 2213 | case tok::kw_struct: |
| 2214 | case tok::kw_union: |
| 2215 | // enum-specifier |
| 2216 | case tok::kw_enum: |
| 2217 | |
| 2218 | // typedef-name |
| 2219 | case tok::annot_typename: |
| 2220 | return true; |
| 2221 | } |
| 2222 | } |
| 2223 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 2224 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2225 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2226 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2227 | switch (Tok.getKind()) { |
| 2228 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2229 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2230 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2231 | if (TryAltiVecVectorToken()) |
| 2232 | return true; |
| 2233 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2234 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2235 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2236 | // recurse to handle whatever we get. |
| 2237 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2238 | return true; |
| 2239 | if (Tok.is(tok::identifier)) |
| 2240 | return false; |
| 2241 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2242 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2243 | case tok::coloncolon: // ::foo::bar |
| 2244 | if (NextToken().is(tok::kw_new) || // ::new |
| 2245 | NextToken().is(tok::kw_delete)) // ::delete |
| 2246 | return false; |
| 2247 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2248 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2249 | return true; |
| 2250 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2252 | // GNU attributes support. |
| 2253 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2254 | // GNU typeof support. |
| 2255 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2257 | // type-specifiers |
| 2258 | case tok::kw_short: |
| 2259 | case tok::kw_long: |
| 2260 | case tok::kw_signed: |
| 2261 | case tok::kw_unsigned: |
| 2262 | case tok::kw__Complex: |
| 2263 | case tok::kw__Imaginary: |
| 2264 | case tok::kw_void: |
| 2265 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2266 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2267 | case tok::kw_char16_t: |
| 2268 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2269 | case tok::kw_int: |
| 2270 | case tok::kw_float: |
| 2271 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 2272 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2273 | case tok::kw__Bool: |
| 2274 | case tok::kw__Decimal32: |
| 2275 | case tok::kw__Decimal64: |
| 2276 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2277 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2278 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 2279 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 2280 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2281 | case tok::kw_struct: |
| 2282 | case tok::kw_union: |
| 2283 | // enum-specifier |
| 2284 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2285 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2286 | // type-qualifier |
| 2287 | case tok::kw_const: |
| 2288 | case tok::kw_volatile: |
| 2289 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2290 | |
| 2291 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 2292 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2293 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2294 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 2295 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 2296 | case tok::less: |
| 2297 | return getLang().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2298 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2299 | case tok::kw___cdecl: |
| 2300 | case tok::kw___stdcall: |
| 2301 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2302 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2303 | case tok::kw___w64: |
| 2304 | case tok::kw___ptr64: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2305 | case tok::kw___pascal: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2306 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 2311 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 2312 | /// |
| 2313 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 2314 | /// this check is to disambiguate between an expression and a declaration. |
| 2315 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2316 | switch (Tok.getKind()) { |
| 2317 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2318 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2319 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 2320 | // Unfortunate hack to support "Class.factoryMethod" notation. |
| 2321 | if (getLang().ObjC1 && NextToken().is(tok::period)) |
| 2322 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2323 | if (TryAltiVecVectorToken()) |
| 2324 | return true; |
| 2325 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2326 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2327 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2328 | // recurse to handle whatever we get. |
| 2329 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2330 | return true; |
| 2331 | if (Tok.is(tok::identifier)) |
| 2332 | return false; |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 2333 | |
| 2334 | // If we're in Objective-C and we have an Objective-C class type followed |
| 2335 | // by an identifier and then either ':' or ']', in a place where an |
| 2336 | // expression is permitted, then this is probably a class message send |
| 2337 | // missing the initial '['. In this case, we won't consider this to be |
| 2338 | // the start of a declaration. |
| 2339 | if (DisambiguatingWithExpression && |
| 2340 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 2341 | return false; |
| 2342 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2343 | return isDeclarationSpecifier(); |
| 2344 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2345 | case tok::coloncolon: // ::foo::bar |
| 2346 | if (NextToken().is(tok::kw_new) || // ::new |
| 2347 | NextToken().is(tok::kw_delete)) // ::delete |
| 2348 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2349 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2350 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2351 | // recurse to handle whatever we get. |
| 2352 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2353 | return true; |
| 2354 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2355 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2356 | // storage-class-specifier |
| 2357 | case tok::kw_typedef: |
| 2358 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2359 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2360 | case tok::kw_static: |
| 2361 | case tok::kw_auto: |
| 2362 | case tok::kw_register: |
| 2363 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2364 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2365 | // type-specifiers |
| 2366 | case tok::kw_short: |
| 2367 | case tok::kw_long: |
| 2368 | case tok::kw_signed: |
| 2369 | case tok::kw_unsigned: |
| 2370 | case tok::kw__Complex: |
| 2371 | case tok::kw__Imaginary: |
| 2372 | case tok::kw_void: |
| 2373 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2374 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2375 | case tok::kw_char16_t: |
| 2376 | case tok::kw_char32_t: |
| 2377 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2378 | case tok::kw_int: |
| 2379 | case tok::kw_float: |
| 2380 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 2381 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2382 | case tok::kw__Bool: |
| 2383 | case tok::kw__Decimal32: |
| 2384 | case tok::kw__Decimal64: |
| 2385 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2386 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2387 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 2388 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 2389 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2390 | case tok::kw_struct: |
| 2391 | case tok::kw_union: |
| 2392 | // enum-specifier |
| 2393 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2394 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2395 | // type-qualifier |
| 2396 | case tok::kw_const: |
| 2397 | case tok::kw_volatile: |
| 2398 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2399 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2400 | // function-specifier |
| 2401 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2402 | case tok::kw_virtual: |
| 2403 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 2404 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2405 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 2406 | case tok::annot_typename: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2407 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 2408 | // GNU typeof support. |
| 2409 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2410 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 2411 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 2412 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2413 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2414 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 2415 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 2416 | case tok::less: |
| 2417 | return getLang().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 2419 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2420 | case tok::kw___cdecl: |
| 2421 | case tok::kw___stdcall: |
| 2422 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2423 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2424 | case tok::kw___w64: |
| 2425 | case tok::kw___ptr64: |
| 2426 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2427 | case tok::kw___pascal: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2428 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2429 | } |
| 2430 | } |
| 2431 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2432 | bool Parser::isConstructorDeclarator() { |
| 2433 | TentativeParsingAction TPA(*this); |
| 2434 | |
| 2435 | // Parse the C++ scope specifier. |
| 2436 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2437 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2438 | TPA.Revert(); |
| 2439 | return false; |
| 2440 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2441 | |
| 2442 | // Parse the constructor name. |
| 2443 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 2444 | // We already know that we have a constructor name; just consume |
| 2445 | // the token. |
| 2446 | ConsumeToken(); |
| 2447 | } else { |
| 2448 | TPA.Revert(); |
| 2449 | return false; |
| 2450 | } |
| 2451 | |
| 2452 | // Current class name must be followed by a left parentheses. |
| 2453 | if (Tok.isNot(tok::l_paren)) { |
| 2454 | TPA.Revert(); |
| 2455 | return false; |
| 2456 | } |
| 2457 | ConsumeParen(); |
| 2458 | |
| 2459 | // A right parentheses or ellipsis signals that we have a constructor. |
| 2460 | if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) { |
| 2461 | TPA.Revert(); |
| 2462 | return true; |
| 2463 | } |
| 2464 | |
| 2465 | // If we need to, enter the specified scope. |
| 2466 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2467 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2468 | DeclScopeObj.EnterDeclaratorScope(); |
| 2469 | |
| 2470 | // Check whether the next token(s) are part of a declaration |
| 2471 | // specifier, in which case we have the start of a parameter and, |
| 2472 | // therefore, we know that this is a constructor. |
| 2473 | bool IsConstructor = isDeclarationSpecifier(); |
| 2474 | TPA.Revert(); |
| 2475 | return IsConstructor; |
| 2476 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2477 | |
| 2478 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2479 | /// type-qualifier-list: [C99 6.7.5] |
| 2480 | /// type-qualifier |
| 2481 | /// [vendor] attributes |
| 2482 | /// [ only if VendorAttributesAllowed=true ] |
| 2483 | /// type-qualifier-list type-qualifier |
| 2484 | /// [vendor] type-qualifier-list attributes |
| 2485 | /// [ only if VendorAttributesAllowed=true ] |
| 2486 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 2487 | /// [ only if CXX0XAttributesAllowed=true ] |
| 2488 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2489 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2490 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 2491 | bool VendorAttributesAllowed, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2492 | bool CXX0XAttributesAllowed) { |
| 2493 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) { |
| 2494 | SourceLocation Loc = Tok.getLocation(); |
| 2495 | CXX0XAttributeList Attr = ParseCXX0XAttributes(); |
| 2496 | if (CXX0XAttributesAllowed) |
| 2497 | DS.AddAttributes(Attr.AttrList); |
| 2498 | else |
| 2499 | Diag(Loc, diag::err_attributes_not_allowed); |
| 2500 | } |
| 2501 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2502 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2503 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2504 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2505 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2506 | SourceLocation Loc = Tok.getLocation(); |
| 2507 | |
| 2508 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 2509 | case tok::code_completion: |
| 2510 | Actions.CodeCompleteTypeQualifiers(DS); |
| 2511 | ConsumeCodeCompletionToken(); |
| 2512 | break; |
| 2513 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2514 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2515 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
| 2516 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2517 | break; |
| 2518 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2519 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
| 2520 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2521 | break; |
| 2522 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2523 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
| 2524 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2525 | break; |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2526 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2527 | case tok::kw___ptr64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2528 | case tok::kw___cdecl: |
| 2529 | case tok::kw___stdcall: |
| 2530 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2531 | case tok::kw___thiscall: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2532 | if (VendorAttributesAllowed) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2533 | DS.AddAttributes(ParseMicrosoftTypeAttributes()); |
| 2534 | continue; |
| 2535 | } |
| 2536 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2537 | case tok::kw___pascal: |
| 2538 | if (VendorAttributesAllowed) { |
| 2539 | DS.AddAttributes(ParseBorlandTypeAttributes()); |
| 2540 | continue; |
| 2541 | } |
| 2542 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2543 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2544 | if (VendorAttributesAllowed) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2545 | DS.AddAttributes(ParseGNUAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 2546 | continue; // do *not* consume the next token! |
| 2547 | } |
| 2548 | // otherwise, FALL THROUGH! |
| 2549 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2550 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 2551 | // If this is not a type-qualifier token, we're done reading type |
| 2552 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2553 | DS.Finish(Diags, PP); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 2554 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2555 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 2556 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2557 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 2558 | if (isInvalid) { |
| 2559 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2560 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2561 | } |
| 2562 | ConsumeToken(); |
| 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | |
| 2567 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 2568 | /// |
| 2569 | void Parser::ParseDeclarator(Declarator &D) { |
| 2570 | /// This implements the 'declarator' production in the C grammar, then checks |
| 2571 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2572 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2575 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 2576 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 2577 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2578 | /// ptr-operator production. |
| 2579 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2580 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 2581 | /// [C] pointer[opt] direct-declarator |
| 2582 | /// [C++] direct-declarator |
| 2583 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2584 | /// |
| 2585 | /// pointer: [C99 6.7.5] |
| 2586 | /// '*' type-qualifier-list[opt] |
| 2587 | /// '*' type-qualifier-list[opt] pointer |
| 2588 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2589 | /// ptr-operator: |
| 2590 | /// '*' cv-qualifier-seq[opt] |
| 2591 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 2592 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2593 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 2594 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2595 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2596 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 2597 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 2598 | if (Diags.hasAllExtensionsSilenced()) |
| 2599 | D.setExtension(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2600 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2601 | // C++ member pointers start with a '::' or a nested-name. |
| 2602 | // Member pointers get special handling, since there's no place for the |
| 2603 | // scope spec in the generic path below. |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 2604 | if (getLang().CPlusPlus && |
| 2605 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 2606 | Tok.is(tok::annot_cxxscope))) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2607 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2608 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2609 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 2610 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2611 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2612 | // The scope spec really belongs to the direct-declarator. |
| 2613 | D.getCXXScopeSpec() = SS; |
| 2614 | if (DirectDeclParser) |
| 2615 | (this->*DirectDeclParser)(D); |
| 2616 | return; |
| 2617 | } |
| 2618 | |
| 2619 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2620 | D.SetRangeEnd(Loc); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2621 | DeclSpec DS; |
| 2622 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2623 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2624 | |
| 2625 | // Recurse to parse whatever is left. |
| 2626 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 2627 | |
| 2628 | // Sema will have to catch (syntactically invalid) pointers into global |
| 2629 | // scope. It has to catch pointers into namespace scope anyway. |
| 2630 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2631 | Loc, DS.TakeAttributes()), |
| 2632 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2633 | return; |
| 2634 | } |
| 2635 | } |
| 2636 | |
| 2637 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2638 | // Not a pointer, C++ reference, or block. |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 2639 | if (Kind != tok::star && Kind != tok::caret && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 2640 | (Kind != tok::amp || !getLang().CPlusPlus) && |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 2641 | // 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] | 2642 | (Kind != tok::ampamp || !getLang().CPlusPlus)) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2643 | if (DirectDeclParser) |
| 2644 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2645 | return; |
| 2646 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2647 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 2648 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 2649 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 2650 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2651 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2652 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 2653 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 2654 | // Is a pointer. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2655 | DeclSpec DS; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2656 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2657 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2658 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2659 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2660 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2661 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2662 | if (Kind == tok::star) |
| 2663 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 2664 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2665 | DS.TakeAttributes()), |
| 2666 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2667 | else |
| 2668 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2669 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
Mike Stump | 75b163f | 2009-04-21 00:51:43 +0000 | [diff] [blame] | 2670 | Loc, DS.TakeAttributes()), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2671 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2672 | } else { |
| 2673 | // Is a reference |
| 2674 | DeclSpec DS; |
| 2675 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 2676 | // Complain about rvalue references in C++03, but then go on and build |
| 2677 | // the declarator. |
| 2678 | if (Kind == tok::ampamp && !getLang().CPlusPlus0x) |
| 2679 | Diag(Loc, diag::err_rvalue_reference); |
| 2680 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2681 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 2682 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 2683 | // template type argument, in which case the cv-qualifiers are ignored. |
| 2684 | // |
| 2685 | // [GNU] Retricted references are allowed. |
| 2686 | // [GNU] Attributes on references are allowed. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2687 | // [C++0x] Attributes on references are not allowed. |
| 2688 | ParseTypeQualifierListOpt(DS, true, false); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2689 | D.ExtendWithDeclSpec(DS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2690 | |
| 2691 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 2692 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 2693 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2694 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2695 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 2696 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2697 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2701 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2702 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 2703 | if (D.getNumTypeObjects() > 0) { |
| 2704 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 2705 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 2706 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 2707 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 2708 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 2709 | << II; |
| 2710 | else |
| 2711 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 2712 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 2713 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2714 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 2715 | // can go ahead and build the (technically ill-formed) |
| 2716 | // declarator: reference collapsing will take care of it. |
| 2717 | } |
| 2718 | } |
| 2719 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2720 | // 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] | 2721 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 2722 | DS.TakeAttributes(), |
| 2723 | Kind == tok::amp), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2724 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2725 | } |
| 2726 | } |
| 2727 | |
| 2728 | /// ParseDirectDeclarator |
| 2729 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2730 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2731 | /// '(' declarator ')' |
| 2732 | /// [GNU] '(' attributes declarator ')' |
| 2733 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 2734 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 2735 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 2736 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 2737 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 2738 | /// direct-declarator '(' parameter-type-list ')' |
| 2739 | /// direct-declarator '(' identifier-list[opt] ')' |
| 2740 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 2741 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2742 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 2743 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2744 | /// [C++] declarator-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2745 | /// |
| 2746 | /// declarator-id: [C++ 8] |
| 2747 | /// id-expression |
| 2748 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 2749 | /// |
| 2750 | /// id-expression: [C++ 5.1] |
| 2751 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 2752 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2753 | /// |
| 2754 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2756 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 2757 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2758 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2759 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 2760 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2761 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2762 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2763 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2764 | if (getLang().CPlusPlus && D.mayHaveIdentifier()) { |
| 2765 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2766 | if (D.getCXXScopeSpec().isEmpty()) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2767 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2770 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2771 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 2772 | // Change the declaration context for name lookup, until this function |
| 2773 | // is exited (and the declarator has been parsed). |
| 2774 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2777 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 2778 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 2779 | // We found something that indicates the start of an unqualified-id. |
| 2780 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2781 | bool AllowConstructorName; |
| 2782 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 2783 | AllowConstructorName = false; |
| 2784 | else if (D.getCXXScopeSpec().isSet()) |
| 2785 | AllowConstructorName = |
| 2786 | (D.getContext() == Declarator::FileContext || |
| 2787 | (D.getContext() == Declarator::MemberContext && |
| 2788 | D.getDeclSpec().isFriendSpecified())); |
| 2789 | else |
| 2790 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 2791 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2792 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 2793 | /*EnteringContext=*/true, |
| 2794 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2795 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2796 | ParsedType(), |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2797 | D.getName()) || |
| 2798 | // Once we're past the identifier, if the scope was bad, mark the |
| 2799 | // whole declarator bad. |
| 2800 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2801 | D.SetIdentifier(0, Tok.getLocation()); |
| 2802 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2803 | } else { |
| 2804 | // Parsed the unqualified-id; update range information and move along. |
| 2805 | if (D.getSourceRange().getBegin().isInvalid()) |
| 2806 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 2807 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2808 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2809 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2810 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2811 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2812 | assert(!getLang().CPlusPlus && |
| 2813 | "There's a C++-specific check for tok::identifier above"); |
| 2814 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 2815 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 2816 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2817 | goto PastIdentifier; |
| 2818 | } |
| 2819 | |
| 2820 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2821 | // direct-declarator: '(' declarator ')' |
| 2822 | // direct-declarator: '(' attributes declarator ')' |
| 2823 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 2824 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2825 | |
| 2826 | // If the declarator was parenthesized, we entered the declarator |
| 2827 | // scope when parsing the parenthesized declarator, then exited |
| 2828 | // the scope already. Re-enter the scope, if we need to. |
| 2829 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 2830 | // If there was an error parsing parenthesized declarator, declarator |
| 2831 | // scope may have been enterred before. Don't do it again. |
| 2832 | if (!D.isInvalidType() && |
| 2833 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2834 | // Change the declaration context for name lookup, until this function |
| 2835 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 2836 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2837 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2838 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2839 | // This could be something simple like "int" (in which case the declarator |
| 2840 | // portion is empty), if an abstract-declarator is allowed. |
| 2841 | D.SetIdentifier(0, Tok.getLocation()); |
| 2842 | } else { |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 2843 | if (D.getContext() == Declarator::MemberContext) |
| 2844 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 2845 | << D.getDeclSpec().getSourceRange(); |
| 2846 | else if (getLang().CPlusPlus) |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2847 | Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2848 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2849 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2850 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 2851 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2852 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2853 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 2854 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2855 | assert(D.isPastIdentifier() && |
| 2856 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2857 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2858 | // Don't parse attributes unless we have an identifier. |
Douglas Gregor | 3c3aaf9 | 2010-02-19 16:47:56 +0000 | [diff] [blame] | 2859 | if (D.getIdentifier() && getLang().CPlusPlus0x |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2860 | && isCXX0XAttributeSpecifier(true)) { |
| 2861 | SourceLocation AttrEndLoc; |
| 2862 | CXX0XAttributeList Attr = ParseCXX0XAttributes(); |
| 2863 | D.AddAttributes(Attr.AttrList, AttrEndLoc); |
| 2864 | } |
| 2865 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2866 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2867 | if (Tok.is(tok::l_paren)) { |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2868 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 2869 | // In such a case, check if we actually have a function declarator; if it |
| 2870 | // is not, the declarator has been fully parsed. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2871 | if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 2872 | // When not in file scope, warn for ambiguous function declarators, just |
| 2873 | // in case the author intended it as a variable definition. |
| 2874 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 2875 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 2876 | break; |
| 2877 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2878 | ParseFunctionDeclarator(ConsumeParen(), D); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2879 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2880 | ParseBracketDeclarator(D); |
| 2881 | } else { |
| 2882 | break; |
| 2883 | } |
| 2884 | } |
| 2885 | } |
| 2886 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2887 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 2888 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2889 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2890 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 2891 | /// |
| 2892 | /// direct-declarator: |
| 2893 | /// '(' declarator ')' |
| 2894 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2895 | /// direct-declarator '(' parameter-type-list ')' |
| 2896 | /// direct-declarator '(' identifier-list[opt] ')' |
| 2897 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 2898 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2899 | /// |
| 2900 | void Parser::ParseParenDeclarator(Declarator &D) { |
| 2901 | SourceLocation StartLoc = ConsumeParen(); |
| 2902 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2903 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2904 | // Eat any attributes before we look at whether this is a grouping or function |
| 2905 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 2906 | // the type being built up, for example: |
| 2907 | // int (__attribute__(()) *x)(long y) |
| 2908 | // If this ends up not being a grouping paren, the attribute applies to the |
| 2909 | // first argument, for example: |
| 2910 | // int (__attribute__(()) int x) |
| 2911 | // In either case, we need to eat any attributes to be able to determine what |
| 2912 | // sort of paren this is. |
| 2913 | // |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 2914 | llvm::OwningPtr<AttributeList> AttrList; |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2915 | bool RequiresArg = false; |
| 2916 | if (Tok.is(tok::kw___attribute)) { |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 2917 | AttrList.reset(ParseGNUAttributes()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2918 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2919 | // We require that the argument list (if this is a non-grouping paren) be |
| 2920 | // present even if the attribute list was empty. |
| 2921 | RequiresArg = true; |
| 2922 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2923 | // Eat any Microsoft extensions. |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2924 | if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2925 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) || |
| 2926 | Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) { |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 2927 | AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take())); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2928 | } |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2929 | // Eat any Borland extensions. |
| 2930 | if (Tok.is(tok::kw___pascal)) { |
| 2931 | AttrList.reset(ParseBorlandTypeAttributes(AttrList.take())); |
| 2932 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2933 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2934 | // If we haven't past the identifier yet (or where the identifier would be |
| 2935 | // stored, if this is an abstract declarator), then this is probably just |
| 2936 | // grouping parens. However, if this could be an abstract-declarator, then |
| 2937 | // this could also be the start of function arguments (consider 'void()'). |
| 2938 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2939 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2940 | if (!D.mayOmitIdentifier()) { |
| 2941 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 2942 | // paren, because we haven't seen the identifier yet. |
| 2943 | isGrouping = true; |
| 2944 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Argyrios Kyrtzidis | e25d270 | 2008-10-06 00:07:55 +0000 | [diff] [blame] | 2945 | (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2946 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 2947 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 2948 | // considered to be a type, not a K&R identifier-list. |
| 2949 | isGrouping = false; |
| 2950 | } else { |
| 2951 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 2952 | isGrouping = true; |
| 2953 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2954 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2955 | // If this is a grouping paren, handle: |
| 2956 | // direct-declarator: '(' declarator ')' |
| 2957 | // direct-declarator: '(' attributes declarator ')' |
| 2958 | if (isGrouping) { |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 2959 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2960 | D.setGroupingParens(true); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2961 | if (AttrList) |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 2962 | D.AddAttributes(AttrList.take(), SourceLocation()); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2963 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2964 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2965 | // Match the ')'. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2966 | SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 2967 | |
| 2968 | D.setGroupingParens(hadGroupingParens); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2969 | D.SetRangeEnd(Loc); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2970 | return; |
| 2971 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2972 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2973 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 2974 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2975 | // identifier (and remember where it would have been), then call into |
| 2976 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2977 | D.SetIdentifier(0, Tok.getLocation()); |
| 2978 | |
Ted Kremenek | 1e37765 | 2010-02-11 02:19:13 +0000 | [diff] [blame] | 2979 | ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 2983 | /// declarator D up to a paren, which indicates that we are parsing function |
| 2984 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2985 | /// |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2986 | /// If AttrList is non-null, then the caller parsed those arguments immediately |
| 2987 | /// after the open paren - they should be considered to be the first argument of |
| 2988 | /// a parameter. If RequiresArg is true, then the first argument of the |
| 2989 | /// function is required to be present and required to not be an identifier |
| 2990 | /// list. |
| 2991 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2992 | /// This method also handles this portion of the grammar: |
| 2993 | /// parameter-type-list: [C99 6.7.5] |
| 2994 | /// parameter-list |
| 2995 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 2996 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2997 | /// |
| 2998 | /// parameter-list: [C99 6.7.5] |
| 2999 | /// parameter-declaration |
| 3000 | /// parameter-list ',' parameter-declaration |
| 3001 | /// |
| 3002 | /// parameter-declaration: [C99 6.7.5] |
| 3003 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3004 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3005 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 3006 | /// declaration-specifiers abstract-declarator[opt] |
| 3007 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3008 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3009 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 3010 | /// |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3011 | /// 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] | 3012 | /// and "exception-specification[opt]". |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3013 | /// |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3014 | void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D, |
| 3015 | AttributeList *AttrList, |
| 3016 | bool RequiresArg) { |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3017 | // lparen is already consumed! |
| 3018 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3019 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3020 | // This parameter list may be empty. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3021 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3022 | if (RequiresArg) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3023 | Diag(Tok, diag::err_argument_required_after_attribute); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3024 | delete AttrList; |
| 3025 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3026 | |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3027 | SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'. |
| 3028 | SourceLocation EndLoc = RParenLoc; |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3029 | |
| 3030 | // cv-qualifier-seq[opt]. |
| 3031 | DeclSpec DS; |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3032 | bool hasExceptionSpec = false; |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 3033 | SourceLocation ThrowLoc; |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3034 | bool hasAnyExceptionSpec = false; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3035 | llvm::SmallVector<ParsedType, 2> Exceptions; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3036 | llvm::SmallVector<SourceRange, 2> ExceptionRanges; |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3037 | if (getLang().CPlusPlus) { |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3038 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3039 | if (!DS.getSourceRange().getEnd().isInvalid()) |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3040 | EndLoc = DS.getSourceRange().getEnd(); |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3041 | |
| 3042 | // Parse exception-specification[opt]. |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3043 | if (Tok.is(tok::kw_throw)) { |
| 3044 | hasExceptionSpec = true; |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 3045 | ThrowLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3046 | ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges, |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3047 | hasAnyExceptionSpec); |
| 3048 | assert(Exceptions.size() == ExceptionRanges.size() && |
| 3049 | "Produced different number of exception types and ranges."); |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3050 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3053 | // Remember that we parsed a function type, and remember the attributes. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3054 | // int() -> no prototype, no '...'. |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3055 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus, |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3056 | /*variadic*/ false, |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3057 | SourceLocation(), |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3058 | /*arglist*/ 0, 0, |
| 3059 | DS.getTypeQualifiers(), |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 3060 | hasExceptionSpec, ThrowLoc, |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3061 | hasAnyExceptionSpec, |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3062 | Exceptions.data(), |
| 3063 | ExceptionRanges.data(), |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3064 | Exceptions.size(), |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3065 | LParenLoc, RParenLoc, D), |
| 3066 | EndLoc); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3067 | return; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3070 | // Alternatively, this parameter list may be an identifier list form for a |
| 3071 | // K&R-style function: void foo(a,b,c) |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3072 | if (!getLang().CPlusPlus && Tok.is(tok::identifier) |
| 3073 | && !TryAltiVecVectorToken()) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3074 | if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3075 | // K&R identifier lists can't have typedefs as identifiers, per |
| 3076 | // C99 6.7.5.3p11. |
Steve Naroff | 2d081c4 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 3077 | if (RequiresArg) { |
| 3078 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 3079 | delete AttrList; |
| 3080 | } |
Chris Lattner | 83a9447 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3081 | |
Steve Naroff | 2d081c4 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 3082 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
Chris Lattner | 83a9447 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3083 | // normal declarators, not for abstract-declarators. Get the first |
| 3084 | // identifier. |
Chris Lattner | 9a65b81 | 2010-05-14 17:44:56 +0000 | [diff] [blame] | 3085 | Token FirstTok = Tok; |
Chris Lattner | 83a9447 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3086 | ConsumeToken(); // eat the first identifier. |
Chris Lattner | 9a65b81 | 2010-05-14 17:44:56 +0000 | [diff] [blame] | 3087 | |
| 3088 | // Identifier lists follow a really simple grammar: the identifiers can |
| 3089 | // be followed *only* by a ", moreidentifiers" or ")". However, K&R |
| 3090 | // identifier lists are really rare in the brave new modern world, and it |
| 3091 | // is very common for someone to typo a type in a non-k&r style list. If |
| 3092 | // we are presented with something like: "void foo(intptr x, float y)", |
| 3093 | // we don't want to start parsing the function declarator as though it is |
| 3094 | // a K&R style declarator just because intptr is an invalid type. |
| 3095 | // |
| 3096 | // To handle this, we check to see if the token after the first identifier |
| 3097 | // is a "," or ")". Only if so, do we parse it as an identifier list. |
| 3098 | if (Tok.is(tok::comma) || Tok.is(tok::r_paren)) |
| 3099 | return ParseFunctionDeclaratorIdentifierList(LParenLoc, |
| 3100 | FirstTok.getIdentifierInfo(), |
| 3101 | FirstTok.getLocation(), D); |
| 3102 | |
| 3103 | // If we get here, the code is invalid. Push the first identifier back |
| 3104 | // into the token stream and parse the first argument as an (invalid) |
| 3105 | // normal argument declarator. |
| 3106 | PP.EnterToken(Tok); |
| 3107 | Tok = FirstTok; |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3108 | } |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3109 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3110 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3111 | // Finally, a normal, non-empty parameter type list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3112 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3113 | // Build up an array of information about the parsed arguments. |
| 3114 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3115 | |
| 3116 | // Enter function-declaration scope, limiting any declarators to the |
| 3117 | // function prototype scope, including parameter declarators. |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 3118 | ParseScope PrototypeScope(this, |
| 3119 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3120 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3121 | bool IsVariadic = false; |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3122 | SourceLocation EllipsisLoc; |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3123 | while (1) { |
| 3124 | if (Tok.is(tok::ellipsis)) { |
| 3125 | IsVariadic = true; |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3126 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3127 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3128 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3129 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3130 | SourceLocation DSStart = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3131 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3132 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3133 | // Just use the ParsingDeclaration "scope" of the declarator. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3134 | DeclSpec DS; |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3135 | |
| 3136 | // If the caller parsed attributes for the first argument, add them now. |
| 3137 | if (AttrList) { |
| 3138 | DS.AddAttributes(AttrList); |
| 3139 | AttrList = 0; // Only apply the attributes to the first parameter. |
| 3140 | } |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 3141 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3142 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3143 | // Parse the declarator. This is "PrototypeContext", because we must |
| 3144 | // accept either 'declarator' or 'abstract-declarator' here. |
| 3145 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 3146 | ParseDeclarator(ParmDecl); |
| 3147 | |
| 3148 | // Parse GNU attributes, if present. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3149 | if (Tok.is(tok::kw___attribute)) { |
| 3150 | SourceLocation Loc; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3151 | AttributeList *AttrList = ParseGNUAttributes(&Loc); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3152 | ParmDecl.AddAttributes(AttrList, Loc); |
| 3153 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3154 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3155 | // Remember this parsed parameter in ParamInfo. |
| 3156 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3157 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3158 | // DefArgToks is used when the parsing of default arguments needs |
| 3159 | // to be delayed. |
| 3160 | CachedTokens *DefArgToks = 0; |
| 3161 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3162 | // If no parameter was specified, verify that *something* was specified, |
| 3163 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 3164 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 3165 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3166 | // Completely missing, emit error. |
| 3167 | Diag(DSStart, diag::err_missing_param); |
| 3168 | } else { |
| 3169 | // Otherwise, we have something. Add it and let semantic analysis try |
| 3170 | // to grok it and add the result to the ParamInfo we are building. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3171 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3172 | // Inform the actions module about the parameter declarator, so it gets |
| 3173 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3174 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3175 | |
| 3176 | // Parse the default argument, if any. We parse the default |
| 3177 | // arguments in all dialects; the semantic analysis in |
| 3178 | // ActOnParamDefaultArgument will reject the default argument in |
| 3179 | // C. |
| 3180 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3181 | SourceLocation EqualLoc = Tok.getLocation(); |
| 3182 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3183 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3184 | if (D.getContext() == Declarator::MemberContext) { |
| 3185 | // If we're inside a class definition, cache the tokens |
| 3186 | // corresponding to the default argument. We'll actually parse |
| 3187 | // them when we see the end of the class definition. |
| 3188 | // FIXME: Templates will require something similar. |
| 3189 | // FIXME: Can we use a smart pointer for Toks? |
| 3190 | DefArgToks = new CachedTokens; |
| 3191 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3192 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 3193 | /*StopAtSemi=*/true, |
| 3194 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3195 | delete DefArgToks; |
| 3196 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3197 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 3198 | } else { |
| 3199 | // Mark the end of the default argument so that we know when to |
| 3200 | // stop when we parse it later on. |
| 3201 | Token DefArgEnd; |
| 3202 | DefArgEnd.startToken(); |
| 3203 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 3204 | DefArgEnd.setLocation(Tok.getLocation()); |
| 3205 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3206 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3207 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 3208 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3209 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3210 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3211 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3212 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3213 | // The argument isn't actually potentially evaluated unless it is |
| 3214 | // used. |
| 3215 | EnterExpressionEvaluationContext Eval(Actions, |
| 3216 | Sema::PotentiallyEvaluatedIfUsed); |
| 3217 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3218 | ExprResult DefArgResult(ParseAssignmentExpression()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3219 | if (DefArgResult.isInvalid()) { |
| 3220 | Actions.ActOnParamDefaultArgumentError(Param); |
| 3221 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 3222 | } else { |
| 3223 | // Inform the actions module about the default argument |
| 3224 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3225 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3226 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3227 | } |
| 3228 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3229 | |
| 3230 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 3231 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3232 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | // If the next token is a comma, consume it and keep reading arguments. |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 3236 | if (Tok.isNot(tok::comma)) { |
| 3237 | if (Tok.is(tok::ellipsis)) { |
| 3238 | IsVariadic = true; |
| 3239 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
| 3240 | |
| 3241 | if (!getLang().CPlusPlus) { |
| 3242 | // We have ellipsis without a preceding ',', which is ill-formed |
| 3243 | // in C. Complain and provide the fix. |
| 3244 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3245 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 3246 | } |
| 3247 | } |
| 3248 | |
| 3249 | break; |
| 3250 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3251 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3252 | // Consume the comma. |
| 3253 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3254 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3255 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3256 | // Leave prototype scope. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 3257 | PrototypeScope.Exit(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3258 | |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3259 | // If we have the closing ')', eat it. |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3260 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 3261 | SourceLocation EndLoc = RParenLoc; |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3262 | |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3263 | DeclSpec DS; |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3264 | bool hasExceptionSpec = false; |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 3265 | SourceLocation ThrowLoc; |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3266 | bool hasAnyExceptionSpec = false; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3267 | llvm::SmallVector<ParsedType, 2> Exceptions; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3268 | llvm::SmallVector<SourceRange, 2> ExceptionRanges; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3269 | |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3270 | if (getLang().CPlusPlus) { |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3271 | // Parse cv-qualifier-seq[opt]. |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3272 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3273 | if (!DS.getSourceRange().getEnd().isInvalid()) |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3274 | EndLoc = DS.getSourceRange().getEnd(); |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3275 | |
| 3276 | // Parse exception-specification[opt]. |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3277 | if (Tok.is(tok::kw_throw)) { |
| 3278 | hasExceptionSpec = true; |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 3279 | ThrowLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3280 | ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges, |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3281 | hasAnyExceptionSpec); |
| 3282 | assert(Exceptions.size() == ExceptionRanges.size() && |
| 3283 | "Produced different number of exception types and ranges."); |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3284 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3287 | // Remember that we parsed a function type, and remember the attributes. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3288 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic, |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3289 | EllipsisLoc, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 3290 | ParamInfo.data(), ParamInfo.size(), |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3291 | DS.getTypeQualifiers(), |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 3292 | hasExceptionSpec, ThrowLoc, |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3293 | hasAnyExceptionSpec, |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3294 | Exceptions.data(), |
| 3295 | ExceptionRanges.data(), |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3296 | Exceptions.size(), |
| 3297 | LParenLoc, RParenLoc, D), |
| 3298 | EndLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3299 | } |
| 3300 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3301 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 3302 | /// we found a K&R-style identifier list instead of a type argument list. The |
Chris Lattner | 83a9447 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3303 | /// first identifier has already been consumed, and the current token is the |
| 3304 | /// token right after it. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3305 | /// |
| 3306 | /// identifier-list: [C99 6.7.5] |
| 3307 | /// identifier |
| 3308 | /// identifier-list ',' identifier |
| 3309 | /// |
| 3310 | void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc, |
Chris Lattner | 83a9447 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3311 | IdentifierInfo *FirstIdent, |
| 3312 | SourceLocation FirstIdentLoc, |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3313 | Declarator &D) { |
| 3314 | // Build up an array of information about the parsed arguments. |
| 3315 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
| 3316 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3317 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3318 | // If there was no identifier specified for the declarator, either we are in |
| 3319 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 3320 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 3321 | // diagnose this. |
| 3322 | if (!D.getIdentifier()) |
Chris Lattner | 83a9447 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3323 | Diag(FirstIdentLoc, diag::ext_ident_list_in_param); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3324 | |
Chris Lattner | 83a9447 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3325 | // The first identifier was already read, and is known to be the first |
| 3326 | // identifier in the list. Remember this identifier in ParamInfo. |
| 3327 | ParamsSoFar.insert(FirstIdent); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3328 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3329 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3330 | while (Tok.is(tok::comma)) { |
| 3331 | // Eat the comma. |
| 3332 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3333 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3334 | // If this isn't an identifier, report the error and skip until ')'. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3335 | if (Tok.isNot(tok::identifier)) { |
| 3336 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3337 | SkipUntil(tok::r_paren); |
| 3338 | return; |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3339 | } |
Chris Lattner | aaf9ddb | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 3340 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3341 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
Chris Lattner | aaf9ddb | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 3342 | |
| 3343 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3344 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3345 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3346 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3347 | // Verify that the argument identifier has not already been mentioned. |
| 3348 | if (!ParamsSoFar.insert(ParmII)) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3349 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3350 | } else { |
| 3351 | // Remember this identifier in ParamInfo. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3352 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3353 | Tok.getLocation(), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3354 | 0)); |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3355 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3356 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3357 | // Eat the identifier. |
| 3358 | ConsumeToken(); |
| 3359 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3360 | |
| 3361 | // If we have the closing ')', eat it and we're done. |
| 3362 | SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 3363 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3364 | // Remember that we parsed a function type, and remember the attributes. This |
| 3365 | // function type is always a K&R style function type, which is not varargs and |
| 3366 | // has no prototype. |
| 3367 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false, |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3368 | SourceLocation(), |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3369 | &ParamInfo[0], ParamInfo.size(), |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3370 | /*TypeQuals*/0, |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 3371 | /*exception*/false, |
| 3372 | SourceLocation(), false, 0, 0, 0, |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3373 | LParenLoc, RLoc, D), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3374 | RLoc); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3375 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3376 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3377 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 3378 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 3379 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 3380 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 3381 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 3382 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 3383 | SourceLocation StartLoc = ConsumeBracket(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3384 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3385 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 3386 | // This code does a fast path to handle some of the most obvious cases. |
| 3387 | if (Tok.getKind() == tok::r_square) { |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3388 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3389 | //FIXME: Use these |
| 3390 | CXX0XAttributeList Attr; |
| 3391 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) { |
| 3392 | Attr = ParseCXX0XAttributes(); |
| 3393 | } |
| 3394 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3395 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3396 | ExprResult NumElements; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3397 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
| 3398 | StartLoc, EndLoc), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3399 | EndLoc); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3400 | return; |
| 3401 | } else if (Tok.getKind() == tok::numeric_constant && |
| 3402 | GetLookAheadToken(1).is(tok::r_square)) { |
| 3403 | // [4] is very common. Parse the numeric constant expression. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3404 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok)); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3405 | ConsumeToken(); |
| 3406 | |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3407 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3408 | //FIXME: Use these |
| 3409 | CXX0XAttributeList Attr; |
| 3410 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) { |
| 3411 | Attr = ParseCXX0XAttributes(); |
| 3412 | } |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3413 | |
| 3414 | // If there was an error parsing the assignment-expression, recover. |
| 3415 | if (ExprRes.isInvalid()) |
| 3416 | ExprRes.release(); // Deallocate expr, just use []. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3417 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3418 | // Remember that we parsed a array type, and remember its features. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3419 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(), |
| 3420 | StartLoc, EndLoc), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3421 | EndLoc); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3422 | return; |
| 3423 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3424 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3425 | // If valid, this location is the position where we read the 'static' keyword. |
| 3426 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3427 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3428 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3429 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3430 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3431 | // Type qualifiers in an array subscript are a C99 feature. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3432 | DeclSpec DS; |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3433 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3434 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3435 | // If we haven't already read 'static', check to see if there is one after the |
| 3436 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3437 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3438 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3439 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3440 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 3441 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3442 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3443 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 3444 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 3445 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 3446 | // the the token after the star is a ']'. Since stars in arrays are |
| 3447 | // infrequent, use of lookahead is not costly here. |
| 3448 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 3449 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3450 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3451 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 3452 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3453 | StaticLoc = SourceLocation(); // Drop the static. |
| 3454 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 3455 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3456 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3457 | // Note, in C89, this production uses the constant-expr production instead |
| 3458 | // of assignment-expr. The only difference is that assignment-expr allows |
| 3459 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 3460 | // are not i-c-e's, so we don't need to distinguish between the two here. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3461 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3462 | // Parse the constant-expression or assignment-expression now (depending |
| 3463 | // on dialect). |
| 3464 | if (getLang().CPlusPlus) |
| 3465 | NumElements = ParseConstantExpression(); |
| 3466 | else |
| 3467 | NumElements = ParseAssignmentExpression(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3468 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3469 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3470 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3471 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 3472 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3473 | // If the expression was invalid, skip it. |
| 3474 | SkipUntil(tok::r_square); |
| 3475 | return; |
| 3476 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3477 | |
| 3478 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
| 3479 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3480 | //FIXME: Use these |
| 3481 | CXX0XAttributeList Attr; |
| 3482 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) { |
| 3483 | Attr = ParseCXX0XAttributes(); |
| 3484 | } |
| 3485 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 3486 | // Remember that we parsed a array type, and remember its features. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3487 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
| 3488 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3489 | NumElements.release(), |
| 3490 | StartLoc, EndLoc), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3491 | EndLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 3494 | /// [GNU] typeof-specifier: |
| 3495 | /// typeof ( expressions ) |
| 3496 | /// typeof ( type-name ) |
| 3497 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3498 | /// |
| 3499 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3500 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3501 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3502 | SourceLocation StartLoc = ConsumeToken(); |
| 3503 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 3504 | const bool hasParens = Tok.is(tok::l_paren); |
| 3505 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3506 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3507 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3508 | SourceRange CastRange; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3509 | ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok, |
John McCall | 911093e | 2010-08-25 02:45:51 +0000 | [diff] [blame] | 3510 | isCastExpr, |
| 3511 | CastTy, |
| 3512 | CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 3513 | if (hasParens) |
| 3514 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3515 | |
| 3516 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 3517 | // FIXME: Not accurate, the range gets one token more than it should. |
| 3518 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3519 | else |
| 3520 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3521 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3522 | if (isCastExpr) { |
| 3523 | if (!CastTy) { |
| 3524 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 3525 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 3526 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 3527 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3528 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3529 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3530 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 3531 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3532 | DiagID, CastTy)) |
| 3533 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 3534 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 3535 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 3536 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 3537 | // If we get here, the operand to the typeof was an expresion. |
| 3538 | if (Operand.isInvalid()) { |
| 3539 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 3540 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3541 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 3542 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 3543 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3544 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 3545 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 3546 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3547 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3548 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3549 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 3550 | |
| 3551 | |
| 3552 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 3553 | /// from TryAltiVecVectorToken. |
| 3554 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 3555 | Token Next = NextToken(); |
| 3556 | switch (Next.getKind()) { |
| 3557 | default: return false; |
| 3558 | case tok::kw_short: |
| 3559 | case tok::kw_long: |
| 3560 | case tok::kw_signed: |
| 3561 | case tok::kw_unsigned: |
| 3562 | case tok::kw_void: |
| 3563 | case tok::kw_char: |
| 3564 | case tok::kw_int: |
| 3565 | case tok::kw_float: |
| 3566 | case tok::kw_double: |
| 3567 | case tok::kw_bool: |
| 3568 | case tok::kw___pixel: |
| 3569 | Tok.setKind(tok::kw___vector); |
| 3570 | return true; |
| 3571 | case tok::identifier: |
| 3572 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 3573 | Tok.setKind(tok::kw___vector); |
| 3574 | return true; |
| 3575 | } |
| 3576 | return false; |
| 3577 | } |
| 3578 | } |
| 3579 | |
| 3580 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 3581 | const char *&PrevSpec, unsigned &DiagID, |
| 3582 | bool &isInvalid) { |
| 3583 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 3584 | Token Next = NextToken(); |
| 3585 | switch (Next.getKind()) { |
| 3586 | case tok::kw_short: |
| 3587 | case tok::kw_long: |
| 3588 | case tok::kw_signed: |
| 3589 | case tok::kw_unsigned: |
| 3590 | case tok::kw_void: |
| 3591 | case tok::kw_char: |
| 3592 | case tok::kw_int: |
| 3593 | case tok::kw_float: |
| 3594 | case tok::kw_double: |
| 3595 | case tok::kw_bool: |
| 3596 | case tok::kw___pixel: |
| 3597 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 3598 | return true; |
| 3599 | case tok::identifier: |
| 3600 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 3601 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 3602 | return true; |
| 3603 | } |
| 3604 | break; |
| 3605 | default: |
| 3606 | break; |
| 3607 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 3608 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 3609 | DS.isTypeAltiVecVector()) { |
| 3610 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 3611 | return true; |
| 3612 | } |
| 3613 | return false; |
| 3614 | } |
| 3615 | |