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