Skip to content

Commit

Permalink
feat: add several duplicated method for #1
Browse files Browse the repository at this point in the history
  • Loading branch information
ApsarasX committed Apr 26, 2021
1 parent 6270f94 commit 5af02bc
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 8 deletions.
2 changes: 2 additions & 0 deletions include/IR/ArrayType.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ class ArrayType : public Napi::ObjectWrap<ArrayType> {
Napi::Value getNumElements(const Napi::CallbackInfo &info);

Napi::Value getElementType(const Napi::CallbackInfo &info);

Napi::Value isStructTy(const Napi::CallbackInfo &info);
};
2 changes: 2 additions & 0 deletions include/IR/GlobalVariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ class GlobalVariable : public Napi::ObjectWrap<GlobalVariable> {

private:
llvm::GlobalVariable *globalVariable = nullptr;

Napi::Value getType(const Napi::CallbackInfo &info);
};
2 changes: 2 additions & 0 deletions include/IR/IntegerType.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ class IntegerType : public Napi::ObjectWrap<IntegerType> {
llvm::IntegerType *integerType = nullptr;

static Napi::Value get(const Napi::CallbackInfo &info);

Napi::Value isStructTy(const Napi::CallbackInfo &info);
};
2 changes: 2 additions & 0 deletions include/IR/PointerType.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ class PointerType : public Napi::ObjectWrap<PointerType> {
Napi::Value getElementType(const Napi::CallbackInfo &info);

Napi::Value isPointerTy(const Napi::CallbackInfo &info);

Napi::Value isStructTy(const Napi::CallbackInfo &info);
};
4 changes: 4 additions & 0 deletions include/IR/StructType.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ class StructType : public Napi::ObjectWrap<StructType> {
static Napi::Value create(const Napi::CallbackInfo &info);

void setBody(const Napi::CallbackInfo &info);

Napi::Value getPointerTo(const Napi::CallbackInfo &info);

Napi::Value isStructTy(const Napi::CallbackInfo &info);
};
1 change: 1 addition & 0 deletions include/Util/ErrMsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ namespace ErrMsg {
"\n\t - (context: LLVMContext, name: string)"
"\n\t - (context: LLVMContext, elementTypes: Type[], name: string)";
static const char *setBody = "StructType.setBody needs to be called with (elementTypes: Type[])";
static const char *getPointerTo = "StructType.getPointer needs to called with: (addrSpace?: number)";
}
namespace Type {
static const char *constructor = "Type.constructor needs to ce called with new (external: Napi::External<llvm::Type>)";
Expand Down
26 changes: 23 additions & 3 deletions llvm-bindings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ declare namespace llvm {

public isAggregateType(): boolean;

public getPointerTo(addrSpace?: number): Type;
public getPointerTo(addrSpace?: number): PointerType;

public getPrimitiveSizeInBits(): number;

Expand All @@ -156,6 +156,9 @@ declare namespace llvm {
class IntegerType extends Type {
public static get(context: LLVMContext, numBits: number): IntegerType;

// duplicated
public isStructTy(): boolean;

protected constructor();
}

Expand All @@ -169,6 +172,9 @@ declare namespace llvm {
// duplicated
public isPointerTy(): boolean;

// duplicated
public isStructTy(): boolean;

protected constructor();
}

Expand All @@ -185,6 +191,12 @@ declare namespace llvm {

public setBody(elementTypes: Type[]): void;

// duplicated
public getPointerTo(addrSpace?: number): PointerType;

// duplicated
public isStructTy(): boolean;

protected constructor();
}

Expand All @@ -197,6 +209,9 @@ declare namespace llvm {

public getElementType(): Type;

// duplicated
public isStructTy(): boolean;

protected constructor();
}

Expand Down Expand Up @@ -420,8 +435,13 @@ declare namespace llvm {
}

class GlobalVariable extends GlobalObject {
public constructor(type: Type, isConstant: boolean, linkage: number, initializer?: Constant, name?: string);
public constructor(module: Module, type: Type, isConstant: boolean, linkage: number, initializer: Constant, name?: string);
// customized
public constructor(type: Type, isConstant: boolean, linkage: number, initializer?: Constant | null, name?: string);
// customized
public constructor(module: Module, type: Type, isConstant: boolean, linkage: number, initializer: Constant | null, name?: string);

// duplicated
public getType(): Type;
}

class Function extends GlobalObject {
Expand Down
7 changes: 6 additions & 1 deletion src/IR/ArrayType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ void ArrayType::Init(Napi::Env env, Napi::Object &exports) {
StaticMethod("get", &ArrayType::get),
StaticMethod("isValidElementType", &ArrayType::isValidElementType),
InstanceMethod("getNumElements", &ArrayType::getNumElements),
InstanceMethod("getElementType", &ArrayType::getElementType)
InstanceMethod("getElementType", &ArrayType::getElementType),
InstanceMethod("isStructTy", &ArrayType::isStructTy)
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
Expand Down Expand Up @@ -66,3 +67,7 @@ Napi::Value ArrayType::getNumElements(const Napi::CallbackInfo &info) {
Napi::Value ArrayType::getElementType(const Napi::CallbackInfo &info) {
return Type::New(info.Env(), arrayType->getElementType());
}

Napi::Value ArrayType::isStructTy(const Napi::CallbackInfo &info) {
return Napi::Boolean::New(info.Env(), arrayType->isStructTy());
}
5 changes: 4 additions & 1 deletion src/IR/Constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ Napi::Object Constant::New(Napi::Env env, llvm::Constant *constant) {
}

bool Constant::IsClassOf(const Napi::Value &value) {
return value.As<Napi::Object>().InstanceOf(constructor.Value());
return value.IsNull() || value.As<Napi::Object>().InstanceOf(constructor.Value());
}

llvm::Constant *Constant::Extract(const Napi::Value &value) {
if (value.IsNull()) {
return nullptr;
}
return Unwrap(value.As<Napi::Object>())->getLLVMPrimitive();
}

Expand Down
7 changes: 7 additions & 0 deletions src/IR/GlobalVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
void GlobalVariable::Init(Napi::Env env, Napi::Object &exports) {
Napi::HandleScope scope(env);
Napi::Function func = DefineClass(env, "GlobalVariable", {
InstanceMethod("getType", &GlobalVariable::getType)
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
Expand Down Expand Up @@ -70,4 +71,10 @@ GlobalVariable::GlobalVariable(const Napi::CallbackInfo &info) : ObjectWrap(info

llvm::GlobalVariable *GlobalVariable::getLLVMPrimitive() {
return globalVariable;
}

Napi::Value GlobalVariable::getType(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
llvm::Type *type = globalVariable->getType();
return Type::New(env, type);
}
7 changes: 6 additions & 1 deletion src/IR/IntegerType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
void IntegerType::Init(Napi::Env env, Napi::Object &exports) {
Napi::HandleScope scope(env);
Napi::Function func = DefineClass(env, "IntegerType", {
StaticMethod("get", &IntegerType::get)
StaticMethod("get", &IntegerType::get),
InstanceMethod("isStructTy", &IntegerType::isStructTy)
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
Expand Down Expand Up @@ -46,4 +47,8 @@ Napi::Value IntegerType::get(const Napi::CallbackInfo &info) {

llvm::IntegerType *IntegerType::getLLVMPrimitive() {
return integerType;
}

Napi::Value IntegerType::isStructTy(const Napi::CallbackInfo &info) {
return Napi::Boolean::New(info.Env(), integerType->isStructTy());
}
7 changes: 6 additions & 1 deletion src/IR/PointerType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ void PointerType::Init(Napi::Env env, Napi::Object &exports) {
StaticMethod("get", &PointerType::get),
StaticMethod("getUnqual", &PointerType::getUnqual),
InstanceMethod("getElementType", &PointerType::getElementType),
InstanceMethod("isPointerTy", &PointerType::isPointerTy)
InstanceMethod("isPointerTy", &PointerType::isPointerTy),
InstanceMethod("isStructTy", &PointerType::isStructTy)
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
Expand Down Expand Up @@ -70,3 +71,7 @@ Napi::Value PointerType::isPointerTy(const Napi::CallbackInfo &info) {
llvm::PointerType *PointerType::getLLVMPrimitive() {
return pointerType;
}

Napi::Value PointerType::isStructTy(const Napi::CallbackInfo &info) {
return Napi::Boolean::New(info.Env(), pointerType->isStructTy());
}
21 changes: 20 additions & 1 deletion src/IR/StructType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ void StructType::Init(Napi::Env env, Napi::Object &exports) {
Napi::HandleScope scope(env);
Napi::Function func = DefineClass(env, "StructType", {
StaticMethod("create", &StructType::create),
InstanceMethod("setBody", &StructType::setBody)
InstanceMethod("setBody", &StructType::setBody),
InstanceMethod("getPointerTo", &StructType::getPointerTo),
InstanceMethod("isStructTy", &StructType::isStructTy)
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
Expand Down Expand Up @@ -75,4 +77,21 @@ void StructType::setBody(const Napi::CallbackInfo &info) {
elementTypes[i] = Type::Extract(eleTypesArray.Get(i));
}
structType->setBody(elementTypes);
}

Napi::Value StructType::getPointerTo(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
if (info.Length() >= 1 && !info[0].IsNumber()) {
throw Napi::TypeError::New(env, ErrMsg::Class::StructType::getPointerTo);
}
unsigned addrSpace = 0;
if (info.Length() >= 1) {
addrSpace = info[0].As<Napi::Number>();
}
llvm::PointerType *pointerType = structType->getPointerTo(addrSpace);
return PointerType::New(env, pointerType);
}

Napi::Value StructType::isStructTy(const Napi::CallbackInfo &info) {
return Napi::Boolean::New(info.Env(), structType->isStructTy());
}

0 comments on commit 5af02bc

Please sign in to comment.