From 1e2ee0118a9a77d9f0d56cb8669bdf204ef0ab05 Mon Sep 17 00:00:00 2001
From: Ary Borenszweig <aborenszweig@manas.com.ar>
Date: Thu, 6 Nov 2014 19:16:27 -0300
Subject: [PATCH] Use enums in LibLLVM

---
 libs/llvm/builder.cr                    |  2 +-
 libs/llvm/function.cr                   |  4 +-
 libs/llvm/lib_llvm.cr                   | 49 ++++++++-----------------
 libs/llvm/module.cr                     |  2 +-
 libs/llvm/target.cr                     |  8 ++--
 libs/llvm/target_machine.cr             |  2 +-
 libs/llvm/type.cr                       |  2 +-
 libs/llvm/value_methods.cr              |  8 ++--
 src/compiler/crystal/codegen/codegen.cr |  4 +-
 9 files changed, 32 insertions(+), 49 deletions(-)

diff --git a/libs/llvm/builder.cr b/libs/llvm/builder.cr
index 75f01b9429..cc781a0062 100644
--- a/libs/llvm/builder.cr
+++ b/libs/llvm/builder.cr
@@ -94,7 +94,7 @@ struct LLVM::Builder
 
   {% for name in %w(icmp fcmp) %}
     def {{name.id}}(op, lhs, rhs, name = "")
-      Value.new LibLLVM.build_{{name.id}}(self, op.value, lhs, rhs, name)
+      Value.new LibLLVM.build_{{name.id}}(self, op, lhs, rhs, name)
     end
   {% end %}
 
diff --git a/libs/llvm/function.cr b/libs/llvm/function.cr
index 39d5764eff..68b4f378b7 100644
--- a/libs/llvm/function.cr
+++ b/libs/llvm/function.cr
@@ -16,11 +16,11 @@ struct LLVM::Function
   end
 
   def add_attribute(attribute)
-    LibLLVM.add_function_attr self, attribute.value
+    LibLLVM.add_function_attr self, attribute
   end
 
   def attributes
-    Attribute.new LibLLVM.get_function_attr(self)
+    LibLLVM.get_function_attr(self)
   end
 
   def function_type
diff --git a/libs/llvm/lib_llvm.cr b/libs/llvm/lib_llvm.cr
index 2aa68ba9f0..1ec1664c2c 100644
--- a/libs/llvm/lib_llvm.cr
+++ b/libs/llvm/lib_llvm.cr
@@ -20,23 +20,17 @@ lib LibLLVM
 
   struct JITCompilerOptions
     opt_level : UInt32
-    code_model : Int32 # TODO LLVM::CodeModel after 0.5.2
+    code_model : LLVM::CodeModel
     no_frame_pointer_elim : Int32
     enable_fast_isel : Int32
   end
 
-  # TODO: use enums after 0.5.2 (right now enums outside libs are not valid as C types, but after 0.5.2 they will)
-
-  # fun add_attribute = LLVMAddAttribute(arg : ValueRef, attr : LLVM::Attribute)
-  fun add_attribute = LLVMAddAttribute(arg : ValueRef, attr : Int32)
-  # fun add_instr_attribute = LLVMAddInstrAttribute(instr : ValueRef, index : UInt32, attr : LLVM::Attribute)
-  fun add_instr_attribute = LLVMAddInstrAttribute(instr : ValueRef, index : UInt32, attr : Int32)
+  fun add_attribute = LLVMAddAttribute(arg : ValueRef, attr : LLVM::Attribute)
+  fun add_instr_attribute = LLVMAddInstrAttribute(instr : ValueRef, index : UInt32, attr : LLVM::Attribute)
   fun add_clause = LLVMAddClause(lpad : ValueRef, clause_val : ValueRef)
   fun add_function = LLVMAddFunction(module : ModuleRef, name : UInt8*, type : TypeRef) : ValueRef
