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