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