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