1//===- bitreader.go - Bindings for bitreader ------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines bindings for the bitreader component.
11//
12//===----------------------------------------------------------------------===//
13
14package llvm
15
16/*
17#include "llvm-c/BitReader.h"
18#include <stdlib.h>
19*/
20import "C"
21
22import (
23	"errors"
24	"unsafe"
25)
26
27// ParseBitcodeFile parses the LLVM IR (bitcode) in the file with the
28// specified name, and returns a new LLVM module.
29func ParseBitcodeFile(name string) (Module, error) {
30	var buf C.LLVMMemoryBufferRef
31	var errmsg *C.char
32	var cfilename *C.char = C.CString(name)
33	defer C.free(unsafe.Pointer(cfilename))
34	result := C.LLVMCreateMemoryBufferWithContentsOfFile(cfilename, &buf, &errmsg)
35	if result != 0 {
36		err := errors.New(C.GoString(errmsg))
37		C.free(unsafe.Pointer(errmsg))
38		return Module{}, err
39	}
40	defer C.LLVMDisposeMemoryBuffer(buf)
41
42	var m Module
43	if C.LLVMParseBitcode(buf, &m.C, &errmsg) == 0 {
44		return m, nil
45	}
46
47	err := errors.New(C.GoString(errmsg))
48	C.free(unsafe.Pointer(errmsg))
49	return Module{}, err
50}
51