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