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