Chris Lattner | e91c134 | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 1 | //===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | e91c134 | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 10 | // This file implements the clang::ParseAST method. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | e91c134 | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 14 | #include "clang/Sema/ParseAST.h" |
Chris Lattner | 498603d | 2009-04-19 05:30:08 +0000 | [diff] [blame] | 15 | #include "Sema.h" |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 16 | #include "clang/Sema/SemaConsumer.h" |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ExternalSemaSource.h" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExternalASTSource.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/Stmt.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "clang/Parse/Parser.h" |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 22 | #include <cstdio> |
| 23 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // Public interface to the file |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 30 | /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as |
Chris Lattner | 3599dbe | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 31 | /// the file is parsed. This inserts the parsed decls into the translation unit |
| 32 | /// held by Ctx. |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 33 | /// |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 34 | void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 35 | ASTContext &Ctx, bool PrintStats, |
| 36 | bool CompleteTranslationUnit) { |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 37 | // Collect global stats on Decls/Stmts (until we have a module streamer). |
| 38 | if (PrintStats) { |
| 39 | Decl::CollectingStats(true); |
| 40 | Stmt::CollectingStats(true); |
| 41 | } |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 42 | |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 43 | Sema S(PP, Ctx, *Consumer, CompleteTranslationUnit); |
Eli Friedman | 80f3346 | 2008-05-27 04:23:47 +0000 | [diff] [blame] | 44 | Parser P(PP, S); |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 45 | PP.EnterMainSourceFile(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 46 | |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 47 | // Initialize the parser. |
| 48 | P.Initialize(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 49 | |
Chris Lattner | 3599dbe | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 50 | Consumer->Initialize(Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 51 | |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 52 | if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer)) |
| 53 | SC->InitializeSema(S); |
| 54 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 55 | if (ExternalASTSource *External = Ctx.getExternalSource()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 56 | if (ExternalSemaSource *ExternalSema = |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 57 | dyn_cast<ExternalSemaSource>(External)) |
| 58 | ExternalSema->InitializeSema(S); |
| 59 | |
| 60 | External->StartTranslationUnit(Consumer); |
| 61 | } |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 63 | Parser::DeclGroupPtrTy ADecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 64 | |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 65 | while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file. |
| 66 | // If we got a null return and something *was* parsed, ignore it. This |
| 67 | // is due to a top-level semicolon, an action override, or a parse error |
| 68 | // skipping something. |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 69 | if (ADecl) |
| 70 | Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>()); |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 71 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 72 | |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 73 | // process any TopLevelDecls generated by #pragma weak |
| 74 | for (llvm::SmallVector<Decl*,2>::iterator |
| 75 | I = S.WeakTopLevelDecls().begin(), |
| 76 | E = S.WeakTopLevelDecls().end(); I != E; ++I) |
| 77 | Consumer->HandleTopLevelDecl(DeclGroupRef(*I)); |
| 78 | |
Chris Lattner | 3599dbe | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 79 | Consumer->HandleTranslationUnit(Ctx); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 81 | if (PrintStats) { |
| 82 | fprintf(stderr, "\nSTATISTICS:\n"); |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 83 | P.getActions().PrintStats(); |
Chris Lattner | 3599dbe | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 84 | Ctx.PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 85 | Decl::PrintStats(); |
| 86 | Stmt::PrintStats(); |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 87 | Consumer->PrintStats(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 88 | |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 89 | Decl::CollectingStats(false); |
| 90 | Stmt::CollectingStats(false); |
| 91 | } |
| 92 | } |