Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 16 | #include "clang/Basic/OpenCL.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Scope.h" |
| 18 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 19 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 20 | #include "RAIIObjectsForParser.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallSet.h" |
| 22 | using namespace clang; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // C99 6.7: Declarations. |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | /// ParseTypeName |
| 29 | /// type-name: [C99 6.7.6] |
| 30 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 31 | /// |
| 32 | /// Called type-id in C++. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 33 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
| 34 | Declarator::TheContext Context) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | // Parse the common declaration-specifiers piece. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 36 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | ParseSpecifierQualifierList(DS); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 38 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 40 | Declarator DeclaratorInfo(DS, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 42 | if (Range) |
| 43 | *Range = DeclaratorInfo.getSourceRange(); |
| 44 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 45 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 46 | return true; |
| 47 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 48 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 51 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | /// |
| 53 | /// [GNU] attributes: |
| 54 | /// attribute |
| 55 | /// attributes attribute |
| 56 | /// |
| 57 | /// [GNU] attribute: |
| 58 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 59 | /// |
| 60 | /// [GNU] attribute-list: |
| 61 | /// attrib |
| 62 | /// attribute_list ',' attrib |
| 63 | /// |
| 64 | /// [GNU] attrib: |
| 65 | /// empty |
| 66 | /// attrib-name |
| 67 | /// attrib-name '(' identifier ')' |
| 68 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 69 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 70 | /// |
| 71 | /// [GNU] attrib-name: |
| 72 | /// identifier |
| 73 | /// typespec |
| 74 | /// typequal |
| 75 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 79 | /// which is followed by a comma or close parenthesis, then the arguments |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | /// start with that identifier; otherwise they are an expression list." |
| 81 | /// |
| 82 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 83 | /// any attributes that don't work (based on my limited testing). Most |
| 84 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 85 | /// a pressing need to implement the 2 token lookahead. |
| 86 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 87 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
| 88 | SourceLocation *endLoc) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 89 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 91 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | ConsumeToken(); |
| 93 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 94 | "attribute")) { |
| 95 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 96 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | } |
| 98 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 99 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 100 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | } |
| 102 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 103 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 104 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
| 106 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 108 | ConsumeToken(); |
| 109 | continue; |
| 110 | } |
| 111 | // we have an identifier or declaration specifier (const, int, etc.) |
| 112 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 113 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 115 | // Availability attributes have their own grammar. |
| 116 | if (AttrName->isStr("availability")) |
| 117 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, attrs, endLoc); |
Douglas Gregor | ec1afbf | 2010-03-16 19:09:18 +0000 | [diff] [blame] | 118 | // check if we have a "parameterized" attribute |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 119 | else if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | ConsumeParen(); // ignore the left paren loc for now |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 122 | if (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | IdentifierInfo *ParmName = Tok.getIdentifierInfo(); |
| 124 | SourceLocation ParmLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
| 126 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 127 | // __attribute__(( mode(byte) )) |
| 128 | ConsumeParen(); // ignore the right paren loc for now |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 129 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 130 | ParmName, ParmLoc, 0, 0); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 131 | } else if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | ConsumeToken(); |
| 133 | // __attribute__(( format(printf, 1, 2) )) |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 134 | ExprVector ArgExprs(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | bool ArgExprsOk = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 137 | // now parse the non-empty comma separated list of expressions |
| 138 | while (1) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 139 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 140 | if (ArgExpr.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 141 | ArgExprsOk = false; |
| 142 | SkipUntil(tok::r_paren); |
| 143 | break; |
| 144 | } else { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 145 | ArgExprs.push_back(ArgExpr.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 146 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 147 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | break; |
| 149 | ConsumeToken(); // Eat the comma, move to the next argument |
| 150 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 151 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 152 | ConsumeParen(); // ignore the right paren loc for now |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 153 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 154 | ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } else { // not an identifier |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 158 | switch (Tok.getKind()) { |
| 159 | case tok::r_paren: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | // parse a possibly empty comma separated list of expressions |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | // __attribute__(( nonnull() )) |
| 162 | ConsumeParen(); // ignore the right paren loc for now |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 163 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 164 | 0, SourceLocation(), 0, 0); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 165 | break; |
| 166 | case tok::kw_char: |
| 167 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 168 | case tok::kw_char16_t: |
| 169 | case tok::kw_char32_t: |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 170 | case tok::kw_bool: |
| 171 | case tok::kw_short: |
| 172 | case tok::kw_int: |
| 173 | case tok::kw_long: |
| 174 | case tok::kw_signed: |
| 175 | case tok::kw_unsigned: |
| 176 | case tok::kw_float: |
| 177 | case tok::kw_double: |
| 178 | case tok::kw_void: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 179 | case tok::kw_typeof: { |
| 180 | AttributeList *attr |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 181 | = attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 182 | 0, SourceLocation(), 0, 0); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 183 | if (attr->getKind() == AttributeList::AT_IBOutletCollection) |
Fariborz Jahanian | 1b72fa7 | 2010-08-17 23:19:16 +0000 | [diff] [blame] | 184 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 185 | // If it's a builtin type name, eat it and expect a rparen |
| 186 | // __attribute__(( vec_type_hint(char) )) |
| 187 | ConsumeToken(); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 188 | if (Tok.is(tok::r_paren)) |
| 189 | ConsumeParen(); |
| 190 | break; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 191 | } |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 192 | default: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 193 | // __attribute__(( aligned(16) )) |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 194 | ExprVector ArgExprs(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 195 | bool ArgExprsOk = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 197 | // now parse the list of expressions |
| 198 | while (1) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 199 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 200 | if (ArgExpr.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | ArgExprsOk = false; |
| 202 | SkipUntil(tok::r_paren); |
| 203 | break; |
| 204 | } else { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 205 | ArgExprs.push_back(ArgExpr.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 206 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 207 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 208 | break; |
| 209 | ConsumeToken(); // Eat the comma, move to the next argument |
| 210 | } |
| 211 | // Match the ')'. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 212 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | ConsumeParen(); // ignore the right paren loc for now |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 214 | attrs.addNew(AttrName, AttrNameLoc, 0, |
| 215 | AttrNameLoc, 0, SourceLocation(), |
| 216 | ArgExprs.take(), ArgExprs.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 217 | } |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 218 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 222 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 223 | 0, SourceLocation(), 0, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 227 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 228 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 229 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 230 | SkipUntil(tok::r_paren, false); |
| 231 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 232 | if (endLoc) |
| 233 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 234 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 237 | /// ParseMicrosoftDeclSpec - Parse an __declspec construct |
| 238 | /// |
| 239 | /// [MS] decl-specifier: |
| 240 | /// __declspec ( extended-decl-modifier-seq ) |
| 241 | /// |
| 242 | /// [MS] extended-decl-modifier-seq: |
| 243 | /// extended-decl-modifier[opt] |
| 244 | /// extended-decl-modifier extended-decl-modifier-seq |
| 245 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 246 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 247 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 248 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 249 | ConsumeToken(); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 250 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 251 | "declspec")) { |
| 252 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 253 | return; |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 254 | } |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 255 | while (Tok.getIdentifierInfo()) { |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 256 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 257 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 258 | if (Tok.is(tok::l_paren)) { |
| 259 | ConsumeParen(); |
| 260 | // FIXME: This doesn't parse __declspec(property(get=get_func_name)) |
| 261 | // correctly. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 262 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 263 | if (!ArgExpr.isInvalid()) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 264 | Expr *ExprList = ArgExpr.take(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 265 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 266 | SourceLocation(), &ExprList, 1, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 267 | } |
| 268 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 269 | SkipUntil(tok::r_paren, false); |
| 270 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 271 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 272 | 0, SourceLocation(), 0, 0, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 276 | SkipUntil(tok::r_paren, false); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 277 | return; |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 278 | } |
| 279 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 280 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 281 | // Treat these like attributes |
| 282 | // FIXME: Allow Sema to distinguish between these and real attributes! |
| 283 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 284 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
| 285 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 286 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 287 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 288 | if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) |
| 289 | // FIXME: Support these properly! |
| 290 | continue; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 291 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 292 | SourceLocation(), 0, 0, true); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 293 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 294 | } |
| 295 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 296 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 297 | // Treat these like attributes |
| 298 | while (Tok.is(tok::kw___pascal)) { |
| 299 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 300 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 301 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 302 | SourceLocation(), 0, 0, true); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 303 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 306 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 307 | // Treat these like attributes |
| 308 | while (Tok.is(tok::kw___kernel)) { |
| 309 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 310 | attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"), |
| 311 | AttrNameLoc, 0, AttrNameLoc, 0, |
| 312 | SourceLocation(), 0, 0, false); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 316 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
| 317 | SourceLocation Loc = Tok.getLocation(); |
| 318 | switch(Tok.getKind()) { |
| 319 | // OpenCL qualifiers: |
| 320 | case tok::kw___private: |
| 321 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 322 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 323 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 324 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 325 | break; |
| 326 | |
| 327 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 328 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 329 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 330 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 331 | break; |
| 332 | |
| 333 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 334 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 335 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 336 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 337 | break; |
| 338 | |
| 339 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 340 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 341 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 342 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 343 | break; |
| 344 | |
| 345 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 346 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 347 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 348 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 349 | break; |
| 350 | |
| 351 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 352 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 353 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 354 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 355 | break; |
| 356 | |
| 357 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 358 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 359 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 360 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 361 | break; |
| 362 | default: break; |
| 363 | } |
| 364 | } |
| 365 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 366 | /// \brief Parse a version number. |
| 367 | /// |
| 368 | /// version: |
| 369 | /// simple-integer |
| 370 | /// simple-integer ',' simple-integer |
| 371 | /// simple-integer ',' simple-integer ',' simple-integer |
| 372 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 373 | Range = Tok.getLocation(); |
| 374 | |
| 375 | if (!Tok.is(tok::numeric_constant)) { |
| 376 | Diag(Tok, diag::err_expected_version); |
| 377 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 378 | return VersionTuple(); |
| 379 | } |
| 380 | |
| 381 | // Parse the major (and possibly minor and subminor) versions, which |
| 382 | // are stored in the numeric constant. We utilize a quirk of the |
| 383 | // lexer, which is that it handles something like 1.2.3 as a single |
| 384 | // numeric constant, rather than two separate tokens. |
| 385 | llvm::SmallString<512> Buffer; |
| 386 | Buffer.resize(Tok.getLength()+1); |
| 387 | const char *ThisTokBegin = &Buffer[0]; |
| 388 | |
| 389 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 390 | bool Invalid = false; |
| 391 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 392 | if (Invalid) |
| 393 | return VersionTuple(); |
| 394 | |
| 395 | // Parse the major version. |
| 396 | unsigned AfterMajor = 0; |
| 397 | unsigned Major = 0; |
| 398 | while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) { |
| 399 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 400 | ++AfterMajor; |
| 401 | } |
| 402 | |
| 403 | if (AfterMajor == 0) { |
| 404 | Diag(Tok, diag::err_expected_version); |
| 405 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 406 | return VersionTuple(); |
| 407 | } |
| 408 | |
| 409 | if (AfterMajor == ActualLength) { |
| 410 | ConsumeToken(); |
| 411 | |
| 412 | // We only had a single version component. |
| 413 | if (Major == 0) { |
| 414 | Diag(Tok, diag::err_zero_version); |
| 415 | return VersionTuple(); |
| 416 | } |
| 417 | |
| 418 | return VersionTuple(Major); |
| 419 | } |
| 420 | |
| 421 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 422 | Diag(Tok, diag::err_expected_version); |
| 423 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 424 | return VersionTuple(); |
| 425 | } |
| 426 | |
| 427 | // Parse the minor version. |
| 428 | unsigned AfterMinor = AfterMajor + 1; |
| 429 | unsigned Minor = 0; |
| 430 | while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) { |
| 431 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 432 | ++AfterMinor; |
| 433 | } |
| 434 | |
| 435 | if (AfterMinor == ActualLength) { |
| 436 | ConsumeToken(); |
| 437 | |
| 438 | // We had major.minor. |
| 439 | if (Major == 0 && Minor == 0) { |
| 440 | Diag(Tok, diag::err_zero_version); |
| 441 | return VersionTuple(); |
| 442 | } |
| 443 | |
| 444 | return VersionTuple(Major, Minor); |
| 445 | } |
| 446 | |
| 447 | // If what follows is not a '.', we have a problem. |
| 448 | if (ThisTokBegin[AfterMinor] != '.') { |
| 449 | Diag(Tok, diag::err_expected_version); |
| 450 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 451 | return VersionTuple(); |
| 452 | } |
| 453 | |
| 454 | // Parse the subminor version. |
| 455 | unsigned AfterSubminor = AfterMinor + 1; |
| 456 | unsigned Subminor = 0; |
| 457 | while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) { |
| 458 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 459 | ++AfterSubminor; |
| 460 | } |
| 461 | |
| 462 | if (AfterSubminor != ActualLength) { |
| 463 | Diag(Tok, diag::err_expected_version); |
| 464 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 465 | return VersionTuple(); |
| 466 | } |
| 467 | ConsumeToken(); |
| 468 | return VersionTuple(Major, Minor, Subminor); |
| 469 | } |
| 470 | |
| 471 | /// \brief Parse the contents of the "availability" attribute. |
| 472 | /// |
| 473 | /// availability-attribute: |
| 474 | /// 'availability' '(' platform ',' version-arg-list ')' |
| 475 | /// |
| 476 | /// platform: |
| 477 | /// identifier |
| 478 | /// |
| 479 | /// version-arg-list: |
| 480 | /// version-arg |
| 481 | /// version-arg ',' version-arg-list |
| 482 | /// |
| 483 | /// version-arg: |
| 484 | /// 'introduced' '=' version |
| 485 | /// 'deprecated' '=' version |
| 486 | /// 'removed' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 487 | /// 'unavailable' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 488 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 489 | SourceLocation AvailabilityLoc, |
| 490 | ParsedAttributes &attrs, |
| 491 | SourceLocation *endLoc) { |
| 492 | SourceLocation PlatformLoc; |
| 493 | IdentifierInfo *Platform = 0; |
| 494 | |
| 495 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 496 | AvailabilityChange Changes[Unknown]; |
| 497 | |
| 498 | // Opening '('. |
| 499 | SourceLocation LParenLoc; |
| 500 | if (!Tok.is(tok::l_paren)) { |
| 501 | Diag(Tok, diag::err_expected_lparen); |
| 502 | return; |
| 503 | } |
| 504 | LParenLoc = ConsumeParen(); |
| 505 | |
| 506 | // Parse the platform name, |
| 507 | if (Tok.isNot(tok::identifier)) { |
| 508 | Diag(Tok, diag::err_availability_expected_platform); |
| 509 | SkipUntil(tok::r_paren); |
| 510 | return; |
| 511 | } |
| 512 | Platform = Tok.getIdentifierInfo(); |
| 513 | PlatformLoc = ConsumeToken(); |
| 514 | |
| 515 | // Parse the ',' following the platform name. |
| 516 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 517 | return; |
| 518 | |
| 519 | // If we haven't grabbed the pointers for the identifiers |
| 520 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 521 | if (!Ident_introduced) { |
| 522 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 523 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 524 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 525 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 529 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 530 | do { |
| 531 | if (Tok.isNot(tok::identifier)) { |
| 532 | Diag(Tok, diag::err_availability_expected_change); |
| 533 | SkipUntil(tok::r_paren); |
| 534 | return; |
| 535 | } |
| 536 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 537 | SourceLocation KeywordLoc = ConsumeToken(); |
| 538 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 539 | if (Keyword == Ident_unavailable) { |
| 540 | if (UnavailableLoc.isValid()) { |
| 541 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 542 | << Keyword << SourceRange(UnavailableLoc); |
| 543 | } |
| 544 | UnavailableLoc = KeywordLoc; |
| 545 | |
| 546 | if (Tok.isNot(tok::comma)) |
| 547 | break; |
| 548 | |
| 549 | ConsumeToken(); |
| 550 | continue; |
| 551 | } |
| 552 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 553 | if (Tok.isNot(tok::equal)) { |
| 554 | Diag(Tok, diag::err_expected_equal_after) |
| 555 | << Keyword; |
| 556 | SkipUntil(tok::r_paren); |
| 557 | return; |
| 558 | } |
| 559 | ConsumeToken(); |
| 560 | |
| 561 | SourceRange VersionRange; |
| 562 | VersionTuple Version = ParseVersionTuple(VersionRange); |
| 563 | |
| 564 | if (Version.empty()) { |
| 565 | SkipUntil(tok::r_paren); |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | unsigned Index; |
| 570 | if (Keyword == Ident_introduced) |
| 571 | Index = Introduced; |
| 572 | else if (Keyword == Ident_deprecated) |
| 573 | Index = Deprecated; |
| 574 | else if (Keyword == Ident_obsoleted) |
| 575 | Index = Obsoleted; |
| 576 | else |
| 577 | Index = Unknown; |
| 578 | |
| 579 | if (Index < Unknown) { |
| 580 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 581 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 582 | << Keyword |
| 583 | << SourceRange(Changes[Index].KeywordLoc, |
| 584 | Changes[Index].VersionRange.getEnd()); |
| 585 | } |
| 586 | |
| 587 | Changes[Index].KeywordLoc = KeywordLoc; |
| 588 | Changes[Index].Version = Version; |
| 589 | Changes[Index].VersionRange = VersionRange; |
| 590 | } else { |
| 591 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 592 | << Keyword << VersionRange; |
| 593 | } |
| 594 | |
| 595 | if (Tok.isNot(tok::comma)) |
| 596 | break; |
| 597 | |
| 598 | ConsumeToken(); |
| 599 | } while (true); |
| 600 | |
| 601 | // Closing ')'. |
| 602 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 603 | if (RParenLoc.isInvalid()) |
| 604 | return; |
| 605 | |
| 606 | if (endLoc) |
| 607 | *endLoc = RParenLoc; |
| 608 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 609 | // The 'unavailable' availability cannot be combined with any other |
| 610 | // availability changes. Make sure that hasn't happened. |
| 611 | if (UnavailableLoc.isValid()) { |
| 612 | bool Complained = false; |
| 613 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 614 | if (Changes[Index].KeywordLoc.isValid()) { |
| 615 | if (!Complained) { |
| 616 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 617 | << SourceRange(Changes[Index].KeywordLoc, |
| 618 | Changes[Index].VersionRange.getEnd()); |
| 619 | Complained = true; |
| 620 | } |
| 621 | |
| 622 | // Clear out the availability. |
| 623 | Changes[Index] = AvailabilityChange(); |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 628 | // Record this attribute |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 629 | attrs.addNew(&Availability, AvailabilityLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 630 | 0, SourceLocation(), |
| 631 | Platform, PlatformLoc, |
| 632 | Changes[Introduced], |
| 633 | Changes[Deprecated], |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 634 | Changes[Obsoleted], |
| 635 | UnavailableLoc, false, false); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 636 | } |
| 637 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 638 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 639 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 640 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 643 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 644 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 645 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 646 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 647 | /// |
| 648 | /// declaration: [C99 6.7] |
| 649 | /// block-declaration -> |
| 650 | /// simple-declaration |
| 651 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 652 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 653 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 654 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 655 | /// [C++] using-declaration |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame^] | 656 | /// [C++0x/C1X] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 657 | /// others... [FIXME] |
| 658 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 659 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 660 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 661 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 662 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 663 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
| 664 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 665 | Decl *SingleDecl = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 666 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 667 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 668 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 669 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 670 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 671 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 672 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 673 | // Could be the start of an inline namespace. Allowed as an ext in C++03. |
| 674 | if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 675 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 676 | SourceLocation InlineLoc = ConsumeToken(); |
| 677 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 678 | break; |
| 679 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 680 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 681 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 682 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 683 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 684 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 685 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 686 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 687 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 688 | DeclEnd, attrs); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 689 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 690 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame^] | 691 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 692 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 693 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 694 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 695 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 696 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 697 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 698 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 699 | // This routine returns a DeclGroup, if the thing we parsed only contains a |
| 700 | // single decl, convert it now. |
| 701 | return Actions.ConvertDeclToDeclGroup(SingleDecl); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 705 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 706 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 707 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 708 | /// |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 709 | /// for-range-declaration: [C++0x 6.5p1: stmt.ranged] |
| 710 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 711 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 712 | /// If RequireSemi is false, this does not check for a ';' at the end of the |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 713 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 714 | /// |
| 715 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 716 | /// of a simple-declaration. If we find that we are, we also parse the |
| 717 | /// for-range-initializer, and place it here. |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 718 | Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts, |
| 719 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 720 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 721 | ParsedAttributes &attrs, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 722 | bool RequireSemi, |
| 723 | ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 724 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 725 | ParsingDeclSpec DS(*this); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 726 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 727 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 728 | getDeclSpecContextFromDeclaratorContext(Context)); |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 729 | StmtResult R = Actions.ActOnVlaStmt(DS); |
| 730 | if (R.isUsable()) |
| 731 | Stmts.push_back(R.release()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 732 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 733 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 734 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 735 | if (Tok.is(tok::semi)) { |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 736 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 737 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
John McCall | aec0371 | 2010-05-21 20:45:30 +0000 | [diff] [blame] | 738 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 739 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 740 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 741 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 743 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 744 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 746 | /// ParseDeclGroup - Having concluded that this is either a function |
| 747 | /// definition or a group of object declarations, actually parse the |
| 748 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 749 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 750 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 751 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 752 | SourceLocation *DeclEnd, |
| 753 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 754 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 755 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 756 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 757 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 758 | // Bail out if the first declarator didn't seem well-formed. |
| 759 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
| 760 | // Skip until ; or }. |
| 761 | SkipUntil(tok::r_brace, true, true); |
| 762 | if (Tok.is(tok::semi)) |
| 763 | ConsumeToken(); |
| 764 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 765 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 767 | // Check to see if we have a function *definition* which must have a body. |
| 768 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 769 | // Look at the next token to make sure that this isn't a function |
| 770 | // declaration. We have to check this because __attribute__ might be the |
| 771 | // start of a function definition in GCC-extended K&R C. |
| 772 | !isDeclarationAfterDeclarator()) { |
| 773 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 774 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 775 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 776 | Diag(Tok, diag::err_function_declared_typedef); |
| 777 | |
| 778 | // Recover by treating the 'typedef' as spurious. |
| 779 | DS.ClearStorageClassSpecs(); |
| 780 | } |
| 781 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 782 | Decl *TheDecl = ParseFunctionDefinition(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 783 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | if (isDeclarationSpecifier()) { |
| 787 | // If there is an invalid declaration specifier right after the function |
| 788 | // prototype, then we must be in a missing semicolon case where this isn't |
| 789 | // actually a body. Just fall through into the code that handles it as a |
| 790 | // prototype, and let the top-level code handle the erroneous declspec |
| 791 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 792 | } else { |
| 793 | Diag(Tok, diag::err_expected_fn_body); |
| 794 | SkipUntil(tok::semi); |
| 795 | return DeclGroupPtrTy(); |
| 796 | } |
| 797 | } |
| 798 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 799 | if (ParseAttributesAfterDeclarator(D)) |
| 800 | return DeclGroupPtrTy(); |
| 801 | |
| 802 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 803 | // must parse and analyze the for-range-initializer before the declaration is |
| 804 | // analyzed. |
| 805 | if (FRI && Tok.is(tok::colon)) { |
| 806 | FRI->ColonLoc = ConsumeToken(); |
| 807 | // FIXME: handle braced-init-list here. |
| 808 | FRI->RangeExpr = ParseExpression(); |
| 809 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 810 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
| 811 | Actions.FinalizeDeclaration(ThisDecl); |
| 812 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 813 | } |
| 814 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 815 | llvm::SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 816 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 817 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 818 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 819 | DeclsInGroup.push_back(FirstDecl); |
| 820 | |
| 821 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 822 | // error, bail out. |
| 823 | while (Tok.is(tok::comma)) { |
| 824 | // Consume the comma. |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 825 | ConsumeToken(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 826 | |
| 827 | // Parse the next declarator. |
| 828 | D.clear(); |
| 829 | |
| 830 | // Accept attributes in an init-declarator. In the first declarator in a |
| 831 | // declaration, these would be part of the declspec. In subsequent |
| 832 | // declarators, they become part of the declarator itself, so that they |
| 833 | // don't apply to declarators after *this* one. Examples: |
| 834 | // short __attribute__((common)) var; -> declspec |
| 835 | // short var __attribute__((common)); -> declarator |
| 836 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 837 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 838 | |
| 839 | ParseDeclarator(D); |
| 840 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 841 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 842 | D.complete(ThisDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 843 | if (ThisDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 844 | DeclsInGroup.push_back(ThisDecl); |
| 845 | } |
| 846 | |
| 847 | if (DeclEnd) |
| 848 | *DeclEnd = Tok.getLocation(); |
| 849 | |
| 850 | if (Context != Declarator::ForContext && |
| 851 | ExpectAndConsume(tok::semi, |
| 852 | Context == Declarator::FileContext |
| 853 | ? diag::err_invalid_token_after_toplevel_declarator |
| 854 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 855 | // Okay, there was no semicolon and one was expected. If we see a |
| 856 | // declaration specifier, just assume it was missing and continue parsing. |
| 857 | // Otherwise things are very confused and we skip to recover. |
| 858 | if (!isDeclarationSpecifier()) { |
| 859 | SkipUntil(tok::r_brace, true, true); |
| 860 | if (Tok.is(tok::semi)) |
| 861 | ConsumeToken(); |
| 862 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 865 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 866 | DeclsInGroup.data(), |
| 867 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 870 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 871 | /// declarator. Returns true on an error. |
| 872 | bool Parser::ParseAttributesAfterDeclarator(Declarator &D) { |
| 873 | // If a simple-asm-expr is present, parse it. |
| 874 | if (Tok.is(tok::kw_asm)) { |
| 875 | SourceLocation Loc; |
| 876 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 877 | if (AsmLabel.isInvalid()) { |
| 878 | SkipUntil(tok::semi, true, true); |
| 879 | return true; |
| 880 | } |
| 881 | |
| 882 | D.setAsmLabel(AsmLabel.release()); |
| 883 | D.SetRangeEnd(Loc); |
| 884 | } |
| 885 | |
| 886 | MaybeParseGNUAttributes(D); |
| 887 | return false; |
| 888 | } |
| 889 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 890 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 891 | /// declarator'. This method parses the remainder of the declaration |
| 892 | /// (including any attributes or initializer, among other things) and |
| 893 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 894 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 895 | /// init-declarator: [C99 6.7] |
| 896 | /// declarator |
| 897 | /// declarator '=' initializer |
| 898 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 899 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 900 | /// [C++] declarator initializer[opt] |
| 901 | /// |
| 902 | /// [C++] initializer: |
| 903 | /// [C++] '=' initializer-clause |
| 904 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 905 | /// [C++0x] '=' 'default' [TODO] |
| 906 | /// [C++0x] '=' 'delete' |
| 907 | /// |
| 908 | /// According to the standard grammar, =default and =delete are function |
| 909 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 910 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 911 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 912 | const ParsedTemplateInfo &TemplateInfo) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 913 | if (ParseAttributesAfterDeclarator(D)) |
| 914 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 916 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 917 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 918 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 919 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 920 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 921 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 922 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 923 | switch (TemplateInfo.Kind) { |
| 924 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 925 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 926 | break; |
| 927 | |
| 928 | case ParsedTemplateInfo::Template: |
| 929 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 930 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 931 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 932 | TemplateInfo.TemplateParams->data(), |
| 933 | TemplateInfo.TemplateParams->size()), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 934 | D); |
| 935 | break; |
| 936 | |
| 937 | case ParsedTemplateInfo::ExplicitInstantiation: { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 938 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 939 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 940 | TemplateInfo.ExternLoc, |
| 941 | TemplateInfo.TemplateLoc, |
| 942 | D); |
| 943 | if (ThisRes.isInvalid()) { |
| 944 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 945 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | ThisDecl = ThisRes.get(); |
| 949 | break; |
| 950 | } |
| 951 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 953 | bool TypeContainsAuto = |
| 954 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 955 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 956 | // Parse declarator '=' initializer. |
Argyrios Kyrtzidis | a6eb5f8 | 2010-10-08 02:39:23 +0000 | [diff] [blame] | 957 | if (isTokenEqualOrMistypedEqualEqual( |
| 958 | diag::err_invalid_equalequal_after_declarator)) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 959 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 960 | if (Tok.is(tok::kw_delete)) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 961 | SourceLocation DelLoc = ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 962 | |
| 963 | if (!getLang().CPlusPlus0x) |
| 964 | Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension); |
| 965 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 966 | Actions.SetDeclDeleted(ThisDecl, DelLoc); |
| 967 | } else { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 968 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
| 969 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 970 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 971 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 972 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 973 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 974 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 975 | ConsumeCodeCompletionToken(); |
| 976 | SkipUntil(tok::comma, true, true); |
| 977 | return ThisDecl; |
| 978 | } |
| 979 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 980 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 981 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 982 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 983 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 984 | ExitScope(); |
| 985 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 986 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 987 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 988 | SkipUntil(tok::comma, true, true); |
| 989 | Actions.ActOnInitializerError(ThisDecl); |
| 990 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 991 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 992 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 993 | } |
| 994 | } else if (Tok.is(tok::l_paren)) { |
| 995 | // Parse C++ direct initializer: '(' expression-list ')' |
| 996 | SourceLocation LParenLoc = ConsumeParen(); |
| 997 | ExprVector Exprs(Actions); |
| 998 | CommaLocsTy CommaLocs; |
| 999 | |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1000 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
| 1001 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1002 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1005 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 1006 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1007 | |
| 1008 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1009 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1010 | ExitScope(); |
| 1011 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1012 | } else { |
| 1013 | // Match the ')'. |
| 1014 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 1015 | |
| 1016 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1017 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1018 | |
| 1019 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1020 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1021 | ExitScope(); |
| 1022 | } |
| 1023 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1024 | Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc, |
| 1025 | move_arg(Exprs), |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1026 | RParenLoc, |
| 1027 | TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1028 | } |
| 1029 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1030 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1033 | Actions.FinalizeDeclaration(ThisDecl); |
| 1034 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1035 | return ThisDecl; |
| 1036 | } |
| 1037 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1038 | /// ParseSpecifierQualifierList |
| 1039 | /// specifier-qualifier-list: |
| 1040 | /// type-specifier specifier-qualifier-list[opt] |
| 1041 | /// type-qualifier specifier-qualifier-list[opt] |
| 1042 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1043 | /// |
| 1044 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { |
| 1045 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1046 | /// parse declaration-specifiers and complain about extra stuff. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1047 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1049 | // Validate declspec for type-name. |
| 1050 | unsigned Specs = DS.getParsedSpecifiers(); |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1051 | if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1052 | !DS.hasAttributes()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1053 | Diag(Tok, diag::err_typename_requires_specqual); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1055 | // Issue diagnostic and remove storage class if present. |
| 1056 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1057 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1058 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1059 | else |
| 1060 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 1061 | DS.ClearStorageClassSpecs(); |
| 1062 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1064 | // Issue diagnostic and remove function specfier if present. |
| 1065 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1066 | if (DS.isInlineSpecified()) |
| 1067 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1068 | if (DS.isVirtualSpecified()) |
| 1069 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1070 | if (DS.isExplicitSpecified()) |
| 1071 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1072 | DS.ClearFunctionSpecs(); |
| 1073 | } |
| 1074 | } |
| 1075 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1076 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1077 | /// specified token is valid after the identifier in a declarator which |
| 1078 | /// immediately follows the declspec. For example, these things are valid: |
| 1079 | /// |
| 1080 | /// int x [ 4]; // direct-declarator |
| 1081 | /// int x ( int y); // direct-declarator |
| 1082 | /// int(int x ) // direct-declarator |
| 1083 | /// int x ; // simple-declaration |
| 1084 | /// int x = 17; // init-declarator-list |
| 1085 | /// int x , y; // init-declarator-list |
| 1086 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1087 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1088 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1089 | /// |
| 1090 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1091 | /// ')' happens to be valid anyway). |
| 1092 | /// int (x) |
| 1093 | /// |
| 1094 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 1095 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 1096 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1097 | T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon); |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1100 | |
| 1101 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 1102 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 1103 | /// the declspec has no type specifier. In this case, the declspec is either |
| 1104 | /// malformed or is "implicit int" (in K&R and C89). |
| 1105 | /// |
| 1106 | /// This method handles diagnosing this prettily and returns false if the |
| 1107 | /// declspec is done being processed. If it recovers and thinks there may be |
| 1108 | /// other pieces of declspec after it, it returns true. |
| 1109 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1110 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1111 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1112 | AccessSpecifier AS) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1113 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1115 | SourceLocation Loc = Tok.getLocation(); |
| 1116 | // If we see an identifier that is not a type name, we normally would |
| 1117 | // parse it as the identifer being declared. However, when a typename |
| 1118 | // is typo'd or the definition is not included, this will incorrectly |
| 1119 | // parse the typename as the identifier name and fall over misparsing |
| 1120 | // later parts of the diagnostic. |
| 1121 | // |
| 1122 | // As such, we try to do some look-ahead in cases where this would |
| 1123 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 1124 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 1125 | // an identifier with implicit int, we'd get a parse error because the |
| 1126 | // next token is obviously invalid for a type. Parse these as a case |
| 1127 | // with an invalid type specifier. |
| 1128 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1130 | // Since we know that this either implicit int (which is rare) or an |
| 1131 | // error, we'd do lookahead to try to do better recovery. |
| 1132 | if (isValidAfterIdentifierInDeclarator(NextToken())) { |
| 1133 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 1134 | // we just avoid eating the identifier, so it will be parsed as the |
| 1135 | // identifier in the declarator. |
| 1136 | return false; |
| 1137 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1139 | // Otherwise, if we don't consume this token, we are going to emit an |
| 1140 | // error anyway. Try to recover from various common problems. Check |
| 1141 | // to see if this was a reference to a tag name without a tag specified. |
| 1142 | // This is a common problem in C (saying 'foo' instead of 'struct foo'). |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1143 | // |
| 1144 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 1145 | if (SS == 0) { |
| 1146 | const char *TagName = 0; |
| 1147 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1149 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1150 | default: break; |
| 1151 | case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break; |
| 1152 | case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break; |
| 1153 | case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break; |
| 1154 | case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break; |
| 1155 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1156 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1157 | if (TagName) { |
| 1158 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
John McCall | 23e907a | 2010-02-14 01:03:10 +0000 | [diff] [blame] | 1159 | << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1160 | << FixItHint::CreateInsertion(Tok.getLocation(),TagName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1161 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1162 | // Parse this as a tag as if the missing tag were present. |
| 1163 | if (TagKind == tok::kw_enum) |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 1164 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1165 | else |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1166 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1167 | return true; |
| 1168 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1169 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1171 | // This is almost certainly an invalid type name. Let the action emit a |
| 1172 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1173 | ParsedType T; |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1174 | if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc, |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1175 | getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1176 | // The action emitted a diagnostic, so we don't have to. |
| 1177 | if (T) { |
| 1178 | // The action has suggested that the type T could be used. Set that as |
| 1179 | // the type in the declaration specifiers, consume the would-be type |
| 1180 | // name token, and we're done. |
| 1181 | const char *PrevSpec; |
| 1182 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1183 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1184 | DS.SetRangeEnd(Tok.getLocation()); |
| 1185 | ConsumeToken(); |
| 1186 | |
| 1187 | // There may be other declaration specifiers after this. |
| 1188 | return true; |
| 1189 | } |
| 1190 | |
| 1191 | // Fall through; the action had no suggestion for us. |
| 1192 | } else { |
| 1193 | // The action did not emit a diagnostic, so emit one now. |
| 1194 | SourceRange R; |
| 1195 | if (SS) R = SS->getRange(); |
| 1196 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 1197 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1198 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1199 | // Mark this as an error. |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1200 | const char *PrevSpec; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1201 | unsigned DiagID; |
| 1202 | DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1203 | DS.SetRangeEnd(Tok.getLocation()); |
| 1204 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1206 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 1207 | // avoid rippling error messages on subsequent uses of the same type, |
| 1208 | // could be useful if #include was forgotten. |
| 1209 | return false; |
| 1210 | } |
| 1211 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1212 | /// \brief Determine the declaration specifier context from the declarator |
| 1213 | /// context. |
| 1214 | /// |
| 1215 | /// \param Context the declarator context, which is one of the |
| 1216 | /// Declarator::TheContext enumerator values. |
| 1217 | Parser::DeclSpecContext |
| 1218 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 1219 | if (Context == Declarator::MemberContext) |
| 1220 | return DSC_class; |
| 1221 | if (Context == Declarator::FileContext) |
| 1222 | return DSC_top_level; |
| 1223 | return DSC_normal; |
| 1224 | } |
| 1225 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1226 | /// ParseDeclarationSpecifiers |
| 1227 | /// declaration-specifiers: [C99 6.7] |
| 1228 | /// storage-class-specifier declaration-specifiers[opt] |
| 1229 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1230 | /// [C99] function-specifier declaration-specifiers[opt] |
| 1231 | /// [GNU] attributes declaration-specifiers[opt] |
| 1232 | /// |
| 1233 | /// storage-class-specifier: [C99 6.7.1] |
| 1234 | /// 'typedef' |
| 1235 | /// 'extern' |
| 1236 | /// 'static' |
| 1237 | /// 'auto' |
| 1238 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1239 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1240 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1241 | /// function-specifier: [C99 6.7.4] |
| 1242 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1243 | /// [C++] 'virtual' |
| 1244 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1245 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1246 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1247 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1248 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1249 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1250 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1251 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1252 | AccessSpecifier AS, |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1253 | DeclSpecContext DSContext) { |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 1254 | DS.SetRangeStart(Tok.getLocation()); |
Chris Lattner | 729ad83 | 2010-11-09 20:14:26 +0000 | [diff] [blame] | 1255 | DS.SetRangeEnd(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1256 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1257 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1258 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1259 | unsigned DiagID = 0; |
| 1260 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1261 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1262 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1263 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1265 | DoneWithDeclSpec: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1266 | // If this is not a declaration specifier token, we're done reading decl |
| 1267 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1268 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1269 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1271 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1272 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1273 | if (DS.hasTypeSpecifier()) { |
| 1274 | bool AllowNonIdentifiers |
| 1275 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 1276 | Scope::BlockScope | |
| 1277 | Scope::TemplateParamScope | |
| 1278 | Scope::FunctionPrototypeScope | |
| 1279 | Scope::AtCatchScope)) == 0; |
| 1280 | bool AllowNestedNameSpecifiers |
| 1281 | = DSContext == DSC_top_level || |
| 1282 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 1283 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 1284 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
| 1285 | AllowNonIdentifiers, |
| 1286 | AllowNestedNameSpecifiers); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1287 | ConsumeCodeCompletionToken(); |
| 1288 | return; |
| 1289 | } |
| 1290 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1291 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 1292 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 1293 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1294 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
| 1295 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1296 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1297 | CCC = Sema::PCC_Class; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1298 | else if (ObjCImpDecl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1299 | CCC = Sema::PCC_ObjCImplementation; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1300 | |
| 1301 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
| 1302 | ConsumeCodeCompletionToken(); |
| 1303 | return; |
| 1304 | } |
| 1305 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1306 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1307 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 1308 | if (TryAnnotateCXXScopeToken(true)) { |
| 1309 | if (!DS.hasTypeSpecifier()) |
| 1310 | DS.SetTypeSpecError(); |
| 1311 | goto DoneWithDeclSpec; |
| 1312 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 1313 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 1314 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1315 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1316 | |
| 1317 | case tok::annot_cxxscope: { |
| 1318 | if (DS.hasTypeSpecifier()) |
| 1319 | goto DoneWithDeclSpec; |
| 1320 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1321 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 1322 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 1323 | Tok.getAnnotationRange(), |
| 1324 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1325 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1326 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1327 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1329 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1330 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1331 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1332 | |
| 1333 | // C++ [class.qual]p2: |
| 1334 | // In a lookup in which the constructor is an acceptable lookup |
| 1335 | // result and the nested-name-specifier nominates a class C: |
| 1336 | // |
| 1337 | // - if the name specified after the |
| 1338 | // nested-name-specifier, when looked up in C, is the |
| 1339 | // injected-class-name of C (Clause 9), or |
| 1340 | // |
| 1341 | // - if the name specified after the nested-name-specifier |
| 1342 | // is the same as the identifier or the |
| 1343 | // simple-template-id's template-name in the last |
| 1344 | // component of the nested-name-specifier, |
| 1345 | // |
| 1346 | // the name is instead considered to name the constructor of |
| 1347 | // class C. |
| 1348 | // |
| 1349 | // Thus, if the template-name is actually the constructor |
| 1350 | // name, then the code is ill-formed; this interpretation is |
| 1351 | // reinforced by the NAD status of core issue 635. |
| 1352 | TemplateIdAnnotation *TemplateId |
| 1353 | = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1354 | if ((DSContext == DSC_top_level || |
| 1355 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 1356 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1357 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1358 | if (isConstructorDeclarator()) { |
| 1359 | // The user meant this to be an out-of-line constructor |
| 1360 | // definition, but template arguments are not allowed |
| 1361 | // there. Just allow this as a constructor; we'll |
| 1362 | // complain about it later. |
| 1363 | goto DoneWithDeclSpec; |
| 1364 | } |
| 1365 | |
| 1366 | // The user meant this to name a type, but it actually names |
| 1367 | // a constructor with some extraneous template |
| 1368 | // arguments. Complain, then parse it as a type as the user |
| 1369 | // intended. |
| 1370 | Diag(TemplateId->TemplateNameLoc, |
| 1371 | diag::err_out_of_line_template_id_names_constructor) |
| 1372 | << TemplateId->Name; |
| 1373 | } |
| 1374 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1375 | DS.getTypeSpecScope() = SS; |
| 1376 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1378 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1379 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1380 | continue; |
| 1381 | } |
| 1382 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1383 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1384 | DS.getTypeSpecScope() = SS; |
| 1385 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1386 | if (Tok.getAnnotationValue()) { |
| 1387 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 1388 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 1389 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1390 | PrevSpec, DiagID, T); |
| 1391 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1392 | else |
| 1393 | DS.SetTypeSpecError(); |
| 1394 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1395 | ConsumeToken(); // The typename |
| 1396 | } |
| 1397 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1398 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1399 | goto DoneWithDeclSpec; |
| 1400 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1401 | // If we're in a context where the identifier could be a class name, |
| 1402 | // check whether this is a constructor declaration. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1403 | if ((DSContext == DSC_top_level || |
| 1404 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1405 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1406 | &SS)) { |
| 1407 | if (isConstructorDeclarator()) |
| 1408 | goto DoneWithDeclSpec; |
| 1409 | |
| 1410 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 1411 | // of the class is qualified in a context where it could name |
| 1412 | // a constructor, its a constructor name. However, we've |
| 1413 | // looked at the declarator, and the user probably meant this |
| 1414 | // to be a type. Complain that it isn't supposed to be treated |
| 1415 | // as a type, then proceed to parse it as a type. |
| 1416 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 1417 | << Next.getIdentifierInfo(); |
| 1418 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1419 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1420 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 1421 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1422 | getCurScope(), &SS, |
| 1423 | false, false, ParsedType(), |
| 1424 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1425 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1426 | // If the referenced identifier is not a type, then this declspec is |
| 1427 | // erroneous: We already checked about that it has no type specifier, and |
| 1428 | // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1430 | if (TypeRep == 0) { |
| 1431 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1432 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1433 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1434 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1436 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1437 | ConsumeToken(); // The C++ scope. |
| 1438 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1439 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1440 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1441 | if (isInvalid) |
| 1442 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1444 | DS.SetRangeEnd(Tok.getLocation()); |
| 1445 | ConsumeToken(); // The typename. |
| 1446 | |
| 1447 | continue; |
| 1448 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1449 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1450 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1451 | if (Tok.getAnnotationValue()) { |
| 1452 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 1453 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1454 | DiagID, T); |
| 1455 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1456 | DS.SetTypeSpecError(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1457 | |
| 1458 | if (isInvalid) |
| 1459 | break; |
| 1460 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1461 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1462 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1464 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1465 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1466 | // Objective-C interface. |
| 1467 | if (Tok.is(tok::less) && getLang().ObjC1) |
| 1468 | ParseObjCProtocolQualifiers(DS); |
| 1469 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1470 | continue; |
| 1471 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1473 | // typedef-name |
| 1474 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1475 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 1476 | // so handle it as such. This is important for ctor parsing. |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1477 | if (getLang().CPlusPlus) { |
| 1478 | if (TryAnnotateCXXScopeToken(true)) { |
| 1479 | if (!DS.hasTypeSpecifier()) |
| 1480 | DS.SetTypeSpecError(); |
| 1481 | goto DoneWithDeclSpec; |
| 1482 | } |
| 1483 | if (!Tok.is(tok::identifier)) |
| 1484 | continue; |
| 1485 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1487 | // This identifier can only be a typedef name if we haven't already seen |
| 1488 | // a type-specifier. Without this check we misparse: |
| 1489 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 1490 | if (DS.hasTypeSpecifier()) |
| 1491 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1493 | // Check for need to substitute AltiVec keyword tokens. |
| 1494 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 1495 | break; |
| 1496 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1497 | // It has to be available as a typedef too! |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1498 | ParsedType TypeRep = |
| 1499 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 1500 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1501 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1502 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 1503 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1504 | if (!TypeRep) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1505 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1506 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1507 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1508 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1509 | // If we're in a context where the identifier could be a class name, |
| 1510 | // check whether this is a constructor declaration. |
| 1511 | if (getLang().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1512 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1513 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1514 | goto DoneWithDeclSpec; |
| 1515 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1516 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1517 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1518 | if (isInvalid) |
| 1519 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1521 | DS.SetRangeEnd(Tok.getLocation()); |
| 1522 | ConsumeToken(); // The identifier |
| 1523 | |
| 1524 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1525 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1526 | // Objective-C interface. |
| 1527 | if (Tok.is(tok::less) && getLang().ObjC1) |
| 1528 | ParseObjCProtocolQualifiers(DS); |
| 1529 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 1530 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 1531 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 1532 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1533 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1534 | |
| 1535 | // type-name |
| 1536 | case tok::annot_template_id: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | TemplateIdAnnotation *TemplateId |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1538 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1539 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1540 | // This template-id does not refer to a type name, so we're |
| 1541 | // done with the type-specifiers. |
| 1542 | goto DoneWithDeclSpec; |
| 1543 | } |
| 1544 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1545 | // If we're in a context where the template-id could be a |
| 1546 | // constructor name or specialization, check whether this is a |
| 1547 | // constructor declaration. |
| 1548 | if (getLang().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1549 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1550 | isConstructorDeclarator()) |
| 1551 | goto DoneWithDeclSpec; |
| 1552 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1553 | // Turn the template-id annotation token into a type annotation |
| 1554 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1555 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1556 | continue; |
| 1557 | } |
| 1558 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1559 | // GNU attributes support. |
| 1560 | case tok::kw___attribute: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1561 | ParseGNUAttributes(DS.getAttributes()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1562 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 1563 | |
| 1564 | // Microsoft declspec support. |
| 1565 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1566 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 1567 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1569 | // Microsoft single token adornments. |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 1570 | case tok::kw___forceinline: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1571 | // FIXME: Add handling here! |
| 1572 | break; |
| 1573 | |
| 1574 | case tok::kw___ptr64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 1575 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1576 | case tok::kw___cdecl: |
| 1577 | case tok::kw___stdcall: |
| 1578 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 1579 | case tok::kw___thiscall: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1580 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1581 | continue; |
| 1582 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1583 | // Borland single token adornments. |
| 1584 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1585 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1586 | continue; |
| 1587 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1588 | // OpenCL single token adornments. |
| 1589 | case tok::kw___kernel: |
| 1590 | ParseOpenCLAttributes(DS.getAttributes()); |
| 1591 | continue; |
| 1592 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1593 | // storage-class-specifier |
| 1594 | case tok::kw_typedef: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1595 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec, |
Peter Collingbourne | e2f82f7 | 2011-02-11 19:59:54 +0000 | [diff] [blame] | 1596 | DiagID, getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1597 | break; |
| 1598 | case tok::kw_extern: |
| 1599 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1600 | Diag(Tok, diag::ext_thread_before) << "extern"; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1601 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec, |
Peter Collingbourne | e2f82f7 | 2011-02-11 19:59:54 +0000 | [diff] [blame] | 1602 | DiagID, getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1603 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 1604 | case tok::kw___private_extern__: |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1605 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, |
Peter Collingbourne | e2f82f7 | 2011-02-11 19:59:54 +0000 | [diff] [blame] | 1606 | PrevSpec, DiagID, getLang()); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 1607 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1608 | case tok::kw_static: |
| 1609 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1610 | Diag(Tok, diag::ext_thread_before) << "static"; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1611 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec, |
Peter Collingbourne | e2f82f7 | 2011-02-11 19:59:54 +0000 | [diff] [blame] | 1612 | DiagID, getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1613 | break; |
| 1614 | case tok::kw_auto: |
Douglas Gregor | 18d8b79 | 2011-03-14 21:43:30 +0000 | [diff] [blame] | 1615 | if (getLang().CPlusPlus0x) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 1616 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
| 1617 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec, |
| 1618 | DiagID, getLang()); |
| 1619 | if (!isInvalid) |
| 1620 | Diag(Tok, diag::auto_storage_class) |
| 1621 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
| 1622 | } |
| 1623 | else |
| 1624 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 1625 | DiagID); |
| 1626 | } |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 1627 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1628 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec, |
Peter Collingbourne | e2f82f7 | 2011-02-11 19:59:54 +0000 | [diff] [blame] | 1629 | DiagID, getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1630 | break; |
| 1631 | case tok::kw_register: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1632 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec, |
Peter Collingbourne | e2f82f7 | 2011-02-11 19:59:54 +0000 | [diff] [blame] | 1633 | DiagID, getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1634 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1635 | case tok::kw_mutable: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1636 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec, |
Peter Collingbourne | e2f82f7 | 2011-02-11 19:59:54 +0000 | [diff] [blame] | 1637 | DiagID, getLang()); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1638 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1639 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1640 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1641 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1643 | // function-specifier |
| 1644 | case tok::kw_inline: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1645 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1646 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1647 | case tok::kw_virtual: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1648 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1649 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1650 | case tok::kw_explicit: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1651 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1652 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1653 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1654 | // friend |
| 1655 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1656 | if (DSContext == DSC_class) |
| 1657 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 1658 | else { |
| 1659 | PrevSpec = ""; // not actually used by the diagnostic |
| 1660 | DiagID = diag::err_friend_invalid_in_context; |
| 1661 | isInvalid = true; |
| 1662 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1663 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1664 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1665 | // constexpr |
| 1666 | case tok::kw_constexpr: |
| 1667 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 1668 | break; |
| 1669 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1670 | // type-specifier |
| 1671 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1672 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 1673 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1674 | break; |
| 1675 | case tok::kw_long: |
| 1676 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1677 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 1678 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1679 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1680 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 1681 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1682 | break; |
| 1683 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1684 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 1685 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1686 | break; |
| 1687 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1688 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 1689 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1690 | break; |
| 1691 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1692 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 1693 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1694 | break; |
| 1695 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1696 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 1697 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1698 | break; |
| 1699 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1700 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 1701 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1702 | break; |
| 1703 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1704 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 1705 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1706 | break; |
| 1707 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1708 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 1709 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1710 | break; |
| 1711 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1712 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 1713 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1714 | break; |
| 1715 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1716 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 1717 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1718 | break; |
| 1719 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1720 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 1721 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1722 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1723 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1724 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 1725 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1726 | break; |
| 1727 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1728 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 1729 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1730 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1731 | case tok::kw_bool: |
| 1732 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 1733 | if (Tok.is(tok::kw_bool) && |
| 1734 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 1735 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1736 | PrevSpec = ""; // Not used by the diagnostic. |
| 1737 | DiagID = diag::err_bool_redeclaration; |
| 1738 | isInvalid = true; |
| 1739 | } else { |
| 1740 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 1741 | DiagID); |
| 1742 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1743 | break; |
| 1744 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1745 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 1746 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1747 | break; |
| 1748 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1749 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 1750 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1751 | break; |
| 1752 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1753 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 1754 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1755 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1756 | case tok::kw___vector: |
| 1757 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 1758 | break; |
| 1759 | case tok::kw___pixel: |
| 1760 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 1761 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 1762 | case tok::kw___unknown_anytype: |
| 1763 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 1764 | PrevSpec, DiagID); |
| 1765 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1766 | |
| 1767 | // class-specifier: |
| 1768 | case tok::kw_class: |
| 1769 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1770 | case tok::kw_union: { |
| 1771 | tok::TokenKind Kind = Tok.getKind(); |
| 1772 | ConsumeToken(); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1773 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1774 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1775 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1776 | |
| 1777 | // enum-specifier: |
| 1778 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 1779 | ConsumeToken(); |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 1780 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1781 | continue; |
| 1782 | |
| 1783 | // cv-qualifier: |
| 1784 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1785 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
| 1786 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1787 | break; |
| 1788 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1789 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
| 1790 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1791 | break; |
| 1792 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1793 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
| 1794 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1795 | break; |
| 1796 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1797 | // C++ typename-specifier: |
| 1798 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1799 | if (TryAnnotateTypeOrScopeToken()) { |
| 1800 | DS.SetTypeSpecError(); |
| 1801 | goto DoneWithDeclSpec; |
| 1802 | } |
| 1803 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1804 | continue; |
| 1805 | break; |
| 1806 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1807 | // GNU typeof support. |
| 1808 | case tok::kw_typeof: |
| 1809 | ParseTypeofSpecifier(DS); |
| 1810 | continue; |
| 1811 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1812 | case tok::kw_decltype: |
| 1813 | ParseDecltypeSpecifier(DS); |
| 1814 | continue; |
| 1815 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1816 | // OpenCL qualifiers: |
| 1817 | case tok::kw_private: |
| 1818 | if (!getLang().OpenCL) |
| 1819 | goto DoneWithDeclSpec; |
| 1820 | case tok::kw___private: |
| 1821 | case tok::kw___global: |
| 1822 | case tok::kw___local: |
| 1823 | case tok::kw___constant: |
| 1824 | case tok::kw___read_only: |
| 1825 | case tok::kw___write_only: |
| 1826 | case tok::kw___read_write: |
| 1827 | ParseOpenCLQualifiers(DS); |
| 1828 | break; |
| 1829 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 1830 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1831 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1832 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 1833 | // but we support it. |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1834 | if (DS.hasTypeSpecifier() || !getLang().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1835 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 1837 | if (!ParseObjCProtocolQualifiers(DS)) |
| 1838 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 1839 | << FixItHint::CreateInsertion(Loc, "id") |
| 1840 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1841 | |
| 1842 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 1843 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 1844 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1845 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1846 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1847 | if (isInvalid) { |
| 1848 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1849 | assert(DiagID); |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 1850 | |
| 1851 | if (DiagID == diag::ext_duplicate_declspec) |
| 1852 | Diag(Tok, DiagID) |
| 1853 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 1854 | else |
| 1855 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1856 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 1857 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 1858 | DS.SetRangeEnd(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1859 | ConsumeToken(); |
| 1860 | } |
| 1861 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1862 | |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 1863 | /// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1864 | /// primarily follow the C++ grammar with additions for C99 and GNU, |
| 1865 | /// which together subsume the C grammar. Note that the C++ |
| 1866 | /// type-specifier also includes the C type-qualifier (for const, |
| 1867 | /// volatile, and C99 restrict). Returns true if a type-specifier was |
| 1868 | /// found (and parsed), false otherwise. |
| 1869 | /// |
| 1870 | /// type-specifier: [C++ 7.1.5] |
| 1871 | /// simple-type-specifier |
| 1872 | /// class-specifier |
| 1873 | /// enum-specifier |
| 1874 | /// elaborated-type-specifier [TODO] |
| 1875 | /// cv-qualifier |
| 1876 | /// |
| 1877 | /// cv-qualifier: [C++ 7.1.5.1] |
| 1878 | /// 'const' |
| 1879 | /// 'volatile' |
| 1880 | /// [C99] 'restrict' |
| 1881 | /// |
| 1882 | /// simple-type-specifier: [ C++ 7.1.5.2] |
| 1883 | /// '::'[opt] nested-name-specifier[opt] type-name [TODO] |
| 1884 | /// '::'[opt] nested-name-specifier 'template' template-id [TODO] |
| 1885 | /// 'char' |
| 1886 | /// 'wchar_t' |
| 1887 | /// 'bool' |
| 1888 | /// 'short' |
| 1889 | /// 'int' |
| 1890 | /// 'long' |
| 1891 | /// 'signed' |
| 1892 | /// 'unsigned' |
| 1893 | /// 'float' |
| 1894 | /// 'double' |
| 1895 | /// 'void' |
| 1896 | /// [C99] '_Bool' |
| 1897 | /// [C99] '_Complex' |
| 1898 | /// [C99] '_Imaginary' // Removed in TC2? |
| 1899 | /// [GNU] '_Decimal32' |
| 1900 | /// [GNU] '_Decimal64' |
| 1901 | /// [GNU] '_Decimal128' |
| 1902 | /// [GNU] typeof-specifier |
| 1903 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
| 1904 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 1905 | /// [C++0x] 'decltype' ( expression ) |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1906 | /// [AltiVec] '__vector' |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1907 | bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid, |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 1908 | const char *&PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1909 | unsigned &DiagID, |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 1910 | const ParsedTemplateInfo &TemplateInfo, |
| 1911 | bool SuppressDeclarations) { |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1912 | SourceLocation Loc = Tok.getLocation(); |
| 1913 | |
| 1914 | switch (Tok.getKind()) { |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1915 | case tok::identifier: // foo::bar |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 1916 | // If we already have a type specifier, this identifier is not a type. |
| 1917 | if (DS.getTypeSpecType() != DeclSpec::TST_unspecified || |
| 1918 | DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified || |
| 1919 | DS.getTypeSpecSign() != DeclSpec::TSS_unspecified) |
| 1920 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1921 | // Check for need to substitute AltiVec keyword tokens. |
| 1922 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 1923 | break; |
| 1924 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1925 | case tok::kw_typename: // typename foo::bar |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1926 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1927 | // recurse to handle whatever we get. |
| 1928 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1929 | return true; |
| 1930 | if (Tok.is(tok::identifier)) |
| 1931 | return false; |
| 1932 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, |
| 1933 | TemplateInfo, SuppressDeclarations); |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1934 | case tok::coloncolon: // ::foo::bar |
| 1935 | if (NextToken().is(tok::kw_new) || // ::new |
| 1936 | NextToken().is(tok::kw_delete)) // ::delete |
| 1937 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1939 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1940 | // recurse to handle whatever we get. |
| 1941 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1942 | return true; |
| 1943 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, |
| 1944 | TemplateInfo, SuppressDeclarations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1946 | // simple-type-specifier: |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 1947 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1948 | if (ParsedType T = getTypeAnnotation(Tok)) { |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 1949 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 1950 | Tok.getAnnotationEndLoc(), PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1951 | DiagID, T); |
| 1952 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1953 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1954 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1955 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1956 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1957 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1958 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 1959 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 1960 | // just a normal reference to a typedef name. |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1961 | if (Tok.is(tok::less) && getLang().ObjC1) |
| 1962 | ParseObjCProtocolQualifiers(DS); |
| 1963 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1964 | return true; |
| 1965 | } |
| 1966 | |
| 1967 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1968 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1969 | break; |
| 1970 | case tok::kw_long: |
| 1971 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1972 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 1973 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1974 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1975 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 1976 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1977 | break; |
| 1978 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1979 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1980 | break; |
| 1981 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1982 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 1983 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1984 | break; |
| 1985 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1986 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 1987 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1988 | break; |
| 1989 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1990 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 1991 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1992 | break; |
| 1993 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1994 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1995 | break; |
| 1996 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1997 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1998 | break; |
| 1999 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2000 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2001 | break; |
| 2002 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2003 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2004 | break; |
| 2005 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2006 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2007 | break; |
| 2008 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2009 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2010 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2011 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2012 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2013 | break; |
| 2014 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2015 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2016 | break; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2017 | case tok::kw_bool: |
| 2018 | case tok::kw__Bool: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2019 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2020 | break; |
| 2021 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2022 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2023 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2024 | break; |
| 2025 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2026 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2027 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2028 | break; |
| 2029 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2030 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2031 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2032 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2033 | case tok::kw___vector: |
| 2034 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2035 | break; |
| 2036 | case tok::kw___pixel: |
| 2037 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2038 | break; |
| 2039 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2040 | // class-specifier: |
| 2041 | case tok::kw_class: |
| 2042 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2043 | case tok::kw_union: { |
| 2044 | tok::TokenKind Kind = Tok.getKind(); |
| 2045 | ConsumeToken(); |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 2046 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none, |
| 2047 | SuppressDeclarations); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2048 | return true; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2049 | } |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2050 | |
| 2051 | // enum-specifier: |
| 2052 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2053 | ConsumeToken(); |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2054 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2055 | return true; |
| 2056 | |
| 2057 | // cv-qualifier: |
| 2058 | case tok::kw_const: |
| 2059 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2060 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2061 | break; |
| 2062 | case tok::kw_volatile: |
| 2063 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2064 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2065 | break; |
| 2066 | case tok::kw_restrict: |
| 2067 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2068 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2069 | break; |
| 2070 | |
| 2071 | // GNU typeof support. |
| 2072 | case tok::kw_typeof: |
| 2073 | ParseTypeofSpecifier(DS); |
| 2074 | return true; |
| 2075 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2076 | // C++0x decltype support. |
| 2077 | case tok::kw_decltype: |
| 2078 | ParseDecltypeSpecifier(DS); |
| 2079 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2080 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2081 | // OpenCL qualifiers: |
| 2082 | case tok::kw_private: |
| 2083 | if (!getLang().OpenCL) |
| 2084 | return false; |
| 2085 | case tok::kw___private: |
| 2086 | case tok::kw___global: |
| 2087 | case tok::kw___local: |
| 2088 | case tok::kw___constant: |
| 2089 | case tok::kw___read_only: |
| 2090 | case tok::kw___write_only: |
| 2091 | case tok::kw___read_write: |
| 2092 | ParseOpenCLQualifiers(DS); |
| 2093 | break; |
| 2094 | |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 2095 | // C++0x auto support. |
| 2096 | case tok::kw_auto: |
| 2097 | if (!getLang().CPlusPlus0x) |
| 2098 | return false; |
| 2099 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2100 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID); |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 2101 | break; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2102 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2103 | case tok::kw___ptr64: |
| 2104 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2105 | case tok::kw___cdecl: |
| 2106 | case tok::kw___stdcall: |
| 2107 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2108 | case tok::kw___thiscall: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2109 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Chris Lattner | 837acd0 | 2009-01-21 19:19:26 +0000 | [diff] [blame] | 2110 | return true; |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2111 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2112 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2113 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2114 | return true; |
| 2115 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2116 | default: |
| 2117 | // Not a type-specifier; do nothing. |
| 2118 | return false; |
| 2119 | } |
| 2120 | |
| 2121 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 2122 | if (isInvalid) { |
| 2123 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2124 | // Pick between error or extwarn. |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2125 | Diag(Tok, DiagID) << PrevSpec; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2126 | } |
| 2127 | DS.SetRangeEnd(Tok.getLocation()); |
| 2128 | ConsumeToken(); // whatever we parsed above. |
| 2129 | return true; |
| 2130 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2131 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2132 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 2133 | /// semicolon. |
| 2134 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2135 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2136 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2137 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2138 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2139 | /// struct-declarator-list: |
| 2140 | /// struct-declarator |
| 2141 | /// struct-declarator-list ',' struct-declarator |
| 2142 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 2143 | /// struct-declarator: |
| 2144 | /// declarator |
| 2145 | /// [GNU] declarator attributes[opt] |
| 2146 | /// declarator[opt] ':' constant-expression |
| 2147 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 2148 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2149 | void Parser:: |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2150 | ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) { |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2151 | if (Tok.is(tok::kw___extension__)) { |
| 2152 | // __extension__ silences extension warnings in the subexpression. |
| 2153 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2154 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2155 | return ParseStructDeclaration(DS, Fields); |
| 2156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2157 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2158 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2159 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2160 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2161 | // If there are no declarators, this is a free-standing declaration |
| 2162 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2163 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2164 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2165 | return; |
| 2166 | } |
| 2167 | |
| 2168 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2169 | bool FirstDeclarator = true; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2170 | while (1) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2171 | ParsingDeclRAIIObject PD(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2172 | FieldDeclarator DeclaratorInfo(DS); |
| 2173 | |
| 2174 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2175 | if (!FirstDeclarator) |
| 2176 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2177 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2178 | /// struct-declarator: declarator |
| 2179 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2180 | if (Tok.isNot(tok::colon)) { |
| 2181 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2182 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2183 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2184 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2186 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2187 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2188 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2189 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2190 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 2191 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2192 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2193 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2194 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2195 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2196 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2197 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2198 | // We're done with this declarator; invoke the callback. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2199 | Decl *D = Fields.invoke(DeclaratorInfo); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2200 | PD.complete(D); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2201 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2202 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2203 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2204 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2205 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2206 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2207 | // Consume the comma. |
| 2208 | ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2209 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2210 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2211 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | /// ParseStructUnionBody |
| 2215 | /// struct-contents: |
| 2216 | /// struct-declaration-list |
| 2217 | /// [EXT] empty |
| 2218 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 2219 | /// struct-declaration-list: |
| 2220 | /// struct-declaration |
| 2221 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2222 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2223 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2224 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2225 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2226 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2227 | "parsing struct/union body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2228 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2229 | SourceLocation LBraceLoc = ConsumeBrace(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2230 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2231 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2232 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2233 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2234 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 2235 | // C++. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2236 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Douglas Gregor | 0333296 | 2010-07-29 14:29:34 +0000 | [diff] [blame] | 2237 | Diag(Tok, diag::ext_empty_struct_union) |
| 2238 | << (TagType == TST_union); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2239 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2240 | llvm::SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2241 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2242 | // While we still have something to read, read the declarations in the struct. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2243 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2244 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2245 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2246 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2247 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2248 | Diag(Tok, diag::ext_extra_struct_semi) |
Douglas Gregor | f13ca06 | 2010-06-16 23:08:59 +0000 | [diff] [blame] | 2249 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2250 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2251 | ConsumeToken(); |
| 2252 | continue; |
| 2253 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2254 | |
| 2255 | // Parse all the comma separated declarators. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2256 | DeclSpec DS(AttrFactory); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2257 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2258 | if (!Tok.is(tok::at)) { |
| 2259 | struct CFieldCallback : FieldCallback { |
| 2260 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2261 | Decl *TagDecl; |
| 2262 | llvm::SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2263 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2264 | CFieldCallback(Parser &P, Decl *TagDecl, |
| 2265 | llvm::SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2266 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 2267 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2268 | virtual Decl *invoke(FieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2269 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2270 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 2271 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 2272 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2273 | FieldDecls.push_back(Field); |
| 2274 | return Field; |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 2275 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2276 | } Callback(*this, TagDecl, FieldDecls); |
| 2277 | |
| 2278 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2279 | } else { // Handle @defs |
| 2280 | ConsumeToken(); |
| 2281 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 2282 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2283 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2284 | continue; |
| 2285 | } |
| 2286 | ConsumeToken(); |
| 2287 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 2288 | if (!Tok.is(tok::identifier)) { |
| 2289 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2290 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2291 | continue; |
| 2292 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2293 | llvm::SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2294 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2295 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2296 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 2297 | ConsumeToken(); |
| 2298 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2299 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2300 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2301 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2302 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2303 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2304 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2305 | break; |
| 2306 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2307 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 2308 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2309 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2310 | // If we stopped at a ';', eat it. |
| 2311 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2312 | } |
| 2313 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2314 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 2315 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2316 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2317 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2318 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2319 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2320 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2321 | Actions.ActOnFields(getCurScope(), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2322 | RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(), |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2323 | LBraceLoc, RBraceLoc, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2324 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2325 | StructScope.Exit(); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2326 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2329 | /// ParseEnumSpecifier |
| 2330 | /// enum-specifier: [C99 6.7.2.2] |
| 2331 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2332 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2333 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2334 | /// '}' attributes[opt] |
| 2335 | /// 'enum' identifier |
| 2336 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2337 | /// |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2338 | /// [C++0x] enum-head '{' enumerator-list[opt] '}' |
| 2339 | /// [C++0x] enum-head '{' enumerator-list ',' '}' |
| 2340 | /// |
| 2341 | /// enum-head: [C++0x] |
| 2342 | /// enum-key attributes[opt] identifier[opt] enum-base[opt] |
| 2343 | /// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt] |
| 2344 | /// |
| 2345 | /// enum-key: [C++0x] |
| 2346 | /// 'enum' |
| 2347 | /// 'enum' 'class' |
| 2348 | /// 'enum' 'struct' |
| 2349 | /// |
| 2350 | /// enum-base: [C++0x] |
| 2351 | /// ':' type-specifier-seq |
| 2352 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2353 | /// [C++] elaborated-type-specifier: |
| 2354 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 2355 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2356 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2357 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2358 | AccessSpecifier AS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2359 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2360 | if (Tok.is(tok::code_completion)) { |
| 2361 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2362 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Douglas Gregor | dc84534 | 2010-05-25 05:58:43 +0000 | [diff] [blame] | 2363 | ConsumeCodeCompletionToken(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2366 | // If attributes exist after tag, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2367 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2368 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2369 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2370 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2371 | if (getLang().CPlusPlus) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2372 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2373 | return; |
| 2374 | |
| 2375 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2376 | Diag(Tok, diag::err_expected_ident); |
| 2377 | if (Tok.isNot(tok::l_brace)) { |
| 2378 | // Has no name and is not a definition. |
| 2379 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2380 | SkipUntil(tok::comma, true); |
| 2381 | return; |
| 2382 | } |
| 2383 | } |
| 2384 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2385 | |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2386 | bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2387 | bool IsScopedEnum = false; |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2388 | bool IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2389 | |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2390 | if (getLang().CPlusPlus0x && |
| 2391 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2392 | IsScopedEnum = true; |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2393 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
| 2394 | ConsumeToken(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2397 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2398 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
| 2399 | (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2400 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2401 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2402 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2403 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2404 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2405 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2406 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2407 | // If an identifier is present, consume and remember it. |
| 2408 | IdentifierInfo *Name = 0; |
| 2409 | SourceLocation NameLoc; |
| 2410 | if (Tok.is(tok::identifier)) { |
| 2411 | Name = Tok.getIdentifierInfo(); |
| 2412 | NameLoc = ConsumeToken(); |
| 2413 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2414 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2415 | if (!Name && IsScopedEnum) { |
| 2416 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 2417 | // declaration of a scoped enumeration. |
| 2418 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
| 2419 | IsScopedEnum = false; |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2420 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
| 2423 | TypeResult BaseType; |
| 2424 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2425 | // Parse the fixed underlying type. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2426 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2427 | bool PossibleBitfield = false; |
| 2428 | if (getCurScope()->getFlags() & Scope::ClassScope) { |
| 2429 | // If we're in class scope, this can either be an enum declaration with |
| 2430 | // an underlying type, or a declaration of a bitfield member. We try to |
| 2431 | // use a simple disambiguation scheme first to catch the common cases |
| 2432 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 2433 | // anything that's a simple-type-specifier followed by '(' as an |
| 2434 | // expression. This suffices because function types are not valid |
| 2435 | // underlying types anyway. |
| 2436 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
| 2437 | // If the next token starts an expression, we know we're parsing a |
| 2438 | // bit-field. This is the common case. |
| 2439 | if (TPR == TPResult::True()) |
| 2440 | PossibleBitfield = true; |
| 2441 | // If the next token starts a type-specifier-seq, it may be either a |
| 2442 | // a fixed underlying type or the start of a function-style cast in C++; |
| 2443 | // lookahead one more token to see if it's obvious that we have a |
| 2444 | // fixed underlying type. |
| 2445 | else if (TPR == TPResult::False() && |
| 2446 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 2447 | // Consume the ':'. |
| 2448 | ConsumeToken(); |
| 2449 | } else { |
| 2450 | // We have the start of a type-specifier-seq, so we have to perform |
| 2451 | // tentative parsing to determine whether we have an expression or a |
| 2452 | // type. |
| 2453 | TentativeParsingAction TPA(*this); |
| 2454 | |
| 2455 | // Consume the ':'. |
| 2456 | ConsumeToken(); |
| 2457 | |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2458 | if ((getLang().CPlusPlus && |
| 2459 | isCXXDeclarationSpecifier() != TPResult::True()) || |
| 2460 | (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2461 | // We'll parse this as a bitfield later. |
| 2462 | PossibleBitfield = true; |
| 2463 | TPA.Revert(); |
| 2464 | } else { |
| 2465 | // We have a type-specifier-seq. |
| 2466 | TPA.Commit(); |
| 2467 | } |
| 2468 | } |
| 2469 | } else { |
| 2470 | // Consume the ':'. |
| 2471 | ConsumeToken(); |
| 2472 | } |
| 2473 | |
| 2474 | if (!PossibleBitfield) { |
| 2475 | SourceRange Range; |
| 2476 | BaseType = ParseTypeName(&Range); |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2477 | |
| 2478 | if (!getLang().CPlusPlus0x) |
| 2479 | Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type) |
| 2480 | << Range; |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2481 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2484 | // There are three options here. If we have 'enum foo;', then this is a |
| 2485 | // forward declaration. If we have 'enum foo {...' then this is a |
| 2486 | // definition. Otherwise we have something like 'enum foo xyz', a reference. |
| 2487 | // |
| 2488 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 2489 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 2490 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 2491 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2492 | Sema::TagUseKind TUK; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2493 | if (Tok.is(tok::l_brace)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2494 | TUK = Sema::TUK_Definition; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2495 | else if (Tok.is(tok::semi)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2496 | TUK = Sema::TUK_Declaration; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2497 | else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2498 | TUK = Sema::TUK_Reference; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 2499 | |
| 2500 | // enums cannot be templates, although they can be referenced from a |
| 2501 | // template. |
| 2502 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2503 | TUK != Sema::TUK_Reference) { |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 2504 | Diag(Tok, diag::err_enum_template); |
| 2505 | |
| 2506 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2507 | SkipUntil(tok::comma, true); |
| 2508 | return; |
| 2509 | } |
| 2510 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2511 | if (!Name && TUK != Sema::TUK_Definition) { |
| 2512 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
| 2513 | |
| 2514 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2515 | SkipUntil(tok::comma, true); |
| 2516 | return; |
| 2517 | } |
| 2518 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2519 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2520 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2521 | const char *PrevSpec = 0; |
| 2522 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2523 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2524 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2525 | AS, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2526 | MultiTemplateParamsArg(Actions), |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2527 | Owned, IsDependent, IsScopedEnum, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2528 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2529 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2530 | if (IsDependent) { |
| 2531 | // This enum has a dependent nested-name-specifier. Handle it as a |
| 2532 | // dependent tag. |
| 2533 | if (!Name) { |
| 2534 | DS.SetTypeSpecError(); |
| 2535 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 2536 | return; |
| 2537 | } |
| 2538 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2539 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2540 | TUK, SS, Name, StartLoc, |
| 2541 | NameLoc); |
| 2542 | if (Type.isInvalid()) { |
| 2543 | DS.SetTypeSpecError(); |
| 2544 | return; |
| 2545 | } |
| 2546 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 2547 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 2548 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 2549 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2550 | Diag(StartLoc, DiagID) << PrevSpec; |
| 2551 | |
| 2552 | return; |
| 2553 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2554 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2555 | if (!TagDecl) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2556 | // The action failed to produce an enumeration tag. If this is a |
| 2557 | // definition, consume the entire definition. |
| 2558 | if (Tok.is(tok::l_brace)) { |
| 2559 | ConsumeBrace(); |
| 2560 | SkipUntil(tok::r_brace); |
| 2561 | } |
| 2562 | |
| 2563 | DS.SetTypeSpecError(); |
| 2564 | return; |
| 2565 | } |
| 2566 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2567 | if (Tok.is(tok::l_brace)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2568 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2569 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 2570 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 2571 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 2572 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2573 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 2577 | /// enumerator-list: |
| 2578 | /// enumerator |
| 2579 | /// enumerator-list ',' enumerator |
| 2580 | /// enumerator: |
| 2581 | /// enumeration-constant |
| 2582 | /// enumeration-constant '=' constant-expression |
| 2583 | /// enumeration-constant: |
| 2584 | /// identifier |
| 2585 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2586 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 2587 | // Enter the scope of the enum body and start the definition. |
| 2588 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2589 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 2590 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2591 | SourceLocation LBraceLoc = ConsumeBrace(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2592 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 2593 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2594 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 2595 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2596 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2597 | llvm::SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2598 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2599 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2600 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2601 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2602 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2603 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 2604 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2605 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 2606 | // If attributes exist after the enumerator, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2607 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2608 | MaybeParseGNUAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 2609 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2610 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2611 | ExprResult AssignedVal; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2612 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2613 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2614 | AssignedVal = ParseConstantExpression(); |
| 2615 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2616 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2617 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2618 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2619 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2620 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 2621 | LastEnumConstDecl, |
| 2622 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2623 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2624 | AssignedVal.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2625 | EnumConstantDecls.push_back(EnumConstDecl); |
| 2626 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 2628 | if (Tok.is(tok::identifier)) { |
| 2629 | // We're missing a comma between enumerators. |
| 2630 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 2631 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
| 2632 | << FixItHint::CreateInsertion(Loc, ", "); |
| 2633 | continue; |
| 2634 | } |
| 2635 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2636 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2637 | break; |
| 2638 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2639 | |
| 2640 | if (Tok.isNot(tok::identifier) && |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2641 | !(getLang().C99 || getLang().CPlusPlus0x)) |
| 2642 | Diag(CommaLoc, diag::ext_enumerator_list_comma) |
| 2643 | << getLang().CPlusPlus |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2644 | << FixItHint::CreateRemoval(CommaLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2645 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2646 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2647 | // Eat the }. |
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 2648 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2649 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2650 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2651 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2652 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2653 | |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 2654 | Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl, |
| 2655 | EnumConstantDecls.data(), EnumConstantDecls.size(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2656 | getCurScope(), attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2658 | EnumScope.Exit(); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2659 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 2663 | /// start of a type-qualifier-list. |
| 2664 | bool Parser::isTypeQualifier() const { |
| 2665 | switch (Tok.getKind()) { |
| 2666 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2667 | |
| 2668 | // type-qualifier only in OpenCL |
| 2669 | case tok::kw_private: |
| 2670 | return getLang().OpenCL; |
| 2671 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 2672 | // type-qualifier |
| 2673 | case tok::kw_const: |
| 2674 | case tok::kw_volatile: |
| 2675 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2676 | case tok::kw___private: |
| 2677 | case tok::kw___local: |
| 2678 | case tok::kw___global: |
| 2679 | case tok::kw___constant: |
| 2680 | case tok::kw___read_only: |
| 2681 | case tok::kw___read_write: |
| 2682 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 2683 | return true; |
| 2684 | } |
| 2685 | } |
| 2686 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 2687 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 2688 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 2689 | /// specifier or if we're not sure. |
| 2690 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 2691 | switch (Tok.getKind()) { |
| 2692 | default: return false; |
| 2693 | // type-specifiers |
| 2694 | case tok::kw_short: |
| 2695 | case tok::kw_long: |
| 2696 | case tok::kw_signed: |
| 2697 | case tok::kw_unsigned: |
| 2698 | case tok::kw__Complex: |
| 2699 | case tok::kw__Imaginary: |
| 2700 | case tok::kw_void: |
| 2701 | case tok::kw_char: |
| 2702 | case tok::kw_wchar_t: |
| 2703 | case tok::kw_char16_t: |
| 2704 | case tok::kw_char32_t: |
| 2705 | case tok::kw_int: |
| 2706 | case tok::kw_float: |
| 2707 | case tok::kw_double: |
| 2708 | case tok::kw_bool: |
| 2709 | case tok::kw__Bool: |
| 2710 | case tok::kw__Decimal32: |
| 2711 | case tok::kw__Decimal64: |
| 2712 | case tok::kw__Decimal128: |
| 2713 | case tok::kw___vector: |
| 2714 | |
| 2715 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 2716 | case tok::kw_class: |
| 2717 | case tok::kw_struct: |
| 2718 | case tok::kw_union: |
| 2719 | // enum-specifier |
| 2720 | case tok::kw_enum: |
| 2721 | |
| 2722 | // typedef-name |
| 2723 | case tok::annot_typename: |
| 2724 | return true; |
| 2725 | } |
| 2726 | } |
| 2727 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 2728 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2729 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2730 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2731 | switch (Tok.getKind()) { |
| 2732 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2733 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2734 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2735 | if (TryAltiVecVectorToken()) |
| 2736 | return true; |
| 2737 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2738 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2739 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2740 | // recurse to handle whatever we get. |
| 2741 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2742 | return true; |
| 2743 | if (Tok.is(tok::identifier)) |
| 2744 | return false; |
| 2745 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2746 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2747 | case tok::coloncolon: // ::foo::bar |
| 2748 | if (NextToken().is(tok::kw_new) || // ::new |
| 2749 | NextToken().is(tok::kw_delete)) // ::delete |
| 2750 | return false; |
| 2751 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2752 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2753 | return true; |
| 2754 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2756 | // GNU attributes support. |
| 2757 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2758 | // GNU typeof support. |
| 2759 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2760 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2761 | // type-specifiers |
| 2762 | case tok::kw_short: |
| 2763 | case tok::kw_long: |
| 2764 | case tok::kw_signed: |
| 2765 | case tok::kw_unsigned: |
| 2766 | case tok::kw__Complex: |
| 2767 | case tok::kw__Imaginary: |
| 2768 | case tok::kw_void: |
| 2769 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2770 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2771 | case tok::kw_char16_t: |
| 2772 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2773 | case tok::kw_int: |
| 2774 | case tok::kw_float: |
| 2775 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 2776 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2777 | case tok::kw__Bool: |
| 2778 | case tok::kw__Decimal32: |
| 2779 | case tok::kw__Decimal64: |
| 2780 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2781 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 2783 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 2784 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2785 | case tok::kw_struct: |
| 2786 | case tok::kw_union: |
| 2787 | // enum-specifier |
| 2788 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2789 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2790 | // type-qualifier |
| 2791 | case tok::kw_const: |
| 2792 | case tok::kw_volatile: |
| 2793 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2794 | |
| 2795 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 2796 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2797 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2798 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 2799 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 2800 | case tok::less: |
| 2801 | return getLang().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2802 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2803 | case tok::kw___cdecl: |
| 2804 | case tok::kw___stdcall: |
| 2805 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2806 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2807 | case tok::kw___w64: |
| 2808 | case tok::kw___ptr64: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2809 | case tok::kw___pascal: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2810 | |
| 2811 | case tok::kw___private: |
| 2812 | case tok::kw___local: |
| 2813 | case tok::kw___global: |
| 2814 | case tok::kw___constant: |
| 2815 | case tok::kw___read_only: |
| 2816 | case tok::kw___read_write: |
| 2817 | case tok::kw___write_only: |
| 2818 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2819 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2820 | |
| 2821 | case tok::kw_private: |
| 2822 | return getLang().OpenCL; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 2827 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 2828 | /// |
| 2829 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 2830 | /// this check is to disambiguate between an expression and a declaration. |
| 2831 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2832 | switch (Tok.getKind()) { |
| 2833 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2834 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2835 | case tok::kw_private: |
| 2836 | return getLang().OpenCL; |
| 2837 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2838 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 2839 | // Unfortunate hack to support "Class.factoryMethod" notation. |
| 2840 | if (getLang().ObjC1 && NextToken().is(tok::period)) |
| 2841 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2842 | if (TryAltiVecVectorToken()) |
| 2843 | return true; |
| 2844 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2845 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2846 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2847 | // recurse to handle whatever we get. |
| 2848 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2849 | return true; |
| 2850 | if (Tok.is(tok::identifier)) |
| 2851 | return false; |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 2852 | |
| 2853 | // If we're in Objective-C and we have an Objective-C class type followed |
| 2854 | // by an identifier and then either ':' or ']', in a place where an |
| 2855 | // expression is permitted, then this is probably a class message send |
| 2856 | // missing the initial '['. In this case, we won't consider this to be |
| 2857 | // the start of a declaration. |
| 2858 | if (DisambiguatingWithExpression && |
| 2859 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 2860 | return false; |
| 2861 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2862 | return isDeclarationSpecifier(); |
| 2863 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2864 | case tok::coloncolon: // ::foo::bar |
| 2865 | if (NextToken().is(tok::kw_new) || // ::new |
| 2866 | NextToken().is(tok::kw_delete)) // ::delete |
| 2867 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2868 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2869 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2870 | // recurse to handle whatever we get. |
| 2871 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2872 | return true; |
| 2873 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2874 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2875 | // storage-class-specifier |
| 2876 | case tok::kw_typedef: |
| 2877 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2878 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2879 | case tok::kw_static: |
| 2880 | case tok::kw_auto: |
| 2881 | case tok::kw_register: |
| 2882 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2883 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2884 | // type-specifiers |
| 2885 | case tok::kw_short: |
| 2886 | case tok::kw_long: |
| 2887 | case tok::kw_signed: |
| 2888 | case tok::kw_unsigned: |
| 2889 | case tok::kw__Complex: |
| 2890 | case tok::kw__Imaginary: |
| 2891 | case tok::kw_void: |
| 2892 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2893 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2894 | case tok::kw_char16_t: |
| 2895 | case tok::kw_char32_t: |
| 2896 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2897 | case tok::kw_int: |
| 2898 | case tok::kw_float: |
| 2899 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 2900 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2901 | case tok::kw__Bool: |
| 2902 | case tok::kw__Decimal32: |
| 2903 | case tok::kw__Decimal64: |
| 2904 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2905 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2906 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 2907 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 2908 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2909 | case tok::kw_struct: |
| 2910 | case tok::kw_union: |
| 2911 | // enum-specifier |
| 2912 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2913 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2914 | // type-qualifier |
| 2915 | case tok::kw_const: |
| 2916 | case tok::kw_volatile: |
| 2917 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2918 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2919 | // function-specifier |
| 2920 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2921 | case tok::kw_virtual: |
| 2922 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 2923 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2924 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 2925 | case tok::annot_typename: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2926 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame^] | 2927 | // static_assert-declaration |
| 2928 | case tok::kw__Static_assert: |
| 2929 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 2930 | // GNU typeof support. |
| 2931 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2932 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 2933 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 2934 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2935 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2936 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 2937 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 2938 | case tok::less: |
| 2939 | return getLang().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2940 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 2941 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2942 | case tok::kw___cdecl: |
| 2943 | case tok::kw___stdcall: |
| 2944 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2945 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2946 | case tok::kw___w64: |
| 2947 | case tok::kw___ptr64: |
| 2948 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2949 | case tok::kw___pascal: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2950 | |
| 2951 | case tok::kw___private: |
| 2952 | case tok::kw___local: |
| 2953 | case tok::kw___global: |
| 2954 | case tok::kw___constant: |
| 2955 | case tok::kw___read_only: |
| 2956 | case tok::kw___read_write: |
| 2957 | case tok::kw___write_only: |
| 2958 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2959 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2960 | } |
| 2961 | } |
| 2962 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2963 | bool Parser::isConstructorDeclarator() { |
| 2964 | TentativeParsingAction TPA(*this); |
| 2965 | |
| 2966 | // Parse the C++ scope specifier. |
| 2967 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2968 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2969 | TPA.Revert(); |
| 2970 | return false; |
| 2971 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2972 | |
| 2973 | // Parse the constructor name. |
| 2974 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 2975 | // We already know that we have a constructor name; just consume |
| 2976 | // the token. |
| 2977 | ConsumeToken(); |
| 2978 | } else { |
| 2979 | TPA.Revert(); |
| 2980 | return false; |
| 2981 | } |
| 2982 | |
| 2983 | // Current class name must be followed by a left parentheses. |
| 2984 | if (Tok.isNot(tok::l_paren)) { |
| 2985 | TPA.Revert(); |
| 2986 | return false; |
| 2987 | } |
| 2988 | ConsumeParen(); |
| 2989 | |
| 2990 | // A right parentheses or ellipsis signals that we have a constructor. |
| 2991 | if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) { |
| 2992 | TPA.Revert(); |
| 2993 | return true; |
| 2994 | } |
| 2995 | |
| 2996 | // If we need to, enter the specified scope. |
| 2997 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2998 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2999 | DeclScopeObj.EnterDeclaratorScope(); |
| 3000 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3001 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3002 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3003 | MaybeParseMicrosoftAttributes(Attrs); |
| 3004 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3005 | // Check whether the next token(s) are part of a declaration |
| 3006 | // specifier, in which case we have the start of a parameter and, |
| 3007 | // therefore, we know that this is a constructor. |
| 3008 | bool IsConstructor = isDeclarationSpecifier(); |
| 3009 | TPA.Revert(); |
| 3010 | return IsConstructor; |
| 3011 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3012 | |
| 3013 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3014 | /// type-qualifier-list: [C99 6.7.5] |
| 3015 | /// type-qualifier |
| 3016 | /// [vendor] attributes |
| 3017 | /// [ only if VendorAttributesAllowed=true ] |
| 3018 | /// type-qualifier-list type-qualifier |
| 3019 | /// [vendor] type-qualifier-list attributes |
| 3020 | /// [ only if VendorAttributesAllowed=true ] |
| 3021 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 3022 | /// [ only if CXX0XAttributesAllowed=true ] |
| 3023 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3024 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3025 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 3026 | bool VendorAttributesAllowed, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3027 | bool CXX0XAttributesAllowed) { |
| 3028 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) { |
| 3029 | SourceLocation Loc = Tok.getLocation(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3030 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3031 | ParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3032 | if (CXX0XAttributesAllowed) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3033 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3034 | else |
| 3035 | Diag(Loc, diag::err_attributes_not_allowed); |
| 3036 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3037 | |
| 3038 | SourceLocation EndLoc; |
| 3039 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3040 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3041 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3042 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3043 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3044 | SourceLocation Loc = Tok.getLocation(); |
| 3045 | |
| 3046 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3047 | case tok::code_completion: |
| 3048 | Actions.CodeCompleteTypeQualifiers(DS); |
| 3049 | ConsumeCodeCompletionToken(); |
| 3050 | break; |
| 3051 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3052 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3053 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
| 3054 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3055 | break; |
| 3056 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3057 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
| 3058 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3059 | break; |
| 3060 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3061 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
| 3062 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3063 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3064 | |
| 3065 | // OpenCL qualifiers: |
| 3066 | case tok::kw_private: |
| 3067 | if (!getLang().OpenCL) |
| 3068 | goto DoneWithTypeQuals; |
| 3069 | case tok::kw___private: |
| 3070 | case tok::kw___global: |
| 3071 | case tok::kw___local: |
| 3072 | case tok::kw___constant: |
| 3073 | case tok::kw___read_only: |
| 3074 | case tok::kw___write_only: |
| 3075 | case tok::kw___read_write: |
| 3076 | ParseOpenCLQualifiers(DS); |
| 3077 | break; |
| 3078 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3079 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3080 | case tok::kw___ptr64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3081 | case tok::kw___cdecl: |
| 3082 | case tok::kw___stdcall: |
| 3083 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3084 | case tok::kw___thiscall: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3085 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3086 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3087 | continue; |
| 3088 | } |
| 3089 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3090 | case tok::kw___pascal: |
| 3091 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3092 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3093 | continue; |
| 3094 | } |
| 3095 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3096 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3097 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3098 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3099 | continue; // do *not* consume the next token! |
| 3100 | } |
| 3101 | // otherwise, FALL THROUGH! |
| 3102 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3103 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3104 | // If this is not a type-qualifier token, we're done reading type |
| 3105 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 3106 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3107 | if (EndLoc.isValid()) |
| 3108 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3109 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3110 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3111 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3112 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 3113 | if (isInvalid) { |
| 3114 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3115 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3116 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3117 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | |
| 3122 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 3123 | /// |
| 3124 | void Parser::ParseDeclarator(Declarator &D) { |
| 3125 | /// This implements the 'declarator' production in the C grammar, then checks |
| 3126 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3127 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3130 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 3131 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 3132 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3133 | /// ptr-operator production. |
| 3134 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3135 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 3136 | /// [C] pointer[opt] direct-declarator |
| 3137 | /// [C++] direct-declarator |
| 3138 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3139 | /// |
| 3140 | /// pointer: [C99 6.7.5] |
| 3141 | /// '*' type-qualifier-list[opt] |
| 3142 | /// '*' type-qualifier-list[opt] pointer |
| 3143 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3144 | /// ptr-operator: |
| 3145 | /// '*' cv-qualifier-seq[opt] |
| 3146 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3147 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3148 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3149 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3150 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3151 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 3152 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3153 | if (Diags.hasAllExtensionsSilenced()) |
| 3154 | D.setExtension(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3155 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3156 | // C++ member pointers start with a '::' or a nested-name. |
| 3157 | // Member pointers get special handling, since there's no place for the |
| 3158 | // scope spec in the generic path below. |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3159 | if (getLang().CPlusPlus && |
| 3160 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 3161 | Tok.is(tok::annot_cxxscope))) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3162 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3163 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3164 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 3165 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3166 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3167 | // The scope spec really belongs to the direct-declarator. |
| 3168 | D.getCXXScopeSpec() = SS; |
| 3169 | if (DirectDeclParser) |
| 3170 | (this->*DirectDeclParser)(D); |
| 3171 | return; |
| 3172 | } |
| 3173 | |
| 3174 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3175 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3176 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3177 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3178 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3179 | |
| 3180 | // Recurse to parse whatever is left. |
| 3181 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 3182 | |
| 3183 | // Sema will have to catch (syntactically invalid) pointers into global |
| 3184 | // scope. It has to catch pointers into namespace scope anyway. |
| 3185 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3186 | Loc), |
| 3187 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3188 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3189 | return; |
| 3190 | } |
| 3191 | } |
| 3192 | |
| 3193 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3194 | // Not a pointer, C++ reference, or block. |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3195 | if (Kind != tok::star && Kind != tok::caret && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3196 | (Kind != tok::amp || !getLang().CPlusPlus) && |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3197 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3198 | (Kind != tok::ampamp || !getLang().CPlusPlus)) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3199 | if (DirectDeclParser) |
| 3200 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3201 | return; |
| 3202 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3203 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3204 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 3205 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3206 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3207 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3208 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3209 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 3210 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3211 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3212 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3213 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3214 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3215 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3216 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3217 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3218 | if (Kind == tok::star) |
| 3219 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 3220 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 3221 | DS.getConstSpecLoc(), |
| 3222 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3223 | DS.getRestrictSpecLoc()), |
| 3224 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3225 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3226 | else |
| 3227 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3228 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3229 | Loc), |
| 3230 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3231 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3232 | } else { |
| 3233 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3234 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3235 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3236 | // Complain about rvalue references in C++03, but then go on and build |
| 3237 | // the declarator. |
| 3238 | if (Kind == tok::ampamp && !getLang().CPlusPlus0x) |
Douglas Gregor | 16cf8f5 | 2011-01-25 02:17:32 +0000 | [diff] [blame] | 3239 | Diag(Loc, diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3240 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3241 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 3242 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 3243 | // template type argument, in which case the cv-qualifiers are ignored. |
| 3244 | // |
| 3245 | // [GNU] Retricted references are allowed. |
| 3246 | // [GNU] Attributes on references are allowed. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3247 | // [C++0x] Attributes on references are not allowed. |
| 3248 | ParseTypeQualifierListOpt(DS, true, false); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3249 | D.ExtendWithDeclSpec(DS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3250 | |
| 3251 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 3252 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3253 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3254 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3255 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 3256 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3257 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3261 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3262 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3263 | if (D.getNumTypeObjects() > 0) { |
| 3264 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 3265 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 3266 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3267 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 3268 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3269 | << II; |
| 3270 | else |
| 3271 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3272 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3273 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3274 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3275 | // can go ahead and build the (technically ill-formed) |
| 3276 | // declarator: reference collapsing will take care of it. |
| 3277 | } |
| 3278 | } |
| 3279 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3280 | // Remember that we parsed a reference type. It doesn't have type-quals. |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 3281 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3282 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3283 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3284 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | /// ParseDirectDeclarator |
| 3289 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3290 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3291 | /// '(' declarator ')' |
| 3292 | /// [GNU] '(' attributes declarator ')' |
| 3293 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 3294 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 3295 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 3296 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 3297 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 3298 | /// direct-declarator '(' parameter-type-list ')' |
| 3299 | /// direct-declarator '(' identifier-list[opt] ')' |
| 3300 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 3301 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3302 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 3303 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3304 | /// [C++] declarator-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3305 | /// |
| 3306 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3307 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3308 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 3309 | /// |
| 3310 | /// id-expression: [C++ 5.1] |
| 3311 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3312 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3313 | /// |
| 3314 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3315 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3316 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3317 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3318 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3319 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 3320 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3321 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3322 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3323 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3324 | if (getLang().CPlusPlus && D.mayHaveIdentifier()) { |
| 3325 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3326 | if (D.getCXXScopeSpec().isEmpty()) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3327 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3330 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3331 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 3332 | // Change the declaration context for name lookup, until this function |
| 3333 | // is exited (and the declarator has been parsed). |
| 3334 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3337 | // C++0x [dcl.fct]p14: |
| 3338 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
| 3339 | // of a parameter-declaration-clause without a preceding comma. In |
| 3340 | // this case, the ellipsis is parsed as part of the |
| 3341 | // abstract-declarator if the type of the parameter names a template |
| 3342 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 3343 | // as part of the parameter-declaration-clause. |
| 3344 | if (Tok.is(tok::ellipsis) && |
| 3345 | !((D.getContext() == Declarator::PrototypeContext || |
| 3346 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3347 | NextToken().is(tok::r_paren) && |
| 3348 | !Actions.containsUnexpandedParameterPacks(D))) |
| 3349 | D.setEllipsisLoc(ConsumeToken()); |
| 3350 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3351 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 3352 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 3353 | // We found something that indicates the start of an unqualified-id. |
| 3354 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 3355 | bool AllowConstructorName; |
| 3356 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 3357 | AllowConstructorName = false; |
| 3358 | else if (D.getCXXScopeSpec().isSet()) |
| 3359 | AllowConstructorName = |
| 3360 | (D.getContext() == Declarator::FileContext || |
| 3361 | (D.getContext() == Declarator::MemberContext && |
| 3362 | D.getDeclSpec().isFriendSpecified())); |
| 3363 | else |
| 3364 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 3365 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3366 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 3367 | /*EnteringContext=*/true, |
| 3368 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3369 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3370 | ParsedType(), |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3371 | D.getName()) || |
| 3372 | // Once we're past the identifier, if the scope was bad, mark the |
| 3373 | // whole declarator bad. |
| 3374 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3375 | D.SetIdentifier(0, Tok.getLocation()); |
| 3376 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3377 | } else { |
| 3378 | // Parsed the unqualified-id; update range information and move along. |
| 3379 | if (D.getSourceRange().getBegin().isInvalid()) |
| 3380 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 3381 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3382 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3383 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3384 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3385 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3386 | assert(!getLang().CPlusPlus && |
| 3387 | "There's a C++-specific check for tok::identifier above"); |
| 3388 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 3389 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 3390 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3391 | goto PastIdentifier; |
| 3392 | } |
| 3393 | |
| 3394 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3395 | // direct-declarator: '(' declarator ')' |
| 3396 | // direct-declarator: '(' attributes declarator ')' |
| 3397 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 3398 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3399 | |
| 3400 | // If the declarator was parenthesized, we entered the declarator |
| 3401 | // scope when parsing the parenthesized declarator, then exited |
| 3402 | // the scope already. Re-enter the scope, if we need to. |
| 3403 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 3404 | // If there was an error parsing parenthesized declarator, declarator |
| 3405 | // scope may have been enterred before. Don't do it again. |
| 3406 | if (!D.isInvalidType() && |
| 3407 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3408 | // Change the declaration context for name lookup, until this function |
| 3409 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 3410 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3411 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3412 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3413 | // This could be something simple like "int" (in which case the declarator |
| 3414 | // portion is empty), if an abstract-declarator is allowed. |
| 3415 | D.SetIdentifier(0, Tok.getLocation()); |
| 3416 | } else { |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 3417 | if (D.getContext() == Declarator::MemberContext) |
| 3418 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 3419 | << D.getDeclSpec().getSourceRange(); |
| 3420 | else if (getLang().CPlusPlus) |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 3421 | Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3422 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3423 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3424 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 3425 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3426 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3427 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3428 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3429 | assert(D.isPastIdentifier() && |
| 3430 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3432 | // Don't parse attributes unless we have an identifier. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3433 | if (D.getIdentifier()) |
| 3434 | MaybeParseCXX0XAttributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3435 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3436 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3437 | if (Tok.is(tok::l_paren)) { |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3438 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 3439 | // In such a case, check if we actually have a function declarator; if it |
| 3440 | // is not, the declarator has been fully parsed. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3441 | if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 3442 | // When not in file scope, warn for ambiguous function declarators, just |
| 3443 | // in case the author intended it as a variable definition. |
| 3444 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 3445 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 3446 | break; |
| 3447 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3448 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3449 | ParseFunctionDeclarator(ConsumeParen(), D, attrs); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3450 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3451 | ParseBracketDeclarator(D); |
| 3452 | } else { |
| 3453 | break; |
| 3454 | } |
| 3455 | } |
| 3456 | } |
| 3457 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3458 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 3459 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3460 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3461 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 3462 | /// |
| 3463 | /// direct-declarator: |
| 3464 | /// '(' declarator ')' |
| 3465 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3466 | /// direct-declarator '(' parameter-type-list ')' |
| 3467 | /// direct-declarator '(' identifier-list[opt] ')' |
| 3468 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 3469 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3470 | /// |
| 3471 | void Parser::ParseParenDeclarator(Declarator &D) { |
| 3472 | SourceLocation StartLoc = ConsumeParen(); |
| 3473 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3474 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3475 | // Eat any attributes before we look at whether this is a grouping or function |
| 3476 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 3477 | // the type being built up, for example: |
| 3478 | // int (__attribute__(()) *x)(long y) |
| 3479 | // If this ends up not being a grouping paren, the attribute applies to the |
| 3480 | // first argument, for example: |
| 3481 | // int (__attribute__(()) int x) |
| 3482 | // In either case, we need to eat any attributes to be able to determine what |
| 3483 | // sort of paren this is. |
| 3484 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3485 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3486 | bool RequiresArg = false; |
| 3487 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3488 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3489 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3490 | // We require that the argument list (if this is a non-grouping paren) be |
| 3491 | // present even if the attribute list was empty. |
| 3492 | RequiresArg = true; |
| 3493 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3494 | // Eat any Microsoft extensions. |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3495 | if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3496 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) || |
| 3497 | Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3498 | ParseMicrosoftTypeAttributes(attrs); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3499 | } |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3500 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 3501 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3502 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3503 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3504 | // If we haven't past the identifier yet (or where the identifier would be |
| 3505 | // stored, if this is an abstract declarator), then this is probably just |
| 3506 | // grouping parens. However, if this could be an abstract-declarator, then |
| 3507 | // this could also be the start of function arguments (consider 'void()'). |
| 3508 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3509 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3510 | if (!D.mayOmitIdentifier()) { |
| 3511 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 3512 | // paren, because we haven't seen the identifier yet. |
| 3513 | isGrouping = true; |
| 3514 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Argyrios Kyrtzidis | e25d270 | 2008-10-06 00:07:55 +0000 | [diff] [blame] | 3515 | (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3516 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 3517 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 3518 | // considered to be a type, not a K&R identifier-list. |
| 3519 | isGrouping = false; |
| 3520 | } else { |
| 3521 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 3522 | isGrouping = true; |
| 3523 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3524 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3525 | // If this is a grouping paren, handle: |
| 3526 | // direct-declarator: '(' declarator ')' |
| 3527 | // direct-declarator: '(' attributes declarator ')' |
| 3528 | if (isGrouping) { |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 3529 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3530 | D.setGroupingParens(true); |
| 3531 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3532 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3533 | // Match the ')'. |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3534 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3535 | D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc), |
| 3536 | attrs, EndLoc); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 3537 | |
| 3538 | D.setGroupingParens(hadGroupingParens); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3539 | return; |
| 3540 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3541 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3542 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 3543 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3544 | // identifier (and remember where it would have been), then call into |
| 3545 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3546 | D.SetIdentifier(0, Tok.getLocation()); |
| 3547 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3548 | ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 3552 | /// declarator D up to a paren, which indicates that we are parsing function |
| 3553 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3554 | /// |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3555 | /// If AttrList is non-null, then the caller parsed those arguments immediately |
| 3556 | /// after the open paren - they should be considered to be the first argument of |
| 3557 | /// a parameter. If RequiresArg is true, then the first argument of the |
| 3558 | /// function is required to be present and required to not be an identifier |
| 3559 | /// list. |
| 3560 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3561 | /// This method also handles this portion of the grammar: |
| 3562 | /// parameter-type-list: [C99 6.7.5] |
| 3563 | /// parameter-list |
| 3564 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 3565 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3566 | /// |
| 3567 | /// parameter-list: [C99 6.7.5] |
| 3568 | /// parameter-declaration |
| 3569 | /// parameter-list ',' parameter-declaration |
| 3570 | /// |
| 3571 | /// parameter-declaration: [C99 6.7.5] |
| 3572 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3573 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3574 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 3575 | /// declaration-specifiers abstract-declarator[opt] |
| 3576 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3577 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3578 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 3579 | /// |
Douglas Gregor | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 3580 | /// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]", |
| 3581 | /// C++0x "ref-qualifier[opt]" and "exception-specification[opt]". |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3582 | /// |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3583 | /// [C++0x] exception-specification: |
| 3584 | /// dynamic-exception-specification |
| 3585 | /// noexcept-specification |
| 3586 | /// |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3587 | void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3588 | ParsedAttributes &attrs, |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3589 | bool RequiresArg) { |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3590 | // lparen is already consumed! |
| 3591 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3592 | |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3593 | ParsedType TrailingReturnType; |
| 3594 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3595 | // This parameter list may be empty. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3596 | if (Tok.is(tok::r_paren)) { |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 3597 | if (RequiresArg) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3598 | Diag(Tok, diag::err_argument_required_after_attribute); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3599 | |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3600 | SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'. |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3601 | |
| 3602 | // cv-qualifier-seq[opt]. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3603 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 3604 | SourceLocation RefQualifierLoc; |
| 3605 | bool RefQualifierIsLValueRef = true; |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3606 | ExceptionSpecificationType ESpecType = EST_None; |
| 3607 | SourceRange ESpecRange; |
| 3608 | llvm::SmallVector<ParsedType, 2> DynamicExceptions; |
| 3609 | llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges; |
| 3610 | ExprResult NoexceptExpr; |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3611 | if (getLang().CPlusPlus) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3612 | MaybeParseCXX0XAttributes(attrs); |
| 3613 | |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3614 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3615 | if (!DS.getSourceRange().getEnd().isInvalid()) |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3616 | EndLoc = DS.getSourceRange().getEnd(); |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3617 | |
Douglas Gregor | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 3618 | // Parse ref-qualifier[opt] |
| 3619 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
| 3620 | if (!getLang().CPlusPlus0x) |
Douglas Gregor | 1f38106 | 2011-01-26 20:35:32 +0000 | [diff] [blame] | 3621 | Diag(Tok, diag::ext_ref_qualifier); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3622 | |
Douglas Gregor | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 3623 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 3624 | RefQualifierLoc = ConsumeToken(); |
| 3625 | EndLoc = RefQualifierLoc; |
| 3626 | } |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3627 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3628 | // Parse exception-specification[opt]. |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3629 | ESpecType = MaybeParseExceptionSpecification(ESpecRange, |
| 3630 | DynamicExceptions, |
| 3631 | DynamicExceptionRanges, |
| 3632 | NoexceptExpr); |
| 3633 | if (ESpecType != EST_None) |
| 3634 | EndLoc = ESpecRange.getEnd(); |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3635 | |
| 3636 | // Parse trailing-return-type. |
| 3637 | if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) { |
| 3638 | TrailingReturnType = ParseTrailingReturnType().get(); |
| 3639 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3642 | // Remember that we parsed a function type, and remember the attributes. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3643 | // int() -> no prototype, no '...'. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3644 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus, |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3645 | /*variadic*/ false, |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3646 | SourceLocation(), |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3647 | /*arglist*/ 0, 0, |
| 3648 | DS.getTypeQualifiers(), |
Douglas Gregor | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 3649 | RefQualifierIsLValueRef, |
| 3650 | RefQualifierLoc, |
Sebastian Redl | 6e5d319 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 3651 | ESpecType, ESpecRange.getBegin(), |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3652 | DynamicExceptions.data(), |
| 3653 | DynamicExceptionRanges.data(), |
| 3654 | DynamicExceptions.size(), |
Sebastian Redl | 6e5d319 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 3655 | NoexceptExpr.isUsable() ? |
| 3656 | NoexceptExpr.get() : 0, |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3657 | LParenLoc, EndLoc, D, |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3658 | TrailingReturnType), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3659 | attrs, EndLoc); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3660 | return; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 3661 | } |
| 3662 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3663 | // Alternatively, this parameter list may be an identifier list form for a |
| 3664 | // K&R-style function: void foo(a,b,c) |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3665 | if (!getLang().CPlusPlus && Tok.is(tok::identifier) |
| 3666 | && !TryAltiVecVectorToken()) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3667 | if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3668 | // K&R identifier lists can't have typedefs as identifiers, per |
| 3669 | // C99 6.7.5.3p11. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 3670 | if (RequiresArg) |
Steve Naroff | 2d081c4 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 3671 | Diag(Tok, diag::err_argument_required_after_attribute); |
Chris Lattner | 83a9447 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3672 | |
Steve Naroff | 2d081c4 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 3673 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
Chris Lattner | 83a9447 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3674 | // normal declarators, not for abstract-declarators. Get the first |
| 3675 | // identifier. |
Chris Lattner | 9a65b81 | 2010-05-14 17:44:56 +0000 | [diff] [blame] | 3676 | Token FirstTok = Tok; |
Chris Lattner | 83a9447 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3677 | ConsumeToken(); // eat the first identifier. |
Chris Lattner | 9a65b81 | 2010-05-14 17:44:56 +0000 | [diff] [blame] | 3678 | |
| 3679 | // Identifier lists follow a really simple grammar: the identifiers can |
| 3680 | // be followed *only* by a ", moreidentifiers" or ")". However, K&R |
| 3681 | // identifier lists are really rare in the brave new modern world, and it |
| 3682 | // is very common for someone to typo a type in a non-k&r style list. If |
| 3683 | // we are presented with something like: "void foo(intptr x, float y)", |
| 3684 | // we don't want to start parsing the function declarator as though it is |
| 3685 | // a K&R style declarator just because intptr is an invalid type. |
| 3686 | // |
| 3687 | // To handle this, we check to see if the token after the first identifier |
| 3688 | // is a "," or ")". Only if so, do we parse it as an identifier list. |
| 3689 | if (Tok.is(tok::comma) || Tok.is(tok::r_paren)) |
| 3690 | return ParseFunctionDeclaratorIdentifierList(LParenLoc, |
| 3691 | FirstTok.getIdentifierInfo(), |
| 3692 | FirstTok.getLocation(), D); |
| 3693 | |
| 3694 | // If we get here, the code is invalid. Push the first identifier back |
| 3695 | // into the token stream and parse the first argument as an (invalid) |
| 3696 | // normal argument declarator. |
| 3697 | PP.EnterToken(Tok); |
| 3698 | Tok = FirstTok; |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3699 | } |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3700 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3702 | // Finally, a normal, non-empty parameter type list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3703 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3704 | // Build up an array of information about the parsed arguments. |
| 3705 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3706 | |
| 3707 | // Enter function-declaration scope, limiting any declarators to the |
| 3708 | // function prototype scope, including parameter declarators. |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 3709 | ParseScope PrototypeScope(this, |
| 3710 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3711 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3712 | bool IsVariadic = false; |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3713 | SourceLocation EllipsisLoc; |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3714 | while (1) { |
| 3715 | if (Tok.is(tok::ellipsis)) { |
| 3716 | IsVariadic = true; |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3717 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3718 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3719 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3720 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3721 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3722 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3723 | DeclSpec DS(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3724 | |
| 3725 | // Skip any Microsoft attributes before a param. |
| 3726 | if (getLang().Microsoft && Tok.is(tok::l_square)) |
| 3727 | ParseMicrosoftAttributes(DS.getAttributes()); |
| 3728 | |
| 3729 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3730 | |
| 3731 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3732 | // Take them so that we only apply the attributes to the first parameter. |
| 3733 | DS.takeAttributesFrom(attrs); |
| 3734 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 3735 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3736 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3737 | // Parse the declarator. This is "PrototypeContext", because we must |
| 3738 | // accept either 'declarator' or 'abstract-declarator' here. |
| 3739 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 3740 | ParseDeclarator(ParmDecl); |
| 3741 | |
| 3742 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3743 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3744 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3745 | // Remember this parsed parameter in ParamInfo. |
| 3746 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3747 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3748 | // DefArgToks is used when the parsing of default arguments needs |
| 3749 | // to be delayed. |
| 3750 | CachedTokens *DefArgToks = 0; |
| 3751 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3752 | // If no parameter was specified, verify that *something* was specified, |
| 3753 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 3754 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 3755 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3756 | // Completely missing, emit error. |
| 3757 | Diag(DSStart, diag::err_missing_param); |
| 3758 | } else { |
| 3759 | // Otherwise, we have something. Add it and let semantic analysis try |
| 3760 | // to grok it and add the result to the ParamInfo we are building. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3761 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3762 | // Inform the actions module about the parameter declarator, so it gets |
| 3763 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3764 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3765 | |
| 3766 | // Parse the default argument, if any. We parse the default |
| 3767 | // arguments in all dialects; the semantic analysis in |
| 3768 | // ActOnParamDefaultArgument will reject the default argument in |
| 3769 | // C. |
| 3770 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3771 | SourceLocation EqualLoc = Tok.getLocation(); |
| 3772 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3773 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3774 | if (D.getContext() == Declarator::MemberContext) { |
| 3775 | // If we're inside a class definition, cache the tokens |
| 3776 | // corresponding to the default argument. We'll actually parse |
| 3777 | // them when we see the end of the class definition. |
| 3778 | // FIXME: Templates will require something similar. |
| 3779 | // FIXME: Can we use a smart pointer for Toks? |
| 3780 | DefArgToks = new CachedTokens; |
| 3781 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3782 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 3783 | /*StopAtSemi=*/true, |
| 3784 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3785 | delete DefArgToks; |
| 3786 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3787 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 3788 | } else { |
| 3789 | // Mark the end of the default argument so that we know when to |
| 3790 | // stop when we parse it later on. |
| 3791 | Token DefArgEnd; |
| 3792 | DefArgEnd.startToken(); |
| 3793 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 3794 | DefArgEnd.setLocation(Tok.getLocation()); |
| 3795 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3796 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3797 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 3798 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3799 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3800 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3801 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3802 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3803 | // The argument isn't actually potentially evaluated unless it is |
| 3804 | // used. |
| 3805 | EnterExpressionEvaluationContext Eval(Actions, |
| 3806 | Sema::PotentiallyEvaluatedIfUsed); |
| 3807 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3808 | ExprResult DefArgResult(ParseAssignmentExpression()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3809 | if (DefArgResult.isInvalid()) { |
| 3810 | Actions.ActOnParamDefaultArgumentError(Param); |
| 3811 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 3812 | } else { |
| 3813 | // Inform the actions module about the default argument |
| 3814 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3815 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3816 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3817 | } |
| 3818 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3819 | |
| 3820 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 3821 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3822 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | // If the next token is a comma, consume it and keep reading arguments. |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 3826 | if (Tok.isNot(tok::comma)) { |
| 3827 | if (Tok.is(tok::ellipsis)) { |
| 3828 | IsVariadic = true; |
| 3829 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
| 3830 | |
| 3831 | if (!getLang().CPlusPlus) { |
| 3832 | // We have ellipsis without a preceding ',', which is ill-formed |
| 3833 | // in C. Complain and provide the fix. |
| 3834 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3835 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 3836 | } |
| 3837 | } |
| 3838 | |
| 3839 | break; |
| 3840 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3841 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 3842 | // Consume the comma. |
| 3843 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3845 | |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3846 | // If we have the closing ')', eat it. |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3847 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3848 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3849 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 3850 | SourceLocation RefQualifierLoc; |
| 3851 | bool RefQualifierIsLValueRef = true; |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3852 | ExceptionSpecificationType ESpecType = EST_None; |
| 3853 | SourceRange ESpecRange; |
| 3854 | llvm::SmallVector<ParsedType, 2> DynamicExceptions; |
| 3855 | llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges; |
| 3856 | ExprResult NoexceptExpr; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3857 | |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3858 | if (getLang().CPlusPlus) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3859 | MaybeParseCXX0XAttributes(attrs); |
| 3860 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3861 | // Parse cv-qualifier-seq[opt]. |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3862 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3863 | if (!DS.getSourceRange().getEnd().isInvalid()) |
Argyrios Kyrtzidis | 82bf010 | 2009-08-19 23:14:54 +0000 | [diff] [blame] | 3864 | EndLoc = DS.getSourceRange().getEnd(); |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3865 | |
Douglas Gregor | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 3866 | // Parse ref-qualifier[opt] |
| 3867 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
| 3868 | if (!getLang().CPlusPlus0x) |
Douglas Gregor | 1f38106 | 2011-01-26 20:35:32 +0000 | [diff] [blame] | 3869 | Diag(Tok, diag::ext_ref_qualifier); |
Douglas Gregor | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 3870 | |
| 3871 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 3872 | RefQualifierLoc = ConsumeToken(); |
| 3873 | EndLoc = RefQualifierLoc; |
| 3874 | } |
| 3875 | |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3876 | // FIXME: We should leave the prototype scope before parsing the exception |
| 3877 | // specification, and then reenter it when parsing the trailing return type. |
| 3878 | // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause. |
| 3879 | |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 3880 | // Parse exception-specification[opt]. |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3881 | ESpecType = MaybeParseExceptionSpecification(ESpecRange, |
| 3882 | DynamicExceptions, |
| 3883 | DynamicExceptionRanges, |
| 3884 | NoexceptExpr); |
| 3885 | if (ESpecType != EST_None) |
| 3886 | EndLoc = ESpecRange.getEnd(); |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3887 | |
| 3888 | // Parse trailing-return-type. |
| 3889 | if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) { |
| 3890 | TrailingReturnType = ParseTrailingReturnType().get(); |
| 3891 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3894 | // Leave prototype scope. |
| 3895 | PrototypeScope.Exit(); |
| 3896 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3897 | // Remember that we parsed a function type, and remember the attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3898 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic, |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3899 | EllipsisLoc, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 3900 | ParamInfo.data(), ParamInfo.size(), |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3901 | DS.getTypeQualifiers(), |
Douglas Gregor | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 3902 | RefQualifierIsLValueRef, |
| 3903 | RefQualifierLoc, |
Sebastian Redl | 6e5d319 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 3904 | ESpecType, ESpecRange.getBegin(), |
Sebastian Redl | 7acafd0 | 2011-03-05 14:45:16 +0000 | [diff] [blame] | 3905 | DynamicExceptions.data(), |
| 3906 | DynamicExceptionRanges.data(), |
| 3907 | DynamicExceptions.size(), |
Sebastian Redl | 6e5d319 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 3908 | NoexceptExpr.isUsable() ? |
| 3909 | NoexceptExpr.get() : 0, |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3910 | LParenLoc, EndLoc, D, |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3911 | TrailingReturnType), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3912 | attrs, EndLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3913 | } |
| 3914 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3915 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 3916 | /// we found a K&R-style identifier list instead of a type argument list. The |
Chris Lattner | 83a9447 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3917 | /// first identifier has already been consumed, and the current token is the |
| 3918 | /// token right after it. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3919 | /// |
| 3920 | /// identifier-list: [C99 6.7.5] |
| 3921 | /// identifier |
| 3922 | /// identifier-list ',' identifier |
| 3923 | /// |
| 3924 | void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc, |
Chris Lattner | 83a9447 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3925 | IdentifierInfo *FirstIdent, |
| 3926 | SourceLocation FirstIdentLoc, |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3927 | Declarator &D) { |
| 3928 | // Build up an array of information about the parsed arguments. |
| 3929 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
| 3930 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3931 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3932 | // If there was no identifier specified for the declarator, either we are in |
| 3933 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 3934 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 3935 | // diagnose this. |
| 3936 | if (!D.getIdentifier()) |
Chris Lattner | 83a9447 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3937 | Diag(FirstIdentLoc, diag::ext_ident_list_in_param); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3938 | |
Chris Lattner | 83a9447 | 2010-05-14 17:23:36 +0000 | [diff] [blame] | 3939 | // The first identifier was already read, and is known to be the first |
| 3940 | // identifier in the list. Remember this identifier in ParamInfo. |
| 3941 | ParamsSoFar.insert(FirstIdent); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3942 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3943 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3944 | while (Tok.is(tok::comma)) { |
| 3945 | // Eat the comma. |
| 3946 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3947 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3948 | // If this isn't an identifier, report the error and skip until ')'. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3949 | if (Tok.isNot(tok::identifier)) { |
| 3950 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3951 | SkipUntil(tok::r_paren); |
| 3952 | return; |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3953 | } |
Chris Lattner | aaf9ddb | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 3954 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3955 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
Chris Lattner | aaf9ddb | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 3956 | |
| 3957 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3958 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3959 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3960 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3961 | // Verify that the argument identifier has not already been mentioned. |
| 3962 | if (!ParamsSoFar.insert(ParmII)) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3963 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3964 | } else { |
| 3965 | // Remember this identifier in ParamInfo. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3966 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3967 | Tok.getLocation(), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3968 | 0)); |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3969 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3970 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3971 | // Eat the identifier. |
| 3972 | ConsumeToken(); |
| 3973 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3974 | |
| 3975 | // If we have the closing ')', eat it and we're done. |
| 3976 | SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 3977 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3978 | // Remember that we parsed a function type, and remember the attributes. This |
| 3979 | // function type is always a K&R style function type, which is not varargs and |
| 3980 | // has no prototype. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3981 | ParsedAttributes attrs(AttrFactory); |
| 3982 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false, |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 3983 | SourceLocation(), |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 3984 | &ParamInfo[0], ParamInfo.size(), |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 3985 | /*TypeQuals*/0, |
Douglas Gregor | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 3986 | true, SourceLocation(), |
Sebastian Redl | 6e5d319 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 3987 | EST_None, SourceLocation(), 0, 0, |
| 3988 | 0, 0, LParenLoc, RLoc, D), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3989 | attrs, RLoc); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 3990 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3991 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3992 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 3993 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 3994 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 3995 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 3996 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 3997 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 3998 | SourceLocation StartLoc = ConsumeBracket(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3999 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4000 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 4001 | // This code does a fast path to handle some of the most obvious cases. |
| 4002 | if (Tok.getKind() == tok::r_square) { |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4003 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4004 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4005 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4006 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4007 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4008 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4009 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4010 | StartLoc, EndLoc), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4011 | attrs, EndLoc); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4012 | return; |
| 4013 | } else if (Tok.getKind() == tok::numeric_constant && |
| 4014 | GetLookAheadToken(1).is(tok::r_square)) { |
| 4015 | // [4] is very common. Parse the numeric constant expression. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4016 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok)); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4017 | ConsumeToken(); |
| 4018 | |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4019 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4020 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4021 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4022 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4023 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4024 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4025 | ExprRes.release(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4026 | StartLoc, EndLoc), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4027 | attrs, EndLoc); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4028 | return; |
| 4029 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4030 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4031 | // If valid, this location is the position where we read the 'static' keyword. |
| 4032 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4033 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4034 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4035 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4036 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4037 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4038 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4039 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4040 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4041 | // If we haven't already read 'static', check to see if there is one after the |
| 4042 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4043 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4044 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4045 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4046 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 4047 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4048 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4049 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4050 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 4051 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 4052 | // the the token after the star is a ']'. Since stars in arrays are |
| 4053 | // infrequent, use of lookahead is not costly here. |
| 4054 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 4055 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4056 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4057 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4058 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4059 | StaticLoc = SourceLocation(); // Drop the static. |
| 4060 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4061 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4062 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4063 | // Note, in C89, this production uses the constant-expr production instead |
| 4064 | // of assignment-expr. The only difference is that assignment-expr allows |
| 4065 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 4066 | // are not i-c-e's, so we don't need to distinguish between the two here. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4067 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4068 | // Parse the constant-expression or assignment-expression now (depending |
| 4069 | // on dialect). |
| 4070 | if (getLang().CPlusPlus) |
| 4071 | NumElements = ParseConstantExpression(); |
| 4072 | else |
| 4073 | NumElements = ParseAssignmentExpression(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4074 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4075 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4076 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4077 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 4078 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4079 | // If the expression was invalid, skip it. |
| 4080 | SkipUntil(tok::r_square); |
| 4081 | return; |
| 4082 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4083 | |
| 4084 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
| 4085 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4086 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4087 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4088 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4089 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4090 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4091 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4092 | NumElements.release(), |
| 4093 | StartLoc, EndLoc), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4094 | attrs, EndLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4097 | /// [GNU] typeof-specifier: |
| 4098 | /// typeof ( expressions ) |
| 4099 | /// typeof ( type-name ) |
| 4100 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4101 | /// |
| 4102 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4103 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4104 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4105 | SourceLocation StartLoc = ConsumeToken(); |
| 4106 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4107 | const bool hasParens = Tok.is(tok::l_paren); |
| 4108 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4109 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4110 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4111 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4112 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 4113 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4114 | if (hasParens) |
| 4115 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4116 | |
| 4117 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4118 | // FIXME: Not accurate, the range gets one token more than it should. |
| 4119 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4120 | else |
| 4121 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4122 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4123 | if (isCastExpr) { |
| 4124 | if (!CastTy) { |
| 4125 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4126 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 4127 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4128 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4129 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4130 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4131 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4132 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4133 | DiagID, CastTy)) |
| 4134 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4135 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4136 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4137 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4138 | // If we get here, the operand to the typeof was an expresion. |
| 4139 | if (Operand.isInvalid()) { |
| 4140 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 4141 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4142 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4143 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4144 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4145 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4146 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4147 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4148 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4149 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4150 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4151 | |
| 4152 | |
| 4153 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 4154 | /// from TryAltiVecVectorToken. |
| 4155 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 4156 | Token Next = NextToken(); |
| 4157 | switch (Next.getKind()) { |
| 4158 | default: return false; |
| 4159 | case tok::kw_short: |
| 4160 | case tok::kw_long: |
| 4161 | case tok::kw_signed: |
| 4162 | case tok::kw_unsigned: |
| 4163 | case tok::kw_void: |
| 4164 | case tok::kw_char: |
| 4165 | case tok::kw_int: |
| 4166 | case tok::kw_float: |
| 4167 | case tok::kw_double: |
| 4168 | case tok::kw_bool: |
| 4169 | case tok::kw___pixel: |
| 4170 | Tok.setKind(tok::kw___vector); |
| 4171 | return true; |
| 4172 | case tok::identifier: |
| 4173 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4174 | Tok.setKind(tok::kw___vector); |
| 4175 | return true; |
| 4176 | } |
| 4177 | return false; |
| 4178 | } |
| 4179 | } |
| 4180 | |
| 4181 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 4182 | const char *&PrevSpec, unsigned &DiagID, |
| 4183 | bool &isInvalid) { |
| 4184 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 4185 | Token Next = NextToken(); |
| 4186 | switch (Next.getKind()) { |
| 4187 | case tok::kw_short: |
| 4188 | case tok::kw_long: |
| 4189 | case tok::kw_signed: |
| 4190 | case tok::kw_unsigned: |
| 4191 | case tok::kw_void: |
| 4192 | case tok::kw_char: |
| 4193 | case tok::kw_int: |
| 4194 | case tok::kw_float: |
| 4195 | case tok::kw_double: |
| 4196 | case tok::kw_bool: |
| 4197 | case tok::kw___pixel: |
| 4198 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4199 | return true; |
| 4200 | case tok::identifier: |
| 4201 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4202 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4203 | return true; |
| 4204 | } |
| 4205 | break; |
| 4206 | default: |
| 4207 | break; |
| 4208 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 4209 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4210 | DS.isTypeAltiVecVector()) { |
| 4211 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 4212 | return true; |
| 4213 | } |
| 4214 | return false; |
| 4215 | } |