summaryrefslogtreecommitdiff
path: root/lang/c/objctypes.cpp
blob: 83a097be13580b8abd143f60b2194e0f146eb949 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "objctypes.h"
#include "binaryninjaapi.h"

using namespace BinaryNinja;

namespace {

bool IsPointerToObjCObject(const Ref<Type>& type)
{
	if (!type || type->GetClass() != PointerTypeClass)
		return false;

	auto childType = type->GetChildType();
	if (!childType || childType->GetClass() != NamedTypeReferenceClass)
		return false;

	auto namedType = childType->GetNamedTypeReference();
	return namedType && namedType->GetName().GetString() == "objc_object";
}

}  // unnamed namespace

PseudoObjCTypePrinter::PseudoObjCTypePrinter() : TypePrinter("Objective-C") {}

std::vector<InstructionTextToken> PseudoObjCTypePrinter::GetTypeTokensBeforeName(
	Ref<Type> type, Ref<Platform> platform, uint8_t baseConfidence, Ref<Type> parentType, BNTokenEscapingType escaping)
{
	// It is idiomatic in Objective-C to use `id` rather than `objc_object*`.
	if (IsPointerToObjCObject(type))
		return {InstructionTextToken {baseConfidence, TypeNameToken, "id"}};

	return TypePrinter::GetDefault()->GetTypeTokensBeforeName(type, platform, baseConfidence, parentType, escaping);
}

std::vector<InstructionTextToken> PseudoObjCTypePrinter::GetTypeTokensAfterName(
	Ref<Type> type, Ref<Platform> platform, uint8_t baseConfidence, Ref<Type> parentType, BNTokenEscapingType escaping)
{
	return TypePrinter::GetDefault()->GetTypeTokensAfterName(type, platform, baseConfidence, parentType, escaping);
}

std::vector<TypeDefinitionLine> PseudoObjCTypePrinter::GetTypeLines(Ref<Type> type, const TypeContainer& types,
	const QualifiedName& name, int paddingCols, bool collapsed, BNTokenEscapingType escaping)
{
	return TypePrinter::GetDefault()->GetTypeLines(type, types, name, paddingCols, collapsed, escaping);
}