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
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1999-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_constant_java).
-include("icforms.hrl").
-include("ic.hrl").
-include("ic_debug.hrl").
%%-----------------------------------------------------------------
%% External exports
%%-----------------------------------------------------------------
-export([gen/3]).
%%-----------------------------------------------------------------
%% Internal exports
%%-----------------------------------------------------------------
-export([]).
%%-----------------------------------------------------------------
%% External functions
%%-----------------------------------------------------------------
%%-----------------------------------------------------------------
%% Func: gen/3
%%-----------------------------------------------------------------
gen(G, N, X) when is_record(X, const) ->
ConstantName = ic_forms:get_java_id(X),
case inInterface(G, N) of
true ->
emit_constant(G, N, X, ConstantName);
false ->
emit_constant_interface(G, N, X, ConstantName)
end;
gen(_G, _N, _X) ->
ok.
%%-----------------------------------------------------------------
%% Internal functions
%%-----------------------------------------------------------------
%%-----------------------------------------------------------------
%% Func: emit_constant/4
%%-----------------------------------------------------------------
emit_constant(G, N, X, ConstantName) ->
Fd = ic_genobj:interfacefiled(G),
%%?PRINTDEBUG2("~p", [Fd]),
Type = ic_java_type:getType(G, N, ic_forms:get_type(X)),
ic_codegen:emit(Fd, " public static final ~s ~s = (~s) ~p;\n",
[Type, ConstantName, Type, X#const.val]),
ic_codegen:nl(Fd).
%%-----------------------------------------------------------------
%% Func: emit_constant_interface/4
%%-----------------------------------------------------------------
emit_constant_interface(G, N, X, ConstantName) ->
{Fd, _} = ic_file:open_java_file(G, N, ConstantName),
ic_codegen:emit(Fd, "final public class ~s {\n",[ConstantName]),
Type = ic_java_type:getType(G, N, ic_forms:get_type(X)),
ic_codegen:emit(Fd, " public static final ~s value = (~s) ~p;\n",
[Type, Type, X#const.val]),
ic_codegen:emit(Fd, "}\n", []),
file:close(Fd).
%%-----------------------------------------------------------------
%% Func: emit_constant_interface/4
%%-----------------------------------------------------------------
inInterface(_G, []) -> % Global constant
false;
inInterface(G, N) ->
[N1 |Ns] = N,
{_FullScopedName, T, _TK, _} =
ic_symtab:get_full_scoped_name(G, Ns, ic_symtab:scoped_id_new(N1)),
case T of
interface -> % Constant declare in an interface
true;
_ -> % Constant declared in a module
false
end.
|