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 | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/CodeCompleteConsumer.h" |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaConsumer.h" |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 18 | #include "clang/Sema/ExternalSemaSource.h" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExternalASTSource.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 21 | #include "clang/AST/Stmt.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "clang/Parse/Parser.h" |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 23 | #include <cstdio> |
| 24 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Daniel Dunbar | b69eca5 | 2010-04-08 02:59:56 +0000 | [diff] [blame] | 27 | static void DumpRecordLayouts(ASTContext &C) { |
| 28 | for (ASTContext::type_iterator I = C.types_begin(), E = C.types_end(); |
| 29 | I != E; ++I) { |
| 30 | const RecordType *RT = dyn_cast<RecordType>(*I); |
| 31 | if (!RT) |
| 32 | continue; |
| 33 | |
| 34 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 35 | if (!RD || RD->isImplicit() || RD->isDependentType() || |
| 36 | RD->isInvalidDecl() || !RD->getDefinition()) |
| 37 | continue; |
| 38 | |
| 39 | // FIXME: Do we really need to hard code this? |
| 40 | if (RD->getQualifiedNameAsString() == "__va_list_tag") |
| 41 | continue; |
| 42 | |
| 43 | C.DumpRecordLayout(RD, llvm::errs()); |
| 44 | } |
| 45 | } |
| 46 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // Public interface to the file |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 51 | /// ParseAST - Parse the entire file specified, notifying the ASTConsumer as |
Chris Lattner | 3599dbe | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 52 | /// the file is parsed. This inserts the parsed decls into the translation unit |
| 53 | /// held by Ctx. |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 54 | /// |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 55 | void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer, |
Douglas Gregor | f807fe0 | 2009-04-14 16:27:31 +0000 | [diff] [blame] | 56 | ASTContext &Ctx, bool PrintStats, |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 57 | bool CompleteTranslationUnit, |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 58 | CodeCompleteConsumer *CompletionConsumer) { |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 59 | // Collect global stats on Decls/Stmts (until we have a module streamer). |
| 60 | if (PrintStats) { |
| 61 | Decl::CollectingStats(true); |
| 62 | Stmt::CollectingStats(true); |
| 63 | } |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 64 | |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 65 | Sema S(PP, Ctx, *Consumer, CompleteTranslationUnit, CompletionConsumer); |
Eli Friedman | 80f3346 | 2008-05-27 04:23:47 +0000 | [diff] [blame] | 66 | Parser P(PP, S); |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 67 | PP.EnterMainSourceFile(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 69 | // Initialize the parser. |
| 70 | P.Initialize(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 3599dbe | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 72 | Consumer->Initialize(Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Douglas Gregor | e778504 | 2009-04-20 15:53:59 +0000 | [diff] [blame] | 74 | if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer)) |
| 75 | SC->InitializeSema(S); |
| 76 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 77 | if (ExternalASTSource *External = Ctx.getExternalSource()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | if (ExternalSemaSource *ExternalSema = |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 79 | dyn_cast<ExternalSemaSource>(External)) |
| 80 | ExternalSema->InitializeSema(S); |
| 81 | |
| 82 | External->StartTranslationUnit(Consumer); |
| 83 | } |
Douglas Gregor | fdd0172 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 85 | Parser::DeclGroupPtrTy ADecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 87 | while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file. |
| 88 | // If we got a null return and something *was* parsed, ignore it. This |
| 89 | // is due to a top-level semicolon, an action override, or a parse error |
| 90 | // skipping something. |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 91 | if (ADecl) |
| 92 | Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>()); |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 93 | }; |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 94 | // Check for any pending objective-c implementation decl. |
Fariborz Jahanian | 1ac7104 | 2009-11-17 17:15:16 +0000 | [diff] [blame] | 95 | while ((ADecl = P.RetrievePendingObjCImpDecl())) |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 96 | Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Daniel Dunbar | ec2a4ed | 2009-12-01 21:57:20 +0000 | [diff] [blame] | 98 | // Process any TopLevelDecls generated by #pragma weak. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 99 | for (llvm::SmallVector<Decl*,2>::iterator |
| 100 | I = S.WeakTopLevelDecls().begin(), |
| 101 | E = S.WeakTopLevelDecls().end(); I != E; ++I) |
| 102 | Consumer->HandleTopLevelDecl(DeclGroupRef(*I)); |
| 103 | |
Daniel Dunbar | b69eca5 | 2010-04-08 02:59:56 +0000 | [diff] [blame] | 104 | // Dump record layouts, if requested. |
| 105 | if (PP.getLangOptions().DumpRecordLayouts) |
| 106 | DumpRecordLayouts(Ctx); |
| 107 | |
Chris Lattner | 3599dbe | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 108 | Consumer->HandleTranslationUnit(Ctx); |
Fariborz Jahanian | 26e4cd3 | 2007-10-26 19:46:17 +0000 | [diff] [blame] | 109 | |
Daniel Dunbar | ec2a4ed | 2009-12-01 21:57:20 +0000 | [diff] [blame] | 110 | if (ExternalSemaSource *ESS = |
| 111 | dyn_cast_or_null<ExternalSemaSource>(Ctx.getExternalSource())) |
| 112 | ESS->ForgetSema(); |
| 113 | |
| 114 | if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer)) |
| 115 | SC->ForgetSema(); |
| 116 | |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 117 | if (PrintStats) { |
| 118 | fprintf(stderr, "\nSTATISTICS:\n"); |
Chris Lattner | a0e328f | 2008-02-06 00:15:02 +0000 | [diff] [blame] | 119 | P.getActions().PrintStats(); |
Chris Lattner | 3599dbe | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 120 | Ctx.PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 121 | Decl::PrintStats(); |
| 122 | Stmt::PrintStats(); |
Chris Lattner | 31e6c7d | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 123 | Consumer->PrintStats(); |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 124 | } |
| 125 | } |