-  # fun add_function_attr = LLVMAddFunctionAttr(fn : ValueRef, pa : LLVM::Attribute)
-  fun add_function_attr = LLVMAddFunctionAttr(fn : ValueRef, pa : Int32)
-  # fun get_function_attr = LLVMGetFunctionAttr(fn : ValueRef) : LLVM::Attribute
-  fun get_function_attr = LLVMGetFunctionAttr(fn : ValueRef) : Int32
+  fun add_function_attr = LLVMAddFunctionAttr(fn : ValueRef, pa : LLVM::Attribute)
+  fun get_function_attr = LLVMGetFunctionAttr(fn : ValueRef) : LLVM::Attribute
   fun add_global = LLVMAddGlobal(module : ModuleRef, type : TypeRef, name : UInt8*) : ValueRef
   fun add_incoming = LLVMAddIncoming(phi_node : ValueRef, incoming_values : ValueRef*, incoming_blocks : BasicBlockRef *, count : Int32)
   fun add_named_metadata_operand = LLVMAddNamedMetadataOperand(mod : ModuleRef, name : UInt8*, val : ValueRef)
@@ -54,8 +48,7 @@ lib LibLLVM
   fun build_exact_sdiv = LLVMBuildExactSDiv(builder : BuilderRef, lhs : ValueRef, rhs : ValueRef, name : UInt8*) : ValueRef
   fun build_extract_value = LLVMBuildExtractValue(builder : BuilderRef, agg_val : ValueRef, index : UInt32, name : UInt8*) : ValueRef
   fun build_fadd = LLVMBuildFAdd(builder : BuilderRef, lhs : ValueRef, rhs : ValueRef, name : UInt8*) : ValueRef
-  # fun build_fcmp = LLVMBuildFCmp(builder : BuilderRef, op : LLVM::RealPredicate, lhs : ValueRef, rhs : ValueRef, name : UInt8*) : ValueRef
-  fun build_fcmp = LLVMBuildFCmp(builder : BuilderRef, op : Int32, lhs : ValueRef, rhs : ValueRef, name : UInt8*) : ValueRef
+  fun build_fcmp = LLVMBuildFCmp(builder : BuilderRef, op : LLVM::RealPredicate, lhs : ValueRef, rhs : ValueRef, name : UInt8*) : ValueRef
   fun build_fdiv = LLVMBuildFDiv(builder : BuilderRef, lhs : ValueRef, rhs : ValueRef, name : UInt8*) : ValueRef
   fun build_fmul = LLVMBuildFMul(builder : BuilderRef, lhs : ValueRef, rhs : ValueRef, name : UInt8*) : ValueRef
   fun build_fp2si = LLVMBuildFPToSI(builder : BuilderRef, val : ValueRef, dest_ty : TypeRef, name : UInt8*) : ValueRef
@@ -66,8 +59,7 @@ lib LibLLVM
   fun build_gep = LLVMBuildGEP(builder : BuilderRef, pointer : ValueRef, indices : ValueRef*, num_indices : UInt32, name : UInt8*) : ValueRef
   fun build_inbounds_gep = LLVMBuildInBoundsGEP(builder : BuilderRef, pointer : ValueRef, indices : ValueRef*, num_indices : UInt32, name : UInt8*) : ValueRef
   fun build_global_string_ptr = LLVMBuildGlobalStringPtr(builder : BuilderRef, str : UInt8*, name : UInt8*) : ValueRef
-  # fun build_icmp = LLVMBuildICmp(builder : BuilderRef, op : LLVM::IntPredicate, lhs : ValueRef, rhs : ValueRef, name : UInt8*) : ValueRef
-  fun build_icmp = LLVMBuildICmp(builder : BuilderRef, op : Int32, lhs : ValueRef, rhs : ValueRef, name : UInt8*) : ValueRef
+  fun build_icmp = LLVMBuildICmp(builder : BuilderRef, op : LLVM::IntPredicate, lhs : ValueRef, rhs : ValueRef, name : UInt8*) : ValueRef
   fun build_int2ptr = LLVMBuildIntToPtr(builder : BuilderRef, val : ValueRef, dest_ty : TypeRef, name : UInt8*) : ValueRef
   fun build_invoke = LLVMBuildInvoke(builder : BuilderRef, fn : ValueRef, args : ValueRef*, num_args : UInt32, then : BasicBlockRef, catch : BasicBlockRef, name : UInt8*) : ValueRef
   fun build_landing_pad = LLVMBuildLandingPad(builder : BuilderRef, ty : TypeRef, pers_fn : ValueRef, num_clauses : UInt32, name : UInt8*) : ValueRef
