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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1998-2009. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%
-module(ic_plainbe).
-export([do_gen/3]).
%%------------------------------------------------------------
%%
%% Internal stuff
%%
%%------------------------------------------------------------
-import(ic_util, [mk_var/1, mk_oe_name/2, to_atom/1, to_list/1]).
-import(ic_forms, [get_id/1, get_id2/1, get_body/1]).
-import(ic_codegen, [emit/3, nl/1]).
-import(lists, [foreach/2, map/2]).
-include("icforms.hrl").
-include("ic.hrl").
%%------------------------------------------------------------
%%
%% Generate the client side Erlang stubs.
%%
%% Each module is generated to a separate file.
%%
%% Export declarations for all interface functions must be
%% generated. Each function then needs to generate a function head and
%% a body. IDL parameters must be converted into Erlang parameters
%% (variables, capitalised) and a type signature list must be
%% generated (for later encode/decode).
%%
%%------------------------------------------------------------
do_gen(G, File, Form) ->
G2 = ic_file:filename_push(G, [], mk_oe_name(G,
ic_file:remove_ext(to_list(File))),
erlang),
gen_head(G2, [], Form),
exportDependency(G2),
gen(G2, [], Form),
genDependency(G2),
ic_file:filename_pop(G2, erlang),
ok.
gen(G, N, [X|Xs]) when is_record(X, preproc) ->
NewG = ic:handle_preproc(G, N, X#preproc.cat, X),
gen(NewG, N, Xs);
gen(G, N, [X|Xs]) when is_record(X, module) ->
CD = ic_code:codeDirective(G,X),
G2 = ic_file:filename_push(G, N, X, CD),
N2 = [get_id2(X) | N],
gen_head(G2, N2, X),
gen(G2, N2, get_body(X)),
G3 = ic_file:filename_pop(G2, CD),
gen(G3, N, Xs);
gen(G, N, [X|Xs]) when is_record(X, interface) ->
%% Add inheritence data to pragmatab
ic_pragma:add_inh_data(G,N,X),
G2 = ic_file:filename_push(G, N, X, erlang),
N2 = [get_id2(X) | N],
gen_head(G2, N2, X),
gen(G2, N2, get_body(X)),
foreach(fun({_Name, Body}) -> gen(G2, N2, Body) end,
X#interface.inherit_body),
G3 = ic_file:filename_pop(G2, erlang),
gen(G3, N, Xs);
gen(G, N, [X|Xs]) when is_record(X, const) ->
% N2 = [get_id2(X) | N],
emit_constant_func(G, X#const.id, X#const.val),
gen(G, N, Xs); %% N or N2?
gen(G, N, [X|Xs]) when is_record(X, op) ->
{Name, ArgNames, TypeList, OutArgs} = extract_info(G, N, X),
emit_func(G, N, X, Name, ArgNames, TypeList, OutArgs),
gen(G, N, Xs);
gen(G, N, [X|Xs]) when is_record(X, attr) ->
emit_attr(G, N, X, fun emit_func/7),
gen(G, N, Xs);
gen(G, N, [X|Xs]) when is_record(X, except) ->
icstruct:except_gen(G, N, X, erlang),
gen(G, N, Xs);
gen(G, N, [X|Xs]) ->
case may_contain_structs(X) of
true -> icstruct:struct_gen(G, N, X, erlang);
false -> ok
end,
gen(G, N, Xs);
gen(_G, _N, []) -> ok.
may_contain_structs(X) when is_record(X, typedef) -> true;
may_contain_structs(X) when is_record(X, struct) -> true;
may_contain_structs(X) when is_record(X, union) -> true;
may_contain_structs(_X) -> false.
%%------------------------------------------------------------
%%
%% Export stuff
%%
%% Gathering of all names that should be exported from a stub
%% file.
%%
gen_head_special(G, N, X) when is_record(X, interface) ->
Fd = ic_genobj:stubfiled(G),
foreach(fun({Name, Body}) ->
ic_codegen:comment(Fd, "Exports from ~p",
[ic_util:to_colon(Name)]),
ic_codegen:export(Fd, exp_top(G, N, Body, [])),
nl(Fd)
end, X#interface.inherit_body),
Fd;
gen_head_special(_G, _N, _X) -> ok.
%% Shall generate all export declarations
gen_head(G, N, X) ->
case ic_genobj:is_stubfile_open(G) of
true ->
F = ic_genobj:stubfiled(G),
ic_codegen:comment(F, "Interface functions"),
ic_codegen:export(F, exp_top(G, N, X, [])),
nl(F),
gen_head_special(G, N, X);
false -> ok
end.
exp_top(_G, _N, X, Acc) when element(1, X) == preproc ->
Acc;
exp_top(G, N, L, Acc) when is_list(L) ->
exp_list(G, N, L, Acc);
exp_top(G, N, M, Acc) when is_record(M, module) ->
exp_list(G, N, get_body(M), Acc);
exp_top(G, N, I, Acc) when is_record(I, interface) ->
exp_list(G, N, get_body(I), Acc);
exp_top(G, N, X, Acc) ->
exp3(G, N, X, Acc).
exp3(_G, _N, C, Acc) when is_record(C, const) ->
[{get_id(C#const.id), 0} | Acc];
exp3(_G, _N, Op, Acc) when is_record(Op, op) ->
FuncName = get_id(Op#op.id),
Arity = length(ic:filter_params([in, inout], Op#op.params)),
[{FuncName, Arity} | Acc];
exp3(_G, _N, A, Acc) when is_record(A, attr) ->
lists:foldr(fun(Id, Acc2) ->
{Get, Set} = mk_attr_func_names([], get_id(Id)),
case A#attr.readonly of
{readonly, _} -> [{Get, 1} | Acc2];
_ -> [{Get, 1}, {Set, 2} | Acc2]
end end, Acc, ic_forms:get_idlist(A));
exp3(_G, _N, _X, Acc) -> Acc.
exp_list(G, N, L, OrigAcc) ->
lists:foldr(fun(X, Acc) -> exp3(G, N, X, Acc) end, OrigAcc, L).
%%------------------------------------------------------------
%%
%% Emit stuff
%%
%% Low level generation primitives
%%
emit_func(G, _N, X, Name, ArgNames, _TypeList, OutArgs) ->
case ic_genobj:is_stubfile_open(G) of
false -> ok;
true ->
Fd = ic_genobj:stubfiled(G),
OpName = list_to_atom(Name),
ArgList = mk_list(ArgNames),
emit_op_comment(G, Fd, X, OpName, ArgNames, OutArgs),
emit(Fd, "~p(~s) ->\n", [OpName,ArgList]),
emit(Fd, " ~p:~p(~s).\n\n", [to_atom(ic_genobj:impl(G)), OpName, ArgList])
end.
emit_attr(G, N, X, F) ->
XX = #id_of{type=X},
{GetType, SetType} = mk_attr_func_types(N, X),
lists:foreach(fun(Id) ->
X2 = XX#id_of{id=Id},
{Get, Set} = mk_attr_func_names(N, get_id(Id)),
F(G, N, X2, Get, [], GetType, []),
case X#attr.readonly of
{readonly, _} -> ok;
_ ->
F(G, N, X2, Set, [ic_util:mk_name(G, "Value")],
SetType, [])
end end, ic_forms:get_idlist(X)).
emit_constant_func(G, Id, Val) ->
case ic_genobj:is_stubfile_open(G) of
false -> ok;
true ->
Fd = ic_genobj:stubfiled(G),
N = list_to_atom(get_id(Id)),
emit_const_comment(G, Fd, Id, N),
emit(Fd, "~p() -> ~p.\n\n", [N, Val])
end.
emit_const_comment(_G, F, _X, Name) ->
ic_codegen:mcomment_light(F,
[io_lib:format("Constant: ~p", [Name])]).
emit_op_comment(G, F, X, Name, InP, OutP) ->
ic_codegen:mcomment_light(F,
[io_lib:format("~s: ~p", [get_title(X), Name]),
"",
get_returns(G, X, InP, OutP) |
get_raises(X)]).
get_title(X) when is_record(X, attr) -> "Attribute Operation";
get_title(_X) -> "Operation".
get_raises(X) when is_record(X, op) ->
if X#op.raises == [] -> [];
true ->
[" Raises: " ++
mk_list(lists:map(fun(E) -> ic_util:to_colon(E) end, X#op.raises))]
end;
get_raises(_X) -> [].
get_returns(_G, _X, _InP, []) ->
" Returns: RetVal";
get_returns(G, _X, _InP, OutP) ->
" Returns: "++mk_list(["RetVal" | mk_erl_vars(G, OutP)]).
%%------------------------------------------------------------
%%
%% Utilities
%%
%% Convenient little go-get functions
%%
%%------------------------------------------------------------
%% The automaticly generated get and set operation names for an
%% attribute.
mk_attr_func_names(_Scope, Name) ->
{"_get_" ++ Name, "_set_" ++ Name}.
%% Returns TK of the Get and Set attribute functions.
mk_attr_func_types(_N, X) ->
TK = ic_forms:get_tk(X),
{{TK, [], []}, {tk_void, [TK], []}}.
%%------------------------------------------------------------
%%
%% Generation utilities and common stuff
%%
%% Convenient stuff for generation
%%
%%------------------------------------------------------------
%% Input is a list of parameters (in parse form) and output is a list
%% of capitalised variable names. mk_var is in icgen
mk_erl_vars(_G, Params) ->
map(fun(P) -> mk_var(get_id(P#param.id)) end, Params).
%% mk_list produces a nice comma separated string of variable names
mk_list([]) -> [];
mk_list([Arg | Args]) ->
Arg ++ mk_list2(Args).
mk_list2([Arg | Args]) ->
", " ++ Arg ++ mk_list2(Args);
mk_list2([]) -> [].
%%------------------------------------------------------------
%%
%% Parser utilities
%%
%% Called from the yecc parser. Expands the identifier list of an
%% attribute so that the attribute generator never has to handle
%% lists.
%%
%%------------------------------------------------------------
%% Export code produce for dependency function
exportDependency(G) ->
Fd = ic_genobj:stubfiled(G),
ic_codegen:export(Fd, [{oe_dependency, 0}]),
nl(Fd).
%% Code produce for dependency function
genDependency(G) ->
Fd = ic_genobj:stubfiled(G),
nl(Fd),nl(Fd),
ic_codegen:comment(Fd, "Idl file dependency list function"),
emit(Fd, "oe_dependency() ->\n", []),
emit(Fd, " ~p.\n\n", [ic_pragma:get_dependencies(G)]).
extract_info(G, _N, X) when is_record(X, op) ->
Name = get_id2(X),
InArgs = ic:filter_params([in,inout], X#op.params),
OutArgs = ic:filter_params([out,inout], X#op.params),
ArgNames = mk_erl_vars(G, InArgs),
TypeList = {ic_forms:get_tk(X),
map(fun(Y) -> ic_forms:get_tk(Y) end, InArgs),
map(fun(Y) -> ic_forms:get_tk(Y) end, OutArgs)
},
{Name, ArgNames, TypeList, OutArgs}.
|