Skip to content

Commit

Permalink
[lldb][NFC] Use ClangASTContext in AppleObjCRuntime interfaces
Browse files Browse the repository at this point in the history
This code actually needs a ClangASTContext but instead takes a
clang::ASTContext and then retrieves the original ClangASTContext
via the global map of ClangASTContexts. Let's change it so
that it takes a ClangASTContext which is simpler and faster.
  • Loading branch information
Teemperor committed Dec 26, 2019
1 parent 54c5224 commit 37339d1
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 61 deletions.
Expand Up @@ -311,7 +311,8 @@ class ObjCRuntimeMethodType {
}

clang::ObjCMethodDecl *
BuildMethod(clang::ObjCInterfaceDecl *interface_decl, const char *name,
BuildMethod(ClangASTContext &clang_ast_ctxt,
clang::ObjCInterfaceDecl *interface_decl, const char *name,
bool instance,
ObjCLanguageRuntime::EncodingToTypeSP type_realizer_sp) {
if (!m_is_valid || m_type_vector.size() < 3)
Expand Down Expand Up @@ -360,8 +361,7 @@ class ObjCRuntimeMethodType {

clang::QualType ret_type =
ClangUtil::GetQualType(type_realizer_sp->RealizeType(
interface_decl->getASTContext(), m_type_vector[0].c_str(),
for_expression));
clang_ast_ctxt, m_type_vector[0].c_str(), for_expression));

if (ret_type.isNull())
return nullptr;
Expand All @@ -378,7 +378,7 @@ class ObjCRuntimeMethodType {
const bool for_expression = true;
clang::QualType arg_type =
ClangUtil::GetQualType(type_realizer_sp->RealizeType(
ast_ctx, m_type_vector[ai].c_str(), for_expression));
clang_ast_ctxt, m_type_vector[ai].c_str(), for_expression));