@@ -112,22 +104,19 @@ lib LibLLVM
   fun create_generic_value_of_pointer = LLVMCreateGenericValueOfPointer(p : Void*) : GenericValueRef
   fun create_jit_compiler_for_module = LLVMCreateJITCompilerForModule (jit : ExecutionEngineRef*, m : ModuleRef, opt_level : Int32, error : UInt8**) : Int32
   fun create_mc_jit_compiler_for_module = LLVMCreateMCJITCompilerForModule(jit : ExecutionEngineRef*, m : ModuleRef, options : JITCompilerOptions*, options_length : UInt32, error : UInt8**) : Int32
-  # fun create_target_machine = LLVMCreateTargetMachine(target : TargetRef, triple : UInt8*, cpu : UInt8*, features : UInt8*, level : LLVM::CodeGenOptLevel, reloc : LLVM::RelocMode, code_model : LLVM::CodeModel) : TargetMachineRef
-  fun create_target_machine = LLVMCreateTargetMachine(target : TargetRef, triple : UInt8*, cpu : UInt8*, features : UInt8*, level : Int32, reloc : Int32, code_model : Int32) : TargetMachineRef
+  fun create_target_machine = LLVMCreateTargetMachine(target : TargetRef, triple : UInt8*, cpu : UInt8*, features : UInt8*, level : LLVM::CodeGenOptLevel, reloc : LLVM::RelocMode, code_model : LLVM::CodeModel) : TargetMachineRef
   fun delete_basic_block = LLVMDeleteBasicBlock(block : BasicBlockRef)
   fun dispose_message = LLVMDisposeMessage(msg : UInt8*)
   fun double_type = LLVMDoubleType : TypeRef
   fun dump_module = LLVMDumpModule(module : ModuleRef)
   fun dump_value = LLVMDumpValue(val : ValueRef)
-  # fun target_machine_emit_to_file = LLVMTargetMachineEmitToFile(t : TargetMachineRef, m : ModuleRef, filename : UInt8*, codegen : LLVM::CodeGenFileType, error_msg : UInt8**) : Int32
-  fun target_machine_emit_to_file = LLVMTargetMachineEmitToFile(t : TargetMachineRef, m : ModuleRef, filename : UInt8*, codegen : Int32, error_msg : UInt8**) : Int32
+  fun target_machine_emit_to_file = LLVMTargetMachineEmitToFile(t : TargetMachineRef, m : ModuleRef, filename : UInt8*, codegen : LLVM::CodeGenFileType, error_msg : UInt8**) : Int32
   fun float_type = LLVMFloatType : TypeRef
   fun function_type = LLVMFunctionType(return_type : TypeRef, param_types : TypeRef*, param_count : UInt32, is_var_arg : Int32) : TypeRef
   fun generic_value_to_float = LLVMGenericValueToFloat(type : TypeRef, value : GenericValueRef) : Float64
   fun generic_value_to_int = LLVMGenericValueToInt(value : GenericValueRef, signed : Int32) : UInt64
   fun generic_value_to_pointer = LLVMGenericValueToPointer(value : GenericValueRef) : Void*
-  # fun get_attribute = LLVMGetAttribute(arg : ValueRef) : LLVM::Attribute
-  fun get_attribute = LLVMGetAttribute(arg : ValueRef) : Int32
+  fun get_attribute = LLVMGetAttribute(arg : ValueRef) : LLVM::Attribute
   fun get_element_type = LLVMGetElementType(ty : TypeRef) : TypeRef
   fun get_first_instruction = LLVMGetFirstInstruction(block : BasicBlockRef) : ValueRef
   fun get_first_target = LLVMGetFirstTarget : TargetRef
@@ -144,8 +133,7 @@ lib LibLLVM
   fun get_target_description = LLVMGetTargetDescription(target : TargetRef) : UInt8*
   fun get_target_machine_data = LLVMGetTargetMachineData(t : TargetMachineRef) : TargetDataRef
   fun get_target_machine_triple = LLVMGetTargetMachineTriple(t : TargetMachineRef) : UInt8*
-  # fun get_type_kind = LLVMGetTypeKind(ty : TypeRef) : LLVM::Type::Kind
-  fun get_type_kind = LLVMGetTypeKind(ty : TypeRef) : Int32
+  fun get_type_kind = LLVMGetTypeKind(ty : TypeRef) : LLVM::Type::Kind
   fun get_undef = LLVMGetUndef(ty : TypeRef) : ValueRef
   fun get_value_name = LLVMGetValueName(value : ValueRef) : UInt8*
   fun initialize_x86_asm_printer = LLVMInitializeX86AsmPrinter
