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