blob: 177290b4381dc66439a938847d7a3cfc88eb740d [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Parser.cpp - C Language Family Parser ----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Parse/DeclSpec.h"
17#include "clang/Parse/Scope.h"
Chris Lattner6b3833c2009-03-05 07:24:28 +000018#include "llvm/Support/raw_ostream.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000019#include "ExtensionRAIIObject.h"
Daniel Dunbar47f99c92008-10-04 19:21:03 +000020#include "ParsePragma.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021using namespace clang;
22
Douglas Gregora252b232009-07-02 17:08:52 +000023/// \brief A comment handler that passes comments found by the preprocessor
24/// to the parser action.
25class ActionCommentHandler : public CommentHandler {
26 Action &Actions;
Mike Stump25cf7602009-09-09 15:08:12 +000027
Douglas Gregora252b232009-07-02 17:08:52 +000028public:
29 explicit ActionCommentHandler(Action &Actions) : Actions(Actions) { }
Mike Stump25cf7602009-09-09 15:08:12 +000030
Douglas Gregora252b232009-07-02 17:08:52 +000031 virtual void HandleComment(Preprocessor &PP, SourceRange Comment) {
32 Actions.ActOnComment(Comment);
33 }
34};
35
Chris Lattner4b009652007-07-25 00:24:17 +000036Parser::Parser(Preprocessor &pp, Action &actions)
Mike Stump25cf7602009-09-09 15:08:12 +000037 : CrashInfo(*this), PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
Douglas Gregorfb875d12009-08-25 00:17:23 +000038 GreaterThanIsOperator(true), TemplateParameterDepth(0) {
Chris Lattner4b009652007-07-25 00:24:17 +000039 Tok.setKind(tok::eof);
40 CurScope = 0;
41 NumCachedScopes = 0;
42 ParenCount = BracketCount = BraceCount = 0;
Chris Lattner5261d0c2009-03-28 19:18:32 +000043 ObjCImpDecl = DeclPtrTy();
Daniel Dunbar47f99c92008-10-04 19:21:03 +000044
45 // Add #pragma handlers. These are removed and destroyed in the
46 // destructor.
Ted Kremenek86a631b2009-03-23 22:28:25 +000047 PackHandler.reset(new
48 PragmaPackHandler(&PP.getIdentifierTable().get("pack"), actions));
49 PP.AddPragmaHandler(0, PackHandler.get());
Mike Stump25cf7602009-09-09 15:08:12 +000050
Ted Kremenek86a631b2009-03-23 22:28:25 +000051 UnusedHandler.reset(new
52 PragmaUnusedHandler(&PP.getIdentifierTable().get("unused"), actions,
53 *this));
54 PP.AddPragmaHandler(0, UnusedHandler.get());
Eli Friedman7e8364d2009-06-05 00:49:58 +000055
56 WeakHandler.reset(new
57 PragmaWeakHandler(&PP.getIdentifierTable().get("weak"), actions));
58 PP.AddPragmaHandler(0, WeakHandler.get());
Mike Stump25cf7602009-09-09 15:08:12 +000059
Douglas Gregora252b232009-07-02 17:08:52 +000060 CommentHandler.reset(new ActionCommentHandler(actions));
Mike Stump25cf7602009-09-09 15:08:12 +000061 PP.AddCommentHandler(CommentHandler.get());
Chris Lattner4b009652007-07-25 00:24:17 +000062}
63
Chris Lattner6b3833c2009-03-05 07:24:28 +000064/// If a crash happens while the parser is active, print out a line indicating
65/// what the current token is.
66void PrettyStackTraceParserEntry::print(llvm::raw_ostream &OS) const {
67 const Token &Tok = P.getCurToken();
Chris Lattner0e572c92009-03-05 07:27:50 +000068 if (Tok.is(tok::eof)) {
Chris Lattner6b3833c2009-03-05 07:24:28 +000069 OS << "<eof> parser at end of file\n";
70 return;
71 }
Mike Stump25cf7602009-09-09 15:08:12 +000072
Chris Lattner0e572c92009-03-05 07:27:50 +000073 if (Tok.getLocation().isInvalid()) {
74 OS << "<unknown> parser at unknown location\n";
75 return;
76 }
Mike Stump25cf7602009-09-09 15:08:12 +000077
Chris Lattner6b3833c2009-03-05 07:24:28 +000078 const Preprocessor &PP = P.getPreprocessor();
79 Tok.getLocation().print(OS, PP.getSourceManager());
80 OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
Douglas Gregor5ff0ee52008-12-30 03:27:21 +000081}
Chris Lattner4b009652007-07-25 00:24:17 +000082
Chris Lattner6b3833c2009-03-05 07:24:28 +000083
Chris Lattner9943e982008-11-22 00:59:29 +000084DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
Chris Lattner6b3833c2009-03-05 07:24:28 +000085 return Diags.Report(FullSourceLoc(Loc, PP.getSourceManager()), DiagID);
Chris Lattnerf006a222008-11-18 07:48:38 +000086}
87
Chris Lattner9943e982008-11-22 00:59:29 +000088DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
Chris Lattnerf006a222008-11-18 07:48:38 +000089 return Diag(Tok.getLocation(), DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +000090}
91
Douglas Gregor3bb30002009-02-26 21:00:50 +000092/// \brief Emits a diagnostic suggesting parentheses surrounding a
93/// given range.
94///
95/// \param Loc The location where we'll emit the diagnostic.
96/// \param Loc The kind of diagnostic to emit.
97/// \param ParenRange Source range enclosing code that should be parenthesized.
98void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
99 SourceRange ParenRange) {
Douglas Gregor61be3602009-02-27 17:53:17 +0000100 SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
101 if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
Douglas Gregor3bb30002009-02-26 21:00:50 +0000102 // We can't display the parentheses, so just dig the
103 // warning/error and return.
104 Diag(Loc, DK);
105 return;
106 }
Mike Stump25cf7602009-09-09 15:08:12 +0000107
108 Diag(Loc, DK)
Douglas Gregor61be3602009-02-27 17:53:17 +0000109 << CodeModificationHint::CreateInsertion(ParenRange.getBegin(), "(")
110 << CodeModificationHint::CreateInsertion(EndLoc, ")");
Douglas Gregor3bb30002009-02-26 21:00:50 +0000111}
112
Chris Lattner4b009652007-07-25 00:24:17 +0000113/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
114/// this helper function matches and consumes the specified RHS token if
115/// present. If not present, it emits the specified diagnostic indicating
116/// that the parser failed to match the RHS of the token at LHSLoc. LHSName
117/// should be the name of the unmatched LHS token.
118SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
119 SourceLocation LHSLoc) {
Mike Stumpeda58eb2008-06-19 19:28:49 +0000120
Chris Lattner17a5fb62007-10-09 17:23:58 +0000121 if (Tok.is(RHSTok))
Chris Lattner4b009652007-07-25 00:24:17 +0000122 return ConsumeAnyToken();
Mike Stumpeda58eb2008-06-19 19:28:49 +0000123
Chris Lattner4b009652007-07-25 00:24:17 +0000124 SourceLocation R = Tok.getLocation();
125 const char *LHSName = "unknown";
126 diag::kind DID = diag::err_parse_error;
127 switch (RHSTok) {
128 default: break;
129 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
130 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
131 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
132 case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break;
133 }
134 Diag(Tok, DID);
Chris Lattner921342c2008-11-23 23:17:07 +0000135 Diag(LHSLoc, diag::note_matching) << LHSName;
Chris Lattner4b009652007-07-25 00:24:17 +0000136 SkipUntil(RHSTok);
137 return R;
138}
139
140/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
141/// input. If so, it is consumed and false is returned.
142///
143/// If the input is malformed, this emits the specified diagnostic. Next, if
144/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
145/// returned.
146bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
147 const char *Msg, tok::TokenKind SkipToTok) {
Chris Lattner17a5fb62007-10-09 17:23:58 +0000148 if (Tok.is(ExpectedTok)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000149 ConsumeAnyToken();
150 return false;
151 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000152
Douglas Gregor3bb30002009-02-26 21:00:50 +0000153 const char *Spelling = 0;
Douglas Gregor61be3602009-02-27 17:53:17 +0000154 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
Mike Stump25cf7602009-09-09 15:08:12 +0000155 if (EndLoc.isValid() &&
Douglas Gregor61be3602009-02-27 17:53:17 +0000156 (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
Douglas Gregor3bb30002009-02-26 21:00:50 +0000157 // Show what code to insert to fix this problem.
Mike Stump25cf7602009-09-09 15:08:12 +0000158 Diag(EndLoc, DiagID)
Douglas Gregor3bb30002009-02-26 21:00:50 +0000159 << Msg
Douglas Gregor61be3602009-02-27 17:53:17 +0000160 << CodeModificationHint::CreateInsertion(EndLoc, Spelling);
Douglas Gregor3bb30002009-02-26 21:00:50 +0000161 } else
162 Diag(Tok, DiagID) << Msg;
163
Chris Lattner4b009652007-07-25 00:24:17 +0000164 if (SkipToTok != tok::unknown)
165 SkipUntil(SkipToTok);
166 return true;
167}
168
169//===----------------------------------------------------------------------===//
170// Error recovery.
171//===----------------------------------------------------------------------===//
172
173/// SkipUntil - Read tokens until we get to the specified token, then consume
174/// it (unless DontConsume is true). Because we cannot guarantee that the
175/// token will ever occur, this skips to the next token, or to some likely
176/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
177/// character.
Mike Stumpeda58eb2008-06-19 19:28:49 +0000178///
Chris Lattner4b009652007-07-25 00:24:17 +0000179/// If SkipUntil finds the specified token, it returns true, otherwise it
Mike Stumpeda58eb2008-06-19 19:28:49 +0000180/// returns false.
Chris Lattner4b009652007-07-25 00:24:17 +0000181bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
182 bool StopAtSemi, bool DontConsume) {
183 // We always want this function to skip at least one token if the first token
184 // isn't T and if not at EOF.
185 bool isFirstTokenSkipped = true;
186 while (1) {
187 // If we found one of the tokens, stop and return true.
188 for (unsigned i = 0; i != NumToks; ++i) {
Chris Lattner17a5fb62007-10-09 17:23:58 +0000189 if (Tok.is(Toks[i])) {
Chris Lattner4b009652007-07-25 00:24:17 +0000190 if (DontConsume) {
191 // Noop, don't consume the token.
192 } else {
193 ConsumeAnyToken();
194 }
195 return true;
196 }
197 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000198
Chris Lattner4b009652007-07-25 00:24:17 +0000199 switch (Tok.getKind()) {
200 case tok::eof:
201 // Ran out of tokens.
202 return false;
Mike Stumpeda58eb2008-06-19 19:28:49 +0000203
Chris Lattner4b009652007-07-25 00:24:17 +0000204 case tok::l_paren:
205 // Recursively skip properly-nested parens.
206 ConsumeParen();
207 SkipUntil(tok::r_paren, false);
208 break;
209 case tok::l_square:
210 // Recursively skip properly-nested square brackets.
211 ConsumeBracket();
212 SkipUntil(tok::r_square, false);
213 break;
214 case tok::l_brace:
215 // Recursively skip properly-nested braces.
216 ConsumeBrace();
217 SkipUntil(tok::r_brace, false);
218 break;
Mike Stumpeda58eb2008-06-19 19:28:49 +0000219
Chris Lattner4b009652007-07-25 00:24:17 +0000220 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
221 // Since the user wasn't looking for this token (if they were, it would
222 // already be handled), this isn't balanced. If there is a LHS token at a
223 // higher level, we will assume that this matches the unbalanced token
224 // and return it. Otherwise, this is a spurious RHS token, which we skip.
225 case tok::r_paren:
226 if (ParenCount && !isFirstTokenSkipped)
227 return false; // Matches something.
228 ConsumeParen();
229 break;
230 case tok::r_square:
231 if (BracketCount && !isFirstTokenSkipped)
232 return false; // Matches something.
233 ConsumeBracket();
234 break;
235 case tok::r_brace:
236 if (BraceCount && !isFirstTokenSkipped)
237 return false; // Matches something.
238 ConsumeBrace();
239 break;
Mike Stumpeda58eb2008-06-19 19:28:49 +0000240
Chris Lattner4b009652007-07-25 00:24:17 +0000241 case tok::string_literal:
242 case tok::wide_string_literal:
243 ConsumeStringToken();
244 break;
245 case tok::semi:
246 if (StopAtSemi)
247 return false;
248 // FALL THROUGH.
249 default:
250 // Skip this token.
251 ConsumeToken();
252 break;
253 }
254 isFirstTokenSkipped = false;
Mike Stumpeda58eb2008-06-19 19:28:49 +0000255 }
Chris Lattner4b009652007-07-25 00:24:17 +0000256}
257
258//===----------------------------------------------------------------------===//
259// Scope manipulation
260//===----------------------------------------------------------------------===//
261
262/// EnterScope - Start a new scope.
263void Parser::EnterScope(unsigned ScopeFlags) {
264 if (NumCachedScopes) {
265 Scope *N = ScopeCache[--NumCachedScopes];
266 N->Init(CurScope, ScopeFlags);
267 CurScope = N;
268 } else {
269 CurScope = new Scope(CurScope, ScopeFlags);
270 }
271}
272
273/// ExitScope - Pop a scope off the scope stack.
274void Parser::ExitScope() {
275 assert(CurScope && "Scope imbalance!");
276
Chris Lattner62231492007-10-09 20:37:18 +0000277 // Inform the actions module that this scope is going away if there are any
278 // decls in it.
279 if (!CurScope->decl_empty())
Steve Naroff9637a9b2007-10-09 22:01:59 +0000280 Actions.ActOnPopScope(Tok.getLocation(), CurScope);
Mike Stumpeda58eb2008-06-19 19:28:49 +0000281
Chris Lattner4b009652007-07-25 00:24:17 +0000282 Scope *OldScope = CurScope;
283 CurScope = OldScope->getParent();
Mike Stumpeda58eb2008-06-19 19:28:49 +0000284
Chris Lattner4b009652007-07-25 00:24:17 +0000285 if (NumCachedScopes == ScopeCacheSize)
286 delete OldScope;
287 else
288 ScopeCache[NumCachedScopes++] = OldScope;
289}
290
291
292
293
294//===----------------------------------------------------------------------===//
295// C99 6.9: External Definitions.
296//===----------------------------------------------------------------------===//
297
298Parser::~Parser() {
299 // If we still have scopes active, delete the scope tree.
300 delete CurScope;
Mike Stumpeda58eb2008-06-19 19:28:49 +0000301
Chris Lattner4b009652007-07-25 00:24:17 +0000302 // Free the scope cache.
303 for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
304 delete ScopeCache[i];
Daniel Dunbar47f99c92008-10-04 19:21:03 +0000305
306 // Remove the pragma handlers we installed.
Ted Kremenek86a631b2009-03-23 22:28:25 +0000307 PP.RemovePragmaHandler(0, PackHandler.get());
308 PackHandler.reset();
309 PP.RemovePragmaHandler(0, UnusedHandler.get());
310 UnusedHandler.reset();
Eli Friedman7e8364d2009-06-05 00:49:58 +0000311 PP.RemovePragmaHandler(0, WeakHandler.get());
312 WeakHandler.reset();
Douglas Gregora252b232009-07-02 17:08:52 +0000313 PP.RemoveCommentHandler(CommentHandler.get());
Chris Lattner4b009652007-07-25 00:24:17 +0000314}
315
316/// Initialize - Warm up the parser.
317///
318void Parser::Initialize() {
319 // Prime the lexer look-ahead.
320 ConsumeToken();
Mike Stumpeda58eb2008-06-19 19:28:49 +0000321
Chris Lattnera7549902007-08-26 06:24:45 +0000322 // Create the translation unit scope. Install it as the current scope.
Chris Lattner4b009652007-07-25 00:24:17 +0000323 assert(CurScope == 0 && "A scope is already active?");
Chris Lattnera7549902007-08-26 06:24:45 +0000324 EnterScope(Scope::DeclScope);
Steve Naroff9637a9b2007-10-09 22:01:59 +0000325 Actions.ActOnTranslationUnitScope(Tok.getLocation(), CurScope);
Mike Stumpeda58eb2008-06-19 19:28:49 +0000326
Chris Lattner17a5fb62007-10-09 17:23:58 +0000327 if (Tok.is(tok::eof) &&
Chris Lattner7bdc85d2007-08-25 05:47:03 +0000328 !getLang().CPlusPlus) // Empty source file is an extension in C
Chris Lattner4b009652007-07-25 00:24:17 +0000329 Diag(Tok, diag::ext_empty_source_file);
Mike Stumpeda58eb2008-06-19 19:28:49 +0000330
Chris Lattner32352462007-08-29 22:54:08 +0000331 // Initialization for Objective-C context sensitive keywords recognition.
Ted Kremenek42730c52008-01-07 19:49:32 +0000332 // Referenced in Parser::ParseObjCTypeQualifierList.
Chris Lattner32352462007-08-29 22:54:08 +0000333 if (getLang().ObjC1) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000334 ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
335 ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
336 ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
337 ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
338 ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
339 ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
Chris Lattner32352462007-08-29 22:54:08 +0000340 }
Daniel Dunbar4837ae72008-08-14 22:04:54 +0000341
342 Ident_super = &PP.getIdentifierTable().get("super");
Chris Lattner4b009652007-07-25 00:24:17 +0000343}
344
345/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
346/// action tells us to. This returns true if the EOF was encountered.
Chris Lattnera17991f2009-03-29 16:50:03 +0000347bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
348 Result = DeclGroupPtrTy();
Chris Lattnerc1aea812008-08-23 03:19:52 +0000349 if (Tok.is(tok::eof)) {
350 Actions.ActOnEndOfTranslationUnit();
351 return true;
352 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000353
Steve Naroffca44ffd2007-11-29 23:05:20 +0000354 Result = ParseExternalDeclaration();
Chris Lattner4b009652007-07-25 00:24:17 +0000355 return false;
356}
357
Chris Lattner4b009652007-07-25 00:24:17 +0000358/// ParseTranslationUnit:
359/// translation-unit: [C99 6.9]
Mike Stumpeda58eb2008-06-19 19:28:49 +0000360/// external-declaration
361/// translation-unit external-declaration
Chris Lattner4b009652007-07-25 00:24:17 +0000362void Parser::ParseTranslationUnit() {
Douglas Gregor95d40792008-12-10 06:34:36 +0000363 Initialize();
Mike Stumpeda58eb2008-06-19 19:28:49 +0000364
Chris Lattnera17991f2009-03-29 16:50:03 +0000365 DeclGroupPtrTy Res;
Steve Naroffca44ffd2007-11-29 23:05:20 +0000366 while (!ParseTopLevelDecl(Res))
Chris Lattner4b009652007-07-25 00:24:17 +0000367 /*parse them all*/;
Mike Stump25cf7602009-09-09 15:08:12 +0000368
Chris Lattnerf7df4d12008-08-23 02:00:52 +0000369 ExitScope();
370 assert(CurScope == 0 && "Scope imbalance!");
Chris Lattner4b009652007-07-25 00:24:17 +0000371}
372
373/// ParseExternalDeclaration:
Chris Lattnereb54a362008-12-08 21:59:01 +0000374///
Douglas Gregor61818c52008-11-21 16:10:08 +0000375/// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
Chris Lattner06f4e752007-08-10 20:57:02 +0000376/// function-definition
377/// declaration
Douglas Gregor383f1932009-08-24 12:17:54 +0000378/// [C++0x] empty-declaration
Chris Lattner4b009652007-07-25 00:24:17 +0000379/// [GNU] asm-definition
Chris Lattner06f4e752007-08-10 20:57:02 +0000380/// [GNU] __extension__ external-declaration
Chris Lattner4b009652007-07-25 00:24:17 +0000381/// [OBJC] objc-class-definition
382/// [OBJC] objc-class-declaration
383/// [OBJC] objc-alias-declaration
384/// [OBJC] objc-protocol-definition
385/// [OBJC] objc-method-definition
386/// [OBJC] @end
Douglas Gregor61818c52008-11-21 16:10:08 +0000387/// [C++] linkage-specification
Chris Lattner4b009652007-07-25 00:24:17 +0000388/// [GNU] asm-definition:
389/// simple-asm-expr ';'
390///
Douglas Gregor383f1932009-08-24 12:17:54 +0000391/// [C++0x] empty-declaration:
392/// ';'
393///
Douglas Gregor7a374722009-09-04 06:33:52 +0000394/// [C++0x/GNU] 'extern' 'template' declaration
Chris Lattnera17991f2009-03-29 16:50:03 +0000395Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration() {
396 DeclPtrTy SingleDecl;
Chris Lattner4b009652007-07-25 00:24:17 +0000397 switch (Tok.getKind()) {
398 case tok::semi:
Douglas Gregor383f1932009-08-24 12:17:54 +0000399 if (!getLang().CPlusPlus0x)
400 Diag(Tok, diag::ext_top_level_semi)
401 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Mike Stump25cf7602009-09-09 15:08:12 +0000402
Chris Lattner4b009652007-07-25 00:24:17 +0000403 ConsumeToken();
404 // TODO: Invoke action for top-level semicolon.
Chris Lattnera17991f2009-03-29 16:50:03 +0000405 return DeclGroupPtrTy();
Chris Lattnereb54a362008-12-08 21:59:01 +0000406 case tok::r_brace:
407 Diag(Tok, diag::err_expected_external_declaration);
408 ConsumeBrace();
Chris Lattnera17991f2009-03-29 16:50:03 +0000409 return DeclGroupPtrTy();
Chris Lattnereb54a362008-12-08 21:59:01 +0000410 case tok::eof:
411 Diag(Tok, diag::err_expected_external_declaration);
Chris Lattnera17991f2009-03-29 16:50:03 +0000412 return DeclGroupPtrTy();
Chris Lattner06f4e752007-08-10 20:57:02 +0000413 case tok::kw___extension__: {
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000414 // __extension__ silences extension warnings in the subexpression.
415 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner658e6872008-10-20 06:51:33 +0000416 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000417 return ParseExternalDeclaration();
Chris Lattner06f4e752007-08-10 20:57:02 +0000418 }
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000419 case tok::kw_asm: {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000420 OwningExprResult Result(ParseSimpleAsm());
Mike Stumpeda58eb2008-06-19 19:28:49 +0000421
Anders Carlssonf41100b2008-02-08 00:23:11 +0000422 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
423 "top-level asm block");
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000424
Chris Lattnera17991f2009-03-29 16:50:03 +0000425 if (Result.isInvalid())
426 return DeclGroupPtrTy();
427 SingleDecl = Actions.ActOnFileScopeAsmDecl(Tok.getLocation(), move(Result));
428 break;
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000429 }
Chris Lattner4b009652007-07-25 00:24:17 +0000430 case tok::at:
Chris Lattnera17991f2009-03-29 16:50:03 +0000431 // @ is not a legal token unless objc is enabled, no need to check for ObjC.
432 /// FIXME: ParseObjCAtDirectives should return a DeclGroup for things like
433 /// @class foo, bar;
434 SingleDecl = ParseObjCAtDirectives();
435 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000436 case tok::minus:
Chris Lattner4b009652007-07-25 00:24:17 +0000437 case tok::plus:
Chris Lattnera17991f2009-03-29 16:50:03 +0000438 if (!getLang().ObjC1) {
439 Diag(Tok, diag::err_expected_external_declaration);
440 ConsumeToken();
441 return DeclGroupPtrTy();
442 }
443 SingleDecl = ParseObjCMethodDefinition();
444 break;
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000445 case tok::kw_using:
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000446 case tok::kw_namespace:
Chris Lattner4b009652007-07-25 00:24:17 +0000447 case tok::kw_typedef:
Douglas Gregorb3bec712008-12-01 23:54:00 +0000448 case tok::kw_template:
449 case tok::kw_export: // As in 'export template'
Anders Carlssonab041982009-03-11 16:27:10 +0000450 case tok::kw_static_assert:
Chris Lattner9c135722007-08-25 18:15:16 +0000451 // A function definition cannot start with a these keywords.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000452 {
453 SourceLocation DeclEnd;
454 return ParseDeclaration(Declarator::FileContext, DeclEnd);
455 }
Douglas Gregor7a374722009-09-04 06:33:52 +0000456 case tok::kw_extern:
457 if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
458 // Extern templates
459 SourceLocation ExternLoc = ConsumeToken();
460 SourceLocation TemplateLoc = ConsumeToken();
461 SourceLocation DeclEnd;
462 return Actions.ConvertDeclToDeclGroup(
463 ParseExplicitInstantiation(ExternLoc, TemplateLoc, DeclEnd));
464 }
Mike Stump25cf7602009-09-09 15:08:12 +0000465
Douglas Gregor7a374722009-09-04 06:33:52 +0000466 // FIXME: Detect C++ linkage specifications here?
Mike Stump25cf7602009-09-09 15:08:12 +0000467
Douglas Gregor7a374722009-09-04 06:33:52 +0000468 // Fall through to handle other declarations or function definitions.
Mike Stump25cf7602009-09-09 15:08:12 +0000469
Chris Lattner4b009652007-07-25 00:24:17 +0000470 default:
471 // We can't tell whether this is a function-definition or declaration yet.
472 return ParseDeclarationOrFunctionDefinition();
473 }
Mike Stump25cf7602009-09-09 15:08:12 +0000474
Chris Lattnera17991f2009-03-29 16:50:03 +0000475 // This routine returns a DeclGroup, if the thing we parsed only contains a
476 // single decl, convert it now.
477 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000478}
479
Douglas Gregore3298aa2009-05-12 21:31:51 +0000480/// \brief Determine whether the current token, if it occurs after a
481/// declarator, continues a declaration or declaration list.
482bool Parser::isDeclarationAfterDeclarator() {
483 return Tok.is(tok::equal) || // int X()= -> not a function def
484 Tok.is(tok::comma) || // int X(), -> not a function def
485 Tok.is(tok::semi) || // int X(); -> not a function def
486 Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
487 Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def
488 (getLang().CPlusPlus &&
489 Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++]
490}
491
492/// \brief Determine whether the current token, if it occurs after a
493/// declarator, indicates the start of a function definition.
494bool Parser::isStartOfFunctionDefinition() {
495 return Tok.is(tok::l_brace) || // int X() {}
Mike Stump25cf7602009-09-09 15:08:12 +0000496 (!getLang().CPlusPlus &&
Douglas Gregore3298aa2009-05-12 21:31:51 +0000497 isDeclarationSpecifier()) || // int X(f) int f; {}
498 (getLang().CPlusPlus &&
499 (Tok.is(tok::colon) || // X() : Base() {} (used for ctors)
500 Tok.is(tok::kw_try))); // X() try { ... }
501}
502
Chris Lattner4b009652007-07-25 00:24:17 +0000503/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
504/// a declaration. We can't tell which we have until we read up to the
Douglas Gregor52473432008-12-24 02:52:09 +0000505/// compound-statement in function-definition. TemplateParams, if
506/// non-NULL, provides the template parameters when we're parsing a
Mike Stump25cf7602009-09-09 15:08:12 +0000507/// C++ template-declaration.
Chris Lattner4b009652007-07-25 00:24:17 +0000508///
509/// function-definition: [C99 6.9.1]
Chris Lattnera15e9d22008-04-05 05:52:15 +0000510/// decl-specs declarator declaration-list[opt] compound-statement
511/// [C90] function-definition: [C99 6.7.1] - implicit int result
Mike Stumpeda58eb2008-06-19 19:28:49 +0000512/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
Chris Lattnera15e9d22008-04-05 05:52:15 +0000513///
Chris Lattner4b009652007-07-25 00:24:17 +0000514/// declaration: [C99 6.7]
Chris Lattneraac973e2007-08-22 06:06:56 +0000515/// declaration-specifiers init-declarator-list[opt] ';'
516/// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
Chris Lattner4b009652007-07-25 00:24:17 +0000517/// [OMP] threadprivate-directive [TODO]
518///
Chris Lattnera17991f2009-03-29 16:50:03 +0000519Parser::DeclGroupPtrTy
Douglas Gregor76406202009-05-12 21:43:46 +0000520Parser::ParseDeclarationOrFunctionDefinition(AccessSpecifier AS) {
Chris Lattner4b009652007-07-25 00:24:17 +0000521 // Parse the common declaration-specifiers piece.
522 DeclSpec DS;
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000523 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stumpeda58eb2008-06-19 19:28:49 +0000524
Chris Lattner4b009652007-07-25 00:24:17 +0000525 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
526 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner17a5fb62007-10-09 17:23:58 +0000527 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000528 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000529 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
530 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000531 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000532
Daniel Dunbar28680d12008-09-26 04:48:09 +0000533 // ObjC2 allows prefix attributes on class interfaces and protocols.
534 // FIXME: This still needs better diagnostics. We should only accept
535 // attributes here, no types, etc.
Chris Lattner17a5fb62007-10-09 17:23:58 +0000536 if (getLang().ObjC2 && Tok.is(tok::at)) {
Steve Narofffb367882007-08-20 21:31:48 +0000537 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump25cf7602009-09-09 15:08:12 +0000538 if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
Daniel Dunbar28680d12008-09-26 04:48:09 +0000539 !Tok.isObjCAtKeyword(tok::objc_protocol)) {
540 Diag(Tok, diag::err_objc_unexpected_attr);
Chris Lattner847f5c12007-12-27 19:57:00 +0000541 SkipUntil(tok::semi); // FIXME: better skip?
Chris Lattnera17991f2009-03-29 16:50:03 +0000542 return DeclGroupPtrTy();
Chris Lattner847f5c12007-12-27 19:57:00 +0000543 }
Fariborz Jahanianf9c0a0d2008-01-02 19:17:38 +0000544 const char *PrevSpec = 0;
John McCall9f6e0972009-08-03 20:12:06 +0000545 unsigned DiagID;
546 if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
547 Diag(AtLoc, DiagID) << PrevSpec;
Mike Stump25cf7602009-09-09 15:08:12 +0000548
Chris Lattnera17991f2009-03-29 16:50:03 +0000549 DeclPtrTy TheDecl;
Daniel Dunbar28680d12008-09-26 04:48:09 +0000550 if (Tok.isObjCAtKeyword(tok::objc_protocol))
Chris Lattnera17991f2009-03-29 16:50:03 +0000551 TheDecl = ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
552 else
553 TheDecl = ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
554 return Actions.ConvertDeclToDeclGroup(TheDecl);
Steve Narofffb367882007-08-20 21:31:48 +0000555 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000556
Chris Lattner806a5f52008-01-12 07:05:38 +0000557 // If the declspec consisted only of 'extern' and we have a string
558 // literal following it, this must be a C++ linkage specifier like
559 // 'extern "C"'.
Chris Lattner1b5c9f72008-01-12 07:08:43 +0000560 if (Tok.is(tok::string_literal) && getLang().CPlusPlus &&
Chris Lattner806a5f52008-01-12 07:05:38 +0000561 DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
Chris Lattnera17991f2009-03-29 16:50:03 +0000562 DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
563 DeclPtrTy TheDecl = ParseLinkage(Declarator::FileContext);
564 return Actions.ConvertDeclToDeclGroup(TheDecl);
565 }
Chris Lattner806a5f52008-01-12 07:05:38 +0000566
Chris Lattner4b009652007-07-25 00:24:17 +0000567 // Parse the first declarator.
568 Declarator DeclaratorInfo(DS, Declarator::FileContext);
569 ParseDeclarator(DeclaratorInfo);
570 // Error parsing the declarator?
Douglas Gregor6704b312008-11-17 22:58:34 +0000571 if (!DeclaratorInfo.hasName()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000572 // If so, skip until the semi-colon or a }.
Douglas Gregor6f730612008-12-01 23:03:32 +0000573 SkipUntil(tok::r_brace, true, true);
Chris Lattner17a5fb62007-10-09 17:23:58 +0000574 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000575 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000576 return DeclGroupPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +0000577 }
578
Douglas Gregore3298aa2009-05-12 21:31:51 +0000579 // If we have a declaration or declarator list, handle it.
580 if (isDeclarationAfterDeclarator()) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000581 // Parse the init-declarator-list for a normal declaration.
Chris Lattner2c41d482009-03-29 17:18:04 +0000582 DeclGroupPtrTy DG =
583 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
584 // Eat the semi colon after the declaration.
John McCallfcb32f42009-07-31 02:20:35 +0000585 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
Chris Lattner2c41d482009-03-29 17:18:04 +0000586 return DG;
Chris Lattnera17991f2009-03-29 16:50:03 +0000587 }
Mike Stump25cf7602009-09-09 15:08:12 +0000588
Chris Lattnera17991f2009-03-29 16:50:03 +0000589 if (DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregore3298aa2009-05-12 21:31:51 +0000590 isStartOfFunctionDefinition()) {
Steve Naroff83298852008-02-14 02:58:32 +0000591 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
592 Diag(Tok, diag::err_function_declared_typedef);
Mike Stumpeda58eb2008-06-19 19:28:49 +0000593
Steve Naroff83298852008-02-14 02:58:32 +0000594 if (Tok.is(tok::l_brace)) {
595 // This recovery skips the entire function body. It would be nice
Douglas Gregordd861062008-12-05 18:15:24 +0000596 // to simply call ParseFunctionDefinition() below, however Sema
Steve Naroff83298852008-02-14 02:58:32 +0000597 // assumes the declarator represents a function, not a typedef.
598 ConsumeBrace();
599 SkipUntil(tok::r_brace, true);
600 } else {
601 SkipUntil(tok::semi);
602 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000603 return DeclGroupPtrTy();
Steve Naroff83298852008-02-14 02:58:32 +0000604 }
Chris Lattnera17991f2009-03-29 16:50:03 +0000605 DeclPtrTy TheDecl = ParseFunctionDefinition(DeclaratorInfo);
606 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner4b009652007-07-25 00:24:17 +0000607 }
Mike Stump25cf7602009-09-09 15:08:12 +0000608
Chris Lattnera17991f2009-03-29 16:50:03 +0000609 if (DeclaratorInfo.isFunctionDeclarator())
610 Diag(Tok, diag::err_expected_fn_body);
611 else
612 Diag(Tok, diag::err_invalid_token_after_toplevel_declarator);
613 SkipUntil(tok::semi);
614 return DeclGroupPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +0000615}
616
617/// ParseFunctionDefinition - We parsed and verified that the specified
618/// Declarator is well formed. If this is a K&R-style function, read the
619/// parameters declaration-list, then start the compound-statement.
620///
Chris Lattnera15e9d22008-04-05 05:52:15 +0000621/// function-definition: [C99 6.9.1]
622/// decl-specs declarator declaration-list[opt] compound-statement
623/// [C90] function-definition: [C99 6.7.1] - implicit int result
Mike Stumpeda58eb2008-06-19 19:28:49 +0000624/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000625/// [C++] function-definition: [C++ 8.4]
Chris Lattner2c41d482009-03-29 17:18:04 +0000626/// decl-specifier-seq[opt] declarator ctor-initializer[opt]
627/// function-body
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000628/// [C++] function-definition: [C++ 8.4]
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000629/// decl-specifier-seq[opt] declarator function-try-block
Chris Lattner4b009652007-07-25 00:24:17 +0000630///
Douglas Gregor19d10652009-06-24 00:54:41 +0000631Parser::DeclPtrTy Parser::ParseFunctionDefinition(Declarator &D,
632 const ParsedTemplateInfo &TemplateInfo) {
Chris Lattner4b009652007-07-25 00:24:17 +0000633 const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0);
634 assert(FnTypeInfo.Kind == DeclaratorChunk::Function &&
635 "This isn't a function declarator!");
636 const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun;
Mike Stumpeda58eb2008-06-19 19:28:49 +0000637
Chris Lattnera15e9d22008-04-05 05:52:15 +0000638 // If this is C90 and the declspecs were completely missing, fudge in an
639 // implicit int. We do this here because this is the only place where
640 // declaration-specifiers are completely optional in the grammar.
Chris Lattner92eca3e2009-02-27 18:35:46 +0000641 if (getLang().ImplicitInt && D.getDeclSpec().isEmpty()) {
Chris Lattnera15e9d22008-04-05 05:52:15 +0000642 const char *PrevSpec;
John McCall9f6e0972009-08-03 20:12:06 +0000643 unsigned DiagID;
Chris Lattner509fc802008-10-20 02:01:34 +0000644 D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
645 D.getIdentifierLoc(),
John McCall9f6e0972009-08-03 20:12:06 +0000646 PrevSpec, DiagID);
Sebastian Redl0c986032009-02-09 18:23:29 +0000647 D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
Chris Lattnera15e9d22008-04-05 05:52:15 +0000648 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000649
Chris Lattner4b009652007-07-25 00:24:17 +0000650 // If this declaration was formed with a K&R-style identifier list for the
651 // arguments, parse declarations for all of the args next.
652 // int foo(a,b) int a; float b; {}
653 if (!FTI.hasPrototype && FTI.NumArgs != 0)
654 ParseKNRParamDeclarations(D);
655
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000656 // We should have either an opening brace or, in a C++ constructor,
657 // we may have a colon.
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000658 if (Tok.isNot(tok::l_brace) && Tok.isNot(tok::colon) &&
659 Tok.isNot(tok::kw_try)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000660 Diag(Tok, diag::err_expected_fn_body);
661
662 // Skip over garbage, until we get to '{'. Don't eat the '{'.
663 SkipUntil(tok::l_brace, true, true);
Mike Stumpeda58eb2008-06-19 19:28:49 +0000664
Chris Lattner4b009652007-07-25 00:24:17 +0000665 // If we didn't find the '{', bail out.
Chris Lattner17a5fb62007-10-09 17:23:58 +0000666 if (Tok.isNot(tok::l_brace))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000667 return DeclPtrTy();
Chris Lattner4b009652007-07-25 00:24:17 +0000668 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000669
Chris Lattnerea148702007-10-09 17:14:05 +0000670 // Enter a scope for the function body.
Douglas Gregor95d40792008-12-10 06:34:36 +0000671 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stumpeda58eb2008-06-19 19:28:49 +0000672
Chris Lattnerea148702007-10-09 17:14:05 +0000673 // Tell the actions module that we have entered a function definition with the
674 // specified Declarator for the function.
Mike Stump25cf7602009-09-09 15:08:12 +0000675 DeclPtrTy Res = TemplateInfo.TemplateParams?
Douglas Gregor19d10652009-06-24 00:54:41 +0000676 Actions.ActOnStartOfFunctionTemplateDef(CurScope,
677 Action::MultiTemplateParamsArg(Actions,
678 TemplateInfo.TemplateParams->data(),
679 TemplateInfo.TemplateParams->size()),
680 D)
681 : Actions.ActOnStartOfFunctionDef(CurScope, D);
Mike Stumpeda58eb2008-06-19 19:28:49 +0000682
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000683 if (Tok.is(tok::kw_try))
684 return ParseFunctionTryBlock(Res);
685
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000686 // If we have a colon, then we're probably parsing a C++
687 // ctor-initializer.
688 if (Tok.is(tok::colon))
689 ParseConstructorInitializer(Res);
Fariborz Jahanianbc397fc2009-07-14 20:06:22 +0000690 else
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000691 Actions.ActOnDefaultCtorInitializers(Res);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000692
Chris Lattner0818a7a2009-03-05 00:49:17 +0000693 return ParseFunctionStatementBody(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000694}
695
696/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
697/// types for a function with a K&R-style identifier list for arguments.
698void Parser::ParseKNRParamDeclarations(Declarator &D) {
699 // We know that the top-level of this declarator is a function.
700 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
701
Chris Lattner3e254fb2008-04-08 04:40:51 +0000702 // Enter function-declaration scope, limiting any declarators to the
703 // function prototype scope, including parameter declarators.
Douglas Gregorcab994d2009-01-09 22:42:13 +0000704 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000705
Chris Lattner4b009652007-07-25 00:24:17 +0000706 // Read all the argument declarations.
707 while (isDeclarationSpecifier()) {
708 SourceLocation DSStart = Tok.getLocation();
Mike Stumpeda58eb2008-06-19 19:28:49 +0000709
Chris Lattner4b009652007-07-25 00:24:17 +0000710 // Parse the common declaration-specifiers piece.
711 DeclSpec DS;
712 ParseDeclarationSpecifiers(DS);
Mike Stumpeda58eb2008-06-19 19:28:49 +0000713
Chris Lattner4b009652007-07-25 00:24:17 +0000714 // C99 6.9.1p6: 'each declaration in the declaration list shall have at
715 // least one declarator'.
716 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
717 // the declarations though. It's trivial to ignore them, really hard to do
718 // anything else with them.
Chris Lattner17a5fb62007-10-09 17:23:58 +0000719 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000720 Diag(DSStart, diag::err_declaration_does_not_declare_param);
721 ConsumeToken();
722 continue;
723 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000724
Chris Lattner4b009652007-07-25 00:24:17 +0000725 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
726 // than register.
727 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
728 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
729 Diag(DS.getStorageClassSpecLoc(),
730 diag::err_invalid_storage_class_in_func_decl);
731 DS.ClearStorageClassSpecs();
732 }
733 if (DS.isThreadSpecified()) {
734 Diag(DS.getThreadSpecLoc(),
735 diag::err_invalid_storage_class_in_func_decl);
736 DS.ClearStorageClassSpecs();
737 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000738
Chris Lattner4b009652007-07-25 00:24:17 +0000739 // Parse the first declarator attached to this declspec.
740 Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
741 ParseDeclarator(ParmDeclarator);
742
743 // Handle the full declarator list.
744 while (1) {
Chris Lattner5261d0c2009-03-28 19:18:32 +0000745 Action::AttrTy *AttrList;
Chris Lattner4b009652007-07-25 00:24:17 +0000746 // If attributes are present, parse them.
Chris Lattner17a5fb62007-10-09 17:23:58 +0000747 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000748 // FIXME: attach attributes too.
749 AttrList = ParseAttributes();
Mike Stumpeda58eb2008-06-19 19:28:49 +0000750
Chris Lattner4b009652007-07-25 00:24:17 +0000751 // Ask the actions module to compute the type for this declarator.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000752 Action::DeclPtrTy Param =
Chris Lattner3e254fb2008-04-08 04:40:51 +0000753 Actions.ActOnParamDeclarator(CurScope, ParmDeclarator);
Steve Narofffaed3bf2007-09-10 20:51:04 +0000754
Mike Stumpeda58eb2008-06-19 19:28:49 +0000755 if (Param &&
Chris Lattner4b009652007-07-25 00:24:17 +0000756 // A missing identifier has already been diagnosed.
757 ParmDeclarator.getIdentifier()) {
758
759 // Scan the argument list looking for the correct param to apply this
760 // type.
761 for (unsigned i = 0; ; ++i) {
762 // C99 6.9.1p6: those declarators shall declare only identifiers from
763 // the identifier list.
764 if (i == FTI.NumArgs) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000765 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
Chris Lattnerb12ef862008-11-19 07:51:13 +0000766 << ParmDeclarator.getIdentifier();
Chris Lattner4b009652007-07-25 00:24:17 +0000767 break;
768 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000769
Chris Lattner4b009652007-07-25 00:24:17 +0000770 if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
771 // Reject redefinitions of parameters.
Chris Lattner3e254fb2008-04-08 04:40:51 +0000772 if (FTI.ArgInfo[i].Param) {
Chris Lattner4b009652007-07-25 00:24:17 +0000773 Diag(ParmDeclarator.getIdentifierLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +0000774 diag::err_param_redefinition)
Chris Lattnerb12ef862008-11-19 07:51:13 +0000775 << ParmDeclarator.getIdentifier();
Chris Lattner4b009652007-07-25 00:24:17 +0000776 } else {
Chris Lattner3e254fb2008-04-08 04:40:51 +0000777 FTI.ArgInfo[i].Param = Param;
Chris Lattner4b009652007-07-25 00:24:17 +0000778 }
779 break;
780 }
781 }
782 }
783
784 // If we don't have a comma, it is either the end of the list (a ';') or
785 // an error, bail out.
Chris Lattner17a5fb62007-10-09 17:23:58 +0000786 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000787 break;
Mike Stumpeda58eb2008-06-19 19:28:49 +0000788
Chris Lattner4b009652007-07-25 00:24:17 +0000789 // Consume the comma.
790 ConsumeToken();
Mike Stumpeda58eb2008-06-19 19:28:49 +0000791
Chris Lattner4b009652007-07-25 00:24:17 +0000792 // Parse the next declarator.
793 ParmDeclarator.clear();
794 ParseDeclarator(ParmDeclarator);
795 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000796
Chris Lattner17a5fb62007-10-09 17:23:58 +0000797 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000798 ConsumeToken();
799 } else {
800 Diag(Tok, diag::err_parse_error);
801 // Skip to end of block or statement
802 SkipUntil(tok::semi, true);
Chris Lattner17a5fb62007-10-09 17:23:58 +0000803 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000804 ConsumeToken();
805 }
806 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000807
Chris Lattner4b009652007-07-25 00:24:17 +0000808 // The actions module must verify that all arguments were declared.
Douglas Gregor3faaa812009-04-01 23:51:29 +0000809 Actions.ActOnFinishKNRParamDeclarations(CurScope, D, Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000810}
811
812
813/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
814/// allowed to be a wide string, and is not subject to character translation.
815///
816/// [GNU] asm-string-literal:
817/// string-literal
818///
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000819Parser::OwningExprResult Parser::ParseAsmStringLiteral() {
Chris Lattner4b009652007-07-25 00:24:17 +0000820 if (!isTokenStringLiteral()) {
821 Diag(Tok, diag::err_expected_string_literal);
Sebastian Redl10c32952008-12-11 19:30:53 +0000822 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000823 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000824
Sebastian Redl39d4f022008-12-11 22:51:44 +0000825 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000826 if (Res.isInvalid()) return move(Res);
Mike Stumpeda58eb2008-06-19 19:28:49 +0000827
Chris Lattner4b009652007-07-25 00:24:17 +0000828 // TODO: Diagnose: wide string literal in 'asm'
Mike Stumpeda58eb2008-06-19 19:28:49 +0000829
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000830 return move(Res);
Chris Lattner4b009652007-07-25 00:24:17 +0000831}
832
833/// ParseSimpleAsm
834///
835/// [GNU] simple-asm-expr:
836/// 'asm' '(' asm-string-literal ')'
837///
Sebastian Redl0c986032009-02-09 18:23:29 +0000838Parser::OwningExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
Chris Lattner17a5fb62007-10-09 17:23:58 +0000839 assert(Tok.is(tok::kw_asm) && "Not an asm!");
Anders Carlsson4f7f4412008-02-08 00:33:21 +0000840 SourceLocation Loc = ConsumeToken();
Mike Stumpeda58eb2008-06-19 19:28:49 +0000841
Chris Lattner17a5fb62007-10-09 17:23:58 +0000842 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000843 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Sebastian Redl10c32952008-12-11 19:30:53 +0000844 return ExprError();
Chris Lattner4b009652007-07-25 00:24:17 +0000845 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000846
Sebastian Redl0c986032009-02-09 18:23:29 +0000847 Loc = ConsumeParen();
Mike Stumpeda58eb2008-06-19 19:28:49 +0000848
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000849 OwningExprResult Result(ParseAsmStringLiteral());
Mike Stumpeda58eb2008-06-19 19:28:49 +0000850
Sebastian Redl0c986032009-02-09 18:23:29 +0000851 if (Result.isInvalid()) {
852 SkipUntil(tok::r_paren, true, true);
853 if (EndLoc)
854 *EndLoc = Tok.getLocation();
855 ConsumeAnyToken();
856 } else {
857 Loc = MatchRHSPunctuation(tok::r_paren, Loc);
858 if (EndLoc)
859 *EndLoc = Loc;
860 }
Mike Stumpeda58eb2008-06-19 19:28:49 +0000861
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000862 return move(Result);
Chris Lattner4b009652007-07-25 00:24:17 +0000863}
864
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000865/// TryAnnotateTypeOrScopeToken - If the current token position is on a
866/// typename (possibly qualified in C++) or a C++ scope specifier not followed
867/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
868/// with a single annotation token representing the typename or C++ scope
869/// respectively.
870/// This simplifies handling of C++ scope specifiers and allows efficient
871/// backtracking without the need to re-parse and resolve nested-names and
872/// typenames.
Argiris Kirtzidisfc332322008-11-26 21:51:07 +0000873/// It will mainly be called when we expect to treat identifiers as typenames
874/// (if they are typenames). For example, in C we do not expect identifiers
875/// inside expressions to be treated as typenames so it will not be called
876/// for expressions in C.
877/// The benefit for C/ObjC is that a typename will be annotated and
Steve Naroff7b36a1b2009-01-28 19:39:02 +0000878/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
Argiris Kirtzidisfc332322008-11-26 21:51:07 +0000879/// will not be called twice, once to check whether we have a declaration
880/// specifier, and another one to get the actual type inside
881/// ParseDeclarationSpecifiers).
Chris Lattner1e015942009-01-04 23:23:14 +0000882///
Chris Lattner8eabc062009-06-26 04:27:47 +0000883/// This returns true if the token was annotated or an unrecoverable error
884/// occurs.
Mike Stump25cf7602009-09-09 15:08:12 +0000885///
Chris Lattner2c301452009-01-05 00:13:00 +0000886/// Note that this routine emits an error if you call it with ::new or ::delete
887/// as the current tokens, so only call it in contexts where these are invalid.
Douglas Gregorc03d3302009-08-25 22:51:20 +0000888bool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext) {
Mike Stump25cf7602009-09-09 15:08:12 +0000889 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
Douglas Gregord3022602009-03-27 23:10:48 +0000890 || Tok.is(tok::kw_typename)) &&
Chris Lattner8376d2e2009-01-05 01:24:05 +0000891 "Cannot be a type or scope token!");
Mike Stump25cf7602009-09-09 15:08:12 +0000892
Douglas Gregord3022602009-03-27 23:10:48 +0000893 if (Tok.is(tok::kw_typename)) {
894 // Parse a C++ typename-specifier, e.g., "typename T::type".
895 //
896 // typename-specifier:
897 // 'typename' '::' [opt] nested-name-specifier identifier
Mike Stump25cf7602009-09-09 15:08:12 +0000898 // 'typename' '::' [opt] nested-name-specifier template [opt]
Douglas Gregor77da5802009-04-01 00:28:59 +0000899 // simple-template-id
Douglas Gregord3022602009-03-27 23:10:48 +0000900 SourceLocation TypenameLoc = ConsumeToken();
901 CXXScopeSpec SS;
Mike Stump25cf7602009-09-09 15:08:12 +0000902 bool HadNestedNameSpecifier
Douglas Gregor681d31d2009-09-02 22:59:36 +0000903 = ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregord3022602009-03-27 23:10:48 +0000904 if (!HadNestedNameSpecifier) {
905 Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
906 return false;
907 }
908
909 TypeResult Ty;
910 if (Tok.is(tok::identifier)) {
911 // FIXME: check whether the next token is '<', first!
Mike Stump25cf7602009-09-09 15:08:12 +0000912 Ty = Actions.ActOnTypenameType(TypenameLoc, SS, *Tok.getIdentifierInfo(),
Douglas Gregord3022602009-03-27 23:10:48 +0000913 Tok.getLocation());
Douglas Gregor77da5802009-04-01 00:28:59 +0000914 } else if (Tok.is(tok::annot_template_id)) {
Mike Stump25cf7602009-09-09 15:08:12 +0000915 TemplateIdAnnotation *TemplateId
Douglas Gregor77da5802009-04-01 00:28:59 +0000916 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
917 if (TemplateId->Kind == TNK_Function_template) {
918 Diag(Tok, diag::err_typename_refers_to_non_type_template)
919 << Tok.getAnnotationRange();
920 return false;
921 }
Douglas Gregord3022602009-03-27 23:10:48 +0000922
Douglas Gregord7cb0372009-04-01 21:51:26 +0000923 AnnotateTemplateIdTokenAsType(0);
Mike Stump25cf7602009-09-09 15:08:12 +0000924 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor77da5802009-04-01 00:28:59 +0000925 "AnnotateTemplateIdTokenAsType isn't working properly");
Douglas Gregord7cb0372009-04-01 21:51:26 +0000926 if (Tok.getAnnotationValue())
927 Ty = Actions.ActOnTypenameType(TypenameLoc, SS, SourceLocation(),
928 Tok.getAnnotationValue());
929 else
930 Ty = true;
Douglas Gregor77da5802009-04-01 00:28:59 +0000931 } else {
932 Diag(Tok, diag::err_expected_type_name_after_typename)
933 << SS.getRange();
934 return false;
935 }
936
Douglas Gregor77da5802009-04-01 00:28:59 +0000937 Tok.setKind(tok::annot_typename);
Douglas Gregord7cb0372009-04-01 21:51:26 +0000938 Tok.setAnnotationValue(Ty.isInvalid()? 0 : Ty.get());
Douglas Gregor77da5802009-04-01 00:28:59 +0000939 Tok.setAnnotationEndLoc(Tok.getLocation());
940 Tok.setLocation(TypenameLoc);
941 PP.AnnotateCachedTokens(Tok);
942 return true;
Douglas Gregord3022602009-03-27 23:10:48 +0000943 }
944
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000945 CXXScopeSpec SS;
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +0000946 if (getLang().CPlusPlus)
Douglas Gregor681d31d2009-09-02 22:59:36 +0000947 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, EnteringContext);
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000948
949 if (Tok.is(tok::identifier)) {
Chris Lattnera9d6ec72009-01-05 01:49:50 +0000950 // Determine whether the identifier is a type name.
Mike Stump25cf7602009-09-09 15:08:12 +0000951 if (TypeTy *Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor1075a162009-02-04 17:00:24 +0000952 Tok.getLocation(), CurScope, &SS)) {
Chris Lattnera9d6ec72009-01-05 01:49:50 +0000953 // This is a typename. Replace the current token in-place with an
954 // annotation type token.
Chris Lattner5d7eace2009-01-06 05:06:21 +0000955 Tok.setKind(tok::annot_typename);
Chris Lattnera9d6ec72009-01-05 01:49:50 +0000956 Tok.setAnnotationValue(Ty);
957 Tok.setAnnotationEndLoc(Tok.getLocation());
958 if (SS.isNotEmpty()) // it was a C++ qualified type name.
959 Tok.setLocation(SS.getBeginLoc());
Mike Stump25cf7602009-09-09 15:08:12 +0000960
Chris Lattnera9d6ec72009-01-05 01:49:50 +0000961 // In case the tokens were cached, have Preprocessor replace
962 // them with the annotation token.
963 PP.AnnotateCachedTokens(Tok);
964 return true;
Mike Stump25cf7602009-09-09 15:08:12 +0000965 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000966
967 if (!getLang().CPlusPlus) {
Chris Lattnera9d6ec72009-01-05 01:49:50 +0000968 // If we're in C, we can't have :: tokens at all (the lexer won't return
969 // them). If the identifier is not a type, then it can't be scope either,
Mike Stump25cf7602009-09-09 15:08:12 +0000970 // just early exit.
Chris Lattnera9d6ec72009-01-05 01:49:50 +0000971 return false;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000972 }
Mike Stump25cf7602009-09-09 15:08:12 +0000973
Douglas Gregor0c281a82009-02-25 19:37:18 +0000974 // If this is a template-id, annotate with a template-id or type token.
Douglas Gregor8e458f42009-02-09 18:46:07 +0000975 if (NextToken().is(tok::less)) {
Douglas Gregordd13e842009-03-30 22:58:21 +0000976 TemplateTy Template;
Mike Stump25cf7602009-09-09 15:08:12 +0000977 if (TemplateNameKind TNK
978 = Actions.isTemplateName(CurScope, *Tok.getIdentifierInfo(),
979 Tok.getLocation(), &SS,
980 /*ObjectType=*/0, EnteringContext,
Douglas Gregor681d31d2009-09-02 22:59:36 +0000981 Template))
Chris Lattner8eabc062009-06-26 04:27:47 +0000982 if (AnnotateTemplateIdToken(Template, TNK, &SS)) {
983 // If an unrecoverable error occurred, we need to return true here,
984 // because the token stream is in a damaged state. We may not return
985 // a valid identifier.
986 return Tok.isNot(tok::identifier);
987 }
Douglas Gregor8e458f42009-02-09 18:46:07 +0000988 }
Douglas Gregor2fa10442008-12-18 19:37:40 +0000989
Douglas Gregor0c281a82009-02-25 19:37:18 +0000990 // The current token, which is either an identifier or a
991 // template-id, is not part of the annotation. Fall through to
992 // push that token back into the stream and complete the C++ scope
993 // specifier annotation.
Mike Stump25cf7602009-09-09 15:08:12 +0000994 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000995
Douglas Gregor0c281a82009-02-25 19:37:18 +0000996 if (Tok.is(tok::annot_template_id)) {
Mike Stump25cf7602009-09-09 15:08:12 +0000997 TemplateIdAnnotation *TemplateId
Douglas Gregor0c281a82009-02-25 19:37:18 +0000998 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000999 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +00001000 // A template-id that refers to a type was parsed into a
1001 // template-id annotation in a context where we weren't allowed
1002 // to produce a type annotation token. Update the template-id
1003 // annotation token to a type annotation token now.
Douglas Gregord7cb0372009-04-01 21:51:26 +00001004 AnnotateTemplateIdTokenAsType(&SS);
1005 return true;
Douglas Gregor0c281a82009-02-25 19:37:18 +00001006 }
1007 }
Douglas Gregor2fa10442008-12-18 19:37:40 +00001008
Chris Lattnerf72f79c2009-01-04 22:32:19 +00001009 if (SS.isEmpty())
Eli Friedman24b2f872009-06-27 08:17:02 +00001010 return Tok.isNot(tok::identifier) && Tok.isNot(tok::coloncolon);
Mike Stump25cf7602009-09-09 15:08:12 +00001011
Chris Lattnerf72f79c2009-01-04 22:32:19 +00001012 // A C++ scope specifier that isn't followed by a typename.
1013 // Push the current token back into the token stream (or revert it if it is
1014 // cached) and use an annotation scope token for current token.
1015 if (PP.isBacktrackEnabled())
1016 PP.RevertCachedTokens(1);
1017 else
1018 PP.EnterToken(Tok);
1019 Tok.setKind(tok::annot_cxxscope);
Douglas Gregor041e9292009-03-26 23:56:24 +00001020 Tok.setAnnotationValue(SS.getScopeRep());
Chris Lattnerf72f79c2009-01-04 22:32:19 +00001021 Tok.setAnnotationRange(SS.getRange());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001022
Chris Lattnerf72f79c2009-01-04 22:32:19 +00001023 // In case the tokens were cached, have Preprocessor replace them with the
1024 // annotation token.
1025 PP.AnnotateCachedTokens(Tok);
Chris Lattner1e015942009-01-04 23:23:14 +00001026 return true;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001027}
1028
1029/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
Douglas Gregor0c281a82009-02-25 19:37:18 +00001030/// annotates C++ scope specifiers and template-ids. This returns
Chris Lattner8eabc062009-06-26 04:27:47 +00001031/// true if the token was annotated or there was an error that could not be
1032/// recovered from.
Mike Stump25cf7602009-09-09 15:08:12 +00001033///
Chris Lattner2c301452009-01-05 00:13:00 +00001034/// Note that this routine emits an error if you call it with ::new or ::delete
1035/// as the current tokens, so only call it in contexts where these are invalid.
Douglas Gregorc03d3302009-08-25 22:51:20 +00001036bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +00001037 assert(getLang().CPlusPlus &&
Chris Lattnerf72f79c2009-01-04 22:32:19 +00001038 "Call sites of this function should be guarded by checking for C++");
Chris Lattner8376d2e2009-01-05 01:24:05 +00001039 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
1040 "Cannot be a type or scope token!");
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001041
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +00001042 CXXScopeSpec SS;
Douglas Gregor681d31d2009-09-02 22:59:36 +00001043 if (!ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, EnteringContext))
Douglas Gregor0c281a82009-02-25 19:37:18 +00001044 return Tok.is(tok::annot_template_id);
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001045
Chris Lattnerf72f79c2009-01-04 22:32:19 +00001046 // Push the current token back into the token stream (or revert it if it is
1047 // cached) and use an annotation scope token for current token.
1048 if (PP.isBacktrackEnabled())
1049 PP.RevertCachedTokens(1);
1050 else
1051 PP.EnterToken(Tok);
1052 Tok.setKind(tok::annot_cxxscope);
Douglas Gregor041e9292009-03-26 23:56:24 +00001053 Tok.setAnnotationValue(SS.getScopeRep());
Chris Lattnerf72f79c2009-01-04 22:32:19 +00001054 Tok.setAnnotationRange(SS.getRange());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001055
Chris Lattnerf72f79c2009-01-04 22:32:19 +00001056 // In case the tokens were cached, have Preprocessor replace them with the
1057 // annotation token.
1058 PP.AnnotateCachedTokens(Tok);
Chris Lattner712f9a32009-01-05 00:07:25 +00001059 return true;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001060}