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