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