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
|
%%
%% %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_util).
-include("icforms.hrl").
-include("ic.hrl").
-include("ic_debug.hrl").
%%-----------------------------------------------------------------
%% External exports
%%-----------------------------------------------------------------
-export([mk_align/1, mk_list/1, join/2, chain/2, mk_name/2,
mk_OE_name/2, mk_oe_name/2, mk_var/1]).
-export([to_atom/1, to_colon/1, to_list/1, to_undersc/1, to_dot/1,
to_dot/2]).
-export([to_uppercase/1, adjustScopeToJava/2, eval_java/3, eval_c/3]).
%%-----------------------------------------------------------------
%% Internal exports
%%-----------------------------------------------------------------
-export([]).
%%-----------------------------------------------------------------
%% External functions
%%-----------------------------------------------------------------
%% 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([]) -> [].
%% Produce a list of items separated by S.
join([E1, E2| Es], S) ->
[E1, S| join([E2| Es], S)];
join([E], _) ->
[E];
join([], _) ->
[].
%% Produce a list of items, each terminated by T.
chain([E| Es], T) ->
[E, T| chain(Es, T)];
chain([], _) ->
[].
%% Shall convert a string to a Erlang variable name (Capitalise)
mk_var( [N | Str] ) when N >= $a, N =< $z ->
[ N+$A-$a | Str ];
mk_var( [N | Str] ) when N >= $A, N =< $Z -> [N | Str].
%% Shall produce a "public" name for name. When we introduce new
%% identifiers in the mapping that must not collide with those from
%% the IDL spec.
%%
%% NOTE: Change name of IFR ID in system exceptions in corba.hrl when
%% prefix is changed here.
%%
mk_name(_Gen, Name) -> lists:flatten(["OE_" | Name]).
mk_OE_name(_Gen, Name) -> lists:flatten(["OE_" | Name]).
mk_oe_name(_Gen, Name) -> lists:flatten(["oe_" | Name]).
mk_align(String) ->
io_lib:format("OE_ALIGN(~s)",[String]).
to_atom(A) when is_atom(A) -> A;
to_atom(L) when is_list(L) -> list_to_atom(L).
to_list(A) when is_list(A) -> A;
to_list(L) when is_atom(L) -> atom_to_list(L);
to_list(X) when is_integer(X) -> integer_to_list(X).
%% Produce a colon (or under score) separated string repr of the name
%% X
%%
to_colon(X) when element(1, X) == scoped_id ->
to_colon2(ic_symtab:scoped_id_strip(X));
to_colon(L) -> to_colon2(L).
to_colon2([X]) -> X;
to_colon2([X | Xs]) -> to_colon2(Xs) ++ "::" ++ X;
to_colon2([]) -> "".
to_undersc(X) when element(1, X) == scoped_id ->
to_undersc2(ic_symtab:scoped_id_strip(X));
to_undersc(L) -> to_undersc2(L).
to_undersc2([X]) -> X;
to_undersc2([X | Xs]) -> to_undersc2(Xs) ++ "_" ++ X;
to_undersc2([]) -> "".
%% Z is a single name
to_uppercase(Z) ->
lists:map(fun(X) when X>=$a, X=<$z -> X-$a+$A;
(X) -> X end, Z).
%%
to_dot(X) when element(1, X) == scoped_id ->
to_dotLoop(ic_symtab:scoped_id_strip(X));
to_dot(L) -> to_dotLoop(L).
to_dotLoop([X]) -> ic_forms:get_java_id(X);
to_dotLoop([X | Xs]) -> to_dotLoop(Xs) ++ "." ++ ic_forms:get_java_id(X);
to_dotLoop([]) -> "".
%%
to_dot(G,X) when element(1, X) == scoped_id ->
S = ic_genobj:pragmatab(G),
ScopedId = ic_symtab:scoped_id_strip(X),
case isConstScopedId(S, ScopedId) of %% Costants are left as is
true ->
to_dotLoop(ScopedId) ++ addDotValue(S, ScopedId);
false ->
to_dotLoop(S,ScopedId)
end;
to_dot(G,ScopedId) ->
S = ic_genobj:pragmatab(G),
case isConstScopedId(S, ScopedId) of %% Costants are left as is
true ->
to_dotLoop(ScopedId) ++ addDotValue(S, ScopedId);
false ->
to_dotLoop(S,ScopedId)
end.
addDotValue(S, [_C | Ss]) ->
case isInterfaceScopedId(S, Ss) of
true ->
"";
false ->
".value"
end.
to_dotLoop(S,[X]) ->
case isInterfaceScopedId(S, [X]) of
true ->
ic_forms:get_java_id(X) ++ "Package";
false ->
ic_forms:get_java_id(X)
end;
to_dotLoop(S,[X | Xs]) ->
case isInterfaceScopedId(S, [X | Xs]) of
true ->
to_dotLoop(S,Xs) ++ "." ++ ic_forms:get_java_id(X) ++ "Package";
false ->
to_dotLoop(S,Xs) ++ "." ++ ic_forms:get_java_id(X)
end;
to_dotLoop(_S,[]) -> "".
isInterfaceScopedId(_S,[]) ->
false;
isInterfaceScopedId(S,[X|Xs]) ->
case ets:match(S,{file_data_local,'_','_',interface,Xs,X,'_','_','_'}) of
[] ->
case ets:match(S,{file_data_included,'_','_',interface,Xs,X,'_','_','_'}) of
[] ->
false;
_ ->
true
end;
_ ->
true
end.
isConstScopedId(_S,[]) ->
false;
isConstScopedId(S,[X|Xs]) ->
case ets:match(S,{file_data_local,'_','_',const,Xs,X,'_','_','_'}) of
[] ->
case ets:match(S,{file_data_included,'_','_',const,Xs,X,'_','_','_'}) of
[] ->
false;
_ ->
true
end;
_ ->
true
end.
%%
adjustScopeToJava(G,X) when element(1, X) == scoped_id ->
S = ic_genobj:pragmatab(G),
ScopedId = ic_symtab:scoped_id_strip(X),
case isConstScopedId(S, ScopedId) of %% Costants are left as is
true ->
ic_forms:get_java_id(ScopedId);
false ->
adjustScopeToJavaLoop(S,ScopedId)
end;
adjustScopeToJava(G,ScopedId) ->
S = ic_genobj:pragmatab(G),
case isConstScopedId(S, ScopedId) of %% Costants are left as is
true ->
ic_forms:get_java_id(ScopedId);
false ->
adjustScopeToJavaLoop(S,ScopedId)
end.
adjustScopeToJavaLoop(_S,[]) ->
[];
adjustScopeToJavaLoop(S,[X | Xs]) ->
case isInterfaceScopedId(S, [X | Xs]) of
true ->
[ic_forms:get_java_id(X) ++ "Package" | adjustScopeToJavaLoop(S,Xs)];
false ->
[ic_forms:get_java_id(X) | adjustScopeToJavaLoop(S,Xs)]
end.
%%
%% Expression evaluator for java
%%
%% Well, this is not an evaluator, it just
%% prints the hole operation, sorry.
%%
eval_java(G,N,Arg) when is_record(Arg, scoped_id) ->
{FSN, _, _, _} =
ic_symtab:get_full_scoped_name(G, N, Arg),
ic_util:to_dot(G,FSN);
eval_java(_G,_N,Arg) when is_tuple(Arg) andalso element(1,Arg) == '<integer_literal>' ->
element(3,Arg);
eval_java(_G,_N,Arg) when is_tuple(Arg) andalso element(1,Arg) == '<character_literal>' ->
element(3,Arg);
eval_java(_G,_N,Arg) when is_tuple(Arg) andalso element(1,Arg) == '<wcharacter_literal>' ->
element(3,Arg);
eval_java(_G,_N,Arg) when is_tuple(Arg) andalso element(1,Arg) == '<boolean_literal>' ->
element(3,Arg);
eval_java(_G,_N,Arg) when is_tuple(Arg) andalso element(1,Arg) == '<floating_pt_literal>' ->
element(3,Arg);
eval_java(_G,_N,Arg) when is_tuple(Arg) andalso element(1,Arg) == '<string_literal>' ->
element(3,Arg);
eval_java(_G,_N,Arg) when is_tuple(Arg) andalso element(1,Arg) == '<wstring_literal>' ->
element(3,Arg);
eval_java(G,N,{Op,Arg1,Arg2}) ->
"(" ++ eval_java(G,N,Arg1) ++
ic_forms:get_java_id(Op) ++
eval_java(G,N,Arg2) ++ ")".
%%
%% Expression evaluator for c
%%
%% Well, this is not an evaluator, it just
%% prints the hole operation, sorry.
%%
eval_c(G,N,Arg) when is_record(Arg, scoped_id) ->
{FSN, _, _, _} =
ic_symtab:get_full_scoped_name(G, N, Arg),
ic_util:to_undersc(FSN);
eval_c(_G,_N,Arg) when is_tuple(Arg) andalso element(1,Arg) == '<integer_literal>' ->
element(3,Arg);
eval_c(_G,_N,Arg) when is_tuple(Arg) andalso element(1,Arg) == '<character_literal>' ->
element(3,Arg);
eval_c(_G,_N,Arg) when is_tuple(Arg) andalso element(1,Arg) == '<wcharacter_literal>' ->
element(3,Arg);
eval_c(_G,_N,Arg) when is_tuple(Arg) andalso element(1,Arg) == '<boolean_literal>' ->
element(3,Arg);
eval_c(_G,_N,Arg) when is_tuple(Arg) andalso element(1,Arg) == '<floating_pt_literal>' ->
element(3,Arg);
eval_c(_G,_N,Arg) when is_tuple(Arg) andalso element(1,Arg) == '<string_literal>' ->
element(3,Arg);
eval_c(_G,_N,Arg) when is_tuple(Arg) andalso element(1,Arg) == '<wstring_literal>' ->
element(3,Arg);
eval_c(G,N,{Op,Arg1,Arg2}) ->
"(" ++ eval_c(G,N,Arg1) ++
atom_to_list(Op) ++
eval_c(G,N,Arg2) ++ ")".
%%-----------------------------------------------------------------
%% Internal functions
%%-----------------------------------------------------------------
|