@@ -188,10 +176,8 @@ lib LibLLVM
   fun is_global_constant = LLVMIsGlobalConstant(global : ValueRef) : Int32
   fun set_initializer = LLVMSetInitializer(global_var : ValueRef, constant_val : ValueRef)
   fun get_initializer = LLVMGetInitializer(global_var : ValueRef) : ValueRef
-  # fun set_linkage = LLVMSetLinkage(global : ValueRef, linkage : LLVM::Linkage)
-  fun set_linkage = LLVMSetLinkage(global : ValueRef, linkage : Int32)
-  # fun get_linkage = LLVMGetLinkage(global : ValueRef) : LLVM::Linkage
-  fun get_linkage = LLVMGetLinkage(global : ValueRef) : Int32
+  fun set_linkage = LLVMSetLinkage(global : ValueRef, linkage : LLVM::Linkage)
+  fun get_linkage = LLVMGetLinkage(global : ValueRef) : LLVM::Linkage
   fun set_metadata = LLVMSetMetadata(value : ValueRef, kind_id : UInt32, node : ValueRef)
   fun set_target = LLVMSetTarget(mod : ModuleRef, triple : UInt8*)
   fun set_thread_local = LLVMSetThreadLocal(global_var : ValueRef, is_thread_local : Int32)
@@ -205,8 +191,7 @@ lib LibLLVM
   fun type_of = LLVMTypeOf(val : ValueRef) : TypeRef
   fun void_type = LLVMVoidType : TypeRef
   fun write_bitcode_to_file = LLVMWriteBitcodeToFile(module : ModuleRef, path : UInt8*) : Int32
-  # fun verify_module = LLVMVerifyModule(module : ModuleRef, action : LLVM::VerifierFailureAction, outmessage : UInt8**) : Int32
-  fun verify_module = LLVMVerifyModule(module : ModuleRef, action : Int32, outmessage : UInt8**) : Int32
+  fun verify_module = LLVMVerifyModule(module : ModuleRef, action : LLVM::VerifierFailureAction, outmessage : UInt8**) : Int32
   fun link_in_jit = LLVMLinkInJIT
   fun link_in_mc_jit = LLVMLinkInMCJIT
   fun start_multithreaded = LLVMStartMultithreaded : Int32
@@ -234,8 +219,6 @@ lib LibLLVM
   fun print_module_to_string = LLVMPrintModuleToString(mod : ModuleRef) : UInt8*
   fun print_type_to_string = LLVMPrintTypeToString(ty : TypeRef) : UInt8*
   fun print_value_to_string = LLVMPrintValueToString(v : ValueRef) : UInt8*
-  # fun get_function_call_convention = LLVMGetFunctionCallConv(fn : ValueRef) : LLVM::CallConvention
-  fun get_function_call_convention = LLVMGetFunctionCallConv(fn : ValueRef) : Int32
-  # fun set_function_call_convention = LLVMSetFunctionCallConv(fn : ValueRef, cc : LLVM::CallConvention)
-  fun set_function_call_convention = LLVMSetFunctionCallConv(fn : ValueRef, cc : Int32)
+  fun get_function_call_convention = LLVMGetFunctionCallConv(fn : ValueRef) : LLVM::CallConvention
+  fun set_function_call_convention = LLVMSetFunctionCallConv(fn : ValueRef, cc : LLVM::CallConvention)
 end
diff --git a/libs/llvm/module.cr b/libs/llvm/module.cr
index fe951f46ae..c6aea98314 100644
--- a/libs/llvm/module.cr
+++ b/libs/llvm/module.cr
@@ -30,7 +30,7 @@ struct LLVM::Module
   end
 
   def verify
-    if LibLLVM.verify_module(self, LLVM::VerifierFailureAction::ReturnStatusAction.value, out message) == 1
+    if LibLLVM.verify_module(self, LLVM::VerifierFailureAction::ReturnStatusAction, out message) == 1
       raise "Module validation failed: #{String.new(message)}"
     end
   end
