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