blob: 898b3c230e873ca1a4ba26c962abf875c3462289 [file] [log] [blame]
Chris Lattner8082d872008-02-06 00:23:21 +00001//===--- ParseAST.cpp - Provide the clang::ParseAST method ----------------===//
Chris Lattner73709ed2006-08-17 06:28:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner73709ed2006-08-17 06:28:25 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner8082d872008-02-06 00:23:21 +000010// This file implements the clang::ParseAST method.
Chris Lattner73709ed2006-08-17 06:28:25 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner8082d872008-02-06 00:23:21 +000014#include "clang/Sema/ParseAST.h"
Chris Lattner4e1f0c62009-04-19 05:30:08 +000015#include "Sema.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000016#include "clang/Sema/CodeCompleteConsumer.h"
Douglas Gregor162dd022009-04-20 15:53:59 +000017#include "clang/Sema/SemaConsumer.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000018#include "clang/Sema/ExternalSemaSource.h"
Chris Lattner75e0c8c2007-09-15 22:56:56 +000019#include "clang/AST/ASTConsumer.h"
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000020#include "clang/AST/ExternalASTSource.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000021#include "clang/AST/Stmt.h"
Chris Lattner73709ed2006-08-17 06:28:25 +000022#include "clang/Parse/Parser.h"
Torok Edwindb714922009-08-24 13:25:12 +000023#include <cstdio>
24
Chris Lattner73709ed2006-08-17 06:28:25 +000025using namespace clang;
26
Chris Lattner73709ed2006-08-17 06:28:25 +000027//===----------------------------------------------------------------------===//
28// Public interface to the file
29//===----------------------------------------------------------------------===//
30
Chris Lattner75e0c8c2007-09-15 22:56:56 +000031/// ParseAST - Parse the entire file specified, notifying the ASTConsumer as
Chris Lattner66918ee2009-03-28 04:13:34 +000032/// the file is parsed. This inserts the parsed decls into the translation unit
33/// held by Ctx.
Daniel Dunbar33d29b32008-10-16 16:54:18 +000034///
Ted Kremenek062115a2009-01-28 04:29:29 +000035void clang::ParseAST(Preprocessor &PP, ASTConsumer *Consumer,
Douglas Gregor54feb842009-04-14 16:27:31 +000036 ASTContext &Ctx, bool PrintStats,
Douglas Gregor2436e712009-09-17 21:32:03 +000037 bool CompleteTranslationUnit,
Daniel Dunbar242ea9a2009-11-13 08:58:20 +000038 CodeCompleteConsumer *CompletionConsumer) {
Chris Lattner75e0c8c2007-09-15 22:56:56 +000039 // Collect global stats on Decls/Stmts (until we have a module streamer).
40 if (PrintStats) {
41 Decl::CollectingStats(true);
42 Stmt::CollectingStats(true);
43 }
Ted Kremenek062115a2009-01-28 04:29:29 +000044
Daniel Dunbar242ea9a2009-11-13 08:58:20 +000045 Sema S(PP, Ctx, *Consumer, CompleteTranslationUnit, CompletionConsumer);
Eli Friedmanac0285a2008-05-27 04:23:47 +000046 Parser P(PP, S);
Chris Lattner4afeded2008-02-06 00:15:02 +000047 PP.EnterMainSourceFile();
Mike Stump11289f42009-09-09 15:08:12 +000048
Chris Lattner4afeded2008-02-06 00:15:02 +000049 // Initialize the parser.
50 P.Initialize();
Mike Stump11289f42009-09-09 15:08:12 +000051
Chris Lattner66918ee2009-03-28 04:13:34 +000052 Consumer->Initialize(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +000053
Douglas Gregor162dd022009-04-20 15:53:59 +000054 if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer))
55 SC->InitializeSema(S);
56
Douglas Gregora868bbd2009-04-21 22:25:48 +000057 if (ExternalASTSource *External = Ctx.getExternalSource()) {
Mike Stump11289f42009-09-09 15:08:12 +000058 if (ExternalSemaSource *ExternalSema =
Douglas Gregora868bbd2009-04-21 22:25:48 +000059 dyn_cast<ExternalSemaSource>(External))
60 ExternalSema->InitializeSema(S);
61
62 External->StartTranslationUnit(Consumer);
63 }
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000064
Chris Lattner5bbb3c82009-03-29 16:50:03 +000065 Parser::DeclGroupPtrTy ADecl;
Mike Stump11289f42009-09-09 15:08:12 +000066
Chris Lattner4afeded2008-02-06 00:15:02 +000067 while (!P.ParseTopLevelDecl(ADecl)) { // Not end of file.
68 // If we got a null return and something *was* parsed, ignore it. This
69 // is due to a top-level semicolon, an action override, or a parse error
70 // skipping something.
Chris Lattner5bbb3c82009-03-29 16:50:03 +000071 if (ADecl)
72 Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>());
Chris Lattner4afeded2008-02-06 00:15:02 +000073 };
Fariborz Jahanian9290ede2009-11-16 18:57:01 +000074 // Check for any pending objective-c implementation decl.
Fariborz Jahanianbc02a102009-11-17 17:15:16 +000075 while ((ADecl = P.RetrievePendingObjCImpDecl()))
Fariborz Jahanian9290ede2009-11-16 18:57:01 +000076 Consumer->HandleTopLevelDecl(ADecl.getAsVal<DeclGroupRef>());
Mike Stump11289f42009-09-09 15:08:12 +000077
Daniel Dunbarec5f8ae2009-12-01 21:57:20 +000078 // Process any TopLevelDecls generated by #pragma weak.
Ryan Flynnd963a492009-07-31 02:52:19 +000079 for (llvm::SmallVector<Decl*,2>::iterator
80 I = S.WeakTopLevelDecls().begin(),
81 E = S.WeakTopLevelDecls().end(); I != E; ++I)
82 Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
83
Chris Lattner66918ee2009-03-28 04:13:34 +000084 Consumer->HandleTranslationUnit(Ctx);
Fariborz Jahanian99e96b02007-10-26 19:46:17 +000085
Daniel Dunbarec5f8ae2009-12-01 21:57:20 +000086 if (ExternalSemaSource *ESS =
87 dyn_cast_or_null<ExternalSemaSource>(Ctx.getExternalSource()))
88 ESS->ForgetSema();
89
90 if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer))
91 SC->ForgetSema();
92
Chris Lattner75e0c8c2007-09-15 22:56:56 +000093 if (PrintStats) {
94 fprintf(stderr, "\nSTATISTICS:\n");
Chris Lattner4afeded2008-02-06 00:15:02 +000095 P.getActions().PrintStats();
Chris Lattner66918ee2009-03-28 04:13:34 +000096 Ctx.PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +000097 Decl::PrintStats();
98 Stmt::PrintStats();
Chris Lattner376cdaf2007-11-03 06:24:16 +000099 Consumer->PrintStats();
Chris Lattner75e0c8c2007-09-15 22:56:56 +0000100 }
101}