diff --git a/libs/llvm/target.cr b/libs/llvm/target.cr
index 1441a5081c..a64c01a75d 100644
--- a/libs/llvm/target.cr
+++ b/libs/llvm/target.cr
@@ -23,10 +23,10 @@ struct LLVM::Target
   end
 
   def create_target_machine(triple, cpu = "", features = "",
-    opt_level = LLVM::CodeGenOptLevel::Default,
-    reloc = LLVM::RelocMode::Default,
-    code_model = LLVM::CodeModel::Default)
-    target_machine = LibLLVM.create_target_machine(self, triple, cpu, features, opt_level.value, reloc.value, code_model.value)
+      opt_level = LLVM::CodeGenOptLevel::Default,
+      reloc = LLVM::RelocMode::Default,
+      code_model = LLVM::CodeModel::Default)
+    target_machine = LibLLVM.create_target_machine(self, triple, cpu, features, opt_level, reloc, code_model)
     target_machine ? TargetMachine.new(target_machine) : nil
   end
 
diff --git a/libs/llvm/target_machine.cr b/libs/llvm/target_machine.cr
index e4824f0038..eae58bb16d 100644
--- a/libs/llvm/target_machine.cr
+++ b/libs/llvm/target_machine.cr
@@ -23,7 +23,7 @@ struct LLVM::TargetMachine
   end
 
   private def emit_to_file(llvm_mod, filename, type)
-    status = LibLLVM.target_machine_emit_to_file(self, llvm_mod, filename, type.value, out error_msg)
+    status = LibLLVM.target_machine_emit_to_file(self, llvm_mod, filename, type, out error_msg)
     unless status == 0
       raise String.new(error_msg)
     end
diff --git a/libs/llvm/type.cr b/libs/llvm/type.cr
index 0d9511d418..22144f489f 100644
--- a/libs/llvm/type.cr
+++ b/libs/llvm/type.cr
@@ -57,7 +57,7 @@ struct LLVM::Type
   end
 
   def kind
-    Kind.new LibLLVM.get_type_kind(self)
+    LibLLVM.get_type_kind(self)
   end
 
   def void?
diff --git a/libs/llvm/value_methods.cr b/libs/llvm/value_methods.cr
index 9c1d5135c2..d2f223f5de 100644
--- a/libs/llvm/value_methods.cr
+++ b/libs/llvm/value_methods.cr
@@ -13,11 +13,11 @@ module LLVM::ValueMethods
   end
 
   def add_attribute(attribute)
-    LibLLVM.add_attribute self, attribute.value
+    LibLLVM.add_attribute self, attribute
   end
 
   def attributes
-    Attribute.new LibLLVM.get_attribute(self)
+    LibLLVM.get_attribute(self)
   end
 
   def constant?
@@ -37,11 +37,11 @@ module LLVM::ValueMethods
   end
 
   def linkage=(linkage)
-    LibLLVM.set_linkage(self, linkage.value)
+    LibLLVM.set_linkage(self, linkage)
   end
 
   def linkage
-    LLVM::Linkage.new LibLLVM.get_linkage(self)
+    LibLLVM.get_linkage(self)
   end
 
   def global_constant=(global_constant)
diff --git a/src/compiler/crystal/codegen/codegen.cr b/src/compiler/crystal/codegen/codegen.cr
index 40894a54bd..3abed10700 100644
--- a/src/compiler/crystal/codegen/codegen.cr
+++ b/src/compiler/crystal/codegen/codegen.cr
@@ -1454,7 +1454,7 @@ module Crystal
         # If the argument is out the type might be a struct but we don't pass anything byval
         next if call_args[i]?.try &.is_a?(Out)
 
-        LibLLVM.add_instr_attribute(@last, (i + arg_offset).to_u32, LLVM::Attribute::ByVal.value)
+        LibLLVM.add_instr_attribute(@last, (i + arg_offset).to_u32, LLVM::Attribute::ByVal)
       end
     end
 
@@ -1464,7 +1464,7 @@ module Crystal
       arg_types = fun_type.try(&.arg_types) || target_def.try &.args.map &.type
       arg_types.try &.each_with_index do |arg_type, i|
         next unless arg_type.passed_by_value?
-        LibLLVM.add_instr_attribute(@last, (i + arg_offset).to_u32, LLVM::Attribute::ByVal.value)
+        LibLLVM.add_instr_attribute(@last, (i + arg_offset).to_u32, LLVM::Attribute::ByVal)
       end
     end
 
-- 
GitLab