aboutsummaryrefslogtreecommitdiffstats
path: root/lib/asn1/src/asn1ct_func.erl
diff options
context:
space:
mode:
authorBjörn Gustavsson <[email protected]>2013-01-25 12:45:26 +0100
committerBjörn Gustavsson <[email protected]>2013-01-25 12:45:26 +0100
commit9afbb879f0397a650a7c403911a8cc30daa6dbbe (patch)
tree8e0d77907994cd3e6397b12349abd35a63375235 /lib/asn1/src/asn1ct_func.erl
parent61f8a41388d95f6b8c0e2e5a06de586cecf184c6 (diff)
parentb06cbaf8cf12a9b6dcbdc6eab873a6212206ef58 (diff)
downloadotp-9afbb879f0397a650a7c403911a8cc30daa6dbbe.tar.gz
otp-9afbb879f0397a650a7c403911a8cc30daa6dbbe.tar.bz2
otp-9afbb879f0397a650a7c403911a8cc30daa6dbbe.zip
Merge branch 'bjorn/asn1/further-cleanup/OTP-10588'
* bjorn/asn1/further-cleanup/OTP-10588: (28 commits) Don't export encode_disp/2 and decode_disp/2 in generated modules Remove vestiges of support for the {TypeName,Value} notation Simplify the functions for decoding lengths per,uper: Optimize decoding of the remaining data types per,uper: Optimize decoding of the remaining string types Share all code for dec_gen_prim/3 between per/uper back-ends per,uper: Optimize decoding of the string data types testPrimStrings: Test some constraints By default, encode BIT STRING to bitstrings Teach encode functions to accept a bitstring term for a BIT STRING Fix EXTERNAL 1990/1994 conversion information loss uper: Look up some SizeConstraints at compile-time Enumeration decoding: Don't emit a default clause if it cannot match Slightly optimize per encoding of large INTEGERs with constraints BER run-time: Refactor decoding of string data types Refactor decoding of BIT STRINGs Optimize encoding of ENUMERATED in per and uper Remove the unused run-time modules eldap: Remove calls to undocumented asn1rt* functions BER: Correct bug in 'undec_rest' ...
Diffstat (limited to 'lib/asn1/src/asn1ct_func.erl')
-rw-r--r--lib/asn1/src/asn1ct_func.erl105
1 files changed, 105 insertions, 0 deletions
diff --git a/lib/asn1/src/asn1ct_func.erl b/lib/asn1/src/asn1ct_func.erl
new file mode 100644
index 0000000000..2d221ca1b9
--- /dev/null
+++ b/lib/asn1/src/asn1ct_func.erl
@@ -0,0 +1,105 @@
+%%
+%% %CopyrightBegin%
+%%
+%% Copyright Ericsson AB 2012. 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(asn1ct_func).
+-export([start_link/0,need/1,call/3,generate/1]).
+-export([init/1,handle_call/3,handle_cast/2,terminate/2]).
+
+start_link() ->
+ {ok,Pid} = gen_server:start_link(?MODULE, [], []),
+ put(?MODULE, Pid),
+ ok.
+
+call(M, F, Args) ->
+ MFA = {M,F,length(Args)},
+ need(MFA),
+ asn1ct_gen:emit([F,"(",call_args(Args, ""),")"]).
+
+need(MFA) ->
+ asn1ct_rtt:assert_defined(MFA),
+ cast({need,MFA}).
+
+generate(Fd) ->
+ req({generate,Fd}),
+ erase(?MODULE),
+ ok.
+
+req(Req) ->
+ gen_server:call(get(?MODULE), Req, infinity).
+
+cast(Req) ->
+ gen_server:cast(get(?MODULE), Req).
+
+%%% Internal functions.
+
+-record(st, {used}).
+
+init([]) ->
+ St = #st{used=gb_sets:empty()},
+ {ok,St}.
+
+handle_cast({need,MFA}, #st{used=Used0}=St) ->
+ case gb_sets:is_member(MFA, Used0) of
+ false ->
+ Used = pull_in_deps(gb_sets:singleton(MFA), Used0),
+ {noreply,St#st{used=Used}};
+ true ->
+ {noreply,St}
+ end.
+
+handle_call({generate,Fd}, _From, #st{used=Used}=St) ->
+ generate(Fd, Used),
+ {stop,normal,ok,St}.
+
+terminate(_, _) ->
+ ok.
+
+call_args([A|As], Sep) ->
+ [Sep,A|call_args(As, ", ")];
+call_args([], _) -> [].
+
+generate(Fd, Used0) ->
+ Used1 = gb_sets:to_list(Used0),
+ Used = sofs:set(Used1, [mfa]),
+ Code = sofs:relation(asn1ct_rtt:code(), [{mfa,code}]),
+ Funcs0 = sofs:image(Code, Used),
+ Funcs = sofs:to_external(Funcs0),
+ io:put_chars(Fd, Funcs).
+
+pull_in_deps(Ws0, Used0) ->
+ case gb_sets:is_empty(Ws0) of
+ true ->
+ Used0;
+ false ->
+ {MFA,Ws1} = gb_sets:take_smallest(Ws0),
+ Used = gb_sets:add(MFA, Used0),
+ Needs = asn1ct_rtt:dependencies(MFA),
+ Ws = update_worklist(Needs, Used, Ws1),
+ pull_in_deps(Ws, Used)
+ end.
+
+update_worklist([H|T], Used, Ws) ->
+ case gb_sets:is_member(H, Used) of
+ false ->
+ update_worklist(T, Used, gb_sets:add(H, Ws));
+ true ->
+ update_worklist(T, Used, Ws)
+ end;
+update_worklist([], _, Ws) -> Ws.