Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- Parser.cpp - C Language Family Parser ----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chris Lattner | 545f39e | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 16 | #include "clang/Parse/DeclSpec.h" |
| 17 | #include "clang/Parse/Scope.h" |
Chris Lattner | 6b3833c | 2009-03-05 07:24:28 +0000 | [diff] [blame^] | 18 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | daa5c00 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 19 | #include "ExtensionRAIIObject.h" |
Daniel Dunbar | 47f99c9 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 20 | #include "ParsePragma.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | Parser::Parser(Preprocessor &pp, Action &actions) |
Chris Lattner | 6b3833c | 2009-03-05 07:24:28 +0000 | [diff] [blame^] | 24 | : CrashInfo(*this), PP(pp), Actions(actions), Diags(PP.getDiagnostics()), |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 25 | GreaterThanIsOperator(true) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 26 | Tok.setKind(tok::eof); |
| 27 | CurScope = 0; |
| 28 | NumCachedScopes = 0; |
| 29 | ParenCount = BracketCount = BraceCount = 0; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 30 | ObjCImpDecl = 0; |
Daniel Dunbar | 47f99c9 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 31 | |
| 32 | // Add #pragma handlers. These are removed and destroyed in the |
| 33 | // destructor. |
| 34 | PackHandler = |
| 35 | new PragmaPackHandler(&PP.getIdentifierTable().get("pack"), actions); |
| 36 | PP.AddPragmaHandler(0, PackHandler); |
| 37 | |
Argiris Kirtzidis | 9d78433 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 38 | // Instantiate a LexedMethodsForTopClass for all the non-nested classes. |
| 39 | PushTopClassStack(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Chris Lattner | 6b3833c | 2009-03-05 07:24:28 +0000 | [diff] [blame^] | 42 | /// If a crash happens while the parser is active, print out a line indicating |
| 43 | /// what the current token is. |
| 44 | void PrettyStackTraceParserEntry::print(llvm::raw_ostream &OS) const { |
| 45 | const Token &Tok = P.getCurToken(); |
| 46 | if (Tok.getLocation().isInvalid()) { |
| 47 | OS << "<eof> parser at end of file\n"; |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | const Preprocessor &PP = P.getPreprocessor(); |
| 52 | Tok.getLocation().print(OS, PP.getSourceManager()); |
| 53 | OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n"; |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 54 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 6b3833c | 2009-03-05 07:24:28 +0000 | [diff] [blame^] | 56 | |
Chris Lattner | 9943e98 | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 57 | DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) { |
Chris Lattner | 6b3833c | 2009-03-05 07:24:28 +0000 | [diff] [blame^] | 58 | return Diags.Report(FullSourceLoc(Loc, PP.getSourceManager()), DiagID); |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Chris Lattner | 9943e98 | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 61 | DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 62 | return Diag(Tok.getLocation(), DiagID); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 65 | /// \brief Emits a diagnostic suggesting parentheses surrounding a |
| 66 | /// given range. |
| 67 | /// |
| 68 | /// \param Loc The location where we'll emit the diagnostic. |
| 69 | /// \param Loc The kind of diagnostic to emit. |
| 70 | /// \param ParenRange Source range enclosing code that should be parenthesized. |
| 71 | void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK, |
| 72 | SourceRange ParenRange) { |
Douglas Gregor | 61be360 | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 73 | SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 74 | if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 75 | // We can't display the parentheses, so just dig the |
| 76 | // warning/error and return. |
| 77 | Diag(Loc, DK); |
| 78 | return; |
| 79 | } |
| 80 | |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 81 | Diag(Loc, DK) |
Douglas Gregor | 61be360 | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 82 | << CodeModificationHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 83 | << CodeModificationHint::CreateInsertion(EndLoc, ")"); |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 86 | /// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'), |
| 87 | /// this helper function matches and consumes the specified RHS token if |
| 88 | /// present. If not present, it emits the specified diagnostic indicating |
| 89 | /// that the parser failed to match the RHS of the token at LHSLoc. LHSName |
| 90 | /// should be the name of the unmatched LHS token. |
| 91 | SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok, |
| 92 | SourceLocation LHSLoc) { |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 94 | if (Tok.is(RHSTok)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 95 | return ConsumeAnyToken(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 97 | SourceLocation R = Tok.getLocation(); |
| 98 | const char *LHSName = "unknown"; |
| 99 | diag::kind DID = diag::err_parse_error; |
| 100 | switch (RHSTok) { |
| 101 | default: break; |
| 102 | case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break; |
| 103 | case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break; |
| 104 | case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break; |
| 105 | case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break; |
| 106 | } |
| 107 | Diag(Tok, DID); |
Chris Lattner | 921342c | 2008-11-23 23:17:07 +0000 | [diff] [blame] | 108 | Diag(LHSLoc, diag::note_matching) << LHSName; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 109 | SkipUntil(RHSTok); |
| 110 | return R; |
| 111 | } |
| 112 | |
| 113 | /// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the |
| 114 | /// input. If so, it is consumed and false is returned. |
| 115 | /// |
| 116 | /// If the input is malformed, this emits the specified diagnostic. Next, if |
| 117 | /// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is |
| 118 | /// returned. |
| 119 | bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID, |
| 120 | const char *Msg, tok::TokenKind SkipToTok) { |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 121 | if (Tok.is(ExpectedTok)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 122 | ConsumeAnyToken(); |
| 123 | return false; |
| 124 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 125 | |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 126 | const char *Spelling = 0; |
Douglas Gregor | 61be360 | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 127 | SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); |
| 128 | if (EndLoc.isValid() && |
| 129 | (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) { |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 130 | // Show what code to insert to fix this problem. |
Douglas Gregor | 61be360 | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 131 | Diag(EndLoc, DiagID) |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 132 | << Msg |
Douglas Gregor | 61be360 | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 133 | << CodeModificationHint::CreateInsertion(EndLoc, Spelling); |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 134 | } else |
| 135 | Diag(Tok, DiagID) << Msg; |
| 136 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 137 | if (SkipToTok != tok::unknown) |
| 138 | SkipUntil(SkipToTok); |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | //===----------------------------------------------------------------------===// |
| 143 | // Error recovery. |
| 144 | //===----------------------------------------------------------------------===// |
| 145 | |
| 146 | /// SkipUntil - Read tokens until we get to the specified token, then consume |
| 147 | /// it (unless DontConsume is true). Because we cannot guarantee that the |
| 148 | /// token will ever occur, this skips to the next token, or to some likely |
| 149 | /// good stopping point. If StopAtSemi is true, skipping will stop at a ';' |
| 150 | /// character. |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 151 | /// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 152 | /// If SkipUntil finds the specified token, it returns true, otherwise it |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 153 | /// returns false. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 154 | bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks, |
| 155 | bool StopAtSemi, bool DontConsume) { |
| 156 | // We always want this function to skip at least one token if the first token |
| 157 | // isn't T and if not at EOF. |
| 158 | bool isFirstTokenSkipped = true; |
| 159 | while (1) { |
| 160 | // If we found one of the tokens, stop and return true. |
| 161 | for (unsigned i = 0; i != NumToks; ++i) { |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 162 | if (Tok.is(Toks[i])) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 163 | if (DontConsume) { |
| 164 | // Noop, don't consume the token. |
| 165 | } else { |
| 166 | ConsumeAnyToken(); |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 172 | switch (Tok.getKind()) { |
| 173 | case tok::eof: |
| 174 | // Ran out of tokens. |
| 175 | return false; |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 177 | case tok::l_paren: |
| 178 | // Recursively skip properly-nested parens. |
| 179 | ConsumeParen(); |
| 180 | SkipUntil(tok::r_paren, false); |
| 181 | break; |
| 182 | case tok::l_square: |
| 183 | // Recursively skip properly-nested square brackets. |
| 184 | ConsumeBracket(); |
| 185 | SkipUntil(tok::r_square, false); |
| 186 | break; |
| 187 | case tok::l_brace: |
| 188 | // Recursively skip properly-nested braces. |
| 189 | ConsumeBrace(); |
| 190 | SkipUntil(tok::r_brace, false); |
| 191 | break; |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 193 | // Okay, we found a ']' or '}' or ')', which we think should be balanced. |
| 194 | // Since the user wasn't looking for this token (if they were, it would |
| 195 | // already be handled), this isn't balanced. If there is a LHS token at a |
| 196 | // higher level, we will assume that this matches the unbalanced token |
| 197 | // and return it. Otherwise, this is a spurious RHS token, which we skip. |
| 198 | case tok::r_paren: |
| 199 | if (ParenCount && !isFirstTokenSkipped) |
| 200 | return false; // Matches something. |
| 201 | ConsumeParen(); |
| 202 | break; |
| 203 | case tok::r_square: |
| 204 | if (BracketCount && !isFirstTokenSkipped) |
| 205 | return false; // Matches something. |
| 206 | ConsumeBracket(); |
| 207 | break; |
| 208 | case tok::r_brace: |
| 209 | if (BraceCount && !isFirstTokenSkipped) |
| 210 | return false; // Matches something. |
| 211 | ConsumeBrace(); |
| 212 | break; |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 214 | case tok::string_literal: |
| 215 | case tok::wide_string_literal: |
| 216 | ConsumeStringToken(); |
| 217 | break; |
| 218 | case tok::semi: |
| 219 | if (StopAtSemi) |
| 220 | return false; |
| 221 | // FALL THROUGH. |
| 222 | default: |
| 223 | // Skip this token. |
| 224 | ConsumeToken(); |
| 225 | break; |
| 226 | } |
| 227 | isFirstTokenSkipped = false; |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 228 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | //===----------------------------------------------------------------------===// |
| 232 | // Scope manipulation |
| 233 | //===----------------------------------------------------------------------===// |
| 234 | |
| 235 | /// EnterScope - Start a new scope. |
| 236 | void Parser::EnterScope(unsigned ScopeFlags) { |
| 237 | if (NumCachedScopes) { |
| 238 | Scope *N = ScopeCache[--NumCachedScopes]; |
| 239 | N->Init(CurScope, ScopeFlags); |
| 240 | CurScope = N; |
| 241 | } else { |
| 242 | CurScope = new Scope(CurScope, ScopeFlags); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /// ExitScope - Pop a scope off the scope stack. |
| 247 | void Parser::ExitScope() { |
| 248 | assert(CurScope && "Scope imbalance!"); |
| 249 | |
Chris Lattner | 6223149 | 2007-10-09 20:37:18 +0000 | [diff] [blame] | 250 | // Inform the actions module that this scope is going away if there are any |
| 251 | // decls in it. |
| 252 | if (!CurScope->decl_empty()) |
Steve Naroff | 9637a9b | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 253 | Actions.ActOnPopScope(Tok.getLocation(), CurScope); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 254 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 255 | Scope *OldScope = CurScope; |
| 256 | CurScope = OldScope->getParent(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 258 | if (NumCachedScopes == ScopeCacheSize) |
| 259 | delete OldScope; |
| 260 | else |
| 261 | ScopeCache[NumCachedScopes++] = OldScope; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | //===----------------------------------------------------------------------===// |
| 268 | // C99 6.9: External Definitions. |
| 269 | //===----------------------------------------------------------------------===// |
| 270 | |
| 271 | Parser::~Parser() { |
| 272 | // If we still have scopes active, delete the scope tree. |
| 273 | delete CurScope; |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 275 | // Free the scope cache. |
| 276 | for (unsigned i = 0, e = NumCachedScopes; i != e; ++i) |
| 277 | delete ScopeCache[i]; |
Daniel Dunbar | 47f99c9 | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 278 | |
| 279 | // Remove the pragma handlers we installed. |
| 280 | PP.RemovePragmaHandler(0, PackHandler); |
| 281 | delete PackHandler; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /// Initialize - Warm up the parser. |
| 285 | /// |
| 286 | void Parser::Initialize() { |
| 287 | // Prime the lexer look-ahead. |
| 288 | ConsumeToken(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 289 | |
Chris Lattner | a754990 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 290 | // Create the translation unit scope. Install it as the current scope. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 291 | assert(CurScope == 0 && "A scope is already active?"); |
Chris Lattner | a754990 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 292 | EnterScope(Scope::DeclScope); |
Steve Naroff | 9637a9b | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 293 | Actions.ActOnTranslationUnitScope(Tok.getLocation(), CurScope); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 294 | |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 295 | if (Tok.is(tok::eof) && |
Chris Lattner | 7bdc85d | 2007-08-25 05:47:03 +0000 | [diff] [blame] | 296 | !getLang().CPlusPlus) // Empty source file is an extension in C |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 297 | Diag(Tok, diag::ext_empty_source_file); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 3235246 | 2007-08-29 22:54:08 +0000 | [diff] [blame] | 299 | // Initialization for Objective-C context sensitive keywords recognition. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 300 | // Referenced in Parser::ParseObjCTypeQualifierList. |
Chris Lattner | 3235246 | 2007-08-29 22:54:08 +0000 | [diff] [blame] | 301 | if (getLang().ObjC1) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 302 | ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in"); |
| 303 | ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out"); |
| 304 | ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout"); |
| 305 | ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway"); |
| 306 | ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy"); |
| 307 | ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref"); |
Chris Lattner | 3235246 | 2007-08-29 22:54:08 +0000 | [diff] [blame] | 308 | } |
Daniel Dunbar | 4837ae7 | 2008-08-14 22:04:54 +0000 | [diff] [blame] | 309 | |
| 310 | Ident_super = &PP.getIdentifierTable().get("super"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /// ParseTopLevelDecl - Parse one top-level declaration, return whatever the |
| 314 | /// action tells us to. This returns true if the EOF was encountered. |
Steve Naroff | ca44ffd | 2007-11-29 23:05:20 +0000 | [diff] [blame] | 315 | bool Parser::ParseTopLevelDecl(DeclTy*& Result) { |
| 316 | Result = 0; |
Chris Lattner | c1aea81 | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 317 | if (Tok.is(tok::eof)) { |
| 318 | Actions.ActOnEndOfTranslationUnit(); |
| 319 | return true; |
| 320 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 321 | |
Steve Naroff | ca44ffd | 2007-11-29 23:05:20 +0000 | [diff] [blame] | 322 | Result = ParseExternalDeclaration(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 323 | return false; |
| 324 | } |
| 325 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 326 | /// ParseTranslationUnit: |
| 327 | /// translation-unit: [C99 6.9] |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 328 | /// external-declaration |
| 329 | /// translation-unit external-declaration |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 330 | void Parser::ParseTranslationUnit() { |
Douglas Gregor | 95d4079 | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 331 | Initialize(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 332 | |
Steve Naroff | ca44ffd | 2007-11-29 23:05:20 +0000 | [diff] [blame] | 333 | DeclTy *Res; |
| 334 | while (!ParseTopLevelDecl(Res)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 335 | /*parse them all*/; |
Chris Lattner | f7df4d1 | 2008-08-23 02:00:52 +0000 | [diff] [blame] | 336 | |
| 337 | ExitScope(); |
| 338 | assert(CurScope == 0 && "Scope imbalance!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /// ParseExternalDeclaration: |
Chris Lattner | eb54a36 | 2008-12-08 21:59:01 +0000 | [diff] [blame] | 342 | /// |
Douglas Gregor | 61818c5 | 2008-11-21 16:10:08 +0000 | [diff] [blame] | 343 | /// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl] |
Chris Lattner | 06f4e75 | 2007-08-10 20:57:02 +0000 | [diff] [blame] | 344 | /// function-definition |
| 345 | /// declaration |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 346 | /// [EXT] ';' |
| 347 | /// [GNU] asm-definition |
Chris Lattner | 06f4e75 | 2007-08-10 20:57:02 +0000 | [diff] [blame] | 348 | /// [GNU] __extension__ external-declaration |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 349 | /// [OBJC] objc-class-definition |
| 350 | /// [OBJC] objc-class-declaration |
| 351 | /// [OBJC] objc-alias-declaration |
| 352 | /// [OBJC] objc-protocol-definition |
| 353 | /// [OBJC] objc-method-definition |
| 354 | /// [OBJC] @end |
Douglas Gregor | 61818c5 | 2008-11-21 16:10:08 +0000 | [diff] [blame] | 355 | /// [C++] linkage-specification |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 356 | /// [GNU] asm-definition: |
| 357 | /// simple-asm-expr ';' |
| 358 | /// |
| 359 | Parser::DeclTy *Parser::ParseExternalDeclaration() { |
| 360 | switch (Tok.getKind()) { |
| 361 | case tok::semi: |
| 362 | Diag(Tok, diag::ext_top_level_semi); |
| 363 | ConsumeToken(); |
| 364 | // TODO: Invoke action for top-level semicolon. |
| 365 | return 0; |
Chris Lattner | eb54a36 | 2008-12-08 21:59:01 +0000 | [diff] [blame] | 366 | case tok::r_brace: |
| 367 | Diag(Tok, diag::err_expected_external_declaration); |
| 368 | ConsumeBrace(); |
| 369 | return 0; |
| 370 | case tok::eof: |
| 371 | Diag(Tok, diag::err_expected_external_declaration); |
| 372 | return 0; |
Chris Lattner | 06f4e75 | 2007-08-10 20:57:02 +0000 | [diff] [blame] | 373 | case tok::kw___extension__: { |
Chris Lattner | daa5c00 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 374 | // __extension__ silences extension warnings in the subexpression. |
| 375 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Chris Lattner | 658e687 | 2008-10-20 06:51:33 +0000 | [diff] [blame] | 376 | ConsumeToken(); |
Chris Lattner | daa5c00 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 377 | return ParseExternalDeclaration(); |
Chris Lattner | 06f4e75 | 2007-08-10 20:57:02 +0000 | [diff] [blame] | 378 | } |
Anders Carlsson | 4f7f441 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 379 | case tok::kw_asm: { |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 380 | OwningExprResult Result(ParseSimpleAsm()); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 381 | |
Anders Carlsson | f41100b | 2008-02-08 00:23:11 +0000 | [diff] [blame] | 382 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, |
| 383 | "top-level asm block"); |
Anders Carlsson | 4f7f441 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 384 | |
Sebastian Redl | bb4dae7 | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 385 | if (!Result.isInvalid()) |
Sebastian Redl | 81db668 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 386 | return Actions.ActOnFileScopeAsmDecl(Tok.getLocation(), move(Result)); |
Chris Lattner | b36c365 | 2008-05-27 23:32:43 +0000 | [diff] [blame] | 387 | return 0; |
Anders Carlsson | 4f7f441 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 388 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 389 | case tok::at: |
| 390 | // @ is not a legal token unless objc is enabled, no need to check. |
Steve Naroff | faed3bf | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 391 | return ParseObjCAtDirectives(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 392 | case tok::minus: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 393 | case tok::plus: |
Fariborz Jahanian | e6f59f1 | 2007-11-10 16:31:34 +0000 | [diff] [blame] | 394 | if (getLang().ObjC1) |
Steve Naroff | 18c8338 | 2007-11-13 23:01:27 +0000 | [diff] [blame] | 395 | return ParseObjCMethodDefinition(); |
Fariborz Jahanian | e6f59f1 | 2007-11-10 16:31:34 +0000 | [diff] [blame] | 396 | else { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 397 | Diag(Tok, diag::err_expected_external_declaration); |
| 398 | ConsumeToken(); |
| 399 | } |
| 400 | return 0; |
Douglas Gregor | 5ff0ee5 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 401 | case tok::kw_using: |
Chris Lattner | f7b2e55 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 402 | case tok::kw_namespace: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 403 | case tok::kw_typedef: |
Douglas Gregor | b3bec71 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 404 | case tok::kw_template: |
| 405 | case tok::kw_export: // As in 'export template' |
Chris Lattner | 9c13572 | 2007-08-25 18:15:16 +0000 | [diff] [blame] | 406 | // A function definition cannot start with a these keywords. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 407 | return ParseDeclaration(Declarator::FileContext); |
| 408 | default: |
| 409 | // We can't tell whether this is a function-definition or declaration yet. |
| 410 | return ParseDeclarationOrFunctionDefinition(); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or |
| 415 | /// a declaration. We can't tell which we have until we read up to the |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 416 | /// compound-statement in function-definition. TemplateParams, if |
| 417 | /// non-NULL, provides the template parameters when we're parsing a |
| 418 | /// C++ template-declaration. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 419 | /// |
| 420 | /// function-definition: [C99 6.9.1] |
Chris Lattner | a15e9d2 | 2008-04-05 05:52:15 +0000 | [diff] [blame] | 421 | /// decl-specs declarator declaration-list[opt] compound-statement |
| 422 | /// [C90] function-definition: [C99 6.7.1] - implicit int result |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 423 | /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement |
Chris Lattner | a15e9d2 | 2008-04-05 05:52:15 +0000 | [diff] [blame] | 424 | /// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 425 | /// declaration: [C99 6.7] |
Chris Lattner | aac973e | 2007-08-22 06:06:56 +0000 | [diff] [blame] | 426 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 427 | /// [!C99] init-declarator-list ';' [TODO: warn in c99 mode] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 428 | /// [OMP] threadprivate-directive [TODO] |
| 429 | /// |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 430 | Parser::DeclTy * |
| 431 | Parser::ParseDeclarationOrFunctionDefinition( |
| 432 | TemplateParameterLists *TemplateParams) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 433 | // Parse the common declaration-specifiers piece. |
| 434 | DeclSpec DS; |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 435 | ParseDeclarationSpecifiers(DS, TemplateParams); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 437 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 438 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 439 | if (Tok.is(tok::semi)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 440 | ConsumeToken(); |
| 441 | return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
| 442 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 443 | |
Daniel Dunbar | 28680d1 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 444 | // ObjC2 allows prefix attributes on class interfaces and protocols. |
| 445 | // FIXME: This still needs better diagnostics. We should only accept |
| 446 | // attributes here, no types, etc. |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 447 | if (getLang().ObjC2 && Tok.is(tok::at)) { |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 448 | SourceLocation AtLoc = ConsumeToken(); // the "@" |
Daniel Dunbar | 28680d1 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 449 | if (!Tok.isObjCAtKeyword(tok::objc_interface) && |
| 450 | !Tok.isObjCAtKeyword(tok::objc_protocol)) { |
| 451 | Diag(Tok, diag::err_objc_unexpected_attr); |
Chris Lattner | 847f5c1 | 2007-12-27 19:57:00 +0000 | [diff] [blame] | 452 | SkipUntil(tok::semi); // FIXME: better skip? |
| 453 | return 0; |
| 454 | } |
Fariborz Jahanian | f9c0a0d | 2008-01-02 19:17:38 +0000 | [diff] [blame] | 455 | const char *PrevSpec = 0; |
| 456 | if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec)) |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 457 | Diag(AtLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Daniel Dunbar | 28680d1 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 458 | if (Tok.isObjCAtKeyword(tok::objc_protocol)) |
| 459 | return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes()); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 460 | return ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()); |
Steve Naroff | fb36788 | 2007-08-20 21:31:48 +0000 | [diff] [blame] | 461 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 463 | // If the declspec consisted only of 'extern' and we have a string |
| 464 | // literal following it, this must be a C++ linkage specifier like |
| 465 | // 'extern "C"'. |
Chris Lattner | 1b5c9f7 | 2008-01-12 07:08:43 +0000 | [diff] [blame] | 466 | if (Tok.is(tok::string_literal) && getLang().CPlusPlus && |
Chris Lattner | 806a5f5 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 467 | DS.getStorageClassSpec() == DeclSpec::SCS_extern && |
| 468 | DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) |
| 469 | return ParseLinkage(Declarator::FileContext); |
| 470 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 471 | // Parse the first declarator. |
| 472 | Declarator DeclaratorInfo(DS, Declarator::FileContext); |
| 473 | ParseDeclarator(DeclaratorInfo); |
| 474 | // Error parsing the declarator? |
Douglas Gregor | 6704b31 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 475 | if (!DeclaratorInfo.hasName()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 476 | // If so, skip until the semi-colon or a }. |
Douglas Gregor | 6f73061 | 2008-12-01 23:03:32 +0000 | [diff] [blame] | 477 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 478 | if (Tok.is(tok::semi)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 479 | ConsumeToken(); |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | // If the declarator is the start of a function definition, handle it. |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 484 | if (Tok.is(tok::equal) || // int X()= -> not a function def |
| 485 | Tok.is(tok::comma) || // int X(), -> not a function def |
| 486 | Tok.is(tok::semi) || // int X(); -> not a function def |
| 487 | Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def |
Argiris Kirtzidis | 9e55d46 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 488 | Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def |
| 489 | (getLang().CPlusPlus && |
| 490 | Tok.is(tok::l_paren)) ) { // int X(0) -> not a function def [C++] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 491 | // FALL THROUGH. |
| 492 | } else if (DeclaratorInfo.isFunctionDeclarator() && |
Argiris Kirtzidis | d1346a5 | 2008-06-21 10:00:56 +0000 | [diff] [blame] | 493 | (Tok.is(tok::l_brace) || // int X() {} |
Chris Lattner | c6f830c | 2009-02-27 17:15:01 +0000 | [diff] [blame] | 494 | (!getLang().CPlusPlus && |
| 495 | isDeclarationSpecifier()))) { // int X(f) int f; {} |
Steve Naroff | 8329885 | 2008-02-14 02:58:32 +0000 | [diff] [blame] | 496 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 497 | Diag(Tok, diag::err_function_declared_typedef); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 498 | |
Steve Naroff | 8329885 | 2008-02-14 02:58:32 +0000 | [diff] [blame] | 499 | if (Tok.is(tok::l_brace)) { |
| 500 | // This recovery skips the entire function body. It would be nice |
Douglas Gregor | dd86106 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 501 | // to simply call ParseFunctionDefinition() below, however Sema |
Steve Naroff | 8329885 | 2008-02-14 02:58:32 +0000 | [diff] [blame] | 502 | // assumes the declarator represents a function, not a typedef. |
| 503 | ConsumeBrace(); |
| 504 | SkipUntil(tok::r_brace, true); |
| 505 | } else { |
| 506 | SkipUntil(tok::semi); |
| 507 | } |
| 508 | return 0; |
| 509 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 510 | return ParseFunctionDefinition(DeclaratorInfo); |
| 511 | } else { |
| 512 | if (DeclaratorInfo.isFunctionDeclarator()) |
| 513 | Diag(Tok, diag::err_expected_fn_body); |
| 514 | else |
Chris Lattner | c6f830c | 2009-02-27 17:15:01 +0000 | [diff] [blame] | 515 | Diag(Tok, diag::err_invalid_token_after_toplevel_declarator); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 516 | SkipUntil(tok::semi); |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | // Parse the init-declarator-list for a normal declaration. |
| 521 | return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
| 522 | } |
| 523 | |
| 524 | /// ParseFunctionDefinition - We parsed and verified that the specified |
| 525 | /// Declarator is well formed. If this is a K&R-style function, read the |
| 526 | /// parameters declaration-list, then start the compound-statement. |
| 527 | /// |
Chris Lattner | a15e9d2 | 2008-04-05 05:52:15 +0000 | [diff] [blame] | 528 | /// function-definition: [C99 6.9.1] |
| 529 | /// decl-specs declarator declaration-list[opt] compound-statement |
| 530 | /// [C90] function-definition: [C99 6.7.1] - implicit int result |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 531 | /// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 532 | /// [C++] function-definition: [C++ 8.4] |
| 533 | /// decl-specifier-seq[opt] declarator ctor-initializer[opt] function-body |
| 534 | /// [C++] function-definition: [C++ 8.4] |
| 535 | /// decl-specifier-seq[opt] declarator function-try-block [TODO] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 536 | /// |
| 537 | Parser::DeclTy *Parser::ParseFunctionDefinition(Declarator &D) { |
| 538 | const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0); |
| 539 | assert(FnTypeInfo.Kind == DeclaratorChunk::Function && |
| 540 | "This isn't a function declarator!"); |
| 541 | const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun; |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 542 | |
Chris Lattner | a15e9d2 | 2008-04-05 05:52:15 +0000 | [diff] [blame] | 543 | // If this is C90 and the declspecs were completely missing, fudge in an |
| 544 | // implicit int. We do this here because this is the only place where |
| 545 | // declaration-specifiers are completely optional in the grammar. |
Chris Lattner | 92eca3e | 2009-02-27 18:35:46 +0000 | [diff] [blame] | 546 | if (getLang().ImplicitInt && D.getDeclSpec().isEmpty()) { |
Chris Lattner | a15e9d2 | 2008-04-05 05:52:15 +0000 | [diff] [blame] | 547 | const char *PrevSpec; |
Chris Lattner | 509fc80 | 2008-10-20 02:01:34 +0000 | [diff] [blame] | 548 | D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int, |
| 549 | D.getIdentifierLoc(), |
| 550 | PrevSpec); |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 551 | D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin()); |
Chris Lattner | a15e9d2 | 2008-04-05 05:52:15 +0000 | [diff] [blame] | 552 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 553 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 554 | // If this declaration was formed with a K&R-style identifier list for the |
| 555 | // arguments, parse declarations for all of the args next. |
| 556 | // int foo(a,b) int a; float b; {} |
| 557 | if (!FTI.hasPrototype && FTI.NumArgs != 0) |
| 558 | ParseKNRParamDeclarations(D); |
| 559 | |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 560 | // We should have either an opening brace or, in a C++ constructor, |
| 561 | // we may have a colon. |
Sebastian Redl | f22270b | 2008-11-24 21:45:59 +0000 | [diff] [blame] | 562 | // FIXME: In C++, we might also find the 'try' keyword. |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 563 | if (Tok.isNot(tok::l_brace) && Tok.isNot(tok::colon)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 564 | Diag(Tok, diag::err_expected_fn_body); |
| 565 | |
| 566 | // Skip over garbage, until we get to '{'. Don't eat the '{'. |
| 567 | SkipUntil(tok::l_brace, true, true); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 568 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 569 | // If we didn't find the '{', bail out. |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 570 | if (Tok.isNot(tok::l_brace)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 571 | return 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 572 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 573 | |
Chris Lattner | ea14870 | 2007-10-09 17:14:05 +0000 | [diff] [blame] | 574 | // Enter a scope for the function body. |
Douglas Gregor | 95d4079 | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 575 | ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 576 | |
Chris Lattner | ea14870 | 2007-10-09 17:14:05 +0000 | [diff] [blame] | 577 | // Tell the actions module that we have entered a function definition with the |
| 578 | // specified Declarator for the function. |
| 579 | DeclTy *Res = Actions.ActOnStartOfFunctionDef(CurScope, D); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 580 | |
Douglas Gregor | a65e8dd | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 581 | // If we have a colon, then we're probably parsing a C++ |
| 582 | // ctor-initializer. |
| 583 | if (Tok.is(tok::colon)) |
| 584 | ParseConstructorInitializer(Res); |
| 585 | |
| 586 | SourceLocation BraceLoc = Tok.getLocation(); |
Chris Lattner | 0818a7a | 2009-03-05 00:49:17 +0000 | [diff] [blame] | 587 | return ParseFunctionStatementBody(Res); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides |
| 591 | /// types for a function with a K&R-style identifier list for arguments. |
| 592 | void Parser::ParseKNRParamDeclarations(Declarator &D) { |
| 593 | // We know that the top-level of this declarator is a function. |
| 594 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 595 | |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 596 | // Enter function-declaration scope, limiting any declarators to the |
| 597 | // function prototype scope, including parameter declarators. |
Douglas Gregor | cab994d | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 598 | ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 599 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 600 | // Read all the argument declarations. |
| 601 | while (isDeclarationSpecifier()) { |
| 602 | SourceLocation DSStart = Tok.getLocation(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 603 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 604 | // Parse the common declaration-specifiers piece. |
| 605 | DeclSpec DS; |
| 606 | ParseDeclarationSpecifiers(DS); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 607 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 608 | // C99 6.9.1p6: 'each declaration in the declaration list shall have at |
| 609 | // least one declarator'. |
| 610 | // NOTE: GCC just makes this an ext-warn. It's not clear what it does with |
| 611 | // the declarations though. It's trivial to ignore them, really hard to do |
| 612 | // anything else with them. |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 613 | if (Tok.is(tok::semi)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 614 | Diag(DSStart, diag::err_declaration_does_not_declare_param); |
| 615 | ConsumeToken(); |
| 616 | continue; |
| 617 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 618 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 619 | // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other |
| 620 | // than register. |
| 621 | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && |
| 622 | DS.getStorageClassSpec() != DeclSpec::SCS_register) { |
| 623 | Diag(DS.getStorageClassSpecLoc(), |
| 624 | diag::err_invalid_storage_class_in_func_decl); |
| 625 | DS.ClearStorageClassSpecs(); |
| 626 | } |
| 627 | if (DS.isThreadSpecified()) { |
| 628 | Diag(DS.getThreadSpecLoc(), |
| 629 | diag::err_invalid_storage_class_in_func_decl); |
| 630 | DS.ClearStorageClassSpecs(); |
| 631 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 632 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 633 | // Parse the first declarator attached to this declspec. |
| 634 | Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext); |
| 635 | ParseDeclarator(ParmDeclarator); |
| 636 | |
| 637 | // Handle the full declarator list. |
| 638 | while (1) { |
| 639 | DeclTy *AttrList; |
| 640 | // If attributes are present, parse them. |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 641 | if (Tok.is(tok::kw___attribute)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 642 | // FIXME: attach attributes too. |
| 643 | AttrList = ParseAttributes(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 644 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 645 | // Ask the actions module to compute the type for this declarator. |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 646 | Action::DeclTy *Param = |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 647 | Actions.ActOnParamDeclarator(CurScope, ParmDeclarator); |
Steve Naroff | faed3bf | 2007-09-10 20:51:04 +0000 | [diff] [blame] | 648 | |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 649 | if (Param && |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 650 | // A missing identifier has already been diagnosed. |
| 651 | ParmDeclarator.getIdentifier()) { |
| 652 | |
| 653 | // Scan the argument list looking for the correct param to apply this |
| 654 | // type. |
| 655 | for (unsigned i = 0; ; ++i) { |
| 656 | // C99 6.9.1p6: those declarators shall declare only identifiers from |
| 657 | // the identifier list. |
| 658 | if (i == FTI.NumArgs) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 659 | Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param) |
Chris Lattner | b12ef86 | 2008-11-19 07:51:13 +0000 | [diff] [blame] | 660 | << ParmDeclarator.getIdentifier(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 661 | break; |
| 662 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 663 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 664 | if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) { |
| 665 | // Reject redefinitions of parameters. |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 666 | if (FTI.ArgInfo[i].Param) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 667 | Diag(ParmDeclarator.getIdentifierLoc(), |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 668 | diag::err_param_redefinition) |
Chris Lattner | b12ef86 | 2008-11-19 07:51:13 +0000 | [diff] [blame] | 669 | << ParmDeclarator.getIdentifier(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 670 | } else { |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 671 | FTI.ArgInfo[i].Param = Param; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 672 | } |
| 673 | break; |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | // If we don't have a comma, it is either the end of the list (a ';') or |
| 679 | // an error, bail out. |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 680 | if (Tok.isNot(tok::comma)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 681 | break; |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 682 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 683 | // Consume the comma. |
| 684 | ConsumeToken(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 685 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 686 | // Parse the next declarator. |
| 687 | ParmDeclarator.clear(); |
| 688 | ParseDeclarator(ParmDeclarator); |
| 689 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 690 | |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 691 | if (Tok.is(tok::semi)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 692 | ConsumeToken(); |
| 693 | } else { |
| 694 | Diag(Tok, diag::err_parse_error); |
| 695 | // Skip to end of block or statement |
| 696 | SkipUntil(tok::semi, true); |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 697 | if (Tok.is(tok::semi)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 698 | ConsumeToken(); |
| 699 | } |
| 700 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 702 | // The actions module must verify that all arguments were declared. |
Douglas Gregor | 65075ec | 2009-01-23 16:23:13 +0000 | [diff] [blame] | 703 | Actions.ActOnFinishKNRParamDeclarations(CurScope, D); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | |
| 707 | /// ParseAsmStringLiteral - This is just a normal string-literal, but is not |
| 708 | /// allowed to be a wide string, and is not subject to character translation. |
| 709 | /// |
| 710 | /// [GNU] asm-string-literal: |
| 711 | /// string-literal |
| 712 | /// |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 713 | Parser::OwningExprResult Parser::ParseAsmStringLiteral() { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 714 | if (!isTokenStringLiteral()) { |
| 715 | Diag(Tok, diag::err_expected_string_literal); |
Sebastian Redl | 10c3295 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 716 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 717 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 718 | |
Sebastian Redl | 39d4f02 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 719 | OwningExprResult Res(ParseStringLiteralExpression()); |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 720 | if (Res.isInvalid()) return move(Res); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 721 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 722 | // TODO: Diagnose: wide string literal in 'asm' |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 723 | |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 724 | return move(Res); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /// ParseSimpleAsm |
| 728 | /// |
| 729 | /// [GNU] simple-asm-expr: |
| 730 | /// 'asm' '(' asm-string-literal ')' |
| 731 | /// |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 732 | Parser::OwningExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) { |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 733 | assert(Tok.is(tok::kw_asm) && "Not an asm!"); |
Anders Carlsson | 4f7f441 | 2008-02-08 00:33:21 +0000 | [diff] [blame] | 734 | SourceLocation Loc = ConsumeToken(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 735 | |
Chris Lattner | 17a5fb6 | 2007-10-09 17:23:58 +0000 | [diff] [blame] | 736 | if (Tok.isNot(tok::l_paren)) { |
Chris Lattner | f006a22 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 737 | Diag(Tok, diag::err_expected_lparen_after) << "asm"; |
Sebastian Redl | 10c3295 | 2008-12-11 19:30:53 +0000 | [diff] [blame] | 738 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 739 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 740 | |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 741 | Loc = ConsumeParen(); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 742 | |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 743 | OwningExprResult Result(ParseAsmStringLiteral()); |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 744 | |
Sebastian Redl | 0c98603 | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 745 | if (Result.isInvalid()) { |
| 746 | SkipUntil(tok::r_paren, true, true); |
| 747 | if (EndLoc) |
| 748 | *EndLoc = Tok.getLocation(); |
| 749 | ConsumeAnyToken(); |
| 750 | } else { |
| 751 | Loc = MatchRHSPunctuation(tok::r_paren, Loc); |
| 752 | if (EndLoc) |
| 753 | *EndLoc = Loc; |
| 754 | } |
Mike Stump | eda58eb | 2008-06-19 19:28:49 +0000 | [diff] [blame] | 755 | |
Sebastian Redl | 6f1ee23 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 756 | return move(Result); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 759 | /// TryAnnotateTypeOrScopeToken - If the current token position is on a |
| 760 | /// typename (possibly qualified in C++) or a C++ scope specifier not followed |
| 761 | /// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens |
| 762 | /// with a single annotation token representing the typename or C++ scope |
| 763 | /// respectively. |
| 764 | /// This simplifies handling of C++ scope specifiers and allows efficient |
| 765 | /// backtracking without the need to re-parse and resolve nested-names and |
| 766 | /// typenames. |
Argiris Kirtzidis | fc33232 | 2008-11-26 21:51:07 +0000 | [diff] [blame] | 767 | /// It will mainly be called when we expect to treat identifiers as typenames |
| 768 | /// (if they are typenames). For example, in C we do not expect identifiers |
| 769 | /// inside expressions to be treated as typenames so it will not be called |
| 770 | /// for expressions in C. |
| 771 | /// The benefit for C/ObjC is that a typename will be annotated and |
Steve Naroff | 7b36a1b | 2009-01-28 19:39:02 +0000 | [diff] [blame] | 772 | /// Actions.getTypeName will not be needed to be called again (e.g. getTypeName |
Argiris Kirtzidis | fc33232 | 2008-11-26 21:51:07 +0000 | [diff] [blame] | 773 | /// will not be called twice, once to check whether we have a declaration |
| 774 | /// specifier, and another one to get the actual type inside |
| 775 | /// ParseDeclarationSpecifiers). |
Chris Lattner | 1e01594 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 776 | /// |
| 777 | /// This returns true if the token was annotated. |
Chris Lattner | 2c30145 | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 778 | /// |
| 779 | /// Note that this routine emits an error if you call it with ::new or ::delete |
| 780 | /// as the current tokens, so only call it in contexts where these are invalid. |
Chris Lattner | 94a15bd | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 781 | bool Parser::TryAnnotateTypeOrScopeToken() { |
Chris Lattner | 8376d2e | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 782 | assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) && |
| 783 | "Cannot be a type or scope token!"); |
| 784 | |
| 785 | // FIXME: Implement template-ids |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 786 | CXXScopeSpec SS; |
Argiris Kirtzidis | 91c80dc | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 787 | if (getLang().CPlusPlus) |
Chris Lattner | d706dc8 | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 788 | ParseOptionalCXXScopeSpecifier(SS); |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 789 | |
| 790 | if (Tok.is(tok::identifier)) { |
Chris Lattner | a9d6ec7 | 2009-01-05 01:49:50 +0000 | [diff] [blame] | 791 | // Determine whether the identifier is a type name. |
Steve Naroff | 7b36a1b | 2009-01-28 19:39:02 +0000 | [diff] [blame] | 792 | if (TypeTy *Ty = Actions.getTypeName(*Tok.getIdentifierInfo(), |
Douglas Gregor | 1075a16 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 793 | Tok.getLocation(), CurScope, &SS)) { |
Chris Lattner | a9d6ec7 | 2009-01-05 01:49:50 +0000 | [diff] [blame] | 794 | // This is a typename. Replace the current token in-place with an |
| 795 | // annotation type token. |
Chris Lattner | 5d7eace | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 796 | Tok.setKind(tok::annot_typename); |
Chris Lattner | a9d6ec7 | 2009-01-05 01:49:50 +0000 | [diff] [blame] | 797 | Tok.setAnnotationValue(Ty); |
| 798 | Tok.setAnnotationEndLoc(Tok.getLocation()); |
| 799 | if (SS.isNotEmpty()) // it was a C++ qualified type name. |
| 800 | Tok.setLocation(SS.getBeginLoc()); |
| 801 | |
| 802 | // In case the tokens were cached, have Preprocessor replace |
| 803 | // them with the annotation token. |
| 804 | PP.AnnotateCachedTokens(Tok); |
| 805 | return true; |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | if (!getLang().CPlusPlus) { |
Chris Lattner | a9d6ec7 | 2009-01-05 01:49:50 +0000 | [diff] [blame] | 809 | // If we're in C, we can't have :: tokens at all (the lexer won't return |
| 810 | // them). If the identifier is not a type, then it can't be scope either, |
| 811 | // just early exit. |
| 812 | return false; |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 813 | } |
Chris Lattner | a9d6ec7 | 2009-01-05 01:49:50 +0000 | [diff] [blame] | 814 | |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 815 | // If this is a template-id, annotate with a template-id or type token. |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 816 | if (NextToken().is(tok::less)) { |
| 817 | DeclTy *Template; |
| 818 | if (TemplateNameKind TNK |
| 819 | = Actions.isTemplateName(*Tok.getIdentifierInfo(), |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 820 | CurScope, Template, &SS)) |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 821 | AnnotateTemplateIdToken(Template, TNK, &SS); |
Douglas Gregor | 8e458f4 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 822 | } |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 823 | |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 824 | // The current token, which is either an identifier or a |
| 825 | // template-id, is not part of the annotation. Fall through to |
| 826 | // push that token back into the stream and complete the C++ scope |
| 827 | // specifier annotation. |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 830 | if (Tok.is(tok::annot_template_id)) { |
| 831 | TemplateIdAnnotation *TemplateId |
| 832 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 833 | if (TemplateId->Kind == TNK_Class_template) { |
| 834 | // A template-id that refers to a type was parsed into a |
| 835 | // template-id annotation in a context where we weren't allowed |
| 836 | // to produce a type annotation token. Update the template-id |
| 837 | // annotation token to a type annotation token now. |
| 838 | return !AnnotateTemplateIdTokenAsType(&SS); |
| 839 | } |
| 840 | } |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 841 | |
Chris Lattner | f72f79c | 2009-01-04 22:32:19 +0000 | [diff] [blame] | 842 | if (SS.isEmpty()) |
Chris Lattner | 1e01594 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 843 | return false; |
Chris Lattner | f72f79c | 2009-01-04 22:32:19 +0000 | [diff] [blame] | 844 | |
| 845 | // A C++ scope specifier that isn't followed by a typename. |
| 846 | // Push the current token back into the token stream (or revert it if it is |
| 847 | // cached) and use an annotation scope token for current token. |
| 848 | if (PP.isBacktrackEnabled()) |
| 849 | PP.RevertCachedTokens(1); |
| 850 | else |
| 851 | PP.EnterToken(Tok); |
| 852 | Tok.setKind(tok::annot_cxxscope); |
| 853 | Tok.setAnnotationValue(SS.getScopeRep()); |
| 854 | Tok.setAnnotationRange(SS.getRange()); |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 855 | |
Chris Lattner | f72f79c | 2009-01-04 22:32:19 +0000 | [diff] [blame] | 856 | // In case the tokens were cached, have Preprocessor replace them with the |
| 857 | // annotation token. |
| 858 | PP.AnnotateCachedTokens(Tok); |
Chris Lattner | 1e01594 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 859 | return true; |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | /// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 863 | /// annotates C++ scope specifiers and template-ids. This returns |
| 864 | /// true if the token was annotated. |
Chris Lattner | 2c30145 | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 865 | /// |
| 866 | /// Note that this routine emits an error if you call it with ::new or ::delete |
| 867 | /// as the current tokens, so only call it in contexts where these are invalid. |
Chris Lattner | 712f9a3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 868 | bool Parser::TryAnnotateCXXScopeToken() { |
Argiris Kirtzidis | 91c80dc | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 869 | assert(getLang().CPlusPlus && |
Chris Lattner | f72f79c | 2009-01-04 22:32:19 +0000 | [diff] [blame] | 870 | "Call sites of this function should be guarded by checking for C++"); |
Chris Lattner | 8376d2e | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 871 | assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) && |
| 872 | "Cannot be a type or scope token!"); |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 873 | |
Argiris Kirtzidis | 91c80dc | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 874 | CXXScopeSpec SS; |
Chris Lattner | d706dc8 | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 875 | if (!ParseOptionalCXXScopeSpecifier(SS)) |
Douglas Gregor | 0c281a8 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 876 | return Tok.is(tok::annot_template_id); |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 877 | |
Chris Lattner | f72f79c | 2009-01-04 22:32:19 +0000 | [diff] [blame] | 878 | // Push the current token back into the token stream (or revert it if it is |
| 879 | // cached) and use an annotation scope token for current token. |
| 880 | if (PP.isBacktrackEnabled()) |
| 881 | PP.RevertCachedTokens(1); |
| 882 | else |
| 883 | PP.EnterToken(Tok); |
| 884 | Tok.setKind(tok::annot_cxxscope); |
| 885 | Tok.setAnnotationValue(SS.getScopeRep()); |
| 886 | Tok.setAnnotationRange(SS.getRange()); |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 887 | |
Chris Lattner | f72f79c | 2009-01-04 22:32:19 +0000 | [diff] [blame] | 888 | // In case the tokens were cached, have Preprocessor replace them with the |
| 889 | // annotation token. |
| 890 | PP.AnnotateCachedTokens(Tok); |
Chris Lattner | 712f9a3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 891 | return true; |
Argiris Kirtzidis | 311db8c | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 892 | } |