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