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