blob: d3f26d875cc4f3940853a34bd857a199347e1c9b [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
Reid Spencer5f016e22007-07-11 17:01:13 +000027//===----------------------------------------------------------------------===//
28// Public interface to the file
29//===----------------------------------------------------------------------===//
30
Chris Lattner556beb72007-09-15 22:56:56 +000031/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner3599dbe2009-03-28 04:13:34 +000032/// the file is parsed. This inserts the parsed decls into the translation unit
33/// held by Ctx.
Daniel Dunbard3db4012008-10-16 16:54:18 +000034///
Ted Kremenek46157b52009-01-28 04:29:29 +000035void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Douglas Gregorf807fe02009-04-14 16:27:31 +000036 ASTContext &Ctx, bool PrintStats,
Douglas Gregor81b747b2009-09-17 21:32:03 +000037 bool CompleteTranslationUnit,
Daniel Dunbare119e842009-09-22 10:19:04 +000038 CodeCompleteConsumer *(*CreateCodeCompleter)(Sema &, void *Data),
Douglas Gregor81b747b2009-09-17 21:32:03 +000039 void *CreateCodeCompleterData) {
Chris Lattner556beb72007-09-15 22:56:56 +000040 // Collect global stats on Decls/Stmts (until we have a module streamer).
41 if (PrintStats) {
42 Decl::CollectingStats(true);
43 Stmt::CollectingStats(true);
44 }
Ted Kremenek46157b52009-01-28 04:29:29 +000045
Douglas Gregorf807fe02009-04-14 16:27:31 +000046 Sema S(PP, Ctx, *Consumer, CompleteTranslationUnit);
Eli Friedman80f33462008-05-27 04:23:47 +000047 Parser P(PP, S);
Chris Lattnera0e328f2008-02-06 00:15:02 +000048 PP.EnterMainSourceFile();
Mike Stump1eb44332009-09-09 15:08:12 +000049
Chris Lattnera0e328f2008-02-06 00:15:02 +000050 // Initialize the parser.
51 P.Initialize();
Mike Stump1eb44332009-09-09 15:08:12 +000052
Chris Lattner3599dbe2009-03-28 04:13:34 +000053 Consumer->Initialize(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +000054
Douglas Gregore7785042009-04-20 15:53:59 +000055 if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer))
56 SC->InitializeSema(S);
57
Douglas Gregor668c1a42009-04-21 22:25:48 +000058 if (ExternalASTSource *External = Ctx.getExternalSource()) {
Mike Stump1eb44332009-09-09 15:08:12 +000059 if (ExternalSemaSource *ExternalSema =
Douglas Gregor668c1a42009-04-21 22:25:48 +000060 dyn_cast<ExternalSemaSource>(External))
61 ExternalSema->InitializeSema(S);
62
63 External->StartTranslationUnit(Consumer);
64 }
Douglas Gregorfdd01722009-04-14 00:24:19 +000065
Douglas Gregor81b747b2009-09-17 21:32:03 +000066 CodeCompleteConsumer *CodeCompleter = 0;
Douglas Gregor86d9a522009-09-21 16:56:56 +000067 if (CreateCodeCompleter) {
Douglas Gregor81b747b2009-09-17 21:32:03 +000068 CodeCompleter = CreateCodeCompleter(S, CreateCodeCompleterData);
Douglas Gregor86d9a522009-09-21 16:56:56 +000069 S.setCodeCompleteConsumer(CodeCompleter);
70 }
Douglas Gregor81b747b2009-09-17 21:32:03 +000071
Chris Lattner682bf922009-03-29 16:50:03 +000072 Parser::DeclGroupPtrTy ADecl;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattnera0e328f2008-02-06 00:15:02 +000074 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
75 // If we got a null return and something *was* parsed, ignore it. This
76 // is due to a top-level semicolon, an action override, or a parse error
77 // skipping something.
Chris Lattner682bf922009-03-29 16:50:03 +000078 if (ADecl)
79 Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>());
Chris Lattnera0e328f2008-02-06 00:15:02 +000080 };
Mike Stump1eb44332009-09-09 15:08:12 +000081
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +000082 // process any TopLevelDecls generated by #pragma weak
83 for (llvm::SmallVector<Decl*,2>::iterator
84 I = S.WeakTopLevelDecls().begin(),
85 E = S.WeakTopLevelDecls().end(); I != E; ++I)
86 Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
87
Chris Lattner3599dbe2009-03-28 04:13:34 +000088 Consumer->HandleTranslationUnit(Ctx);
Fariborz Jahanian26e4cd32007-10-26 19:46:17 +000089
Douglas Gregor81b747b2009-09-17 21:32:03 +000090 if (CreateCodeCompleter)
91 delete CodeCompleter;
92
Chris Lattner556beb72007-09-15 22:56:56 +000093 if (PrintStats) {
94 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattnera0e328f2008-02-06 00:15:02 +000095 P.getActions().PrintStats();
Chris Lattner3599dbe2009-03-28 04:13:34 +000096 Ctx.PrintStats();
Chris Lattner556beb72007-09-15 22:56:56 +000097 Decl::PrintStats();
98 Stmt::PrintStats();
Chris Lattner31e6c7d2007-11-03 06:24:16 +000099 Consumer->PrintStats();
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Chris Lattner556beb72007-09-15 22:56:56 +0000101 Decl::CollectingStats(false);
102 Stmt::CollectingStats(false);
103 }
104}