Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions deprecated.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===- deprecated.c - Wrappers for deprecated LLVM C API --------*- C -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file wraps LLVMGetGlobalContext, which was deprecated in LLVM 22 but
// has no non-deprecated replacement. All other wrapped functions are
// handled by calling their context-aware counterparts from Go.
//
//===----------------------------------------------------------------------===//

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

#include "deprecated.h"

LLVMContextRef LLVMGetGlobalContext_wrap(void) {
return LLVMGetGlobalContext();
}

#pragma GCC diagnostic pop
30 changes: 30 additions & 0 deletions deprecated.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===- deprecated.h - Wrappers for deprecated LLVM C API --------*- C -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file wraps LLVMGetGlobalContext, which was deprecated in LLVM 22 but
// has no non-deprecated replacement. All other wrapped functions are
// handled by calling their context-aware counterparts from Go.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_BINDINGS_GO_LLVM_DEPRECATED_H
#define LLVM_BINDINGS_GO_LLVM_DEPRECATED_H

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor documentation nit: "All other wrapped functions are handled by calling their context-aware counterparts from Go" is a bit misleading — this file wraps exactly one function (LLVMGetGlobalContext_wrap) and contains no other wrappers. The intent is that other deprecated context-less C APIs are avoided by calling their ...InContext counterparts directly from Go, so no additional wrappers are needed here. Consider rewording, e.g.: "The other deprecated context-less C APIs are avoided by calling their context-aware counterparts directly from Go, so no additional wrappers are needed." (Same text in deprecated.c.)


#include "llvm-c/Core.h"

#ifdef __cplusplus
extern "C" {
#endif

LLVMContextRef LLVMGetGlobalContext_wrap(void);

#ifdef __cplusplus
}
#endif

#endif // LLVM_BINDINGS_GO_LLVM_DEPRECATED_H
37 changes: 8 additions & 29 deletions ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package llvm
#include "llvm-c/Core.h"
#include "llvm-c/Comdat.h"
#include "IRBindings.h"
#include "deprecated.h"
#include <stdlib.h>
*/
import "C"
Expand Down Expand Up @@ -368,7 +369,7 @@ const (
//-------------------------------------------------------------------------

func NewContext() Context { return Context{C.LLVMContextCreate()} }
func GlobalContext() Context { return Context{C.LLVMGetGlobalContext()} }
func GlobalContext() Context { return Context{C.LLVMGetGlobalContext_wrap()} }
func (c Context) Dispose() { C.LLVMContextDispose(c.C) }

func (c Context) MDKindID(name string) (id int) {
Expand All @@ -379,10 +380,7 @@ func (c Context) MDKindID(name string) (id int) {
}

func MDKindID(name string) (id int) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
id = int(C.LLVMGetMDKindID(cname, C.unsigned(len(name))))
return
return GlobalContext().MDKindID(name)
}

func LookupIntrinsicID(name string) (id int) {
Expand Down Expand Up @@ -646,14 +644,7 @@ func (c Context) StructType(elementTypes []Type, packed bool) (t Type) {
}

func StructType(elementTypes []Type, packed bool) (t Type) {
var pt *C.LLVMTypeRef
var ptlen C.unsigned
if len(elementTypes) > 0 {
pt = llvmTypeRefPtr(&elementTypes[0])
ptlen = C.unsigned(len(elementTypes))
}
t.C = C.LLVMStructType(pt, ptlen, boolToLLVMBool(packed))
return
return GlobalContext().StructType(elementTypes, packed)
}

func (c Context) StructCreateNamed(name string) (t Type) {
Expand Down Expand Up @@ -942,21 +933,15 @@ func ConstNamedStruct(t Type, constVals []Value) (v Value) {
return
}
func ConstString(str string, addnull bool) (v Value) {
cstr := C.CString(str)
defer C.free(unsafe.Pointer(cstr))
v.C = C.LLVMConstString(cstr,
C.unsigned(len(str)), boolToLLVMBool(!addnull))
return
return GlobalContext().ConstString(str, addnull)
}
func ConstArray(t Type, constVals []Value) (v Value) {
ptr, nvals := llvmValueRefs(constVals)
v.C = C.LLVMConstArray(t.C, ptr, nvals)
return
}
func ConstStruct(constVals []Value, packed bool) (v Value) {
ptr, nvals := llvmValueRefs(constVals)
v.C = C.LLVMConstStruct(ptr, nvals, boolToLLVMBool(packed))
return
return GlobalContext().ConstStruct(constVals, packed)
}
func ConstVector(scalarConstVals []Value, packed bool) (v Value) {
ptr, nvals := llvmValueRefs(scalarConstVals)
Expand Down Expand Up @@ -1282,16 +1267,10 @@ func (c Context) InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
return
}
func AddBasicBlock(f Value, name string) (bb BasicBlock) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
bb.C = C.LLVMAppendBasicBlock(f.C, cname)
return
return GlobalContext().AddBasicBlock(f, name)
}
func InsertBasicBlock(ref BasicBlock, name string) (bb BasicBlock) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
bb.C = C.LLVMInsertBasicBlock(ref.C, cname)
return
return GlobalContext().InsertBasicBlock(ref, name)
}
func AppendExistingBasicBlock(f Value, bb BasicBlock) {
C.LLVMAppendExistingBasicBlock(f.C, bb.C)
Expand Down
6 changes: 5 additions & 1 deletion target.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package llvm
#include "llvm-c/Core.h"
#include "llvm-c/Target.h"
#include "llvm-c/TargetMachine.h"
#include "deprecated.h"
#include "TargetBindings.h"
#include <stdlib.h>
*/
Expand Down Expand Up @@ -155,7 +156,10 @@ func (td TargetData) PointerSize() int { return int(C.LLVMPointerSize(td.C)) }

// Returns the integer type that is the same size as a pointer on a target.
// See the method llvm::TargetData::getIntPtrType.
func (td TargetData) IntPtrType() (t Type) { t.C = C.LLVMIntPtrType(td.C); return }
func (td TargetData) IntPtrType() (t Type) {
t.C = C.LLVMIntPtrTypeInContext(C.LLVMGetGlobalContext_wrap(), td.C)
return
}

// Computes the size of a type in bytes for a target.
// See the method llvm::TargetData::getTypeSizeInBits.
Expand Down
Loading