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