| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | // This file implements the Expression parsing implementation for C++. | 
|  | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
|  | 13 |  | 
| Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 14 | #include "clang/Parse/ParseDiagnostic.h" | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Parser.h" | 
| Douglas Gregor | bc61bd8 | 2011-01-11 00:33:19 +0000 | [diff] [blame] | 16 | #include "RAIIObjectsForParser.h" | 
| John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/DeclSpec.h" | 
| Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Scope.h" | 
| John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ParsedTemplate.h" | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" | 
|  | 21 |  | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; | 
|  | 23 |  | 
| Richard Smith | ea698b3 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 24 | static int SelectDigraphErrorMessage(tok::TokenKind Kind) { | 
|  | 25 | switch (Kind) { | 
|  | 26 | case tok::kw_template:         return 0; | 
|  | 27 | case tok::kw_const_cast:       return 1; | 
|  | 28 | case tok::kw_dynamic_cast:     return 2; | 
|  | 29 | case tok::kw_reinterpret_cast: return 3; | 
|  | 30 | case tok::kw_static_cast:      return 4; | 
|  | 31 | default: | 
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 32 | llvm_unreachable("Unknown type for digraph error message."); | 
| Richard Smith | ea698b3 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 33 | } | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | // Are the two tokens adjacent in the same source file? | 
|  | 37 | static bool AreTokensAdjacent(Preprocessor &PP, Token &First, Token &Second) { | 
|  | 38 | SourceManager &SM = PP.getSourceManager(); | 
|  | 39 | SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation()); | 
| Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 40 | SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength()); | 
| Richard Smith | ea698b3 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 41 | return FirstEnd == SM.getSpellingLoc(Second.getLocation()); | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | // Suggest fixit for "<::" after a cast. | 
|  | 45 | static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken, | 
|  | 46 | Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) { | 
|  | 47 | // Pull '<:' and ':' off token stream. | 
|  | 48 | if (!AtDigraph) | 
|  | 49 | PP.Lex(DigraphToken); | 
|  | 50 | PP.Lex(ColonToken); | 
|  | 51 |  | 
|  | 52 | SourceRange Range; | 
|  | 53 | Range.setBegin(DigraphToken.getLocation()); | 
|  | 54 | Range.setEnd(ColonToken.getLocation()); | 
|  | 55 | P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph) | 
|  | 56 | << SelectDigraphErrorMessage(Kind) | 
|  | 57 | << FixItHint::CreateReplacement(Range, "< ::"); | 
|  | 58 |  | 
|  | 59 | // Update token information to reflect their change in token type. | 
|  | 60 | ColonToken.setKind(tok::coloncolon); | 
| Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 61 | ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1)); | 
| Richard Smith | ea698b3 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 62 | ColonToken.setLength(2); | 
|  | 63 | DigraphToken.setKind(tok::less); | 
|  | 64 | DigraphToken.setLength(1); | 
|  | 65 |  | 
|  | 66 | // Push new tokens back to token stream. | 
|  | 67 | PP.EnterToken(ColonToken); | 
|  | 68 | if (!AtDigraph) | 
|  | 69 | PP.EnterToken(DigraphToken); | 
|  | 70 | } | 
|  | 71 |  | 
| Richard Trieu | 950be71 | 2011-09-19 19:01:00 +0000 | [diff] [blame] | 72 | // Check for '<::' which should be '< ::' instead of '[:' when following | 
|  | 73 | // a template name. | 
|  | 74 | void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType, | 
|  | 75 | bool EnteringContext, | 
|  | 76 | IdentifierInfo &II, CXXScopeSpec &SS) { | 
| Richard Trieu | c11030e | 2011-09-20 20:03:50 +0000 | [diff] [blame] | 77 | if (!Next.is(tok::l_square) || Next.getLength() != 2) | 
| Richard Trieu | 950be71 | 2011-09-19 19:01:00 +0000 | [diff] [blame] | 78 | return; | 
|  | 79 |  | 
|  | 80 | Token SecondToken = GetLookAheadToken(2); | 
|  | 81 | if (!SecondToken.is(tok::colon) || !AreTokensAdjacent(PP, Next, SecondToken)) | 
|  | 82 | return; | 
|  | 83 |  | 
|  | 84 | TemplateTy Template; | 
|  | 85 | UnqualifiedId TemplateName; | 
|  | 86 | TemplateName.setIdentifier(&II, Tok.getLocation()); | 
|  | 87 | bool MemberOfUnknownSpecialization; | 
|  | 88 | if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false, | 
|  | 89 | TemplateName, ObjectType, EnteringContext, | 
|  | 90 | Template, MemberOfUnknownSpecialization)) | 
|  | 91 | return; | 
|  | 92 |  | 
|  | 93 | FixDigraph(*this, PP, Next, SecondToken, tok::kw_template, | 
|  | 94 | /*AtDigraph*/false); | 
|  | 95 | } | 
|  | 96 |  | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | /// \brief Parse global scope or nested-name-specifier if present. | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 98 | /// | 
|  | 99 | /// Parses a C++ global scope specifier ('::') or nested-name-specifier (which | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | /// may be preceded by '::'). Note that this routine will not parse ::new or | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 101 | /// ::delete; it will just leave them in the token stream. | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 102 | /// | 
|  | 103 | ///       '::'[opt] nested-name-specifier | 
|  | 104 | ///       '::' | 
|  | 105 | /// | 
|  | 106 | ///       nested-name-specifier: | 
|  | 107 | ///         type-name '::' | 
|  | 108 | ///         namespace-name '::' | 
|  | 109 | ///         nested-name-specifier identifier '::' | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 110 | ///         nested-name-specifier 'template'[opt] simple-template-id '::' | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 111 | /// | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 112 | /// | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | /// \param SS the scope specifier that will be set to the parsed | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 114 | /// nested-name-specifier (or empty) | 
|  | 115 | /// | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | /// \param ObjectType if this nested-name-specifier is being parsed following | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 117 | /// the "." or "->" of a member access expression, this parameter provides the | 
|  | 118 | /// type of the object whose members are being accessed. | 
|  | 119 | /// | 
|  | 120 | /// \param EnteringContext whether we will be entering into the context of | 
|  | 121 | /// the nested-name-specifier after parsing it. | 
|  | 122 | /// | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 123 | /// \param MayBePseudoDestructor When non-NULL, points to a flag that | 
|  | 124 | /// indicates whether this nested-name-specifier may be part of a | 
|  | 125 | /// pseudo-destructor name. In this case, the flag will be set false | 
|  | 126 | /// if we don't actually end up parsing a destructor name. Moreorover, | 
|  | 127 | /// if we do end up determining that we are parsing a destructor name, | 
|  | 128 | /// the last component of the nested-name-specifier is not parsed as | 
|  | 129 | /// part of the scope specifier. | 
|  | 130 |  | 
| Douglas Gregor | b10cd04 | 2010-02-21 18:36:56 +0000 | [diff] [blame] | 131 | /// member access expression, e.g., the \p T:: in \p p->T::m. | 
|  | 132 | /// | 
| John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 133 | /// \returns true if there was an error parsing a scope specifier | 
| Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 134 | bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS, | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 135 | ParsedType ObjectType, | 
| Douglas Gregor | b10cd04 | 2010-02-21 18:36:56 +0000 | [diff] [blame] | 136 | bool EnteringContext, | 
| Francois Pichet | 4147d30 | 2011-03-27 19:41:34 +0000 | [diff] [blame] | 137 | bool *MayBePseudoDestructor, | 
|  | 138 | bool IsTypename) { | 
| Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 139 | assert(getLang().CPlusPlus && | 
| Chris Lattner | 7452c6f | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 140 | "Call sites of this function should be guarded by checking for C++"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 |  | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 142 | if (Tok.is(tok::annot_cxxscope)) { | 
| Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 143 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), | 
|  | 144 | Tok.getAnnotationRange(), | 
|  | 145 | SS); | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 146 | ConsumeToken(); | 
| John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 147 | return false; | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 148 | } | 
| Chris Lattner | e607e80 | 2009-01-04 21:14:15 +0000 | [diff] [blame] | 149 |  | 
| Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 150 | bool HasScopeSpecifier = false; | 
|  | 151 |  | 
| Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 152 | if (Tok.is(tok::coloncolon)) { | 
|  | 153 | // ::new and ::delete aren't nested-name-specifiers. | 
|  | 154 | tok::TokenKind NextKind = NextToken().getKind(); | 
|  | 155 | if (NextKind == tok::kw_new || NextKind == tok::kw_delete) | 
|  | 156 | return false; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 |  | 
| Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 158 | // '::' - Global scope qualifier. | 
| Douglas Gregor | 2e4c34a | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 159 | if (Actions.ActOnCXXGlobalScopeSpecifier(getCurScope(), ConsumeToken(), SS)) | 
|  | 160 | return true; | 
|  | 161 |  | 
| Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 162 | HasScopeSpecifier = true; | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 165 | bool CheckForDestructor = false; | 
|  | 166 | if (MayBePseudoDestructor && *MayBePseudoDestructor) { | 
|  | 167 | CheckForDestructor = true; | 
|  | 168 | *MayBePseudoDestructor = false; | 
|  | 169 | } | 
|  | 170 |  | 
| David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 171 | if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) { | 
|  | 172 | DeclSpec DS(AttrFactory); | 
|  | 173 | SourceLocation DeclLoc = Tok.getLocation(); | 
|  | 174 | SourceLocation EndLoc  = ParseDecltypeSpecifier(DS); | 
|  | 175 | if (Tok.isNot(tok::coloncolon)) { | 
|  | 176 | AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc); | 
|  | 177 | return false; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | SourceLocation CCLoc = ConsumeToken(); | 
|  | 181 | if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc)) | 
|  | 182 | SS.SetInvalid(SourceRange(DeclLoc, CCLoc)); | 
|  | 183 |  | 
|  | 184 | HasScopeSpecifier = true; | 
|  | 185 | } | 
|  | 186 |  | 
| Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 187 | while (true) { | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 188 | if (HasScopeSpecifier) { | 
|  | 189 | // C++ [basic.lookup.classref]p5: | 
|  | 190 | //   If the qualified-id has the form | 
| Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 191 | // | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 192 | //       ::class-name-or-namespace-name::... | 
| Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 193 | // | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 194 | //   the class-name-or-namespace-name is looked up in global scope as a | 
|  | 195 | //   class-name or namespace-name. | 
|  | 196 | // | 
|  | 197 | // To implement this, we clear out the object type as soon as we've | 
|  | 198 | // seen a leading '::' or part of a nested-name-specifier. | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 199 | ObjectType = ParsedType(); | 
| Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 200 |  | 
|  | 201 | if (Tok.is(tok::code_completion)) { | 
|  | 202 | // Code completion for a nested-name-specifier, where the code | 
|  | 203 | // code completion token follows the '::'. | 
| Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 204 | Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext); | 
| Argyrios Kyrtzidis | b6b2b18 | 2011-04-23 01:04:12 +0000 | [diff] [blame] | 205 | // Include code completion token into the range of the scope otherwise | 
|  | 206 | // when we try to annotate the scope tokens the dangling code completion | 
|  | 207 | // token will cause assertion in | 
|  | 208 | // Preprocessor::AnnotatePreviousCachedTokens. | 
| Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 209 | SS.setEndLoc(Tok.getLocation()); | 
|  | 210 | cutOffParsing(); | 
|  | 211 | return true; | 
| Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 212 | } | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 213 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 |  | 
| Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 215 | // nested-name-specifier: | 
| Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 216 | //   nested-name-specifier 'template'[opt] simple-template-id '::' | 
|  | 217 |  | 
|  | 218 | // Parse the optional 'template' keyword, then make sure we have | 
|  | 219 | // 'identifier <' after it. | 
|  | 220 | if (Tok.is(tok::kw_template)) { | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 221 | // If we don't have a scope specifier or an object type, this isn't a | 
| Eli Friedman | eab975d | 2009-08-29 04:08:08 +0000 | [diff] [blame] | 222 | // nested-name-specifier, since they aren't allowed to start with | 
|  | 223 | // 'template'. | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 224 | if (!HasScopeSpecifier && !ObjectType) | 
| Eli Friedman | eab975d | 2009-08-29 04:08:08 +0000 | [diff] [blame] | 225 | break; | 
|  | 226 |  | 
| Douglas Gregor | 7bb87fc | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 227 | TentativeParsingAction TPA(*this); | 
| Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 228 | SourceLocation TemplateKWLoc = ConsumeToken(); | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 229 |  | 
|  | 230 | UnqualifiedId TemplateName; | 
|  | 231 | if (Tok.is(tok::identifier)) { | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 232 | // Consume the identifier. | 
| Douglas Gregor | 7bb87fc | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 233 | TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 234 | ConsumeToken(); | 
|  | 235 | } else if (Tok.is(tok::kw_operator)) { | 
|  | 236 | if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, | 
| Douglas Gregor | 7bb87fc | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 237 | TemplateName)) { | 
|  | 238 | TPA.Commit(); | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 239 | break; | 
| Douglas Gregor | 7bb87fc | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 240 | } | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 241 |  | 
| Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 242 | if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId && | 
|  | 243 | TemplateName.getKind() != UnqualifiedId::IK_LiteralOperatorId) { | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 244 | Diag(TemplateName.getSourceRange().getBegin(), | 
|  | 245 | diag::err_id_after_template_in_nested_name_spec) | 
|  | 246 | << TemplateName.getSourceRange(); | 
| Douglas Gregor | 7bb87fc | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 247 | TPA.Commit(); | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 248 | break; | 
|  | 249 | } | 
|  | 250 | } else { | 
| Douglas Gregor | 7bb87fc | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 251 | TPA.Revert(); | 
| Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 252 | break; | 
|  | 253 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 |  | 
| Douglas Gregor | 7bb87fc | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 255 | // If the next token is not '<', we have a qualified-id that refers | 
|  | 256 | // to a template name, such as T::template apply, but is not a | 
|  | 257 | // template-id. | 
|  | 258 | if (Tok.isNot(tok::less)) { | 
|  | 259 | TPA.Revert(); | 
|  | 260 | break; | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | // Commit to parsing the template-id. | 
|  | 264 | TPA.Commit(); | 
| Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 265 | TemplateTy Template; | 
| Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 266 | if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName(getCurScope(), | 
| Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 267 | TemplateKWLoc, | 
|  | 268 | SS, | 
|  | 269 | TemplateName, | 
|  | 270 | ObjectType, | 
|  | 271 | EnteringContext, | 
|  | 272 | Template)) { | 
| Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 273 | if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName, | 
| Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 274 | TemplateKWLoc, false)) | 
|  | 275 | return true; | 
|  | 276 | } else | 
| John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 277 | return true; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 |  | 
| Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 279 | continue; | 
|  | 280 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 |  | 
| Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 282 | if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | // We have | 
| Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 284 | // | 
|  | 285 | //   simple-template-id '::' | 
|  | 286 | // | 
|  | 287 | // So we need to check whether the simple-template-id is of the | 
| Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 288 | // right kind (it should name a type or be dependent), and then | 
|  | 289 | // convert it into a type within the nested-name-specifier. | 
| Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 290 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 291 | if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) { | 
|  | 292 | *MayBePseudoDestructor = true; | 
| John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 293 | return false; | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 294 | } | 
|  | 295 |  | 
| Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 296 | // Consume the template-id token. | 
|  | 297 | ConsumeToken(); | 
|  | 298 |  | 
|  | 299 | assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!"); | 
|  | 300 | SourceLocation CCLoc = ConsumeToken(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 |  | 
| David Blaikie | 6796fc1 | 2011-11-07 03:30:03 +0000 | [diff] [blame] | 302 | HasScopeSpecifier = true; | 
| Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 303 |  | 
|  | 304 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, | 
|  | 305 | TemplateId->getTemplateArgs(), | 
|  | 306 | TemplateId->NumArgs); | 
|  | 307 |  | 
|  | 308 | if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), | 
|  | 309 | /*FIXME:*/SourceLocation(), | 
|  | 310 | SS, | 
|  | 311 | TemplateId->Template, | 
|  | 312 | TemplateId->TemplateNameLoc, | 
|  | 313 | TemplateId->LAngleLoc, | 
|  | 314 | TemplateArgsPtr, | 
|  | 315 | TemplateId->RAngleLoc, | 
|  | 316 | CCLoc, | 
|  | 317 | EnteringContext)) { | 
|  | 318 | SourceLocation StartLoc | 
|  | 319 | = SS.getBeginLoc().isValid()? SS.getBeginLoc() | 
|  | 320 | : TemplateId->TemplateNameLoc; | 
|  | 321 | SS.SetInvalid(SourceRange(StartLoc, CCLoc)); | 
| Chris Lattner | 67b9e83 | 2009-06-26 03:45:46 +0000 | [diff] [blame] | 322 | } | 
| Argyrios Kyrtzidis | eccce7e | 2011-05-03 18:45:38 +0000 | [diff] [blame] | 323 |  | 
| Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 324 | continue; | 
| Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 325 | } | 
|  | 326 |  | 
| Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 327 |  | 
|  | 328 | // The rest of the nested-name-specifier possibilities start with | 
|  | 329 | // tok::identifier. | 
|  | 330 | if (Tok.isNot(tok::identifier)) | 
|  | 331 | break; | 
|  | 332 |  | 
|  | 333 | IdentifierInfo &II = *Tok.getIdentifierInfo(); | 
|  | 334 |  | 
|  | 335 | // nested-name-specifier: | 
|  | 336 | //   type-name '::' | 
|  | 337 | //   namespace-name '::' | 
|  | 338 | //   nested-name-specifier identifier '::' | 
|  | 339 | Token Next = NextToken(); | 
| Chris Lattner | 4664649 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 340 |  | 
|  | 341 | // If we get foo:bar, this is almost certainly a typo for foo::bar.  Recover | 
|  | 342 | // and emit a fixit hint for it. | 
| Douglas Gregor | b10cd04 | 2010-02-21 18:36:56 +0000 | [diff] [blame] | 343 | if (Next.is(tok::colon) && !ColonIsSacred) { | 
| Douglas Gregor | 2e4c34a | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 344 | if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, II, | 
|  | 345 | Tok.getLocation(), | 
|  | 346 | Next.getLocation(), ObjectType, | 
| Douglas Gregor | b10cd04 | 2010-02-21 18:36:56 +0000 | [diff] [blame] | 347 | EnteringContext) && | 
|  | 348 | // If the token after the colon isn't an identifier, it's still an | 
|  | 349 | // error, but they probably meant something else strange so don't | 
|  | 350 | // recover like this. | 
|  | 351 | PP.LookAhead(1).is(tok::identifier)) { | 
|  | 352 | Diag(Next, diag::err_unexected_colon_in_nested_name_spec) | 
| Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 353 | << FixItHint::CreateReplacement(Next.getLocation(), "::"); | 
| Douglas Gregor | b10cd04 | 2010-02-21 18:36:56 +0000 | [diff] [blame] | 354 |  | 
|  | 355 | // Recover as if the user wrote '::'. | 
|  | 356 | Next.setKind(tok::coloncolon); | 
|  | 357 | } | 
| Chris Lattner | 4664649 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 358 | } | 
|  | 359 |  | 
| Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 360 | if (Next.is(tok::coloncolon)) { | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 361 | if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) && | 
| Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 362 | !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, Tok.getLocation(), | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 363 | II, ObjectType)) { | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 364 | *MayBePseudoDestructor = true; | 
| John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 365 | return false; | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 366 | } | 
|  | 367 |  | 
| Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 368 | // We have an identifier followed by a '::'. Lookup this name | 
|  | 369 | // as the name in a nested-name-specifier. | 
|  | 370 | SourceLocation IdLoc = ConsumeToken(); | 
| Chris Lattner | 4664649 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 371 | assert((Tok.is(tok::coloncolon) || Tok.is(tok::colon)) && | 
|  | 372 | "NextToken() not working properly!"); | 
| Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 373 | SourceLocation CCLoc = ConsumeToken(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 |  | 
| Douglas Gregor | 2e4c34a | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 375 | HasScopeSpecifier = true; | 
|  | 376 | if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), II, IdLoc, CCLoc, | 
|  | 377 | ObjectType, EnteringContext, SS)) | 
|  | 378 | SS.SetInvalid(SourceRange(IdLoc, CCLoc)); | 
|  | 379 |  | 
| Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 380 | continue; | 
|  | 381 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 |  | 
| Richard Trieu | 950be71 | 2011-09-19 19:01:00 +0000 | [diff] [blame] | 383 | CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS); | 
| Richard Smith | ea698b3 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 384 |  | 
| Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 385 | // nested-name-specifier: | 
|  | 386 | //   type-name '<' | 
|  | 387 | if (Next.is(tok::less)) { | 
|  | 388 | TemplateTy Template; | 
| Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 389 | UnqualifiedId TemplateName; | 
|  | 390 | TemplateName.setIdentifier(&II, Tok.getLocation()); | 
| Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 391 | bool MemberOfUnknownSpecialization; | 
| Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 392 | if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS, | 
| Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 393 | /*hasTemplateKeyword=*/false, | 
| Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 394 | TemplateName, | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 395 | ObjectType, | 
| Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 396 | EnteringContext, | 
| Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 397 | Template, | 
|  | 398 | MemberOfUnknownSpecialization)) { | 
| David Blaikie | 6796fc1 | 2011-11-07 03:30:03 +0000 | [diff] [blame] | 399 | // We have found a template name, so annotate this token | 
| Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 400 | // with a template-id annotation. We do not permit the | 
|  | 401 | // template-id to be translated into a type annotation, | 
|  | 402 | // because some clients (e.g., the parsing of class template | 
|  | 403 | // specializations) still want to see the original template-id | 
|  | 404 | // token. | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 405 | ConsumeToken(); | 
| Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 406 | if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName, | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 407 | SourceLocation(), false)) | 
| John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 408 | return true; | 
| Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 409 | continue; | 
| Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 410 | } | 
|  | 411 |  | 
|  | 412 | if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) && | 
| Francois Pichet | 4147d30 | 2011-03-27 19:41:34 +0000 | [diff] [blame] | 413 | (IsTypename || IsTemplateArgumentList(1))) { | 
| Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 414 | // We have something like t::getAs<T>, where getAs is a | 
|  | 415 | // member of an unknown specialization. However, this will only | 
|  | 416 | // parse correctly as a template, so suggest the keyword 'template' | 
|  | 417 | // before 'getAs' and treat this as a dependent template name. | 
| Francois Pichet | 4147d30 | 2011-03-27 19:41:34 +0000 | [diff] [blame] | 418 | unsigned DiagID = diag::err_missing_dependent_template_keyword; | 
| Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 419 | if (getLang().MicrosoftExt) | 
| Francois Pichet | cf320c6 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 420 | DiagID = diag::warn_missing_dependent_template_keyword; | 
| Francois Pichet | 4147d30 | 2011-03-27 19:41:34 +0000 | [diff] [blame] | 421 |  | 
|  | 422 | Diag(Tok.getLocation(), DiagID) | 
| Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 423 | << II.getName() | 
|  | 424 | << FixItHint::CreateInsertion(Tok.getLocation(), "template "); | 
|  | 425 |  | 
| Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 426 | if (TemplateNameKind TNK | 
| Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 427 | = Actions.ActOnDependentTemplateName(getCurScope(), | 
| Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 428 | Tok.getLocation(), SS, | 
|  | 429 | TemplateName, ObjectType, | 
|  | 430 | EnteringContext, Template)) { | 
|  | 431 | // Consume the identifier. | 
|  | 432 | ConsumeToken(); | 
| Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 433 | if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName, | 
| Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 434 | SourceLocation(), false)) | 
|  | 435 | return true; | 
|  | 436 | } | 
|  | 437 | else | 
| Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 438 | return true; | 
| Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 439 |  | 
| Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 440 | continue; | 
| Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 441 | } | 
|  | 442 | } | 
|  | 443 |  | 
| Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 444 | // We don't have any tokens that form the beginning of a | 
|  | 445 | // nested-name-specifier, so we're done. | 
|  | 446 | break; | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 447 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 |  | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 449 | // Even if we didn't see any pieces of a nested-name-specifier, we | 
|  | 450 | // still check whether there is a tilde in this position, which | 
|  | 451 | // indicates a potential pseudo-destructor. | 
|  | 452 | if (CheckForDestructor && Tok.is(tok::tilde)) | 
|  | 453 | *MayBePseudoDestructor = true; | 
|  | 454 |  | 
| John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 455 | return false; | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 456 | } | 
|  | 457 |  | 
|  | 458 | /// ParseCXXIdExpression - Handle id-expression. | 
|  | 459 | /// | 
|  | 460 | ///       id-expression: | 
|  | 461 | ///         unqualified-id | 
|  | 462 | ///         qualified-id | 
|  | 463 | /// | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 464 | ///       qualified-id: | 
|  | 465 | ///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id | 
|  | 466 | ///         '::' identifier | 
|  | 467 | ///         '::' operator-function-id | 
| Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 468 | ///         '::' template-id | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 469 | /// | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 470 | /// NOTE: The standard specifies that, for qualified-id, the parser does not | 
|  | 471 | /// expect: | 
|  | 472 | /// | 
|  | 473 | ///   '::' conversion-function-id | 
|  | 474 | ///   '::' '~' class-name | 
|  | 475 | /// | 
|  | 476 | /// This may cause a slight inconsistency on diagnostics: | 
|  | 477 | /// | 
|  | 478 | /// class C {}; | 
|  | 479 | /// namespace A {} | 
|  | 480 | /// void f() { | 
|  | 481 | ///   :: A :: ~ C(); // Some Sema error about using destructor with a | 
|  | 482 | ///                  // namespace. | 
|  | 483 | ///   :: ~ C(); // Some Parser error like 'unexpected ~'. | 
|  | 484 | /// } | 
|  | 485 | /// | 
|  | 486 | /// We simplify the parser a bit and make it work like: | 
|  | 487 | /// | 
|  | 488 | ///       qualified-id: | 
|  | 489 | ///         '::'[opt] nested-name-specifier 'template'[opt] unqualified-id | 
|  | 490 | ///         '::' unqualified-id | 
|  | 491 | /// | 
|  | 492 | /// That way Sema can handle and report similar errors for namespaces and the | 
|  | 493 | /// global scope. | 
|  | 494 | /// | 
| Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 495 | /// The isAddressOfOperand parameter indicates that this id-expression is a | 
|  | 496 | /// direct operand of the address-of operator. This is, besides member contexts, | 
|  | 497 | /// the only place where a qualified-id naming a non-static class member may | 
|  | 498 | /// appear. | 
|  | 499 | /// | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 500 | ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) { | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 501 | // qualified-id: | 
|  | 502 | //   '::'[opt] nested-name-specifier 'template'[opt] unqualified-id | 
|  | 503 | //   '::' unqualified-id | 
|  | 504 | // | 
|  | 505 | CXXScopeSpec SS; | 
| Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 506 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false); | 
| Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 507 |  | 
|  | 508 | UnqualifiedId Name; | 
|  | 509 | if (ParseUnqualifiedId(SS, | 
|  | 510 | /*EnteringContext=*/false, | 
|  | 511 | /*AllowDestructorName=*/false, | 
|  | 512 | /*AllowConstructorName=*/false, | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 513 | /*ObjectType=*/ ParsedType(), | 
| Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 514 | Name)) | 
|  | 515 | return ExprError(); | 
| John McCall | b681b61 | 2009-11-22 02:49:43 +0000 | [diff] [blame] | 516 |  | 
|  | 517 | // This is only the direct operand of an & operator if it is not | 
|  | 518 | // followed by a postfix-expression suffix. | 
| John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 519 | if (isAddressOfOperand && isPostfixExpressionSuffixStart()) | 
|  | 520 | isAddressOfOperand = false; | 
| Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 521 |  | 
| Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 522 | return Actions.ActOnIdExpression(getCurScope(), SS, Name, Tok.is(tok::l_paren), | 
| Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 523 | isAddressOfOperand); | 
|  | 524 |  | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 525 | } | 
|  | 526 |  | 
| Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 527 | /// ParseLambdaExpression - Parse a C++0x lambda expression. | 
|  | 528 | /// | 
|  | 529 | ///       lambda-expression: | 
|  | 530 | ///         lambda-introducer lambda-declarator[opt] compound-statement | 
|  | 531 | /// | 
|  | 532 | ///       lambda-introducer: | 
|  | 533 | ///         '[' lambda-capture[opt] ']' | 
|  | 534 | /// | 
|  | 535 | ///       lambda-capture: | 
|  | 536 | ///         capture-default | 
|  | 537 | ///         capture-list | 
|  | 538 | ///         capture-default ',' capture-list | 
|  | 539 | /// | 
|  | 540 | ///       capture-default: | 
|  | 541 | ///         '&' | 
|  | 542 | ///         '=' | 
|  | 543 | /// | 
|  | 544 | ///       capture-list: | 
|  | 545 | ///         capture | 
|  | 546 | ///         capture-list ',' capture | 
|  | 547 | /// | 
|  | 548 | ///       capture: | 
|  | 549 | ///         identifier | 
|  | 550 | ///         '&' identifier | 
|  | 551 | ///         'this' | 
|  | 552 | /// | 
|  | 553 | ///       lambda-declarator: | 
|  | 554 | ///         '(' parameter-declaration-clause ')' attribute-specifier[opt] | 
|  | 555 | ///           'mutable'[opt] exception-specification[opt] | 
|  | 556 | ///           trailing-return-type[opt] | 
|  | 557 | /// | 
|  | 558 | ExprResult Parser::ParseLambdaExpression() { | 
|  | 559 | // Parse lambda-introducer. | 
|  | 560 | LambdaIntroducer Intro; | 
|  | 561 |  | 
|  | 562 | llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro)); | 
|  | 563 | if (DiagID) { | 
|  | 564 | Diag(Tok, DiagID.getValue()); | 
|  | 565 | SkipUntil(tok::r_square); | 
|  | 566 | } | 
|  | 567 |  | 
|  | 568 | return ParseLambdaExpressionAfterIntroducer(Intro); | 
|  | 569 | } | 
|  | 570 |  | 
|  | 571 | /// TryParseLambdaExpression - Use lookahead and potentially tentative | 
|  | 572 | /// parsing to determine if we are looking at a C++0x lambda expression, and parse | 
|  | 573 | /// it if we are. | 
|  | 574 | /// | 
|  | 575 | /// If we are not looking at a lambda expression, returns ExprError(). | 
|  | 576 | ExprResult Parser::TryParseLambdaExpression() { | 
|  | 577 | assert(getLang().CPlusPlus0x | 
|  | 578 | && Tok.is(tok::l_square) | 
|  | 579 | && "Not at the start of a possible lambda expression."); | 
|  | 580 |  | 
|  | 581 | const Token Next = NextToken(), After = GetLookAheadToken(2); | 
|  | 582 |  | 
|  | 583 | // If lookahead indicates this is a lambda... | 
|  | 584 | if (Next.is(tok::r_square) ||     // [] | 
|  | 585 | Next.is(tok::equal) ||        // [= | 
|  | 586 | (Next.is(tok::amp) &&         // [&] or [&, | 
|  | 587 | (After.is(tok::r_square) || | 
|  | 588 | After.is(tok::comma))) || | 
|  | 589 | (Next.is(tok::identifier) &&  // [identifier] | 
|  | 590 | After.is(tok::r_square))) { | 
|  | 591 | return ParseLambdaExpression(); | 
|  | 592 | } | 
|  | 593 |  | 
|  | 594 | // If lookahead indicates this is an Objective-C message... | 
|  | 595 | if (Next.is(tok::identifier) && After.is(tok::identifier)) { | 
|  | 596 | return ExprError(); | 
|  | 597 | } | 
|  | 598 |  | 
|  | 599 | LambdaIntroducer Intro; | 
|  | 600 | if (TryParseLambdaIntroducer(Intro)) | 
|  | 601 | return ExprError(); | 
|  | 602 | return ParseLambdaExpressionAfterIntroducer(Intro); | 
|  | 603 | } | 
|  | 604 |  | 
|  | 605 | /// ParseLambdaExpression - Parse a lambda introducer. | 
|  | 606 | /// | 
|  | 607 | /// Returns a DiagnosticID if it hit something unexpected. | 
|  | 608 | llvm::Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro) { | 
|  | 609 | typedef llvm::Optional<unsigned> DiagResult; | 
|  | 610 |  | 
|  | 611 | assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['."); | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 612 | BalancedDelimiterTracker T(*this, tok::l_square); | 
|  | 613 | T.consumeOpen(); | 
|  | 614 |  | 
|  | 615 | Intro.Range.setBegin(T.getOpenLocation()); | 
| Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 616 |  | 
|  | 617 | bool first = true; | 
|  | 618 |  | 
|  | 619 | // Parse capture-default. | 
|  | 620 | if (Tok.is(tok::amp) && | 
|  | 621 | (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) { | 
|  | 622 | Intro.Default = LCD_ByRef; | 
|  | 623 | ConsumeToken(); | 
|  | 624 | first = false; | 
|  | 625 | } else if (Tok.is(tok::equal)) { | 
|  | 626 | Intro.Default = LCD_ByCopy; | 
|  | 627 | ConsumeToken(); | 
|  | 628 | first = false; | 
|  | 629 | } | 
|  | 630 |  | 
|  | 631 | while (Tok.isNot(tok::r_square)) { | 
|  | 632 | if (!first) { | 
|  | 633 | if (Tok.isNot(tok::comma)) | 
|  | 634 | return DiagResult(diag::err_expected_comma_or_rsquare); | 
|  | 635 | ConsumeToken(); | 
|  | 636 | } | 
|  | 637 |  | 
|  | 638 | first = false; | 
|  | 639 |  | 
|  | 640 | // Parse capture. | 
|  | 641 | LambdaCaptureKind Kind = LCK_ByCopy; | 
|  | 642 | SourceLocation Loc; | 
|  | 643 | IdentifierInfo* Id = 0; | 
|  | 644 |  | 
|  | 645 | if (Tok.is(tok::kw_this)) { | 
|  | 646 | Kind = LCK_This; | 
|  | 647 | Loc = ConsumeToken(); | 
|  | 648 | } else { | 
|  | 649 | if (Tok.is(tok::amp)) { | 
|  | 650 | Kind = LCK_ByRef; | 
|  | 651 | ConsumeToken(); | 
|  | 652 | } | 
|  | 653 |  | 
|  | 654 | if (Tok.is(tok::identifier)) { | 
|  | 655 | Id = Tok.getIdentifierInfo(); | 
|  | 656 | Loc = ConsumeToken(); | 
|  | 657 | } else if (Tok.is(tok::kw_this)) { | 
|  | 658 | // FIXME: If we want to suggest a fixit here, will need to return more | 
|  | 659 | // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be | 
|  | 660 | // Clear()ed to prevent emission in case of tentative parsing? | 
|  | 661 | return DiagResult(diag::err_this_captured_by_reference); | 
|  | 662 | } else { | 
|  | 663 | return DiagResult(diag::err_expected_capture); | 
|  | 664 | } | 
|  | 665 | } | 
|  | 666 |  | 
|  | 667 | Intro.addCapture(Kind, Loc, Id); | 
|  | 668 | } | 
|  | 669 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 670 | T.consumeClose(); | 
|  | 671 | Intro.Range.setEnd(T.getCloseLocation()); | 
| Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 672 |  | 
|  | 673 | return DiagResult(); | 
|  | 674 | } | 
|  | 675 |  | 
|  | 676 | /// TryParseLambdaExpression - Tentatively parse a lambda introducer. | 
|  | 677 | /// | 
|  | 678 | /// Returns true if it hit something unexpected. | 
|  | 679 | bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) { | 
|  | 680 | TentativeParsingAction PA(*this); | 
|  | 681 |  | 
|  | 682 | llvm::Optional<unsigned> DiagID(ParseLambdaIntroducer(Intro)); | 
|  | 683 |  | 
|  | 684 | if (DiagID) { | 
|  | 685 | PA.Revert(); | 
|  | 686 | return true; | 
|  | 687 | } | 
|  | 688 |  | 
|  | 689 | PA.Commit(); | 
|  | 690 | return false; | 
|  | 691 | } | 
|  | 692 |  | 
|  | 693 | /// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda | 
|  | 694 | /// expression. | 
|  | 695 | ExprResult Parser::ParseLambdaExpressionAfterIntroducer( | 
|  | 696 | LambdaIntroducer &Intro) { | 
| Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 697 | Diag(Intro.Range.getBegin(), diag::warn_cxx98_compat_lambda); | 
|  | 698 |  | 
| Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 699 | // Parse lambda-declarator[opt]. | 
|  | 700 | DeclSpec DS(AttrFactory); | 
|  | 701 | Declarator D(DS, Declarator::PrototypeContext); | 
|  | 702 |  | 
|  | 703 | if (Tok.is(tok::l_paren)) { | 
|  | 704 | ParseScope PrototypeScope(this, | 
|  | 705 | Scope::FunctionPrototypeScope | | 
|  | 706 | Scope::DeclScope); | 
|  | 707 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 708 | SourceLocation DeclLoc, DeclEndLoc; | 
|  | 709 | BalancedDelimiterTracker T(*this, tok::l_paren); | 
|  | 710 | T.consumeOpen(); | 
|  | 711 | DeclLoc = T.getOpenLocation(); | 
| Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 712 |  | 
|  | 713 | // Parse parameter-declaration-clause. | 
|  | 714 | ParsedAttributes Attr(AttrFactory); | 
|  | 715 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; | 
|  | 716 | SourceLocation EllipsisLoc; | 
|  | 717 |  | 
|  | 718 | if (Tok.isNot(tok::r_paren)) | 
|  | 719 | ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc); | 
|  | 720 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 721 | T.consumeClose(); | 
|  | 722 | DeclEndLoc = T.getCloseLocation(); | 
| Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 723 |  | 
|  | 724 | // Parse 'mutable'[opt]. | 
|  | 725 | SourceLocation MutableLoc; | 
|  | 726 | if (Tok.is(tok::kw_mutable)) { | 
|  | 727 | MutableLoc = ConsumeToken(); | 
|  | 728 | DeclEndLoc = MutableLoc; | 
|  | 729 | } | 
|  | 730 |  | 
|  | 731 | // Parse exception-specification[opt]. | 
|  | 732 | ExceptionSpecificationType ESpecType = EST_None; | 
|  | 733 | SourceRange ESpecRange; | 
|  | 734 | llvm::SmallVector<ParsedType, 2> DynamicExceptions; | 
|  | 735 | llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges; | 
|  | 736 | ExprResult NoexceptExpr; | 
|  | 737 | ESpecType = MaybeParseExceptionSpecification(ESpecRange, | 
|  | 738 | DynamicExceptions, | 
|  | 739 | DynamicExceptionRanges, | 
|  | 740 | NoexceptExpr); | 
|  | 741 |  | 
|  | 742 | if (ESpecType != EST_None) | 
|  | 743 | DeclEndLoc = ESpecRange.getEnd(); | 
|  | 744 |  | 
|  | 745 | // Parse attribute-specifier[opt]. | 
|  | 746 | MaybeParseCXX0XAttributes(Attr, &DeclEndLoc); | 
|  | 747 |  | 
|  | 748 | // Parse trailing-return-type[opt]. | 
|  | 749 | ParsedType TrailingReturnType; | 
|  | 750 | if (Tok.is(tok::arrow)) { | 
|  | 751 | SourceRange Range; | 
|  | 752 | TrailingReturnType = ParseTrailingReturnType(Range).get(); | 
|  | 753 | if (Range.getEnd().isValid()) | 
|  | 754 | DeclEndLoc = Range.getEnd(); | 
|  | 755 | } | 
|  | 756 |  | 
|  | 757 | PrototypeScope.Exit(); | 
|  | 758 |  | 
|  | 759 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*hasProto=*/true, | 
|  | 760 | /*isVariadic=*/EllipsisLoc.isValid(), | 
|  | 761 | EllipsisLoc, | 
|  | 762 | ParamInfo.data(), ParamInfo.size(), | 
|  | 763 | DS.getTypeQualifiers(), | 
|  | 764 | /*RefQualifierIsLValueRef=*/true, | 
|  | 765 | /*RefQualifierLoc=*/SourceLocation(), | 
| Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 766 | /*ConstQualifierLoc=*/SourceLocation(), | 
|  | 767 | /*VolatileQualifierLoc=*/SourceLocation(), | 
| Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 768 | MutableLoc, | 
|  | 769 | ESpecType, ESpecRange.getBegin(), | 
|  | 770 | DynamicExceptions.data(), | 
|  | 771 | DynamicExceptionRanges.data(), | 
|  | 772 | DynamicExceptions.size(), | 
|  | 773 | NoexceptExpr.isUsable() ? | 
|  | 774 | NoexceptExpr.get() : 0, | 
|  | 775 | DeclLoc, DeclEndLoc, D, | 
|  | 776 | TrailingReturnType), | 
|  | 777 | Attr, DeclEndLoc); | 
|  | 778 | } | 
|  | 779 |  | 
|  | 780 | // Parse compound-statement. | 
|  | 781 | if (Tok.is(tok::l_brace)) { | 
|  | 782 | // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using | 
|  | 783 | // it. | 
|  | 784 | ParseScope BodyScope(this, Scope::BlockScope | Scope::FnScope | | 
|  | 785 | Scope::BreakScope | Scope::ContinueScope | | 
|  | 786 | Scope::DeclScope); | 
|  | 787 |  | 
|  | 788 | StmtResult Stmt(ParseCompoundStatementBody()); | 
|  | 789 |  | 
|  | 790 | BodyScope.Exit(); | 
|  | 791 | } else { | 
|  | 792 | Diag(Tok, diag::err_expected_lambda_body); | 
|  | 793 | } | 
|  | 794 |  | 
|  | 795 | return ExprEmpty(); | 
|  | 796 | } | 
|  | 797 |  | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 798 | /// ParseCXXCasts - This handles the various ways to cast expressions to another | 
|  | 799 | /// type. | 
|  | 800 | /// | 
|  | 801 | ///       postfix-expression: [C++ 5.2p1] | 
|  | 802 | ///         'dynamic_cast' '<' type-name '>' '(' expression ')' | 
|  | 803 | ///         'static_cast' '<' type-name '>' '(' expression ')' | 
|  | 804 | ///         'reinterpret_cast' '<' type-name '>' '(' expression ')' | 
|  | 805 | ///         'const_cast' '<' type-name '>' '(' expression ')' | 
|  | 806 | /// | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 807 | ExprResult Parser::ParseCXXCasts() { | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 808 | tok::TokenKind Kind = Tok.getKind(); | 
|  | 809 | const char *CastName = 0;     // For error messages | 
|  | 810 |  | 
|  | 811 | switch (Kind) { | 
| David Blaikie | eb2d1f1 | 2011-09-23 20:26:49 +0000 | [diff] [blame] | 812 | default: llvm_unreachable("Unknown C++ cast!"); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 813 | case tok::kw_const_cast:       CastName = "const_cast";       break; | 
|  | 814 | case tok::kw_dynamic_cast:     CastName = "dynamic_cast";     break; | 
|  | 815 | case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break; | 
|  | 816 | case tok::kw_static_cast:      CastName = "static_cast";      break; | 
|  | 817 | } | 
|  | 818 |  | 
|  | 819 | SourceLocation OpLoc = ConsumeToken(); | 
|  | 820 | SourceLocation LAngleBracketLoc = Tok.getLocation(); | 
|  | 821 |  | 
| Richard Smith | ea698b3 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 822 | // Check for "<::" which is parsed as "[:".  If found, fix token stream, | 
|  | 823 | // diagnose error, suggest fix, and recover parsing. | 
|  | 824 | Token Next = NextToken(); | 
|  | 825 | if (Tok.is(tok::l_square) && Tok.getLength() == 2 && Next.is(tok::colon) && | 
|  | 826 | AreTokensAdjacent(PP, Tok, Next)) | 
|  | 827 | FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true); | 
|  | 828 |  | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 829 | if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName)) | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 830 | return ExprError(); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 831 |  | 
| Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 832 | // Parse the common declaration-specifiers piece. | 
|  | 833 | DeclSpec DS(AttrFactory); | 
|  | 834 | ParseSpecifierQualifierList(DS); | 
|  | 835 |  | 
|  | 836 | // Parse the abstract-declarator, if present. | 
|  | 837 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); | 
|  | 838 | ParseDeclarator(DeclaratorInfo); | 
|  | 839 |  | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 840 | SourceLocation RAngleBracketLoc = Tok.getLocation(); | 
|  | 841 |  | 
| Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 842 | if (ExpectAndConsume(tok::greater, diag::err_expected_greater)) | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 843 | return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<"); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 844 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 845 | SourceLocation LParenLoc, RParenLoc; | 
|  | 846 | BalancedDelimiterTracker T(*this, tok::l_paren); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 847 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 848 | if (T.expectAndConsume(diag::err_expected_lparen_after, CastName)) | 
| Argyrios Kyrtzidis | 21e7ad2 | 2009-05-22 10:23:16 +0000 | [diff] [blame] | 849 | return ExprError(); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 850 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 851 | ExprResult Result = ParseExpression(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 |  | 
| Argyrios Kyrtzidis | 21e7ad2 | 2009-05-22 10:23:16 +0000 | [diff] [blame] | 853 | // Match the ')'. | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 854 | T.consumeClose(); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 855 |  | 
| Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 856 | if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType()) | 
| Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 857 | Result = Actions.ActOnCXXNamedCast(OpLoc, Kind, | 
| Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 858 | LAngleBracketLoc, DeclaratorInfo, | 
| Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 859 | RAngleBracketLoc, | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 860 | T.getOpenLocation(), Result.take(), | 
|  | 861 | T.getCloseLocation()); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 862 |  | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 863 | return move(Result); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 864 | } | 
|  | 865 |  | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 866 | /// ParseCXXTypeid - This handles the C++ typeid expression. | 
|  | 867 | /// | 
|  | 868 | ///       postfix-expression: [C++ 5.2p1] | 
|  | 869 | ///         'typeid' '(' expression ')' | 
|  | 870 | ///         'typeid' '(' type-id ')' | 
|  | 871 | /// | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 872 | ExprResult Parser::ParseCXXTypeid() { | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 873 | assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!"); | 
|  | 874 |  | 
|  | 875 | SourceLocation OpLoc = ConsumeToken(); | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 876 | SourceLocation LParenLoc, RParenLoc; | 
|  | 877 | BalancedDelimiterTracker T(*this, tok::l_paren); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 878 |  | 
|  | 879 | // typeid expressions are always parenthesized. | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 880 | if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid")) | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 881 | return ExprError(); | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 882 | LParenLoc = T.getOpenLocation(); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 883 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 884 | ExprResult Result; | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 885 |  | 
|  | 886 | if (isTypeIdInParens()) { | 
| Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 887 | TypeResult Ty = ParseTypeName(); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 888 |  | 
|  | 889 | // Match the ')'. | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 890 | T.consumeClose(); | 
|  | 891 | RParenLoc = T.getCloseLocation(); | 
| Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 892 | if (Ty.isInvalid() || RParenLoc.isInvalid()) | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 893 | return ExprError(); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 894 |  | 
|  | 895 | Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true, | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 896 | Ty.get().getAsOpaquePtr(), RParenLoc); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 897 | } else { | 
| Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 898 | // C++0x [expr.typeid]p3: | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | //   When typeid is applied to an expression other than an lvalue of a | 
|  | 900 | //   polymorphic class type [...] The expression is an unevaluated | 
| Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 901 | //   operand (Clause 5). | 
|  | 902 | // | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | // Note that we can't tell whether the expression is an lvalue of a | 
| Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 904 | // polymorphic class type until after we've parsed the expression, so | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 905 | // we the expression is potentially potentially evaluated. | 
|  | 906 | EnterExpressionEvaluationContext Unevaluated(Actions, | 
| John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 907 | Sema::PotentiallyPotentiallyEvaluated); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 908 | Result = ParseExpression(); | 
|  | 909 |  | 
|  | 910 | // Match the ')'. | 
| Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 911 | if (Result.isInvalid()) | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 912 | SkipUntil(tok::r_paren); | 
|  | 913 | else { | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 914 | T.consumeClose(); | 
|  | 915 | RParenLoc = T.getCloseLocation(); | 
| Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 916 | if (RParenLoc.isInvalid()) | 
|  | 917 | return ExprError(); | 
| Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 918 |  | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 919 | Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false, | 
| Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 920 | Result.release(), RParenLoc); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 921 | } | 
|  | 922 | } | 
|  | 923 |  | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 924 | return move(Result); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 925 | } | 
|  | 926 |  | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 927 | /// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression. | 
|  | 928 | /// | 
|  | 929 | ///         '__uuidof' '(' expression ')' | 
|  | 930 | ///         '__uuidof' '(' type-id ')' | 
|  | 931 | /// | 
|  | 932 | ExprResult Parser::ParseCXXUuidof() { | 
|  | 933 | assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!"); | 
|  | 934 |  | 
|  | 935 | SourceLocation OpLoc = ConsumeToken(); | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 936 | BalancedDelimiterTracker T(*this, tok::l_paren); | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 937 |  | 
|  | 938 | // __uuidof expressions are always parenthesized. | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 939 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof")) | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 940 | return ExprError(); | 
|  | 941 |  | 
|  | 942 | ExprResult Result; | 
|  | 943 |  | 
|  | 944 | if (isTypeIdInParens()) { | 
|  | 945 | TypeResult Ty = ParseTypeName(); | 
|  | 946 |  | 
|  | 947 | // Match the ')'. | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 948 | T.consumeClose(); | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 949 |  | 
|  | 950 | if (Ty.isInvalid()) | 
|  | 951 | return ExprError(); | 
|  | 952 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 953 | Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true, | 
|  | 954 | Ty.get().getAsOpaquePtr(), | 
|  | 955 | T.getCloseLocation()); | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 956 | } else { | 
|  | 957 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); | 
|  | 958 | Result = ParseExpression(); | 
|  | 959 |  | 
|  | 960 | // Match the ')'. | 
|  | 961 | if (Result.isInvalid()) | 
|  | 962 | SkipUntil(tok::r_paren); | 
|  | 963 | else { | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 964 | T.consumeClose(); | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 965 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 966 | Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), | 
|  | 967 | /*isType=*/false, | 
|  | 968 | Result.release(), T.getCloseLocation()); | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 969 | } | 
|  | 970 | } | 
|  | 971 |  | 
|  | 972 | return move(Result); | 
|  | 973 | } | 
|  | 974 |  | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 975 | /// \brief Parse a C++ pseudo-destructor expression after the base, | 
|  | 976 | /// . or -> operator, and nested-name-specifier have already been | 
|  | 977 | /// parsed. | 
|  | 978 | /// | 
|  | 979 | ///       postfix-expression: [C++ 5.2] | 
|  | 980 | ///         postfix-expression . pseudo-destructor-name | 
|  | 981 | ///         postfix-expression -> pseudo-destructor-name | 
|  | 982 | /// | 
|  | 983 | ///       pseudo-destructor-name: | 
|  | 984 | ///         ::[opt] nested-name-specifier[opt] type-name :: ~type-name | 
|  | 985 | ///         ::[opt] nested-name-specifier template simple-template-id :: | 
|  | 986 | ///                 ~type-name | 
|  | 987 | ///         ::[opt] nested-name-specifier[opt] ~type-name | 
|  | 988 | /// | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 989 | ExprResult | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 990 | Parser::ParseCXXPseudoDestructor(ExprArg Base, SourceLocation OpLoc, | 
|  | 991 | tok::TokenKind OpKind, | 
|  | 992 | CXXScopeSpec &SS, | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 993 | ParsedType ObjectType) { | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 994 | // We're parsing either a pseudo-destructor-name or a dependent | 
|  | 995 | // member access that has the same form as a | 
|  | 996 | // pseudo-destructor-name. We parse both in the same way and let | 
|  | 997 | // the action model sort them out. | 
|  | 998 | // | 
|  | 999 | // Note that the ::[opt] nested-name-specifier[opt] has already | 
|  | 1000 | // been parsed, and if there was a simple-template-id, it has | 
|  | 1001 | // been coalesced into a template-id annotation token. | 
|  | 1002 | UnqualifiedId FirstTypeName; | 
|  | 1003 | SourceLocation CCLoc; | 
|  | 1004 | if (Tok.is(tok::identifier)) { | 
|  | 1005 | FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); | 
|  | 1006 | ConsumeToken(); | 
|  | 1007 | assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail"); | 
|  | 1008 | CCLoc = ConsumeToken(); | 
|  | 1009 | } else if (Tok.is(tok::annot_template_id)) { | 
|  | 1010 | FirstTypeName.setTemplateId( | 
|  | 1011 | (TemplateIdAnnotation *)Tok.getAnnotationValue()); | 
|  | 1012 | ConsumeToken(); | 
|  | 1013 | assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail"); | 
|  | 1014 | CCLoc = ConsumeToken(); | 
|  | 1015 | } else { | 
|  | 1016 | FirstTypeName.setIdentifier(0, SourceLocation()); | 
|  | 1017 | } | 
|  | 1018 |  | 
|  | 1019 | // Parse the tilde. | 
|  | 1020 | assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail"); | 
|  | 1021 | SourceLocation TildeLoc = ConsumeToken(); | 
| David Blaikie | 91ec789 | 2011-12-16 16:03:09 +0000 | [diff] [blame^] | 1022 |  | 
|  | 1023 | if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) { | 
|  | 1024 | DeclSpec DS(AttrFactory); | 
|  | 1025 | SourceLocation EndLoc = ParseDecltypeSpecifier(DS); | 
|  | 1026 | if (DS.getTypeSpecType() == TST_error) | 
|  | 1027 | return ExprError(); | 
|  | 1028 | return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc, | 
|  | 1029 | OpKind, TildeLoc, DS, | 
|  | 1030 | Tok.is(tok::l_paren)); | 
|  | 1031 | } | 
|  | 1032 |  | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1033 | if (!Tok.is(tok::identifier)) { | 
|  | 1034 | Diag(Tok, diag::err_destructor_tilde_identifier); | 
|  | 1035 | return ExprError(); | 
|  | 1036 | } | 
|  | 1037 |  | 
|  | 1038 | // Parse the second type. | 
|  | 1039 | UnqualifiedId SecondTypeName; | 
|  | 1040 | IdentifierInfo *Name = Tok.getIdentifierInfo(); | 
|  | 1041 | SourceLocation NameLoc = ConsumeToken(); | 
|  | 1042 | SecondTypeName.setIdentifier(Name, NameLoc); | 
|  | 1043 |  | 
|  | 1044 | // If there is a '<', the second type name is a template-id. Parse | 
|  | 1045 | // it as such. | 
|  | 1046 | if (Tok.is(tok::less) && | 
|  | 1047 | ParseUnqualifiedIdTemplateId(SS, Name, NameLoc, false, ObjectType, | 
| Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1048 | SecondTypeName, /*AssumeTemplateName=*/true, | 
|  | 1049 | /*TemplateKWLoc*/SourceLocation())) | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1050 | return ExprError(); | 
|  | 1051 |  | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1052 | return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, | 
|  | 1053 | OpLoc, OpKind, | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1054 | SS, FirstTypeName, CCLoc, | 
|  | 1055 | TildeLoc, SecondTypeName, | 
|  | 1056 | Tok.is(tok::l_paren)); | 
|  | 1057 | } | 
|  | 1058 |  | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1059 | /// ParseCXXBoolLiteral - This handles the C++ Boolean literals. | 
|  | 1060 | /// | 
|  | 1061 | ///       boolean-literal: [C++ 2.13.5] | 
|  | 1062 | ///         'true' | 
|  | 1063 | ///         'false' | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1064 | ExprResult Parser::ParseCXXBoolLiteral() { | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1065 | tok::TokenKind Kind = Tok.getKind(); | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1066 | return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1067 | } | 
| Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 1068 |  | 
|  | 1069 | /// ParseThrowExpression - This handles the C++ throw expression. | 
|  | 1070 | /// | 
|  | 1071 | ///       throw-expression: [C++ 15] | 
|  | 1072 | ///         'throw' assignment-expression[opt] | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1073 | ExprResult Parser::ParseThrowExpression() { | 
| Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 1074 | assert(Tok.is(tok::kw_throw) && "Not throw!"); | 
| Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 1075 | SourceLocation ThrowLoc = ConsumeToken();           // Eat the throw token. | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1076 |  | 
| Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 1077 | // If the current token isn't the start of an assignment-expression, | 
|  | 1078 | // then the expression is not present.  This handles things like: | 
|  | 1079 | //   "C ? throw : (void)42", which is crazy but legal. | 
|  | 1080 | switch (Tok.getKind()) {  // FIXME: move this predicate somewhere common. | 
|  | 1081 | case tok::semi: | 
|  | 1082 | case tok::r_paren: | 
|  | 1083 | case tok::r_square: | 
|  | 1084 | case tok::r_brace: | 
|  | 1085 | case tok::colon: | 
|  | 1086 | case tok::comma: | 
| Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 1087 | return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, 0); | 
| Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 1088 |  | 
| Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 1089 | default: | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1090 | ExprResult Expr(ParseAssignmentExpression()); | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1091 | if (Expr.isInvalid()) return move(Expr); | 
| Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 1092 | return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.take()); | 
| Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 1093 | } | 
| Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 1094 | } | 
| Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1095 |  | 
|  | 1096 | /// ParseCXXThis - This handles the C++ 'this' pointer. | 
|  | 1097 | /// | 
|  | 1098 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this is | 
|  | 1099 | /// a non-lvalue expression whose value is the address of the object for which | 
|  | 1100 | /// the function is called. | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1101 | ExprResult Parser::ParseCXXThis() { | 
| Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1102 | assert(Tok.is(tok::kw_this) && "Not 'this'!"); | 
|  | 1103 | SourceLocation ThisLoc = ConsumeToken(); | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1104 | return Actions.ActOnCXXThis(ThisLoc); | 
| Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1105 | } | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1106 |  | 
|  | 1107 | /// ParseCXXTypeConstructExpression - Parse construction of a specified type. | 
|  | 1108 | /// Can be interpreted either as function-style casting ("int(x)") | 
|  | 1109 | /// or class type construction ("ClassType(x,y,z)") | 
|  | 1110 | /// or creation of a value-initialized type ("int()"). | 
| Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1111 | /// See [C++ 5.2.3]. | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1112 | /// | 
|  | 1113 | ///       postfix-expression: [C++ 5.2p1] | 
| Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1114 | ///         simple-type-specifier '(' expression-list[opt] ')' | 
|  | 1115 | /// [C++0x] simple-type-specifier braced-init-list | 
|  | 1116 | ///         typename-specifier '(' expression-list[opt] ')' | 
|  | 1117 | /// [C++0x] typename-specifier braced-init-list | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1118 | /// | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1119 | ExprResult | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1120 | Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) { | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1121 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1122 | ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get(); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1123 |  | 
| Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1124 | assert((Tok.is(tok::l_paren) || | 
|  | 1125 | (getLang().CPlusPlus0x && Tok.is(tok::l_brace))) | 
|  | 1126 | && "Expected '(' or '{'!"); | 
| Douglas Gregor | bc61bd8 | 2011-01-11 00:33:19 +0000 | [diff] [blame] | 1127 |  | 
| Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1128 | if (Tok.is(tok::l_brace)) { | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1129 |  | 
| Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1130 | // FIXME: Convert to a proper type construct expression. | 
|  | 1131 | return ParseBraceInitializer(); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1132 |  | 
| Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1133 | } else { | 
|  | 1134 | GreaterThanIsOperatorScope G(GreaterThanIsOperator, true); | 
|  | 1135 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1136 | BalancedDelimiterTracker T(*this, tok::l_paren); | 
|  | 1137 | T.consumeOpen(); | 
| Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1138 |  | 
|  | 1139 | ExprVector Exprs(Actions); | 
|  | 1140 | CommaLocsTy CommaLocs; | 
|  | 1141 |  | 
|  | 1142 | if (Tok.isNot(tok::r_paren)) { | 
|  | 1143 | if (ParseExpressionList(Exprs, CommaLocs)) { | 
|  | 1144 | SkipUntil(tok::r_paren); | 
|  | 1145 | return ExprError(); | 
|  | 1146 | } | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1147 | } | 
| Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1148 |  | 
|  | 1149 | // Match the ')'. | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1150 | T.consumeClose(); | 
| Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1151 |  | 
|  | 1152 | // TypeRep could be null, if it references an invalid typedef. | 
|  | 1153 | if (!TypeRep) | 
|  | 1154 | return ExprError(); | 
|  | 1155 |  | 
|  | 1156 | assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&& | 
|  | 1157 | "Unexpected number of commas!"); | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1158 | return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(), | 
|  | 1159 | move_arg(Exprs), | 
|  | 1160 | T.getCloseLocation()); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1161 | } | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1162 | } | 
|  | 1163 |  | 
| Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1164 | /// ParseCXXCondition - if/switch/while condition expression. | 
| Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1165 | /// | 
|  | 1166 | ///       condition: | 
|  | 1167 | ///         expression | 
|  | 1168 | ///         type-specifier-seq declarator '=' assignment-expression | 
|  | 1169 | /// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt] | 
|  | 1170 | ///             '=' assignment-expression | 
|  | 1171 | /// | 
| Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1172 | /// \param ExprResult if the condition was parsed as an expression, the | 
|  | 1173 | /// parsed expression. | 
|  | 1174 | /// | 
|  | 1175 | /// \param DeclResult if the condition was parsed as a declaration, the | 
|  | 1176 | /// parsed declaration. | 
|  | 1177 | /// | 
| Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1178 | /// \param Loc The location of the start of the statement that requires this | 
|  | 1179 | /// condition, e.g., the "for" in a for loop. | 
|  | 1180 | /// | 
|  | 1181 | /// \param ConvertToBoolean Whether the condition expression should be | 
|  | 1182 | /// converted to a boolean value. | 
|  | 1183 | /// | 
| Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1184 | /// \returns true if there was a parsing, false otherwise. | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1185 | bool Parser::ParseCXXCondition(ExprResult &ExprOut, | 
|  | 1186 | Decl *&DeclOut, | 
| Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1187 | SourceLocation Loc, | 
|  | 1188 | bool ConvertToBoolean) { | 
| Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1189 | if (Tok.is(tok::code_completion)) { | 
| John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1190 | Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition); | 
| Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1191 | cutOffParsing(); | 
|  | 1192 | return true; | 
| Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1193 | } | 
|  | 1194 |  | 
| Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1195 | if (!isCXXConditionDeclaration()) { | 
| Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1196 | // Parse the expression. | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1197 | ExprOut = ParseExpression(); // expression | 
|  | 1198 | DeclOut = 0; | 
|  | 1199 | if (ExprOut.isInvalid()) | 
| Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1200 | return true; | 
|  | 1201 |  | 
|  | 1202 | // If required, convert to a boolean value. | 
|  | 1203 | if (ConvertToBoolean) | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1204 | ExprOut | 
|  | 1205 | = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprOut.get()); | 
|  | 1206 | return ExprOut.isInvalid(); | 
| Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1207 | } | 
| Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1208 |  | 
|  | 1209 | // type-specifier-seq | 
| John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1210 | DeclSpec DS(AttrFactory); | 
| Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1211 | ParseSpecifierQualifierList(DS); | 
|  | 1212 |  | 
|  | 1213 | // declarator | 
|  | 1214 | Declarator DeclaratorInfo(DS, Declarator::ConditionContext); | 
|  | 1215 | ParseDeclarator(DeclaratorInfo); | 
|  | 1216 |  | 
|  | 1217 | // simple-asm-expr[opt] | 
|  | 1218 | if (Tok.is(tok::kw_asm)) { | 
| Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1219 | SourceLocation Loc; | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1220 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); | 
| Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1221 | if (AsmLabel.isInvalid()) { | 
| Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1222 | SkipUntil(tok::semi); | 
| Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1223 | return true; | 
| Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1224 | } | 
| Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1225 | DeclaratorInfo.setAsmLabel(AsmLabel.release()); | 
| Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1226 | DeclaratorInfo.SetRangeEnd(Loc); | 
| Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1227 | } | 
|  | 1228 |  | 
|  | 1229 | // If attributes are present, parse them. | 
| John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1230 | MaybeParseGNUAttributes(DeclaratorInfo); | 
| Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1231 |  | 
| Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1232 | // Type-check the declaration itself. | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1233 | DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(), | 
| John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1234 | DeclaratorInfo); | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1235 | DeclOut = Dcl.get(); | 
|  | 1236 | ExprOut = ExprError(); | 
| Argyrios Kyrtzidis | a6eb5f8 | 2010-10-08 02:39:23 +0000 | [diff] [blame] | 1237 |  | 
| Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1238 | // '=' assignment-expression | 
| Argyrios Kyrtzidis | a6eb5f8 | 2010-10-08 02:39:23 +0000 | [diff] [blame] | 1239 | if (isTokenEqualOrMistypedEqualEqual( | 
|  | 1240 | diag::err_invalid_equalequal_after_declarator)) { | 
| Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1241 | ConsumeToken(); | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1242 | ExprResult AssignExpr(ParseAssignmentExpression()); | 
| Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1243 | if (!AssignExpr.isInvalid()) | 
| Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1244 | Actions.AddInitializerToDecl(DeclOut, AssignExpr.take(), false, | 
|  | 1245 | DS.getTypeSpecType() == DeclSpec::TST_auto); | 
| Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1246 | } else { | 
|  | 1247 | // FIXME: C++0x allows a braced-init-list | 
|  | 1248 | Diag(Tok, diag::err_expected_equal_after_declarator); | 
|  | 1249 | } | 
|  | 1250 |  | 
| Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1251 | // FIXME: Build a reference to this declaration? Convert it to bool? | 
|  | 1252 | // (This is currently handled by Sema). | 
| Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1253 |  | 
|  | 1254 | Actions.FinalizeDeclaration(DeclOut); | 
| Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1255 |  | 
| Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1256 | return false; | 
| Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1257 | } | 
|  | 1258 |  | 
| Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 1259 | /// \brief Determine whether the current token starts a C++ | 
|  | 1260 | /// simple-type-specifier. | 
|  | 1261 | bool Parser::isCXXSimpleTypeSpecifier() const { | 
|  | 1262 | switch (Tok.getKind()) { | 
|  | 1263 | case tok::annot_typename: | 
|  | 1264 | case tok::kw_short: | 
|  | 1265 | case tok::kw_long: | 
| Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 1266 | case tok::kw___int64: | 
| Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 1267 | case tok::kw_signed: | 
|  | 1268 | case tok::kw_unsigned: | 
|  | 1269 | case tok::kw_void: | 
|  | 1270 | case tok::kw_char: | 
|  | 1271 | case tok::kw_int: | 
| Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1272 | case tok::kw_half: | 
| Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 1273 | case tok::kw_float: | 
|  | 1274 | case tok::kw_double: | 
|  | 1275 | case tok::kw_wchar_t: | 
|  | 1276 | case tok::kw_char16_t: | 
|  | 1277 | case tok::kw_char32_t: | 
|  | 1278 | case tok::kw_bool: | 
| Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 1279 | case tok::kw_decltype: | 
| Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 1280 | case tok::kw_typeof: | 
| Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 1281 | case tok::kw___underlying_type: | 
| Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 1282 | return true; | 
|  | 1283 |  | 
|  | 1284 | default: | 
|  | 1285 | break; | 
|  | 1286 | } | 
|  | 1287 |  | 
|  | 1288 | return false; | 
|  | 1289 | } | 
|  | 1290 |  | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1291 | /// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers. | 
|  | 1292 | /// This should only be called when the current token is known to be part of | 
|  | 1293 | /// simple-type-specifier. | 
|  | 1294 | /// | 
|  | 1295 | ///       simple-type-specifier: | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1296 | ///         '::'[opt] nested-name-specifier[opt] type-name | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1297 | ///         '::'[opt] nested-name-specifier 'template' simple-template-id [TODO] | 
|  | 1298 | ///         char | 
|  | 1299 | ///         wchar_t | 
|  | 1300 | ///         bool | 
|  | 1301 | ///         short | 
|  | 1302 | ///         int | 
|  | 1303 | ///         long | 
|  | 1304 | ///         signed | 
|  | 1305 | ///         unsigned | 
|  | 1306 | ///         float | 
|  | 1307 | ///         double | 
|  | 1308 | ///         void | 
|  | 1309 | /// [GNU]   typeof-specifier | 
|  | 1310 | /// [C++0x] auto               [TODO] | 
|  | 1311 | /// | 
|  | 1312 | ///       type-name: | 
|  | 1313 | ///         class-name | 
|  | 1314 | ///         enum-name | 
|  | 1315 | ///         typedef-name | 
|  | 1316 | /// | 
|  | 1317 | void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) { | 
|  | 1318 | DS.SetRangeStart(Tok.getLocation()); | 
|  | 1319 | const char *PrevSpec; | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1320 | unsigned DiagID; | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1321 | SourceLocation Loc = Tok.getLocation(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 |  | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1323 | switch (Tok.getKind()) { | 
| Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 1324 | case tok::identifier:   // foo::bar | 
|  | 1325 | case tok::coloncolon:   // ::foo::bar | 
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1326 | llvm_unreachable("Annotation token should already be formed!"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1327 | default: | 
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1328 | llvm_unreachable("Not a simple-type-specifier token!"); | 
| Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 1329 |  | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1330 | // type-name | 
| Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 1331 | case tok::annot_typename: { | 
| Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1332 | if (getTypeAnnotation(Tok)) | 
|  | 1333 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, | 
|  | 1334 | getTypeAnnotation(Tok)); | 
|  | 1335 | else | 
|  | 1336 | DS.SetTypeSpecError(); | 
| Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1337 |  | 
|  | 1338 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); | 
|  | 1339 | ConsumeToken(); | 
|  | 1340 |  | 
|  | 1341 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' | 
|  | 1342 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an | 
|  | 1343 | // Objective-C interface.  If we don't have Objective-C or a '<', this is | 
|  | 1344 | // just a normal reference to a typedef name. | 
|  | 1345 | if (Tok.is(tok::less) && getLang().ObjC1) | 
|  | 1346 | ParseObjCProtocolQualifiers(DS); | 
|  | 1347 |  | 
|  | 1348 | DS.Finish(Diags, PP); | 
|  | 1349 | return; | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1350 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 |  | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1352 | // builtin types | 
|  | 1353 | case tok::kw_short: | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1354 | DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1355 | break; | 
|  | 1356 | case tok::kw_long: | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1357 | DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1358 | break; | 
| Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 1359 | case tok::kw___int64: | 
|  | 1360 | DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID); | 
|  | 1361 | break; | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1362 | case tok::kw_signed: | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1363 | DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1364 | break; | 
|  | 1365 | case tok::kw_unsigned: | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1366 | DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1367 | break; | 
|  | 1368 | case tok::kw_void: | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1369 | DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1370 | break; | 
|  | 1371 | case tok::kw_char: | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1372 | DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1373 | break; | 
|  | 1374 | case tok::kw_int: | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1375 | DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1376 | break; | 
| Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1377 | case tok::kw_half: | 
|  | 1378 | DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID); | 
|  | 1379 | break; | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1380 | case tok::kw_float: | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1381 | DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1382 | break; | 
|  | 1383 | case tok::kw_double: | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1384 | DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1385 | break; | 
|  | 1386 | case tok::kw_wchar_t: | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1387 | DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1388 | break; | 
| Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1389 | case tok::kw_char16_t: | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1390 | DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID); | 
| Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1391 | break; | 
|  | 1392 | case tok::kw_char32_t: | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1393 | DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID); | 
| Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1394 | break; | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1395 | case tok::kw_bool: | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1396 | DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1397 | break; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1398 |  | 
| Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 1399 | // FIXME: C++0x decltype support. | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1400 | // GNU typeof support. | 
|  | 1401 | case tok::kw_typeof: | 
|  | 1402 | ParseTypeofSpecifier(DS); | 
| Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1403 | DS.Finish(Diags, PP); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1404 | return; | 
|  | 1405 | } | 
| Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 1406 | if (Tok.is(tok::annot_typename)) | 
| Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1407 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); | 
|  | 1408 | else | 
|  | 1409 | DS.SetRangeEnd(Tok.getLocation()); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1410 | ConsumeToken(); | 
| Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1411 | DS.Finish(Diags, PP); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1412 | } | 
| Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1413 |  | 
| Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1414 | /// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++ | 
|  | 1415 | /// [dcl.name]), which is a non-empty sequence of type-specifiers, | 
|  | 1416 | /// e.g., "const short int". Note that the DeclSpec is *not* finished | 
|  | 1417 | /// by parsing the type-specifier-seq, because these sequences are | 
|  | 1418 | /// typically followed by some form of declarator. Returns true and | 
|  | 1419 | /// emits diagnostics if this is not a type-specifier-seq, false | 
|  | 1420 | /// otherwise. | 
|  | 1421 | /// | 
|  | 1422 | ///   type-specifier-seq: [C++ 8.1] | 
|  | 1423 | ///     type-specifier type-specifier-seq[opt] | 
|  | 1424 | /// | 
|  | 1425 | bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) { | 
|  | 1426 | DS.SetRangeStart(Tok.getLocation()); | 
|  | 1427 | const char *PrevSpec = 0; | 
| John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1428 | unsigned DiagID; | 
|  | 1429 | bool isInvalid = 0; | 
| Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1430 |  | 
|  | 1431 | // Parse one or more of the type specifiers. | 
| Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 1432 | if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, | 
|  | 1433 | ParsedTemplateInfo(), /*SuppressDeclarations*/true)) { | 
| Nick Lewycky | 9fa8e56 | 2010-11-03 17:52:57 +0000 | [diff] [blame] | 1434 | Diag(Tok, diag::err_expected_type); | 
| Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1435 | return true; | 
|  | 1436 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1437 |  | 
| Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 1438 | while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, | 
|  | 1439 | ParsedTemplateInfo(), /*SuppressDeclarations*/true)) | 
|  | 1440 | {} | 
| Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1441 |  | 
| Douglas Gregor | 396a9f2 | 2010-02-24 23:13:13 +0000 | [diff] [blame] | 1442 | DS.Finish(Diags, PP); | 
| Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1443 | return false; | 
|  | 1444 | } | 
|  | 1445 |  | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1446 | /// \brief Finish parsing a C++ unqualified-id that is a template-id of | 
|  | 1447 | /// some form. | 
|  | 1448 | /// | 
|  | 1449 | /// This routine is invoked when a '<' is encountered after an identifier or | 
|  | 1450 | /// operator-function-id is parsed by \c ParseUnqualifiedId() to determine | 
|  | 1451 | /// whether the unqualified-id is actually a template-id. This routine will | 
|  | 1452 | /// then parse the template arguments and form the appropriate template-id to | 
|  | 1453 | /// return to the caller. | 
|  | 1454 | /// | 
|  | 1455 | /// \param SS the nested-name-specifier that precedes this template-id, if | 
|  | 1456 | /// we're actually parsing a qualified-id. | 
|  | 1457 | /// | 
|  | 1458 | /// \param Name for constructor and destructor names, this is the actual | 
|  | 1459 | /// identifier that may be a template-name. | 
|  | 1460 | /// | 
|  | 1461 | /// \param NameLoc the location of the class-name in a constructor or | 
|  | 1462 | /// destructor. | 
|  | 1463 | /// | 
|  | 1464 | /// \param EnteringContext whether we're entering the scope of the | 
|  | 1465 | /// nested-name-specifier. | 
|  | 1466 | /// | 
| Douglas Gregor | 46df8cc | 2009-11-03 21:24:04 +0000 | [diff] [blame] | 1467 | /// \param ObjectType if this unqualified-id occurs within a member access | 
|  | 1468 | /// expression, the type of the base object whose member is being accessed. | 
|  | 1469 | /// | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1470 | /// \param Id as input, describes the template-name or operator-function-id | 
|  | 1471 | /// that precedes the '<'. If template arguments were parsed successfully, | 
|  | 1472 | /// will be updated with the template-id. | 
|  | 1473 | /// | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1474 | /// \param AssumeTemplateId When true, this routine will assume that the name | 
|  | 1475 | /// refers to a template without performing name lookup to verify. | 
|  | 1476 | /// | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1477 | /// \returns true if a parse error occurred, false otherwise. | 
|  | 1478 | bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS, | 
|  | 1479 | IdentifierInfo *Name, | 
|  | 1480 | SourceLocation NameLoc, | 
|  | 1481 | bool EnteringContext, | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1482 | ParsedType ObjectType, | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1483 | UnqualifiedId &Id, | 
| Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1484 | bool AssumeTemplateId, | 
|  | 1485 | SourceLocation TemplateKWLoc) { | 
|  | 1486 | assert((AssumeTemplateId || Tok.is(tok::less)) && | 
|  | 1487 | "Expected '<' to finish parsing a template-id"); | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1488 |  | 
|  | 1489 | TemplateTy Template; | 
|  | 1490 | TemplateNameKind TNK = TNK_Non_template; | 
|  | 1491 | switch (Id.getKind()) { | 
|  | 1492 | case UnqualifiedId::IK_Identifier: | 
| Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1493 | case UnqualifiedId::IK_OperatorFunctionId: | 
| Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1494 | case UnqualifiedId::IK_LiteralOperatorId: | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1495 | if (AssumeTemplateId) { | 
| Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1496 | TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS, | 
| Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1497 | Id, ObjectType, EnteringContext, | 
|  | 1498 | Template); | 
|  | 1499 | if (TNK == TNK_Non_template) | 
|  | 1500 | return true; | 
| Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1501 | } else { | 
|  | 1502 | bool MemberOfUnknownSpecialization; | 
| Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 1503 | TNK = Actions.isTemplateName(getCurScope(), SS, | 
|  | 1504 | TemplateKWLoc.isValid(), Id, | 
|  | 1505 | ObjectType, EnteringContext, Template, | 
| Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1506 | MemberOfUnknownSpecialization); | 
|  | 1507 |  | 
|  | 1508 | if (TNK == TNK_Non_template && MemberOfUnknownSpecialization && | 
|  | 1509 | ObjectType && IsTemplateArgumentList()) { | 
|  | 1510 | // We have something like t->getAs<T>(), where getAs is a | 
|  | 1511 | // member of an unknown specialization. However, this will only | 
|  | 1512 | // parse correctly as a template, so suggest the keyword 'template' | 
|  | 1513 | // before 'getAs' and treat this as a dependent template name. | 
|  | 1514 | std::string Name; | 
|  | 1515 | if (Id.getKind() == UnqualifiedId::IK_Identifier) | 
|  | 1516 | Name = Id.Identifier->getName(); | 
|  | 1517 | else { | 
|  | 1518 | Name = "operator "; | 
|  | 1519 | if (Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) | 
|  | 1520 | Name += getOperatorSpelling(Id.OperatorFunctionId.Operator); | 
|  | 1521 | else | 
|  | 1522 | Name += Id.Identifier->getName(); | 
|  | 1523 | } | 
|  | 1524 | Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword) | 
|  | 1525 | << Name | 
|  | 1526 | << FixItHint::CreateInsertion(Id.StartLocation, "template "); | 
| Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1527 | TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, | 
| Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1528 | SS, Id, ObjectType, | 
|  | 1529 | EnteringContext, Template); | 
|  | 1530 | if (TNK == TNK_Non_template) | 
| Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1531 | return true; | 
|  | 1532 | } | 
|  | 1533 | } | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1534 | break; | 
|  | 1535 |  | 
| Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1536 | case UnqualifiedId::IK_ConstructorName: { | 
|  | 1537 | UnqualifiedId TemplateName; | 
| Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1538 | bool MemberOfUnknownSpecialization; | 
| Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1539 | TemplateName.setIdentifier(Name, NameLoc); | 
| Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 1540 | TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(), | 
|  | 1541 | TemplateName, ObjectType, | 
| Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1542 | EnteringContext, Template, | 
|  | 1543 | MemberOfUnknownSpecialization); | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1544 | break; | 
|  | 1545 | } | 
|  | 1546 |  | 
| Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1547 | case UnqualifiedId::IK_DestructorName: { | 
|  | 1548 | UnqualifiedId TemplateName; | 
| Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1549 | bool MemberOfUnknownSpecialization; | 
| Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1550 | TemplateName.setIdentifier(Name, NameLoc); | 
| Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 1551 | if (ObjectType) { | 
| Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1552 | TNK = Actions.ActOnDependentTemplateName(getCurScope(), TemplateKWLoc, SS, | 
| Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1553 | TemplateName, ObjectType, | 
|  | 1554 | EnteringContext, Template); | 
|  | 1555 | if (TNK == TNK_Non_template) | 
| Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 1556 | return true; | 
|  | 1557 | } else { | 
| Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 1558 | TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(), | 
|  | 1559 | TemplateName, ObjectType, | 
| Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1560 | EnteringContext, Template, | 
|  | 1561 | MemberOfUnknownSpecialization); | 
| Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 1562 |  | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1563 | if (TNK == TNK_Non_template && !Id.DestructorName.get()) { | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 1564 | Diag(NameLoc, diag::err_destructor_template_id) | 
|  | 1565 | << Name << SS.getRange(); | 
| Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 1566 | return true; | 
|  | 1567 | } | 
|  | 1568 | } | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1569 | break; | 
| Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1570 | } | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1571 |  | 
|  | 1572 | default: | 
|  | 1573 | return false; | 
|  | 1574 | } | 
|  | 1575 |  | 
|  | 1576 | if (TNK == TNK_Non_template) | 
|  | 1577 | return false; | 
|  | 1578 |  | 
|  | 1579 | // Parse the enclosed template argument list. | 
|  | 1580 | SourceLocation LAngleLoc, RAngleLoc; | 
|  | 1581 | TemplateArgList TemplateArgs; | 
| Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1582 | if (Tok.is(tok::less) && | 
|  | 1583 | ParseTemplateIdAfterTemplateName(Template, Id.StartLocation, | 
| Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1584 | SS, true, LAngleLoc, | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1585 | TemplateArgs, | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1586 | RAngleLoc)) | 
|  | 1587 | return true; | 
|  | 1588 |  | 
|  | 1589 | if (Id.getKind() == UnqualifiedId::IK_Identifier || | 
| Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1590 | Id.getKind() == UnqualifiedId::IK_OperatorFunctionId || | 
|  | 1591 | Id.getKind() == UnqualifiedId::IK_LiteralOperatorId) { | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1592 | // Form a parsed representation of the template-id to be stored in the | 
|  | 1593 | // UnqualifiedId. | 
|  | 1594 | TemplateIdAnnotation *TemplateId | 
|  | 1595 | = TemplateIdAnnotation::Allocate(TemplateArgs.size()); | 
|  | 1596 |  | 
|  | 1597 | if (Id.getKind() == UnqualifiedId::IK_Identifier) { | 
|  | 1598 | TemplateId->Name = Id.Identifier; | 
| Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1599 | TemplateId->Operator = OO_None; | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1600 | TemplateId->TemplateNameLoc = Id.StartLocation; | 
|  | 1601 | } else { | 
| Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1602 | TemplateId->Name = 0; | 
|  | 1603 | TemplateId->Operator = Id.OperatorFunctionId.Operator; | 
|  | 1604 | TemplateId->TemplateNameLoc = Id.StartLocation; | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1605 | } | 
|  | 1606 |  | 
| Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1607 | TemplateId->SS = SS; | 
| John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1608 | TemplateId->Template = Template; | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1609 | TemplateId->Kind = TNK; | 
|  | 1610 | TemplateId->LAngleLoc = LAngleLoc; | 
|  | 1611 | TemplateId->RAngleLoc = RAngleLoc; | 
| Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1612 | ParsedTemplateArgument *Args = TemplateId->getTemplateArgs(); | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1613 | for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); | 
| Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1614 | Arg != ArgEnd; ++Arg) | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1615 | Args[Arg] = TemplateArgs[Arg]; | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1616 |  | 
|  | 1617 | Id.setTemplateId(TemplateId); | 
|  | 1618 | return false; | 
|  | 1619 | } | 
|  | 1620 |  | 
|  | 1621 | // Bundle the template arguments together. | 
|  | 1622 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(), | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1623 | TemplateArgs.size()); | 
|  | 1624 |  | 
|  | 1625 | // Constructor and destructor names. | 
| John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1626 | TypeResult Type | 
| Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1627 | = Actions.ActOnTemplateIdType(SS, Template, NameLoc, | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1628 | LAngleLoc, TemplateArgsPtr, | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1629 | RAngleLoc); | 
|  | 1630 | if (Type.isInvalid()) | 
|  | 1631 | return true; | 
|  | 1632 |  | 
|  | 1633 | if (Id.getKind() == UnqualifiedId::IK_ConstructorName) | 
|  | 1634 | Id.setConstructorName(Type.get(), NameLoc, RAngleLoc); | 
|  | 1635 | else | 
|  | 1636 | Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc); | 
|  | 1637 |  | 
|  | 1638 | return false; | 
|  | 1639 | } | 
|  | 1640 |  | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1641 | /// \brief Parse an operator-function-id or conversion-function-id as part | 
|  | 1642 | /// of a C++ unqualified-id. | 
|  | 1643 | /// | 
|  | 1644 | /// This routine is responsible only for parsing the operator-function-id or | 
|  | 1645 | /// conversion-function-id; it does not handle template arguments in any way. | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1646 | /// | 
|  | 1647 | /// \code | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1648 | ///       operator-function-id: [C++ 13.5] | 
|  | 1649 | ///         'operator' operator | 
|  | 1650 | /// | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1651 | ///       operator: one of | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1652 | ///            new   delete  new[]   delete[] | 
|  | 1653 | ///            +     -    *  /    %  ^    &   |   ~ | 
|  | 1654 | ///            !     =    <  >    += -=   *=  /=  %= | 
|  | 1655 | ///            ^=    &=   |= <<   >> >>= <<=  ==  != | 
|  | 1656 | ///            <=    >=   && ||   ++ --   ,   ->* -> | 
|  | 1657 | ///            ()    [] | 
|  | 1658 | /// | 
|  | 1659 | ///       conversion-function-id: [C++ 12.3.2] | 
|  | 1660 | ///         operator conversion-type-id | 
|  | 1661 | /// | 
|  | 1662 | ///       conversion-type-id: | 
|  | 1663 | ///         type-specifier-seq conversion-declarator[opt] | 
|  | 1664 | /// | 
|  | 1665 | ///       conversion-declarator: | 
|  | 1666 | ///         ptr-operator conversion-declarator[opt] | 
|  | 1667 | /// \endcode | 
|  | 1668 | /// | 
|  | 1669 | /// \param The nested-name-specifier that preceded this unqualified-id. If | 
|  | 1670 | /// non-empty, then we are parsing the unqualified-id of a qualified-id. | 
|  | 1671 | /// | 
|  | 1672 | /// \param EnteringContext whether we are entering the scope of the | 
|  | 1673 | /// nested-name-specifier. | 
|  | 1674 | /// | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1675 | /// \param ObjectType if this unqualified-id occurs within a member access | 
|  | 1676 | /// expression, the type of the base object whose member is being accessed. | 
|  | 1677 | /// | 
|  | 1678 | /// \param Result on a successful parse, contains the parsed unqualified-id. | 
|  | 1679 | /// | 
|  | 1680 | /// \returns true if parsing fails, false otherwise. | 
|  | 1681 | bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext, | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1682 | ParsedType ObjectType, | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1683 | UnqualifiedId &Result) { | 
|  | 1684 | assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword"); | 
|  | 1685 |  | 
|  | 1686 | // Consume the 'operator' keyword. | 
|  | 1687 | SourceLocation KeywordLoc = ConsumeToken(); | 
|  | 1688 |  | 
|  | 1689 | // Determine what kind of operator name we have. | 
|  | 1690 | unsigned SymbolIdx = 0; | 
|  | 1691 | SourceLocation SymbolLocations[3]; | 
|  | 1692 | OverloadedOperatorKind Op = OO_None; | 
|  | 1693 | switch (Tok.getKind()) { | 
|  | 1694 | case tok::kw_new: | 
|  | 1695 | case tok::kw_delete: { | 
|  | 1696 | bool isNew = Tok.getKind() == tok::kw_new; | 
|  | 1697 | // Consume the 'new' or 'delete'. | 
|  | 1698 | SymbolLocations[SymbolIdx++] = ConsumeToken(); | 
|  | 1699 | if (Tok.is(tok::l_square)) { | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1700 | // Consume the '[' and ']'. | 
|  | 1701 | BalancedDelimiterTracker T(*this, tok::l_square); | 
|  | 1702 | T.consumeOpen(); | 
|  | 1703 | T.consumeClose(); | 
|  | 1704 | if (T.getCloseLocation().isInvalid()) | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1705 | return true; | 
|  | 1706 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1707 | SymbolLocations[SymbolIdx++] = T.getOpenLocation(); | 
|  | 1708 | SymbolLocations[SymbolIdx++] = T.getCloseLocation(); | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1709 | Op = isNew? OO_Array_New : OO_Array_Delete; | 
|  | 1710 | } else { | 
|  | 1711 | Op = isNew? OO_New : OO_Delete; | 
|  | 1712 | } | 
|  | 1713 | break; | 
|  | 1714 | } | 
|  | 1715 |  | 
|  | 1716 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ | 
|  | 1717 | case tok::Token:                                                     \ | 
|  | 1718 | SymbolLocations[SymbolIdx++] = ConsumeToken();                     \ | 
|  | 1719 | Op = OO_##Name;                                                    \ | 
|  | 1720 | break; | 
|  | 1721 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) | 
|  | 1722 | #include "clang/Basic/OperatorKinds.def" | 
|  | 1723 |  | 
|  | 1724 | case tok::l_paren: { | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1725 | // Consume the '(' and ')'. | 
|  | 1726 | BalancedDelimiterTracker T(*this, tok::l_paren); | 
|  | 1727 | T.consumeOpen(); | 
|  | 1728 | T.consumeClose(); | 
|  | 1729 | if (T.getCloseLocation().isInvalid()) | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1730 | return true; | 
|  | 1731 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1732 | SymbolLocations[SymbolIdx++] = T.getOpenLocation(); | 
|  | 1733 | SymbolLocations[SymbolIdx++] = T.getCloseLocation(); | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1734 | Op = OO_Call; | 
|  | 1735 | break; | 
|  | 1736 | } | 
|  | 1737 |  | 
|  | 1738 | case tok::l_square: { | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1739 | // Consume the '[' and ']'. | 
|  | 1740 | BalancedDelimiterTracker T(*this, tok::l_square); | 
|  | 1741 | T.consumeOpen(); | 
|  | 1742 | T.consumeClose(); | 
|  | 1743 | if (T.getCloseLocation().isInvalid()) | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1744 | return true; | 
|  | 1745 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1746 | SymbolLocations[SymbolIdx++] = T.getOpenLocation(); | 
|  | 1747 | SymbolLocations[SymbolIdx++] = T.getCloseLocation(); | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1748 | Op = OO_Subscript; | 
|  | 1749 | break; | 
|  | 1750 | } | 
|  | 1751 |  | 
|  | 1752 | case tok::code_completion: { | 
|  | 1753 | // Code completion for the operator name. | 
| Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1754 | Actions.CodeCompleteOperatorName(getCurScope()); | 
| Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1755 | cutOffParsing(); | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1756 | // Don't try to parse any further. | 
|  | 1757 | return true; | 
|  | 1758 | } | 
|  | 1759 |  | 
|  | 1760 | default: | 
|  | 1761 | break; | 
|  | 1762 | } | 
|  | 1763 |  | 
|  | 1764 | if (Op != OO_None) { | 
|  | 1765 | // We have parsed an operator-function-id. | 
|  | 1766 | Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations); | 
|  | 1767 | return false; | 
|  | 1768 | } | 
| Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 1769 |  | 
|  | 1770 | // Parse a literal-operator-id. | 
|  | 1771 | // | 
|  | 1772 | //   literal-operator-id: [C++0x 13.5.8] | 
|  | 1773 | //     operator "" identifier | 
|  | 1774 |  | 
|  | 1775 | if (getLang().CPlusPlus0x && Tok.is(tok::string_literal)) { | 
| Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1776 | Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator); | 
| Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 1777 | if (Tok.getLength() != 2) | 
|  | 1778 | Diag(Tok.getLocation(), diag::err_operator_string_not_empty); | 
|  | 1779 | ConsumeStringToken(); | 
|  | 1780 |  | 
|  | 1781 | if (Tok.isNot(tok::identifier)) { | 
|  | 1782 | Diag(Tok.getLocation(), diag::err_expected_ident); | 
|  | 1783 | return true; | 
|  | 1784 | } | 
|  | 1785 |  | 
|  | 1786 | IdentifierInfo *II = Tok.getIdentifierInfo(); | 
|  | 1787 | Result.setLiteralOperatorId(II, KeywordLoc, ConsumeToken()); | 
| Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 1788 | return false; | 
| Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 1789 | } | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1790 |  | 
|  | 1791 | // Parse a conversion-function-id. | 
|  | 1792 | // | 
|  | 1793 | //   conversion-function-id: [C++ 12.3.2] | 
|  | 1794 | //     operator conversion-type-id | 
|  | 1795 | // | 
|  | 1796 | //   conversion-type-id: | 
|  | 1797 | //     type-specifier-seq conversion-declarator[opt] | 
|  | 1798 | // | 
|  | 1799 | //   conversion-declarator: | 
|  | 1800 | //     ptr-operator conversion-declarator[opt] | 
|  | 1801 |  | 
|  | 1802 | // Parse the type-specifier-seq. | 
| John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1803 | DeclSpec DS(AttrFactory); | 
| Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 1804 | if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType? | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1805 | return true; | 
|  | 1806 |  | 
|  | 1807 | // Parse the conversion-declarator, which is merely a sequence of | 
|  | 1808 | // ptr-operators. | 
|  | 1809 | Declarator D(DS, Declarator::TypeNameContext); | 
|  | 1810 | ParseDeclaratorInternal(D, /*DirectDeclParser=*/0); | 
|  | 1811 |  | 
|  | 1812 | // Finish up the type. | 
| John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1813 | TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D); | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1814 | if (Ty.isInvalid()) | 
|  | 1815 | return true; | 
|  | 1816 |  | 
|  | 1817 | // Note that this is a conversion-function-id. | 
|  | 1818 | Result.setConversionFunctionId(KeywordLoc, Ty.get(), | 
|  | 1819 | D.getSourceRange().getEnd()); | 
|  | 1820 | return false; | 
|  | 1821 | } | 
|  | 1822 |  | 
|  | 1823 | /// \brief Parse a C++ unqualified-id (or a C identifier), which describes the | 
|  | 1824 | /// name of an entity. | 
|  | 1825 | /// | 
|  | 1826 | /// \code | 
|  | 1827 | ///       unqualified-id: [C++ expr.prim.general] | 
|  | 1828 | ///         identifier | 
|  | 1829 | ///         operator-function-id | 
|  | 1830 | ///         conversion-function-id | 
|  | 1831 | /// [C++0x] literal-operator-id [TODO] | 
|  | 1832 | ///         ~ class-name | 
|  | 1833 | ///         template-id | 
|  | 1834 | /// | 
|  | 1835 | /// \endcode | 
|  | 1836 | /// | 
|  | 1837 | /// \param The nested-name-specifier that preceded this unqualified-id. If | 
|  | 1838 | /// non-empty, then we are parsing the unqualified-id of a qualified-id. | 
|  | 1839 | /// | 
|  | 1840 | /// \param EnteringContext whether we are entering the scope of the | 
|  | 1841 | /// nested-name-specifier. | 
|  | 1842 | /// | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1843 | /// \param AllowDestructorName whether we allow parsing of a destructor name. | 
|  | 1844 | /// | 
|  | 1845 | /// \param AllowConstructorName whether we allow parsing a constructor name. | 
|  | 1846 | /// | 
| Douglas Gregor | 46df8cc | 2009-11-03 21:24:04 +0000 | [diff] [blame] | 1847 | /// \param ObjectType if this unqualified-id occurs within a member access | 
|  | 1848 | /// expression, the type of the base object whose member is being accessed. | 
|  | 1849 | /// | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1850 | /// \param Result on a successful parse, contains the parsed unqualified-id. | 
|  | 1851 | /// | 
|  | 1852 | /// \returns true if parsing fails, false otherwise. | 
|  | 1853 | bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext, | 
|  | 1854 | bool AllowDestructorName, | 
|  | 1855 | bool AllowConstructorName, | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1856 | ParsedType ObjectType, | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1857 | UnqualifiedId &Result) { | 
| Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1858 |  | 
|  | 1859 | // Handle 'A::template B'. This is for template-ids which have not | 
|  | 1860 | // already been annotated by ParseOptionalCXXScopeSpecifier(). | 
|  | 1861 | bool TemplateSpecified = false; | 
|  | 1862 | SourceLocation TemplateKWLoc; | 
|  | 1863 | if (getLang().CPlusPlus && Tok.is(tok::kw_template) && | 
|  | 1864 | (ObjectType || SS.isSet())) { | 
|  | 1865 | TemplateSpecified = true; | 
|  | 1866 | TemplateKWLoc = ConsumeToken(); | 
|  | 1867 | } | 
|  | 1868 |  | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1869 | // unqualified-id: | 
|  | 1870 | //   identifier | 
|  | 1871 | //   template-id (when it hasn't already been annotated) | 
|  | 1872 | if (Tok.is(tok::identifier)) { | 
|  | 1873 | // Consume the identifier. | 
|  | 1874 | IdentifierInfo *Id = Tok.getIdentifierInfo(); | 
|  | 1875 | SourceLocation IdLoc = ConsumeToken(); | 
|  | 1876 |  | 
| Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 1877 | if (!getLang().CPlusPlus) { | 
|  | 1878 | // If we're not in C++, only identifiers matter. Record the | 
|  | 1879 | // identifier and return. | 
|  | 1880 | Result.setIdentifier(Id, IdLoc); | 
|  | 1881 | return false; | 
|  | 1882 | } | 
|  | 1883 |  | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1884 | if (AllowConstructorName && | 
| Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1885 | Actions.isCurrentClassName(*Id, getCurScope(), &SS)) { | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1886 | // We have parsed a constructor name. | 
| Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1887 | Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, getCurScope(), | 
| Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1888 | &SS, false, false, | 
|  | 1889 | ParsedType(), | 
|  | 1890 | /*NonTrivialTypeSourceInfo=*/true), | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1891 | IdLoc, IdLoc); | 
|  | 1892 | } else { | 
|  | 1893 | // We have parsed an identifier. | 
|  | 1894 | Result.setIdentifier(Id, IdLoc); | 
|  | 1895 | } | 
|  | 1896 |  | 
|  | 1897 | // If the next token is a '<', we may have a template. | 
| Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1898 | if (TemplateSpecified || Tok.is(tok::less)) | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1899 | return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext, | 
| Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1900 | ObjectType, Result, | 
|  | 1901 | TemplateSpecified, TemplateKWLoc); | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1902 |  | 
|  | 1903 | return false; | 
|  | 1904 | } | 
|  | 1905 |  | 
|  | 1906 | // unqualified-id: | 
|  | 1907 | //   template-id (already parsed and annotated) | 
|  | 1908 | if (Tok.is(tok::annot_template_id)) { | 
| Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1909 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); | 
| Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1910 |  | 
|  | 1911 | // If the template-name names the current class, then this is a constructor | 
|  | 1912 | if (AllowConstructorName && TemplateId->Name && | 
| Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1913 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { | 
| Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1914 | if (SS.isSet()) { | 
|  | 1915 | // C++ [class.qual]p2 specifies that a qualified template-name | 
|  | 1916 | // is taken as the constructor name where a constructor can be | 
|  | 1917 | // declared. Thus, the template arguments are extraneous, so | 
|  | 1918 | // complain about them and remove them entirely. | 
|  | 1919 | Diag(TemplateId->TemplateNameLoc, | 
|  | 1920 | diag::err_out_of_line_constructor_template_id) | 
|  | 1921 | << TemplateId->Name | 
| Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1922 | << FixItHint::CreateRemoval( | 
| Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1923 | SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc)); | 
|  | 1924 | Result.setConstructorName(Actions.getTypeName(*TemplateId->Name, | 
|  | 1925 | TemplateId->TemplateNameLoc, | 
| Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1926 | getCurScope(), | 
| Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1927 | &SS, false, false, | 
|  | 1928 | ParsedType(), | 
|  | 1929 | /*NontrivialTypeSourceInfo=*/true), | 
| Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1930 | TemplateId->TemplateNameLoc, | 
|  | 1931 | TemplateId->RAngleLoc); | 
| Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1932 | ConsumeToken(); | 
|  | 1933 | return false; | 
|  | 1934 | } | 
|  | 1935 |  | 
|  | 1936 | Result.setConstructorTemplateId(TemplateId); | 
|  | 1937 | ConsumeToken(); | 
|  | 1938 | return false; | 
|  | 1939 | } | 
|  | 1940 |  | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1941 | // We have already parsed a template-id; consume the annotation token as | 
|  | 1942 | // our unqualified-id. | 
| Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1943 | Result.setTemplateId(TemplateId); | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1944 | ConsumeToken(); | 
|  | 1945 | return false; | 
|  | 1946 | } | 
|  | 1947 |  | 
|  | 1948 | // unqualified-id: | 
|  | 1949 | //   operator-function-id | 
|  | 1950 | //   conversion-function-id | 
|  | 1951 | if (Tok.is(tok::kw_operator)) { | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1952 | if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result)) | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1953 | return true; | 
|  | 1954 |  | 
| Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1955 | // If we have an operator-function-id or a literal-operator-id and the next | 
|  | 1956 | // token is a '<', we may have a | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1957 | // | 
|  | 1958 | //   template-id: | 
|  | 1959 | //     operator-function-id < template-argument-list[opt] > | 
| Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1960 | if ((Result.getKind() == UnqualifiedId::IK_OperatorFunctionId || | 
|  | 1961 | Result.getKind() == UnqualifiedId::IK_LiteralOperatorId) && | 
| Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1962 | (TemplateSpecified || Tok.is(tok::less))) | 
| Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1963 | return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(), | 
|  | 1964 | EnteringContext, ObjectType, | 
| Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1965 | Result, | 
|  | 1966 | TemplateSpecified, TemplateKWLoc); | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1967 |  | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1968 | return false; | 
|  | 1969 | } | 
|  | 1970 |  | 
| Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 1971 | if (getLang().CPlusPlus && | 
|  | 1972 | (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) { | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1973 | // C++ [expr.unary.op]p10: | 
|  | 1974 | //   There is an ambiguity in the unary-expression ~X(), where X is a | 
|  | 1975 | //   class-name. The ambiguity is resolved in favor of treating ~ as a | 
|  | 1976 | //    unary complement rather than treating ~X as referring to a destructor. | 
|  | 1977 |  | 
|  | 1978 | // Parse the '~'. | 
|  | 1979 | SourceLocation TildeLoc = ConsumeToken(); | 
| David Blaikie | 53a75c0 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 1980 |  | 
|  | 1981 | if (SS.isEmpty() && Tok.is(tok::kw_decltype)) { | 
|  | 1982 | DeclSpec DS(AttrFactory); | 
|  | 1983 | SourceLocation EndLoc = ParseDecltypeSpecifier(DS); | 
|  | 1984 | if (ParsedType Type = Actions.getDestructorType(DS, ObjectType)) { | 
|  | 1985 | Result.setDestructorName(TildeLoc, Type, EndLoc); | 
|  | 1986 | return false; | 
|  | 1987 | } | 
|  | 1988 | return true; | 
|  | 1989 | } | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1990 |  | 
|  | 1991 | // Parse the class-name. | 
|  | 1992 | if (Tok.isNot(tok::identifier)) { | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 1993 | Diag(Tok, diag::err_destructor_tilde_identifier); | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1994 | return true; | 
|  | 1995 | } | 
|  | 1996 |  | 
|  | 1997 | // Parse the class-name (or template-name in a simple-template-id). | 
|  | 1998 | IdentifierInfo *ClassName = Tok.getIdentifierInfo(); | 
|  | 1999 | SourceLocation ClassNameLoc = ConsumeToken(); | 
|  | 2000 |  | 
| Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2001 | if (TemplateSpecified || Tok.is(tok::less)) { | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2002 | Result.setDestructorName(TildeLoc, ParsedType(), ClassNameLoc); | 
| Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2003 | return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc, | 
| Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2004 | EnteringContext, ObjectType, Result, | 
|  | 2005 | TemplateSpecified, TemplateKWLoc); | 
| Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2006 | } | 
|  | 2007 |  | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2008 | // Note that this is a destructor name. | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2009 | ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName, | 
|  | 2010 | ClassNameLoc, getCurScope(), | 
|  | 2011 | SS, ObjectType, | 
|  | 2012 | EnteringContext); | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 2013 | if (!Ty) | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2014 | return true; | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 2015 |  | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2016 | Result.setDestructorName(TildeLoc, Ty, ClassNameLoc); | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2017 | return false; | 
|  | 2018 | } | 
|  | 2019 |  | 
| Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2020 | Diag(Tok, diag::err_expected_unqualified_id) | 
|  | 2021 | << getLang().CPlusPlus; | 
| Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2022 | return true; | 
|  | 2023 | } | 
|  | 2024 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2025 | /// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate | 
|  | 2026 | /// memory in a typesafe manner and call constructors. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | /// | 
| Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 2028 | /// This method is called to parse the new expression after the optional :: has | 
|  | 2029 | /// been already parsed.  If the :: was present, "UseGlobal" is true and "Start" | 
|  | 2030 | /// is its location.  Otherwise, "Start" is the location of the 'new' token. | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2031 | /// | 
|  | 2032 | ///        new-expression: | 
|  | 2033 | ///                   '::'[opt] 'new' new-placement[opt] new-type-id | 
|  | 2034 | ///                                     new-initializer[opt] | 
|  | 2035 | ///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')' | 
|  | 2036 | ///                                     new-initializer[opt] | 
|  | 2037 | /// | 
|  | 2038 | ///        new-placement: | 
|  | 2039 | ///                   '(' expression-list ')' | 
|  | 2040 | /// | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2041 | ///        new-type-id: | 
|  | 2042 | ///                   type-specifier-seq new-declarator[opt] | 
| Douglas Gregor | 893e1cc | 2011-04-15 19:40:02 +0000 | [diff] [blame] | 2043 | /// [GNU]             attributes type-specifier-seq new-declarator[opt] | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2044 | /// | 
|  | 2045 | ///        new-declarator: | 
|  | 2046 | ///                   ptr-operator new-declarator[opt] | 
|  | 2047 | ///                   direct-new-declarator | 
|  | 2048 | /// | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2049 | ///        new-initializer: | 
|  | 2050 | ///                   '(' expression-list[opt] ')' | 
| Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2051 | /// [C++0x]           braced-init-list | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2052 | /// | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2053 | ExprResult | 
| Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 2054 | Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) { | 
|  | 2055 | assert(Tok.is(tok::kw_new) && "expected 'new' token"); | 
|  | 2056 | ConsumeToken();   // Consume 'new' | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2057 |  | 
|  | 2058 | // A '(' now can be a new-placement or the '(' wrapping the type-id in the | 
|  | 2059 | // second form of new-expression. It can't be a new-type-id. | 
|  | 2060 |  | 
| Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 2061 | ExprVector PlacementArgs(Actions); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2062 | SourceLocation PlacementLParen, PlacementRParen; | 
|  | 2063 |  | 
| Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 2064 | SourceRange TypeIdParens; | 
| John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2065 | DeclSpec DS(AttrFactory); | 
| Argyrios Kyrtzidis | 0b8c98f | 2011-06-28 03:01:23 +0000 | [diff] [blame] | 2066 | Declarator DeclaratorInfo(DS, Declarator::CXXNewContext); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2067 | if (Tok.is(tok::l_paren)) { | 
|  | 2068 | // If it turns out to be a placement, we change the type location. | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2069 | BalancedDelimiterTracker T(*this, tok::l_paren); | 
|  | 2070 | T.consumeOpen(); | 
|  | 2071 | PlacementLParen = T.getOpenLocation(); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2072 | if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) { | 
|  | 2073 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 2074 | return ExprError(); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2075 | } | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2076 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2077 | T.consumeClose(); | 
|  | 2078 | PlacementRParen = T.getCloseLocation(); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2079 | if (PlacementRParen.isInvalid()) { | 
|  | 2080 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 2081 | return ExprError(); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2082 | } | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2083 |  | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2084 | if (PlacementArgs.empty()) { | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2085 | // Reset the placement locations. There was no placement. | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2086 | TypeIdParens = T.getRange(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2087 | PlacementLParen = PlacementRParen = SourceLocation(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2088 | } else { | 
|  | 2089 | // We still need the type. | 
|  | 2090 | if (Tok.is(tok::l_paren)) { | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2091 | BalancedDelimiterTracker T(*this, tok::l_paren); | 
|  | 2092 | T.consumeOpen(); | 
| Douglas Gregor | 893e1cc | 2011-04-15 19:40:02 +0000 | [diff] [blame] | 2093 | MaybeParseGNUAttributes(DeclaratorInfo); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2094 | ParseSpecifierQualifierList(DS); | 
| Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2095 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2096 | ParseDeclarator(DeclaratorInfo); | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2097 | T.consumeClose(); | 
|  | 2098 | TypeIdParens = T.getRange(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2099 | } else { | 
| Douglas Gregor | 893e1cc | 2011-04-15 19:40:02 +0000 | [diff] [blame] | 2100 | MaybeParseGNUAttributes(DeclaratorInfo); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2101 | if (ParseCXXTypeSpecifierSeq(DS)) | 
|  | 2102 | DeclaratorInfo.setInvalidType(true); | 
| Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2103 | else { | 
|  | 2104 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2105 | ParseDeclaratorInternal(DeclaratorInfo, | 
|  | 2106 | &Parser::ParseDirectNewDeclarator); | 
| Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2107 | } | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2108 | } | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2109 | } | 
|  | 2110 | } else { | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2111 | // A new-type-id is a simplified type-id, where essentially the | 
|  | 2112 | // direct-declarator is replaced by a direct-new-declarator. | 
| Douglas Gregor | 893e1cc | 2011-04-15 19:40:02 +0000 | [diff] [blame] | 2113 | MaybeParseGNUAttributes(DeclaratorInfo); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2114 | if (ParseCXXTypeSpecifierSeq(DS)) | 
|  | 2115 | DeclaratorInfo.setInvalidType(true); | 
| Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2116 | else { | 
|  | 2117 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2118 | ParseDeclaratorInternal(DeclaratorInfo, | 
|  | 2119 | &Parser::ParseDirectNewDeclarator); | 
| Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2120 | } | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2121 | } | 
| Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 2122 | if (DeclaratorInfo.isInvalidType()) { | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2123 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 2124 | return ExprError(); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2125 | } | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2126 |  | 
| Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 2127 | ExprVector ConstructorArgs(Actions); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2128 | SourceLocation ConstructorLParen, ConstructorRParen; | 
|  | 2129 |  | 
|  | 2130 | if (Tok.is(tok::l_paren)) { | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2131 | BalancedDelimiterTracker T(*this, tok::l_paren); | 
|  | 2132 | T.consumeOpen(); | 
|  | 2133 | ConstructorLParen = T.getOpenLocation(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2134 | if (Tok.isNot(tok::r_paren)) { | 
|  | 2135 | CommaLocsTy CommaLocs; | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2136 | if (ParseExpressionList(ConstructorArgs, CommaLocs)) { | 
|  | 2137 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 2138 | return ExprError(); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2139 | } | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2140 | } | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2141 | T.consumeClose(); | 
|  | 2142 | ConstructorRParen = T.getCloseLocation(); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2143 | if (ConstructorRParen.isInvalid()) { | 
|  | 2144 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 2145 | return ExprError(); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2146 | } | 
| Richard Smith | 29e3a31 | 2011-10-15 03:38:41 +0000 | [diff] [blame] | 2147 | } else if (Tok.is(tok::l_brace) && getLang().CPlusPlus0x) { | 
| Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2148 | Diag(Tok.getLocation(), | 
|  | 2149 | diag::warn_cxx98_compat_generalized_initializer_lists); | 
| Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2150 | // FIXME: Have to communicate the init-list to ActOnCXXNew. | 
|  | 2151 | ParseBraceInitializer(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2152 | } | 
|  | 2153 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2154 | return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen, | 
|  | 2155 | move_arg(PlacementArgs), PlacementRParen, | 
| Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 2156 | TypeIdParens, DeclaratorInfo, ConstructorLParen, | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2157 | move_arg(ConstructorArgs), ConstructorRParen); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2158 | } | 
|  | 2159 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2160 | /// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be | 
|  | 2161 | /// passed to ParseDeclaratorInternal. | 
|  | 2162 | /// | 
|  | 2163 | ///        direct-new-declarator: | 
|  | 2164 | ///                   '[' expression ']' | 
|  | 2165 | ///                   direct-new-declarator '[' constant-expression ']' | 
|  | 2166 | /// | 
| Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 2167 | void Parser::ParseDirectNewDeclarator(Declarator &D) { | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2168 | // Parse the array dimensions. | 
|  | 2169 | bool first = true; | 
|  | 2170 | while (Tok.is(tok::l_square)) { | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2171 | BalancedDelimiterTracker T(*this, tok::l_square); | 
|  | 2172 | T.consumeOpen(); | 
|  | 2173 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2174 | ExprResult Size(first ? ParseExpression() | 
| Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 2175 | : ParseConstantExpression()); | 
| Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2176 | if (Size.isInvalid()) { | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2177 | // Recover | 
|  | 2178 | SkipUntil(tok::r_square); | 
|  | 2179 | return; | 
|  | 2180 | } | 
|  | 2181 | first = false; | 
|  | 2182 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2183 | T.consumeClose(); | 
| John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2184 |  | 
|  | 2185 | ParsedAttributes attrs(AttrFactory); | 
|  | 2186 | D.AddTypeInfo(DeclaratorChunk::getArray(0, | 
| John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2187 | /*static=*/false, /*star=*/false, | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2188 | Size.release(), | 
|  | 2189 | T.getOpenLocation(), | 
|  | 2190 | T.getCloseLocation()), | 
|  | 2191 | attrs, T.getCloseLocation()); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2192 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2193 | if (T.getCloseLocation().isInvalid()) | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2194 | return; | 
|  | 2195 | } | 
|  | 2196 | } | 
|  | 2197 |  | 
|  | 2198 | /// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id. | 
|  | 2199 | /// This ambiguity appears in the syntax of the C++ new operator. | 
|  | 2200 | /// | 
|  | 2201 | ///        new-expression: | 
|  | 2202 | ///                   '::'[opt] 'new' new-placement[opt] '(' type-id ')' | 
|  | 2203 | ///                                     new-initializer[opt] | 
|  | 2204 | /// | 
|  | 2205 | ///        new-placement: | 
|  | 2206 | ///                   '(' expression-list ')' | 
|  | 2207 | /// | 
| John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2208 | bool Parser::ParseExpressionListOrTypeId( | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2209 | SmallVectorImpl<Expr*> &PlacementArgs, | 
| Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 2210 | Declarator &D) { | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2211 | // The '(' was already consumed. | 
|  | 2212 | if (isTypeIdInParens()) { | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2213 | ParseSpecifierQualifierList(D.getMutableDeclSpec()); | 
| Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2214 | D.SetSourceRange(D.getDeclSpec().getSourceRange()); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2215 | ParseDeclarator(D); | 
| Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 2216 | return D.isInvalidType(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2217 | } | 
|  | 2218 |  | 
|  | 2219 | // It's not a type, it has to be an expression list. | 
|  | 2220 | // Discard the comma locations - ActOnCXXNew has enough parameters. | 
|  | 2221 | CommaLocsTy CommaLocs; | 
|  | 2222 | return ParseExpressionList(PlacementArgs, CommaLocs); | 
|  | 2223 | } | 
|  | 2224 |  | 
|  | 2225 | /// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used | 
|  | 2226 | /// to free memory allocated by new. | 
|  | 2227 | /// | 
| Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 2228 | /// This method is called to parse the 'delete' expression after the optional | 
|  | 2229 | /// '::' has been already parsed.  If the '::' was present, "UseGlobal" is true | 
|  | 2230 | /// and "Start" is its location.  Otherwise, "Start" is the location of the | 
|  | 2231 | /// 'delete' token. | 
|  | 2232 | /// | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2233 | ///        delete-expression: | 
|  | 2234 | ///                   '::'[opt] 'delete' cast-expression | 
|  | 2235 | ///                   '::'[opt] 'delete' '[' ']' cast-expression | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2236 | ExprResult | 
| Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 2237 | Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) { | 
|  | 2238 | assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword"); | 
|  | 2239 | ConsumeToken(); // Consume 'delete' | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2240 |  | 
|  | 2241 | // Array delete? | 
|  | 2242 | bool ArrayDelete = false; | 
|  | 2243 | if (Tok.is(tok::l_square)) { | 
|  | 2244 | ArrayDelete = true; | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2245 | BalancedDelimiterTracker T(*this, tok::l_square); | 
|  | 2246 |  | 
|  | 2247 | T.consumeOpen(); | 
|  | 2248 | T.consumeClose(); | 
|  | 2249 | if (T.getCloseLocation().isInvalid()) | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 2250 | return ExprError(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2251 | } | 
|  | 2252 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2253 | ExprResult Operand(ParseCastExpression(false)); | 
| Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2254 | if (Operand.isInvalid()) | 
| Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 2255 | return move(Operand); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2256 |  | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2257 | return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.take()); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2258 | } | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2259 |  | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2260 | static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) { | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2261 | switch(kind) { | 
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2262 | default: llvm_unreachable("Not a known unary type trait."); | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2263 | case tok::kw___has_nothrow_assign:      return UTT_HasNothrowAssign; | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2264 | case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor; | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2265 | case tok::kw___has_nothrow_copy:           return UTT_HasNothrowCopy; | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2266 | case tok::kw___has_trivial_assign:      return UTT_HasTrivialAssign; | 
| Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2267 | case tok::kw___has_trivial_constructor: | 
|  | 2268 | return UTT_HasTrivialDefaultConstructor; | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2269 | case tok::kw___has_trivial_copy:           return UTT_HasTrivialCopy; | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2270 | case tok::kw___has_trivial_destructor:  return UTT_HasTrivialDestructor; | 
|  | 2271 | case tok::kw___has_virtual_destructor:  return UTT_HasVirtualDestructor; | 
|  | 2272 | case tok::kw___is_abstract:             return UTT_IsAbstract; | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2273 | case tok::kw___is_arithmetic:              return UTT_IsArithmetic; | 
|  | 2274 | case tok::kw___is_array:                   return UTT_IsArray; | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2275 | case tok::kw___is_class:                return UTT_IsClass; | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2276 | case tok::kw___is_complete_type:           return UTT_IsCompleteType; | 
|  | 2277 | case tok::kw___is_compound:                return UTT_IsCompound; | 
|  | 2278 | case tok::kw___is_const:                   return UTT_IsConst; | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2279 | case tok::kw___is_empty:                return UTT_IsEmpty; | 
|  | 2280 | case tok::kw___is_enum:                 return UTT_IsEnum; | 
| Douglas Gregor | 5e9392b | 2011-12-03 18:14:24 +0000 | [diff] [blame] | 2281 | case tok::kw___is_final:                 return UTT_IsFinal; | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2282 | case tok::kw___is_floating_point:          return UTT_IsFloatingPoint; | 
|  | 2283 | case tok::kw___is_function:                return UTT_IsFunction; | 
|  | 2284 | case tok::kw___is_fundamental:             return UTT_IsFundamental; | 
|  | 2285 | case tok::kw___is_integral:                return UTT_IsIntegral; | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2286 | case tok::kw___is_lvalue_reference:        return UTT_IsLvalueReference; | 
|  | 2287 | case tok::kw___is_member_function_pointer: return UTT_IsMemberFunctionPointer; | 
|  | 2288 | case tok::kw___is_member_object_pointer:   return UTT_IsMemberObjectPointer; | 
|  | 2289 | case tok::kw___is_member_pointer:          return UTT_IsMemberPointer; | 
|  | 2290 | case tok::kw___is_object:                  return UTT_IsObject; | 
| Chandler Carruth | 4e61ddd | 2011-04-23 10:47:20 +0000 | [diff] [blame] | 2291 | case tok::kw___is_literal:              return UTT_IsLiteral; | 
| Chandler Carruth | 3840281 | 2011-04-24 02:49:28 +0000 | [diff] [blame] | 2292 | case tok::kw___is_literal_type:         return UTT_IsLiteral; | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2293 | case tok::kw___is_pod:                  return UTT_IsPOD; | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2294 | case tok::kw___is_pointer:                 return UTT_IsPointer; | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2295 | case tok::kw___is_polymorphic:          return UTT_IsPolymorphic; | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2296 | case tok::kw___is_reference:               return UTT_IsReference; | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2297 | case tok::kw___is_rvalue_reference:        return UTT_IsRvalueReference; | 
|  | 2298 | case tok::kw___is_scalar:                  return UTT_IsScalar; | 
|  | 2299 | case tok::kw___is_signed:                  return UTT_IsSigned; | 
|  | 2300 | case tok::kw___is_standard_layout:         return UTT_IsStandardLayout; | 
|  | 2301 | case tok::kw___is_trivial:                 return UTT_IsTrivial; | 
| Sean Hunt | feb375d | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 2302 | case tok::kw___is_trivially_copyable:      return UTT_IsTriviallyCopyable; | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2303 | case tok::kw___is_union:                return UTT_IsUnion; | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2304 | case tok::kw___is_unsigned:                return UTT_IsUnsigned; | 
|  | 2305 | case tok::kw___is_void:                    return UTT_IsVoid; | 
|  | 2306 | case tok::kw___is_volatile:                return UTT_IsVolatile; | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2307 | } | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2308 | } | 
|  | 2309 |  | 
|  | 2310 | static BinaryTypeTrait BinaryTypeTraitFromTokKind(tok::TokenKind kind) { | 
|  | 2311 | switch(kind) { | 
| Francois Pichet | 38c2b73 | 2010-12-07 00:55:57 +0000 | [diff] [blame] | 2312 | default: llvm_unreachable("Not a known binary type trait"); | 
| Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 2313 | case tok::kw___is_base_of:                 return BTT_IsBaseOf; | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2314 | case tok::kw___is_convertible:             return BTT_IsConvertible; | 
|  | 2315 | case tok::kw___is_same:                    return BTT_IsSame; | 
| Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 2316 | case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible; | 
| Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 2317 | case tok::kw___is_convertible_to:          return BTT_IsConvertibleTo; | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2318 | } | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2319 | } | 
|  | 2320 |  | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2321 | static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) { | 
|  | 2322 | switch(kind) { | 
|  | 2323 | default: llvm_unreachable("Not a known binary type trait"); | 
|  | 2324 | case tok::kw___array_rank:                 return ATT_ArrayRank; | 
|  | 2325 | case tok::kw___array_extent:               return ATT_ArrayExtent; | 
|  | 2326 | } | 
|  | 2327 | } | 
|  | 2328 |  | 
| John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2329 | static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) { | 
|  | 2330 | switch(kind) { | 
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2331 | default: llvm_unreachable("Not a known unary expression trait."); | 
| John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2332 | case tok::kw___is_lvalue_expr:             return ET_IsLValueExpr; | 
|  | 2333 | case tok::kw___is_rvalue_expr:             return ET_IsRValueExpr; | 
|  | 2334 | } | 
|  | 2335 | } | 
|  | 2336 |  | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2337 | /// ParseUnaryTypeTrait - Parse the built-in unary type-trait | 
|  | 2338 | /// pseudo-functions that allow implementation of the TR1/C++0x type traits | 
|  | 2339 | /// templates. | 
|  | 2340 | /// | 
|  | 2341 | ///       primary-expression: | 
|  | 2342 | /// [GNU]             unary-type-trait '(' type-id ')' | 
|  | 2343 | /// | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2344 | ExprResult Parser::ParseUnaryTypeTrait() { | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2345 | UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind()); | 
|  | 2346 | SourceLocation Loc = ConsumeToken(); | 
|  | 2347 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2348 | BalancedDelimiterTracker T(*this, tok::l_paren); | 
|  | 2349 | if (T.expectAndConsume(diag::err_expected_lparen)) | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2350 | return ExprError(); | 
|  | 2351 |  | 
|  | 2352 | // FIXME: Error reporting absolutely sucks! If the this fails to parse a type | 
|  | 2353 | // there will be cryptic errors about mismatched parentheses and missing | 
|  | 2354 | // specifiers. | 
| Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 2355 | TypeResult Ty = ParseTypeName(); | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2356 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2357 | T.consumeClose(); | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2358 |  | 
| Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 2359 | if (Ty.isInvalid()) | 
|  | 2360 | return ExprError(); | 
|  | 2361 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2362 | return Actions.ActOnUnaryTypeTrait(UTT, Loc, Ty.get(), T.getCloseLocation()); | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2363 | } | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2364 |  | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2365 | /// ParseBinaryTypeTrait - Parse the built-in binary type-trait | 
|  | 2366 | /// pseudo-functions that allow implementation of the TR1/C++0x type traits | 
|  | 2367 | /// templates. | 
|  | 2368 | /// | 
|  | 2369 | ///       primary-expression: | 
|  | 2370 | /// [GNU]             binary-type-trait '(' type-id ',' type-id ')' | 
|  | 2371 | /// | 
|  | 2372 | ExprResult Parser::ParseBinaryTypeTrait() { | 
|  | 2373 | BinaryTypeTrait BTT = BinaryTypeTraitFromTokKind(Tok.getKind()); | 
|  | 2374 | SourceLocation Loc = ConsumeToken(); | 
|  | 2375 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2376 | BalancedDelimiterTracker T(*this, tok::l_paren); | 
|  | 2377 | if (T.expectAndConsume(diag::err_expected_lparen)) | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2378 | return ExprError(); | 
|  | 2379 |  | 
|  | 2380 | TypeResult LhsTy = ParseTypeName(); | 
|  | 2381 | if (LhsTy.isInvalid()) { | 
|  | 2382 | SkipUntil(tok::r_paren); | 
|  | 2383 | return ExprError(); | 
|  | 2384 | } | 
|  | 2385 |  | 
|  | 2386 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) { | 
|  | 2387 | SkipUntil(tok::r_paren); | 
|  | 2388 | return ExprError(); | 
|  | 2389 | } | 
|  | 2390 |  | 
|  | 2391 | TypeResult RhsTy = ParseTypeName(); | 
|  | 2392 | if (RhsTy.isInvalid()) { | 
|  | 2393 | SkipUntil(tok::r_paren); | 
|  | 2394 | return ExprError(); | 
|  | 2395 | } | 
|  | 2396 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2397 | T.consumeClose(); | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2398 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2399 | return Actions.ActOnBinaryTypeTrait(BTT, Loc, LhsTy.get(), RhsTy.get(), | 
|  | 2400 | T.getCloseLocation()); | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2401 | } | 
|  | 2402 |  | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2403 | /// ParseArrayTypeTrait - Parse the built-in array type-trait | 
|  | 2404 | /// pseudo-functions. | 
|  | 2405 | /// | 
|  | 2406 | ///       primary-expression: | 
|  | 2407 | /// [Embarcadero]     '__array_rank' '(' type-id ')' | 
|  | 2408 | /// [Embarcadero]     '__array_extent' '(' type-id ',' expression ')' | 
|  | 2409 | /// | 
|  | 2410 | ExprResult Parser::ParseArrayTypeTrait() { | 
|  | 2411 | ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind()); | 
|  | 2412 | SourceLocation Loc = ConsumeToken(); | 
|  | 2413 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2414 | BalancedDelimiterTracker T(*this, tok::l_paren); | 
|  | 2415 | if (T.expectAndConsume(diag::err_expected_lparen)) | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2416 | return ExprError(); | 
|  | 2417 |  | 
|  | 2418 | TypeResult Ty = ParseTypeName(); | 
|  | 2419 | if (Ty.isInvalid()) { | 
|  | 2420 | SkipUntil(tok::comma); | 
|  | 2421 | SkipUntil(tok::r_paren); | 
|  | 2422 | return ExprError(); | 
|  | 2423 | } | 
|  | 2424 |  | 
|  | 2425 | switch (ATT) { | 
|  | 2426 | case ATT_ArrayRank: { | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2427 | T.consumeClose(); | 
|  | 2428 | return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), NULL, | 
|  | 2429 | T.getCloseLocation()); | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2430 | } | 
|  | 2431 | case ATT_ArrayExtent: { | 
|  | 2432 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma)) { | 
|  | 2433 | SkipUntil(tok::r_paren); | 
|  | 2434 | return ExprError(); | 
|  | 2435 | } | 
|  | 2436 |  | 
|  | 2437 | ExprResult DimExpr = ParseExpression(); | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2438 | T.consumeClose(); | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2439 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2440 | return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(), | 
|  | 2441 | T.getCloseLocation()); | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 2442 | } | 
|  | 2443 | default: | 
|  | 2444 | break; | 
|  | 2445 | } | 
|  | 2446 | return ExprError(); | 
|  | 2447 | } | 
|  | 2448 |  | 
| John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2449 | /// ParseExpressionTrait - Parse built-in expression-trait | 
|  | 2450 | /// pseudo-functions like __is_lvalue_expr( xxx ). | 
|  | 2451 | /// | 
|  | 2452 | ///       primary-expression: | 
|  | 2453 | /// [Embarcadero]     expression-trait '(' expression ')' | 
|  | 2454 | /// | 
|  | 2455 | ExprResult Parser::ParseExpressionTrait() { | 
|  | 2456 | ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind()); | 
|  | 2457 | SourceLocation Loc = ConsumeToken(); | 
|  | 2458 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2459 | BalancedDelimiterTracker T(*this, tok::l_paren); | 
|  | 2460 | if (T.expectAndConsume(diag::err_expected_lparen)) | 
| John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2461 | return ExprError(); | 
|  | 2462 |  | 
|  | 2463 | ExprResult Expr = ParseExpression(); | 
|  | 2464 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2465 | T.consumeClose(); | 
| John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2466 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2467 | return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(), | 
|  | 2468 | T.getCloseLocation()); | 
| John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 2469 | } | 
|  | 2470 |  | 
|  | 2471 |  | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2472 | /// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a | 
|  | 2473 | /// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate | 
|  | 2474 | /// based on the context past the parens. | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2475 | ExprResult | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2476 | Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType, | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2477 | ParsedType &CastTy, | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2478 | BalancedDelimiterTracker &Tracker) { | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2479 | assert(getLang().CPlusPlus && "Should only be called for C++!"); | 
|  | 2480 | assert(ExprType == CastExpr && "Compound literals are not ambiguous!"); | 
|  | 2481 | assert(isTypeIdInParens() && "Not a type-id!"); | 
|  | 2482 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2483 | ExprResult Result(true); | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2484 | CastTy = ParsedType(); | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2485 |  | 
|  | 2486 | // We need to disambiguate a very ugly part of the C++ syntax: | 
|  | 2487 | // | 
|  | 2488 | // (T())x;  - type-id | 
|  | 2489 | // (T())*x; - type-id | 
|  | 2490 | // (T())/x; - expression | 
|  | 2491 | // (T());   - expression | 
|  | 2492 | // | 
|  | 2493 | // The bad news is that we cannot use the specialized tentative parser, since | 
|  | 2494 | // it can only verify that the thing inside the parens can be parsed as | 
|  | 2495 | // type-id, it is not useful for determining the context past the parens. | 
|  | 2496 | // | 
|  | 2497 | // The good news is that the parser can disambiguate this part without | 
| Argyrios Kyrtzidis | a558a89 | 2009-05-22 15:12:46 +0000 | [diff] [blame] | 2498 | // making any unnecessary Action calls. | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2499 | // | 
|  | 2500 | // It uses a scheme similar to parsing inline methods. The parenthesized | 
|  | 2501 | // tokens are cached, the context that follows is determined (possibly by | 
|  | 2502 | // parsing a cast-expression), and then we re-introduce the cached tokens | 
|  | 2503 | // into the token stream and parse them appropriately. | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2504 |  | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2505 | ParenParseOption ParseAs; | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2506 | CachedTokens Toks; | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2507 |  | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2508 | // Store the tokens of the parentheses. We will parse them after we determine | 
|  | 2509 | // the context that follows them. | 
| Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 2510 | if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) { | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2511 | // We didn't find the ')' we expected. | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2512 | Tracker.consumeClose(); | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2513 | return ExprError(); | 
|  | 2514 | } | 
|  | 2515 |  | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2516 | if (Tok.is(tok::l_brace)) { | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2517 | ParseAs = CompoundLiteral; | 
|  | 2518 | } else { | 
|  | 2519 | bool NotCastExpr; | 
| Eli Friedman | b53f08a | 2009-05-25 19:41:42 +0000 | [diff] [blame] | 2520 | // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression | 
|  | 2521 | if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) { | 
|  | 2522 | NotCastExpr = true; | 
|  | 2523 | } else { | 
|  | 2524 | // Try parsing the cast-expression that may follow. | 
|  | 2525 | // If it is not a cast-expression, NotCastExpr will be true and no token | 
|  | 2526 | // will be consumed. | 
|  | 2527 | Result = ParseCastExpression(false/*isUnaryExpression*/, | 
|  | 2528 | false/*isAddressofOperand*/, | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2529 | NotCastExpr, | 
| Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 2530 | // type-id has priority. | 
|  | 2531 | true/*isTypeCast*/); | 
| Eli Friedman | b53f08a | 2009-05-25 19:41:42 +0000 | [diff] [blame] | 2532 | } | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2533 |  | 
|  | 2534 | // If we parsed a cast-expression, it's really a type-id, otherwise it's | 
|  | 2535 | // an expression. | 
|  | 2536 | ParseAs = NotCastExpr ? SimpleExpr : CastExpr; | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2537 | } | 
|  | 2538 |  | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2539 | // The current token should go after the cached tokens. | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2540 | Toks.push_back(Tok); | 
|  | 2541 | // Re-enter the stored parenthesized tokens into the token stream, so we may | 
|  | 2542 | // parse them now. | 
|  | 2543 | PP.EnterTokenStream(Toks.data(), Toks.size(), | 
|  | 2544 | true/*DisableMacroExpansion*/, false/*OwnsTokens*/); | 
|  | 2545 | // Drop the current token and bring the first cached one. It's the same token | 
|  | 2546 | // as when we entered this function. | 
|  | 2547 | ConsumeAnyToken(); | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2548 |  | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2549 | if (ParseAs >= CompoundLiteral) { | 
| Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 2550 | // Parse the type declarator. | 
|  | 2551 | DeclSpec DS(AttrFactory); | 
|  | 2552 | ParseSpecifierQualifierList(DS); | 
|  | 2553 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); | 
|  | 2554 | ParseDeclarator(DeclaratorInfo); | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2555 |  | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2556 | // Match the ')'. | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2557 | Tracker.consumeClose(); | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2558 |  | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2559 | if (ParseAs == CompoundLiteral) { | 
|  | 2560 | ExprType = CompoundLiteral; | 
| Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 2561 | TypeResult Ty = ParseTypeName(); | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2562 | return ParseCompoundLiteralExpression(Ty.get(), | 
|  | 2563 | Tracker.getOpenLocation(), | 
|  | 2564 | Tracker.getCloseLocation()); | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2565 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2566 |  | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2567 | // We parsed '(' type-id ')' and the thing after it wasn't a '{'. | 
|  | 2568 | assert(ParseAs == CastExpr); | 
|  | 2569 |  | 
| Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 2570 | if (DeclaratorInfo.isInvalidType()) | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2571 | return ExprError(); | 
|  | 2572 |  | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2573 | // Result is what ParseCastExpression returned earlier. | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2574 | if (!Result.isInvalid()) | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2575 | Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(), | 
|  | 2576 | DeclaratorInfo, CastTy, | 
|  | 2577 | Tracker.getCloseLocation(), Result.take()); | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2578 | return move(Result); | 
|  | 2579 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 |  | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2581 | // Not a compound literal, and not followed by a cast-expression. | 
|  | 2582 | assert(ParseAs == SimpleExpr); | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2583 |  | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2584 | ExprType = SimpleExpr; | 
| Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 2585 | Result = ParseExpression(); | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2586 | if (!Result.isInvalid() && Tok.is(tok::r_paren)) | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2587 | Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(), | 
|  | 2588 | Tok.getLocation(), Result.take()); | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2589 |  | 
|  | 2590 | // Match the ')'. | 
|  | 2591 | if (Result.isInvalid()) { | 
|  | 2592 | SkipUntil(tok::r_paren); | 
|  | 2593 | return ExprError(); | 
|  | 2594 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 |  | 
| Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2596 | Tracker.consumeClose(); | 
| Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 2597 | return move(Result); | 
|  | 2598 | } |