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