File: cxxwrap.py

package info (click to toggle)
lfortran 0.45.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 46,332 kB
  • sloc: cpp: 137,068; f90: 51,260; python: 6,444; ansic: 4,277; yacc: 2,285; fortran: 806; sh: 524; makefile: 30; javascript: 15
file content (66 lines) | stat: -rw-r--r-- 2,123 bytes parent folder | download
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Install Python wrappers to Clang:

    conda install -c conda-forge python-clang
"""
from clang.cindex import Index, CursorKind, TypeKind

import sys
sys.path.append("../..")
from lfortran.asr import asr
from lfortran.asr.asr_check import verify_asr
from lfortran.asr.asr_to_ast import asr_to_ast
from lfortran.ast.ast_to_src import ast_to_src
from lfortran.asr.builder import (make_translation_unit,
    translation_unit_make_module, scope_add_function, make_type_integer,
    make_type_real, type_eq, make_binop, scope_add_symbol, Scope,
    function_make_var, array_is_assumed_shape)

index = Index.create()
tu = index.parse("a.h")

def process(node):
    unit = make_translation_unit()
    module = translation_unit_make_module(unit, "cxx_wrapper")
    for child in node.get_children():
        if child.kind == CursorKind.FUNCTION_DECL:
            process_function(module.symtab, child)
        else:
            print(child.kind.name, child.spelling, child.location)
            #import IPython; IPython.embed()
    verify_asr(unit)
    return unit

def process_type(t):
    if t.kind == TypeKind.FUNCTIONPROTO:
        result = process_type(t.get_result())
        args = []
        for arg in t.argument_types():
            args.append(process_type(arg))
        return "result(%s), args(%s)" % (result, ", ".join(args))
    elif t.kind == TypeKind.POINTER:
        pointee = process_type(t.get_pointee())
        return "pointer(%s)" % pointee
    elif t.kind == TypeKind.INT:
        return "int"
    else:
        print(t.spelling)
        raise Exception("Type not supported")

def process_function(scope, node):
    types = process_type(node.type)
    arg_names = []
    for arg in node.get_arguments():
        arg_names.append(arg.spelling)
    type = make_type_integer()
    return_var = asr.VariableOld(name=node.spelling, type=type)
    f = scope_add_function(scope, node.spelling, args=arg_names,
            return_var=return_var)
    for a in f.args:
        a.intent = "in"
    f.bind = asr.Bind(lang="c", name=node.spelling)

u = process(tu.cursor)
a = asr_to_ast(u)
s = ast_to_src(a)
print(s)