if (arg_type.isNull())
return nullptr; // well, we just wasted a bunch of time. Wish we could
Expand Down Expand Up @@ -455,8 +455,8 @@ bool AppleObjCDeclVendor::FinishDecl(clang::ObjCInterfaceDecl *interface_decl) {

ObjCRuntimeMethodType method_type(types);

clang::ObjCMethodDecl *method_decl =
method_type.BuildMethod(interface_decl, name, true, m_type_realizer_sp);
clang::ObjCMethodDecl *method_decl = method_type.BuildMethod(
m_ast_ctx, interface_decl, name, true, m_type_realizer_sp);

LLDB_LOGF(log, "[ AOTV::FD] Instance method [%s] [%s]", name, types);

Expand All @@ -474,7 +474,7 @@ bool AppleObjCDeclVendor::FinishDecl(clang::ObjCInterfaceDecl *interface_decl) {
ObjCRuntimeMethodType method_type(types);

clang::ObjCMethodDecl *method_decl = method_type.BuildMethod(
interface_decl, name, false, m_type_realizer_sp);
m_ast_ctx, interface_decl, name, false, m_type_realizer_sp);

LLDB_LOGF(log, "[ AOTV::FD] Class method [%s] [%s]", name, types);

Expand Down
Expand Up @@ -63,7 +63,7 @@ AppleObjCTypeEncodingParser::StructElement::StructElement()
: name(""), type(clang::QualType()), bitfield(0) {}

AppleObjCTypeEncodingParser::StructElement
AppleObjCTypeEncodingParser::ReadStructElement(clang::ASTContext &ast_ctx,
AppleObjCTypeEncodingParser::ReadStructElement(ClangASTContext &ast_ctx,
StringLexer &type,
bool for_expression) {
StructElement retval;
Expand All @@ -78,19 +78,19 @@ AppleObjCTypeEncodingParser::ReadStructElement(clang::ASTContext &ast_ctx,
}

clang::QualType AppleObjCTypeEncodingParser::BuildStruct(
clang::ASTContext &ast_ctx, StringLexer &type, bool for_expression) {
ClangASTContext &ast_ctx, StringLexer &type, bool for_expression) {
return BuildAggregate(ast_ctx, type, for_expression, '{', '}',
clang::TTK_Struct);
}

clang::QualType AppleObjCTypeEncodingParser::BuildUnion(
clang::ASTContext &ast_ctx, StringLexer &type, bool for_expression) {
ClangASTContext &ast_ctx, StringLexer &type, bool for_expression) {
return BuildAggregate(ast_ctx, type, for_expression, '(', ')',
clang::TTK_Union);
}

clang::QualType AppleObjCTypeEncodingParser::BuildAggregate(
clang::ASTContext &ast_ctx, StringLexer &type, bool for_expression,
ClangASTContext &ast_ctx, StringLexer &type, bool for_expression,
char opener, char closer, uint32_t kind) {
if (!type.NextIf(opener))
return clang::QualType();
Expand Down Expand Up @@ -124,10 +124,7 @@ clang::QualType AppleObjCTypeEncodingParser::BuildAggregate(
if (is_templated)
return clang::QualType(); // This is where we bail out. Sorry!

ClangASTContext *lldb_ctx = ClangASTContext::GetASTContext(&ast_ctx);
if (!lldb_ctx)
return clang::QualType();
CompilerType union_type(lldb_ctx->CreateRecordType(
CompilerType union_type(ast_ctx.CreateRecordType(
nullptr, lldb::eAccessPublic, name, kind, lldb::eLanguageTypeC));
if (union_type) {
ClangASTContext::StartTagDeclarationDefinition(union_type);
Expand All @@ -141,8 +138,7 @@ clang::QualType AppleObjCTypeEncodingParser::BuildAggregate(
}
ClangASTContext::AddFieldToRecordType(
union_type, element.name.c_str(),
CompilerType(ClangASTContext::GetASTContext(&ast_ctx),
element.type.getAsOpaquePtr()),
CompilerType(&ast_ctx, element.type.getAsOpaquePtr()),
lldb::eAccessPublic, element.bitfield);
++count;
}
Expand All @@ -152,20 +148,15 @@ clang::QualType AppleObjCTypeEncodingParser::BuildAggregate(
}

clang::QualType AppleObjCTypeEncodingParser::BuildArray(
clang::ASTContext &ast_ctx, StringLexer &type, bool for_expression) {
ClangASTContext &ast_ctx, StringLexer &type, bool for_expression) {
if (!type.NextIf('['))
return clang::QualType();
uint32_t size = ReadNumber(type);
clang::QualType element_type(BuildType(ast_ctx, type, for_expression));
if (!type.NextIf(']'))
return clang::QualType();
ClangASTContext *lldb_ctx = ClangASTContext::GetASTContext(&ast_ctx);
if (!lldb_ctx)
return clang::QualType();
CompilerType array_type(lldb_ctx->CreateArrayType(
CompilerType(ClangASTContext::GetASTContext(&ast_ctx),
element_type.getAsOpaquePtr()),
size, false));
CompilerType array_type(ast_ctx.CreateArrayType(
CompilerType(&ast_ctx, element_type.getAsOpaquePtr()), size, false));
return ClangUtil::GetQualType(array_type);
}

Expand All @@ -175,10 +166,12 @@ clang::QualType AppleObjCTypeEncodingParser::BuildArray(
// consume but ignore the type info and always return an 'id'; if anything,
// dynamic typing will resolve things for us anyway
clang::QualType AppleObjCTypeEncodingParser::BuildObjCObjectPointerType(
clang::ASTContext &ast_ctx, StringLexer &type, bool for_expression) {
ClangASTContext &clang_ast_ctx, StringLexer &type, bool for_expression) {
if (!type.NextIf('@'))
return clang::QualType();

clang::ASTContext &ast_ctx = clang_ast_ctx.getASTContext();

std::string name;

if (type.NextIf('"')) {
Expand Down Expand Up @@ -257,23 +250,25 @@ clang::QualType AppleObjCTypeEncodingParser::BuildObjCObjectPointerType(
}

clang::QualType
AppleObjCTypeEncodingParser::BuildType(clang::ASTContext &ast_ctx,
AppleObjCTypeEncodingParser::BuildType(ClangASTContext &clang_ast_ctx,
StringLexer &type, bool for_expression,
uint32_t *bitfield_bit_size) {
if (!type.HasAtLeast(1))
return clang::QualType();

clang::ASTContext &ast_ctx = clang_ast_ctx.getASTContext();

switch (type.Peek()) {
default:
break;
case '{':
return BuildStruct(ast_ctx, type, for_expression);
return BuildStruct(clang_ast_ctx, type, for_expression);
case '[':
return BuildArray(ast_ctx, type, for_expression);
return BuildArray(clang_ast_ctx, type, for_expression);
case '(':
return BuildUnion(ast_ctx, type, for_expression);
return BuildUnion(clang_ast_ctx, type, for_expression);
case '@':
return BuildObjCObjectPointerType(ast_ctx, type, for_expression);
return BuildObjCObjectPointerType(clang_ast_ctx, type, for_expression);
}

switch (type.Next()) {
Expand All @@ -289,10 +284,7 @@ AppleObjCTypeEncodingParser::BuildType(clang::ASTContext &ast_ctx,
case 'l':
return ast_ctx.getIntTypeForBitwidth(32, true);
// this used to be done like this:
// ClangASTContext *lldb_ctx = ClangASTContext::GetASTContext(&ast_ctx);
// if (!lldb_ctx)
// return clang::QualType();
// return lldb_ctx->GetIntTypeFromBitSize(32, true).GetQualType();
// return clang_ast_ctx->GetIntTypeFromBitSize(32, true).GetQualType();
// which uses one of the constants if one is available, but we don't think
// all this work is necessary.
case 'q':
Expand Down Expand Up @@ -331,7 +323,8 @@ AppleObjCTypeEncodingParser::BuildType(clang::ASTContext &ast_ctx,
return clang::QualType();
}
case 'r': {
clang::QualType target_type = BuildType(ast_ctx, type, for_expression);
clang::QualType target_type =
BuildType(clang_ast_ctx, type, for_expression);
if (target_type.isNull())
return clang::QualType();
else if (target_type == ast_ctx.UnknownAnyTy)
Expand All @@ -348,7 +341,8 @@ AppleObjCTypeEncodingParser::BuildType(clang::ASTContext &ast_ctx,
// practical cases
return ast_ctx.VoidPtrTy;
} else {
clang::QualType target_type = BuildType(ast_ctx, type, for_expression);
clang::QualType target_type =
BuildType(clang_ast_ctx, type, for_expression);
if (target_type.isNull())
return clang::QualType();
else if (target_type == ast_ctx.UnknownAnyTy)
Expand All @@ -362,13 +356,13 @@ AppleObjCTypeEncodingParser::BuildType(clang::ASTContext &ast_ctx,
}
}

CompilerType AppleObjCTypeEncodingParser::RealizeType(
clang::ASTContext &ast_ctx, const char *name, bool for_expression) {
CompilerType AppleObjCTypeEncodingParser::RealizeType(ClangASTContext &ast_ctx,
const char *name,
bool for_expression) {
if (name && name[0]) {
StringLexer lexer(name);
clang::QualType qual_type = BuildType(ast_ctx, lexer, for_expression);
return CompilerType(ClangASTContext::GetASTContext(&ast_ctx),
qual_type.getAsOpaquePtr());
return CompilerType(&ast_ctx, qual_type.getAsOpaquePtr());
}
return CompilerType();
}
Expand Up @@ -22,7 +22,7 @@ class AppleObjCTypeEncodingParser : public ObjCLanguageRuntime::EncodingToType {
AppleObjCTypeEncodingParser(ObjCLanguageRuntime &runtime);
~AppleObjCTypeEncodingParser() override = default;

CompilerType RealizeType(clang::ASTContext &ast_ctx, const char *name,
CompilerType RealizeType(ClangASTContext &ast_ctx, const char *name,
bool for_expression) override;

private:
Expand All @@ -35,29 +35,29 @@ class AppleObjCTypeEncodingParser : public ObjCLanguageRuntime::EncodingToType {
~StructElement() = default;
};

clang::QualType BuildType(clang::ASTContext &ast_ctx, StringLexer &type,
clang::QualType BuildType(ClangASTContext &clang_ast_ctx, StringLexer &type,
bool for_expression,
uint32_t *bitfield_bit_size = nullptr);

clang::QualType BuildStruct(clang::ASTContext &ast_ctx, StringLexer &type,
clang::QualType BuildStruct(ClangASTContext &ast_ctx, StringLexer &type,
bool for_expression);

clang::QualType BuildAggregate(clang::ASTContext &ast_ctx, StringLexer &type,
bool for_expression, char opener, char closer,
uint32_t kind);
clang::QualType BuildAggregate(ClangASTContext &clang_ast_ctx,
StringLexer &type, bool for_expression,
char opener, char closer, uint32_t kind);

clang::QualType BuildUnion(clang::ASTContext &ast_ctx, StringLexer &type,
clang::QualType BuildUnion(ClangASTContext &ast_ctx, StringLexer &type,
bool for_expression);

clang::QualType BuildArray(clang::ASTContext &ast_ctx, StringLexer &type,
clang::QualType BuildArray(ClangASTContext &ast_ctx, StringLexer &type,
bool for_expression);

std::string ReadStructName(StringLexer &type);

StructElement ReadStructElement(clang::ASTContext &ast_ctx, StringLexer &type,
StructElement ReadStructElement(ClangASTContext &ast_ctx, StringLexer &type,
bool for_expression);

clang::QualType BuildObjCObjectPointerType(clang::ASTContext &ast_ctx,
clang::QualType BuildObjCObjectPointerType(ClangASTContext &clang_ast_ctx,
StringLexer &type,
bool for_expression);

Expand Down
Expand Up @@ -313,12 +313,6 @@ ObjCLanguageRuntime::EncodingToType::RealizeType(const char *name,
return CompilerType();
}

CompilerType ObjCLanguageRuntime::EncodingToType::RealizeType(
ClangASTContext &ast_ctx, const char *name, bool for_expression) {
clang::ASTContext &clang_ast = ast_ctx.getASTContext();
return RealizeType(clang_ast, name, for_expression);
}

ObjCLanguageRuntime::EncodingToType::~EncodingToType() {}

ObjCLanguageRuntime::EncodingToTypeSP ObjCLanguageRuntime::GetEncodingToType() {
Expand Down
Expand Up @@ -145,12 +145,9 @@ class ObjCLanguageRuntime : public LanguageRuntime {
virtual ~EncodingToType();

virtual CompilerType RealizeType(ClangASTContext &ast_ctx, const char *name,
bool for_expression);
bool for_expression) = 0;
virtual CompilerType RealizeType(const char *name, bool for_expression);

virtual CompilerType RealizeType(clang::ASTContext &ast_ctx,
const char *name, bool for_expression) = 0;

protected:
std::unique_ptr<ClangASTContext> m_scratch_ast_ctx_up;
};
Expand Down

0 comments on commit 37339d1

Please sign in to comment.