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