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