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