-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathresolve_deps.jl
176 lines (154 loc) · 6.53 KB
/
resolve_deps.jl
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
resolve_dependency!(dag::ExprDAG, node::ExprNode)
Build the DAG, resolve dependency.
"""
function resolve_dependency! end
resolve_dependency!(dag::ExprDAG, node::ExprNode) = dag
function resolve_dependency!(dag::ExprDAG, node::ExprNode{FunctionProto})
cursor = node.cursor
args = get_function_args(cursor)
types = CLType[getArgType(getCursorType(cursor), i - 1) for i in 1:length(args)]
push!(types, getCursorResultType(cursor))
for ty in types
# for arrays and pointers, meaningful types need to be recursively retrieved
jlty = tojulia(ty)
leaf_ty = get_jl_leaf_type(jlty)
# there is no dependency that needs to be recorded for those julia basic types
is_jl_basic(leaf_ty) && continue
hasref = has_elaborated_reference(ty)
if hasref && haskey(dag.tags, leaf_ty.sym)
push!(node.adj, dag.tags[leaf_ty.sym])
elseif !hasref && haskey(dag.ids, leaf_ty.sym)
push!(node.adj, dag.ids[leaf_ty.sym])
elseif haskey(dag.ids_extra, leaf_ty.sym)
# extra identifiers are printed at the top of the file, so there is no need to
# add them as dependencies.
# pass
elseif !hasref && haskey(dag.tags, leaf_ty.sym)
# FIXME: in some cases, this system-header symbol is in dag.tags
push!(node.adj, dag.tags[leaf_ty.sym])
elseif hasref && is_jl_pointer(jlty)
# if opaque pointers are being used as function argument like
# void func(struct foo *x);
# we can simply translate this argument type to `Ptr{Cvoid}` because it is only
# visible to local.
# pass
else
tycu = getTypeDeclaration(ty)
file, line, col = get_file_line_column(cursor)
cspell = spelling(cursor)
tspell = spelling(ty)
error("There is no definition for $cspell's parameter type: [`$tspell`] at $file:$line:$col")
end
end
return dag
end
function resolve_dependency!(dag::ExprDAG, node::ExprNode{FunctionNoProto})
cursor = node.cursor
@assert isempty(get_function_args(cursor))
ty = getCursorResultType(cursor)
jlty = tojulia(ty)
leaf_ty = get_jl_leaf_type(jlty)
is_jl_basic(leaf_ty) && return dag
hasref = has_elaborated_reference(ty)
if hasref && haskey(dag.tags, leaf_ty.sym)
push!(node.adj, dag.tags[leaf_ty.sym])
elseif !hasref && haskey(dag.ids, leaf_ty.sym)
push!(node.adj, dag.ids[leaf_ty.sym])
elseif haskey(dag.ids_extra, leaf_ty.sym)
# pass
elseif !hasref && haskey(dag.tags, leaf_ty.sym)
# FIXME: in some cases, this system-header symbol is in dag.tags
push!(node.adj, dag.tags[leaf_ty.sym])
else
tycu = getTypeDeclaration(ty)
file, line, col = get_file_line_column(cursor)
cspell = spelling(cursor)
tspell = spelling(ty)
error("There is no definition for $cspell's return type: [`$tspell`] at $file:$line:$col")
end
return dag
end
function resolve_dependency!(dag::ExprDAG, node::ExprNode{TypedefElaborated})
cursor = node.cursor
ty = getTypedefDeclUnderlyingType(cursor)
jlty = tojulia(ty)
leaf_ty = get_jl_leaf_type(jlty)
is_jl_basic(leaf_ty) && return dag
if haskey(dag.tags, leaf_ty.sym)
push!(node.adj, dag.tags[leaf_ty.sym])
else
tycu = getTypeDeclaration(ty)
file, line, col = get_file_line_column(cursor)
cspell = spelling(cursor)
tspell = spelling(tycu)
error("There is no definition for $cspell's underlying type: [`$tspell`] at $file:$line:$col")
end
return dag
end
function resolve_dependency!(dag::ExprDAG, node::ExprNode{TypedefDefault})
cursor = node.cursor
ty = getTypedefDeclUnderlyingType(cursor)
jlty = tojulia(ty)
leaf_ty = get_jl_leaf_type(jlty)
is_jl_basic(leaf_ty) && return dag
# do nothing for unknowns since we just skip them in the downstream passes
is_jl_unknown(leaf_ty) && return dag
if haskey(dag.ids, leaf_ty.sym)
push!(node.adj, dag.ids[leaf_ty.sym])
elseif haskey(dag.ids_extra, leaf_ty.sym)
# pass
else
tycu = getTypeDeclaration(ty)
file, line, col = get_file_line_column(cursor)
cspell = spelling(cursor)
tspell = spelling(tycu)
error("There is no definition for $cspell's underlying type: [`$tspell`] at $file:$line:$col")
end
return dag
end
function resolve_dependency!(dag::ExprDAG, node::ExprNode{TypedefToAnonymous})
s = node.type.sym
@assert haskey(dag.tags, s)
push!(node.adj, dag.tags[s])
return dag
end
# ignore non-opaque forward decls, that's our purpose
resolve_dependency!(dag::ExprDAG, node::ExprNode{StructForwardDecl}) = dag
function resolve_dependency!(dag::ExprDAG, node::ExprNode{<:AbstractStructNodeType})
cursor = node.cursor
for c in fields(getCursorType(cursor))
ty = getCursorType(c)
jlty = tojulia(ty)
leaf_ty = get_jl_leaf_type(jlty)
is_jl_basic(leaf_ty) && continue
hasref = has_elaborated_reference(ty)
if hasref && haskey(dag.tags, leaf_ty.sym)
push!(node.adj, dag.tags[leaf_ty.sym])
elseif !hasref && haskey(dag.ids, leaf_ty.sym)
push!(node.adj, dag.ids[leaf_ty.sym])
elseif haskey(dag.ids_extra, leaf_ty.sym)
# pass
elseif !hasref && haskey(dag.tags, leaf_ty.sym)
# FIXME: in some cases, this system-header symbol is in dag.tags
push!(node.adj, dag.tags[leaf_ty.sym])
elseif occursin("anonymous", spelling(ty))
# it could be a nested anonymous tag-type.
# pass
else
file, line, col = get_file_line_column(cursor)
cspell = spelling(cursor)
fspell = spelling(c)
tspell = spelling(getCursorType(c))
error("There is no definition for $cspell's field: $fspell's type: [`$tspell`] at $file:$line:$col")
end
end
return dag
end
# enums are just "named integers", no dependency needs to be resolved.
resolve_dependency!(dag::ExprDAG, node::ExprNode{<:AbstractEnumNodeType}) = dag
# to workaround some nasty field alignment problems, we simply generate Julia structs with
# a single `data::NTuple` field for union nodes, so no need to add any dependencies here.
resolve_dependency!(dag::ExprDAG, node::ExprNode{<:AbstractUnionNodeType}) = dag
# for now, do nothing for macros, just assume they are written in a correct order
resolve_dependency!(dag::ExprDAG, node::ExprNode{<:AbstractMacroNodeType}) = dag