2007-07-11 21:01:13 +04:00
|
|
|
//===--- ModuleBuilder.cpp - Emit LLVM Code from ASTs ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This builds an AST and converts it to LLVM Code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/CodeGen/ModuleBuilder.h"
|
|
|
|
#include "CodeGenModule.h"
|
2010-06-16 03:19:56 +04:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2008-02-06 05:01:47 +03:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 08:54:23 +04:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
2008-02-06 05:01:47 +03:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2009-07-01 21:00:06 +04:00
|
|
|
#include "llvm/LLVMContext.h"
|
2008-02-06 05:01:47 +03:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Target/TargetData.h"
|
2008-08-05 22:50:11 +04:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2009-03-26 08:00:52 +03:00
|
|
|
using namespace clang;
|
2008-08-05 22:50:11 +04:00
|
|
|
|
2008-02-06 05:01:47 +03:00
|
|
|
namespace {
|
2009-11-28 22:45:26 +03:00
|
|
|
class CodeGeneratorImpl : public CodeGenerator {
|
2011-09-26 03:23:43 +04:00
|
|
|
DiagnosticsEngine &Diags;
|
2012-02-05 06:12:40 +04:00
|
|
|
OwningPtr<const llvm::TargetData> TD;
|
2008-02-06 05:01:47 +03:00
|
|
|
ASTContext *Ctx;
|
2009-11-12 20:24:48 +03:00
|
|
|
const CodeGenOptions CodeGenOpts; // Intentionally copied in.
|
2008-02-06 05:01:47 +03:00
|
|
|
protected:
|
2012-02-05 06:12:40 +04:00
|
|
|
OwningPtr<llvm::Module> M;
|
|
|
|
OwningPtr<CodeGen::CodeGenModule> Builder;
|
2008-02-06 05:01:47 +03:00
|
|
|
public:
|
2011-09-26 03:23:43 +04:00
|
|
|
CodeGeneratorImpl(DiagnosticsEngine &diags, const std::string& ModuleName,
|
2010-03-04 07:29:44 +03:00
|
|
|
const CodeGenOptions &CGO, llvm::LLVMContext& C)
|
|
|
|
: Diags(diags), CodeGenOpts(CGO), M(new llvm::Module(ModuleName, C)) {}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-08-05 22:50:11 +04:00
|
|
|
virtual ~CodeGeneratorImpl() {}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-10-21 23:55:09 +04:00
|
|
|
virtual llvm::Module* GetModule() {
|
|
|
|
return M.get();
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-08-07 23:47:41 +04:00
|
|
|
virtual llvm::Module* ReleaseModule() {
|
2008-08-05 22:50:11 +04:00
|
|
|
return M.take();
|
2008-02-06 05:01:47 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-02-06 05:01:47 +03:00
|
|
|
virtual void Initialize(ASTContext &Context) {
|
|
|
|
Ctx = &Context;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-09-02 04:18:52 +04:00
|
|
|
M->setTargetTriple(Ctx->getTargetInfo().getTriple().getTriple());
|
|
|
|
M->setDataLayout(Ctx->getTargetInfo().getTargetDescription());
|
|
|
|
TD.reset(new llvm::TargetData(Ctx->getTargetInfo().getTargetDescription()));
|
2009-11-12 20:24:48 +03:00
|
|
|
Builder.reset(new CodeGen::CodeGenModule(Context, CodeGenOpts,
|
2010-03-04 07:29:44 +03:00
|
|
|
*M, *TD, Diags));
|
2008-02-06 05:01:47 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-11-18 04:26:59 +04:00
|
|
|
virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
|
2009-01-20 04:17:11 +03:00
|
|
|
// Make sure to emit all elements of a Decl.
|
2009-03-29 20:50:03 +04:00
|
|
|
for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
|
|
|
|
Builder->EmitTopLevelDecl(*I);
|
2011-11-18 04:26:59 +04:00
|
|
|
return true;
|
2008-02-06 05:01:47 +03:00
|
|
|
}
|
2008-08-16 03:26:23 +04:00
|
|
|
|
2008-02-06 07:51:19 +03:00
|
|
|
/// HandleTagDeclDefinition - This callback is invoked each time a TagDecl
|
2009-03-29 20:50:03 +04:00
|
|
|
/// to (e.g. struct, union, enum, class) is completed. This allows the
|
|
|
|
/// client hack on the type, which can occur at any point in the file
|
|
|
|
/// (because these can be defined in declspecs).
|
2008-02-06 07:51:19 +03:00
|
|
|
virtual void HandleTagDeclDefinition(TagDecl *D) {
|
2008-02-06 08:08:19 +03:00
|
|
|
Builder->UpdateCompletedType(D);
|
2011-02-15 21:11:42 +03:00
|
|
|
|
|
|
|
// In C++, we may have member functions that need to be emitted at this
|
|
|
|
// point.
|
|
|
|
if (Ctx->getLangOptions().CPlusPlus && !D->isDependentContext()) {
|
|
|
|
for (DeclContext::decl_iterator M = D->decls_begin(),
|
|
|
|
MEnd = D->decls_end();
|
|
|
|
M != MEnd; ++M)
|
|
|
|
if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
|
2011-05-07 00:44:56 +04:00
|
|
|
if (Method->doesThisDeclarationHaveABody() &&
|
2011-02-20 00:54:50 +03:00
|
|
|
(Method->hasAttr<UsedAttr>() ||
|
|
|
|
Method->hasAttr<ConstructorAttr>()))
|
2011-02-15 21:11:42 +03:00
|
|
|
Builder->EmitTopLevelDecl(Method);
|
|
|
|
}
|
2008-02-06 07:51:19 +03:00
|
|
|
}
|
2008-08-07 23:47:41 +04:00
|
|
|
|
2009-03-28 07:11:33 +03:00
|
|
|
virtual void HandleTranslationUnit(ASTContext &Ctx) {
|
2008-08-07 23:47:41 +04:00
|
|
|
if (Diags.hasErrorOccurred()) {
|
|
|
|
M.reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Builder)
|
|
|
|
Builder->Release();
|
2009-12-19 20:50:07 +03:00
|
|
|
}
|
2009-04-21 21:11:58 +04:00
|
|
|
|
|
|
|
virtual void CompleteTentativeDefinition(VarDecl *D) {
|
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Builder->EmitTentativeDefinition(D);
|
|
|
|
}
|
Rework when and how vtables are emitted, by tracking where vtables are
"used" (e.g., we will refer to the vtable in the generated code) and
when they are defined (i.e., because we've seen the key function
definition). Previously, we were effectively tracking "potential
definitions" rather than uses, so we were a bit too eager about emitting
vtables for classes without key functions.
The new scheme:
- For every use of a vtable, Sema calls MarkVTableUsed() to indicate
the use. For example, this occurs when calling a virtual member
function of the class, defining a constructor of that class type,
dynamic_cast'ing from that type to a derived class, casting
to/through a virtual base class, etc.
- For every definition of a vtable, Sema calls MarkVTableUsed() to
indicate the definition. This happens at the end of the translation
unit for classes whose key function has been defined (so we can
delay computation of the key function; see PR6564), and will also
occur with explicit template instantiation definitions.
- For every vtable defined/used, we mark all of the virtual member
functions of that vtable as defined/used, unless we know that the key
function is in another translation unit. This instantiates virtual
member functions when needed.
- At the end of the translation unit, Sema tells CodeGen (via the
ASTConsumer) which vtables must be defined (CodeGen will define
them) and which may be used (for which CodeGen will define the
vtables lazily).
From a language perspective, both the old and the new schemes are
permissible: we're allowed to instantiate virtual member functions
whenever we want per the standard. However, all other C++ compilers
were more lazy than we were, and our eagerness was both a performance
issue (we instantiated too much) and a portability problem (we broke
Boost test cases, which now pass).
Notes:
(1) There's a ton of churn in the tests, because the order in which
vtables get emitted to IR has changed. I've tried to isolate some of
the larger tests from these issues.
(2) Some diagnostics related to
implicitly-instantiated/implicitly-defined virtual member functions
have moved to the point of first use/definition. It's better this
way.
(3) I could use a review of the places where we MarkVTableUsed, to
see if I missed any place where the language effectively requires a
vtable.
Fixes PR7114 and PR6564.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@103718 91177308-0d34-0410-b5e6-96231b3b80d8
2010-05-13 20:44:06 +04:00
|
|
|
|
|
|
|
virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) {
|
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Builder->EmitVTable(RD, DefinitionRequired);
|
|
|
|
}
|
2008-02-06 05:01:47 +03:00
|
|
|
};
|
2007-07-13 09:13:43 +04:00
|
|
|
}
|
|
|
|
|
2011-12-20 06:48:34 +04:00
|
|
|
void CodeGenerator::anchor() { }
|
|
|
|
|
2011-09-26 03:23:43 +04:00
|
|
|
CodeGenerator *clang::CreateLLVMCodeGen(DiagnosticsEngine &Diags,
|
2008-08-05 22:50:11 +04:00
|
|
|
const std::string& ModuleName,
|
2009-11-12 20:24:48 +03:00
|
|
|
const CodeGenOptions &CGO,
|
2009-07-02 03:14:14 +04:00
|
|
|
llvm::LLVMContext& C) {
|
2010-03-04 07:29:44 +03:00
|
|
|
return new CodeGeneratorImpl(Diags, ModuleName, CGO, C);
|
2008-02-05 11:06:13 +03:00
|
|
|
}
|