blob: 73f1167d0e684a0f4129e9d261c66713f9ba3ffb [file] [log] [blame]
Chris Lattnere91c1342008-02-06 00:23:21 +00001//===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnere91c1342008-02-06 00:23:21 +000010// This file implements the clang::ParseAST method.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnere91c1342008-02-06 00:23:21 +000014#include "clang/Sema/ParseAST.h"
Chris Lattner498603d2009-04-19 05:30:08 +000015#include "Sema.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000016#include "clang/Sema/CodeCompleteConsumer.h"
Douglas Gregore7785042009-04-20 15:53:59 +000017#include "clang/Sema/SemaConsumer.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000018#include "clang/Sema/ExternalSemaSource.h"
Chris Lattner556beb72007-09-15 22:56:56 +000019#include "clang/AST/ASTConsumer.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000020#include "clang/AST/ExternalASTSource.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Stmt.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/Parse/Parser.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000023#include <cstdio>
24
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
Daniel Dunbarb69eca52010-04-08 02:59:56 +000027static 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 Spencer5f016e22007-07-11 17:01:13 +000047//===----------------------------------------------------------------------===//
48// Public interface to the file
49//===----------------------------------------------------------------------===//
50
Chris Lattner556beb72007-09-15 22:56:56 +000051/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner3599dbe2009-03-28 04:13:34 +000052/// the file is parsed. This inserts the parsed decls into the translation unit
53/// held by Ctx.
Daniel Dunbard3db4012008-10-16 16:54:18 +000054///
Ted Kremenek46157b52009-01-28 04:29:29 +000055void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Douglas Gregorf807fe02009-04-14 16:27:31 +000056 ASTContext &Ctx, bool PrintStats,
Douglas Gregor81b747b2009-09-17 21:32:03 +000057 bool CompleteTranslationUnit,
Daniel Dunbar3a2838d2009-11-13 08:58:20 +000058 CodeCompleteConsumer *CompletionConsumer) {
Chris Lattner556beb72007-09-15 22:56:56 +000059 // 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 Kremenek46157b52009-01-28 04:29:29 +000064
Daniel Dunbar3a2838d2009-11-13 08:58:20 +000065 Sema S(PP, Ctx, *Consumer, CompleteTranslationUnit, CompletionConsumer);
Eli Friedman80f33462008-05-27 04:23:47 +000066 Parser P(PP, S);
Chris Lattnere127a0d2010-04-20 20:35:58 +000067 PP.EnterMainSourceFile();
Mike Stump1eb44332009-09-09 15:08:12 +000068
Chris Lattnera0e328f2008-02-06 00:15:02 +000069 // Initialize the parser.
70 P.Initialize();
Mike Stump1eb44332009-09-09 15:08:12 +000071
Chris Lattner3599dbe2009-03-28 04:13:34 +000072 Consumer->Initialize(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +000073
Douglas Gregore7785042009-04-20 15:53:59 +000074 if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer))
75 SC->InitializeSema(S);
76
Douglas Gregor668c1a42009-04-21 22:25:48 +000077 if (ExternalASTSource *External = Ctx.getExternalSource()) {
Mike Stump1eb44332009-09-09 15:08:12 +000078 if (ExternalSemaSource *ExternalSema =
Douglas Gregor668c1a42009-04-21 22:25:48 +000079 dyn_cast<ExternalSemaSource>(External))
80 ExternalSema->InitializeSema(S);
81
82 External->StartTranslationUnit(Consumer);
83 }
Douglas Gregorfdd01722009-04-14 00:24:19 +000084
Chris Lattner682bf922009-03-29 16:50:03 +000085 Parser::DeclGroupPtrTy ADecl;
Mike Stump1eb44332009-09-09 15:08:12 +000086
Chris Lattnera0e328f2008-02-06 00:15:02 +000087 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 Lattner682bf922009-03-29 16:50:03 +000091 if (ADecl)
92 Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>());
Chris Lattnera0e328f2008-02-06 00:15:02 +000093 };
Fariborz Jahanian63e963c2009-11-16 18:57:01 +000094 // Check for any pending objective-c implementation decl.
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000095 while ((ADecl = P.FinishPendingObjCActions()))
Fariborz Jahanian63e963c2009-11-16 18:57:01 +000096 Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>());
Mike Stump1eb44332009-09-09 15:08:12 +000097
Daniel Dunbarec2a4ed2009-12-01 21:57:20 +000098 // Process any TopLevelDecls generated by #pragma weak.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +000099 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 Dunbarb69eca52010-04-08 02:59:56 +0000104 // Dump record layouts, if requested.
105 if (PP.getLangOptions().DumpRecordLayouts)
106 DumpRecordLayouts(Ctx);
107
Chris Lattner3599dbe2009-03-28 04:13:34 +0000108 Consumer->HandleTranslationUnit(Ctx);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +0000109
Daniel Dunbarec2a4ed2009-12-01 21:57:20 +0000110 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 Lattner556beb72007-09-15 22:56:56 +0000117 if (PrintStats) {
118 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattnera0e328f2008-02-06 00:15:02 +0000119 P.getActions().PrintStats();
Chris Lattner3599dbe2009-03-28 04:13:34 +0000120 Ctx.PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +0000121 Decl::PrintStats();
122 Stmt::PrintStats();
Chris Lattner31e6c7d2007-11-03 06:24:16 +0000123 Consumer->PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +0000124 }
125}