aboutsummaryrefslogtreecommitdiffstats
path: root/lib/dialyzer/test/opaque_SUITE_data/src/big_external_type.erl
blob: d286a378ed7bf3936b4dae77b9161df12f894232 (plain) (blame)
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2001-2015. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%

%%% A copy of small_SUITE_data/src/big_external_type.erl, where
%%% abstract_expr() is opaque. The transformation of forms to types is
%%% now much faster than it used to be, for this module.

-module(big_external_type).

-export([parse_form/1,parse_exprs/1,parse_term/1]).
-export([normalise/1,tokens/1,tokens/2]).
-export([inop_prec/1,preop_prec/1,func_prec/0,max_prec/0]).

-export_type([abstract_clause/0, abstract_expr/0, abstract_form/0,
              error_info/0]).

%% Start of Abstract Format

-type line() :: erl_anno:line().

-export_type([af_record_index/0, af_record_field/1, af_record_name/0,
              af_field_name/0, af_function_decl/0]).

-export_type([af_module/0, af_export/0, af_import/0, af_fa_list/0,
              af_compile/0, af_file/0, af_record_decl/0,
              af_field_decl/0, af_wild_attribute/0,
              af_record_update/1, af_catch/0, af_local_call/0,
              af_remote_call/0, af_args/0, af_local_function/0,
              af_remote_function/0, af_list_comprehension/0,
              af_binary_comprehension/0, af_template/0,
              af_qualifier_seq/0, af_qualifier/0, af_generator/0,
              af_filter/0, af_block/0, af_if/0, af_case/0, af_try/0,
              af_clause_seq/0, af_catch_clause_seq/0, af_receive/0,
              af_local_fun/0, af_remote_fun/0, af_fun/0, af_query/0,
              af_query_access/0, af_clause/0,
              af_catch_clause/0, af_catch_pattern/0, af_catch_class/0,
              af_body/0, af_guard_seq/0, af_guard/0, af_guard_test/0,
              af_record_access/1, af_guard_call/0,
              af_remote_guard_call/0, af_pattern/0, af_literal/0,
              af_atom/0, af_lit_atom/1, af_integer/0, af_float/0,
              af_string/0, af_match/1, af_variable/0,
              af_anon_variable/0, af_tuple/1, af_nil/0, af_cons/1,
              af_bin/1, af_binelement/1, af_binelement_size/0,
              af_binary_op/1, af_binop/0, af_unary_op/1, af_unop/0]).

-type abstract_form() :: ?MODULE:af_module()
                       | ?MODULE:af_export()
                       | ?MODULE:af_import()
                       | ?MODULE:af_compile()
                       | ?MODULE:af_file()
                       | ?MODULE:af_record_decl()
                       | ?MODULE:af_wild_attribute()
                       | ?MODULE:af_function_decl().

-type af_module() :: {attribute, line(), module, module()}.

-type af_export() :: {attribute, line(), export, ?MODULE:af_fa_list()}.

-type af_import() :: {attribute, line(), import, ?MODULE:af_fa_list()}.

-type af_fa_list() :: [{function(), arity()}].

-type af_compile() :: {attribute, line(), compile, any()}.

-type af_file() :: {attribute, line(), file, {string(), line()}}.

-type af_record_decl() ::
        {attribute, line(), record, ?MODULE:af_record_name(), [?MODULE:af_field_decl()]}.

-type af_field_decl() :: {record_field, line(), ?MODULE:af_atom()}
                       | {record_field, line(), ?MODULE:af_atom(), ?MODULE:abstract_expr()}.

%% Types and specs, among other things...
-type af_wild_attribute() :: {attribute, line(), ?MODULE:af_atom(), any()}.

-type af_function_decl() ::
        {function, line(), function(), arity(), ?MODULE:af_clause_seq()}.

-opaque abstract_expr() :: ?MODULE:af_literal()
                       | ?MODULE:af_match(?MODULE:abstract_expr())
                       | ?MODULE:af_variable()
                       | ?MODULE:af_tuple(?MODULE:abstract_expr())
                       | ?MODULE:af_nil()
                       | ?MODULE:af_cons(?MODULE:abstract_expr())
                       | ?MODULE:af_bin(?MODULE:abstract_expr())
                       | ?MODULE:af_binary_op(?MODULE:abstract_expr())
                       | ?MODULE:af_unary_op(?MODULE:abstract_expr())
                       | ?MODULE:af_record_access(?MODULE:abstract_expr())
                       | ?MODULE:af_record_update(?MODULE:abstract_expr())
                       | ?MODULE:af_record_index()
                       | ?MODULE:af_record_field(?MODULE:abstract_expr())
                       | ?MODULE:af_catch()
                       | ?MODULE:af_local_call()
                       | ?MODULE:af_remote_call()
                       | ?MODULE:af_list_comprehension()
                       | ?MODULE:af_binary_comprehension()
                       | ?MODULE:af_block()
                       | ?MODULE:af_if()
                       | ?MODULE:af_case()
                       | ?MODULE:af_try()
                       | ?MODULE:af_receive()
                       | ?MODULE:af_local_fun()
                       | ?MODULE:af_remote_fun()
                       | ?MODULE:af_fun()
                       | ?MODULE:af_query()
                       | ?MODULE:af_query_access().

-type af_record_update(T) :: {record,
                              line(),
                              ?MODULE:abstract_expr(),
                              ?MODULE:af_record_name(),
                              [?MODULE:af_record_field(T)]}.

-type af_catch() :: {'catch', line(), ?MODULE:abstract_expr()}.

-type af_local_call() :: {call, line(), ?MODULE:af_local_function(), ?MODULE:af_args()}.

-type af_remote_call() :: {call, line(), ?MODULE:af_remote_function(), ?MODULE:af_args()}.

-type af_args() :: [?MODULE:abstract_expr()].

-type af_local_function() :: ?MODULE:abstract_expr().

-type af_remote_function() ::
        {remote, line(), ?MODULE:abstract_expr(), ?MODULE:abstract_expr()}.

-type af_list_comprehension() ::
        {lc, line(), ?MODULE:af_template(), ?MODULE:af_qualifier_seq()}.

-type af_binary_comprehension() ::
        {bc, line(), ?MODULE:af_template(), ?MODULE:af_qualifier_seq()}.

-type af_template() :: ?MODULE:abstract_expr().

-type af_qualifier_seq() :: [?MODULE:af_qualifier()].

-type af_qualifier() :: ?MODULE:af_generator() | ?MODULE:af_filter().

-type af_generator() :: {generate, line(), ?MODULE:af_pattern(), ?MODULE:abstract_expr()}
                      | {b_generate, line(), ?MODULE:af_pattern(), ?MODULE:abstract_expr()}.

-type af_filter() :: ?MODULE:abstract_expr().

-type af_block() :: {block, line(), ?MODULE:af_body()}.

-type af_if() :: {'if', line(), ?MODULE:af_clause_seq()}.

-type af_case() :: {'case', line(), ?MODULE:abstract_expr(), ?MODULE:af_clause_seq()}.

-type af_try() :: {'try',
                   line(),
                   ?MODULE:af_body(),
                   ?MODULE:af_clause_seq(),
                   ?MODULE:af_catch_clause_seq(),
                   ?MODULE:af_body()}.

-type af_clause_seq() :: [?MODULE:af_clause(), ...].

-type af_catch_clause_seq() :: [?MODULE:af_clause(), ...].

-type af_receive() ::
        {'receive', line(), ?MODULE:af_clause_seq()}
      | {'receive', line(), ?MODULE:af_clause_seq(), ?MODULE:abstract_expr(), ?MODULE:af_body()}.

-type af_local_fun() :: {'fun', line(), {function, function(), arity()}}.

-type af_remote_fun() ::
        {'fun', line(), {function, module(), function(), arity()}}
      | {'fun', line(), {function, ?MODULE:af_atom(), ?MODULE:af_atom(), ?MODULE:af_integer()}}.

-type af_fun() :: {'fun', line(), {clauses, ?MODULE:af_clause_seq()}}.

-type af_query() :: {'query', line(), ?MODULE:af_list_comprehension()}.

-type af_query_access() ::
        {record_field, line(), ?MODULE:abstract_expr(), ?MODULE:af_field_name()}.

-type abstract_clause() :: ?MODULE:af_clause() | ?MODULE:af_catch_clause().

-type af_clause() ::
        {clause, line(), [?MODULE:af_pattern()], ?MODULE:af_guard_seq(), ?MODULE:af_body()}.

-type af_catch_clause() ::
        {clause, line(), [?MODULE:af_catch_pattern()], ?MODULE:af_guard_seq(), ?MODULE:af_body()}.

-type af_catch_pattern() ::
        {?MODULE:af_catch_class(), ?MODULE:af_pattern(), ?MODULE:af_anon_variable()}.

-type af_catch_class() ::
        ?MODULE:af_variable()
      | ?MODULE:af_lit_atom(throw) | ?MODULE:af_lit_atom(error) | ?MODULE:af_lit_atom(exit).

-type af_body() :: [?MODULE:abstract_expr(), ...].

-type af_guard_seq() :: [?MODULE:af_guard()].

-type af_guard() :: [?MODULE:af_guard_test(), ...].

-type af_guard_test() :: ?MODULE:af_literal()
                       | ?MODULE:af_variable()
                       | ?MODULE:af_tuple(?MODULE:af_guard_test())
                       | ?MODULE:af_nil()
                       | ?MODULE:af_cons(?MODULE:af_guard_test())
                       | ?MODULE:af_bin(?MODULE:af_guard_test())
                       | ?MODULE:af_binary_op(?MODULE:af_guard_test())
                       | ?MODULE:af_unary_op(?MODULE:af_guard_test())
                       | ?MODULE:af_record_access(?MODULE:af_guard_test())
                       | ?MODULE:af_record_index()
                       | ?MODULE:af_record_field(?MODULE:af_guard_test())
                       | ?MODULE:af_guard_call()
                       | ?MODULE:af_remote_guard_call().

-type af_record_access(T) ::
        {record, line(), ?MODULE:af_record_name(), [?MODULE:af_record_field(T)]}.

-type af_guard_call() :: {call, line(), function(), [?MODULE:af_guard_test()]}.

-type af_remote_guard_call() ::
        {call, line(), atom(), ?MODULE:af_lit_atom(erlang), [?MODULE:af_guard_test()]}.

-type af_pattern() :: ?MODULE:af_literal()
                    | ?MODULE:af_match(?MODULE:af_pattern())
                    | ?MODULE:af_variable()
                    | ?MODULE:af_anon_variable()
                    | ?MODULE:af_tuple(?MODULE:af_pattern())
                    | ?MODULE:af_nil()
                    | ?MODULE:af_cons(?MODULE:af_pattern())
                    | ?MODULE:af_bin(?MODULE:af_pattern())
                    | ?MODULE:af_binary_op(?MODULE:af_pattern())
                    | ?MODULE:af_unary_op(?MODULE:af_pattern())
                    | ?MODULE:af_record_index()
                    | ?MODULE:af_record_field(?MODULE:af_pattern()).

-type af_literal() :: ?MODULE:af_atom() | ?MODULE:af_integer() | ?MODULE:af_float() | ?MODULE:af_string().

-type af_atom() :: ?MODULE:af_lit_atom(atom()).

-type af_lit_atom(A) :: {atom, line(), A}.

-type af_integer() :: {integer, line(), non_neg_integer()}.

-type af_float() :: {float, line(), float()}.

-type af_string() :: {string, line(), [byte()]}.

-type af_match(T) :: {match, line(), T, T}.

-type af_variable() :: {var, line(), atom()}.

-type af_anon_variable() :: {var, line(), '_'}.

-type af_tuple(T) :: {tuple, line(), [T]}.

-type af_nil() :: {nil, line()}.

-type af_cons(T) :: {cons, line, T, T}.

-type af_bin(T) :: {bin, line(), [?MODULE:af_binelement(T)]}.

-type af_binelement(T) :: {bin_element,
                           line(),
                           T,
                           ?MODULE:af_binelement_size(),
                           type_specifier_list()}.

-type af_binelement_size() :: default | ?MODULE:abstract_expr().

-type af_binary_op(T) :: {op, line(), T, ?MODULE:af_binop(), T}.

-type af_binop() :: '/' | '*' | 'div' | 'rem' | 'band' | 'and' | '+' | '-'
                  | 'bor' | 'bxor' | 'bsl' | 'bsr' | 'or' | 'xor' | '++'
                  | '--' | '==' | '/=' | '=<' | '<'  | '>=' | '>' | '=:='
                  | '=/='.

-type af_unary_op(T) :: {op, line(), ?MODULE:af_unop(), T}.

-type af_unop() :: '+' | '*' | 'bnot' | 'not'.

%% See also lib/stdlib/{src/erl_bits.erl,include/erl_bits.hrl}.
-type type_specifier_list() :: default | [type_specifier(), ...].

-type type_specifier() :: af_type()
                        | af_signedness()
                        | af_endianness()
                        | af_unit().

-type af_type() :: integer
                 | float
                 | binary
                 | bytes
                 | bitstring
                 | bits
                 | utf8
                 | utf16
                 | utf32.

-type af_signedness() :: signed | unsigned.

-type af_endianness() :: big | little | native.

-type af_unit() :: {unit, 1..256}.

-type af_record_index() ::
        {record_index, line(), af_record_name(), af_field_name()}.

-type af_record_field(T) :: {record_field, line(), af_field_name(), T}.

-type af_record_name() :: atom().

-type af_field_name() :: atom().

%% End of Abstract Format

-type error_description() :: term().
-type error_info() :: {erl_anno:line(), module(), error_description()}.
-type token() :: {Tag :: atom(), Line :: erl_anno:line()}.

%% mkop(Op, Arg) -> {op,Line,Op,Arg}.
%% mkop(Left, Op, Right) -> {op,Line,Op,Left,Right}.

-define(mkop2(L, OpPos, R),
        begin
            {Op,Pos} = OpPos,
            {op,Pos,Op,L,R}
        end).

-define(mkop1(OpPos, A),
        begin
            {Op,Pos} = OpPos,
            {op,Pos,Op,A}
        end).

%% keep track of line info in tokens
-define(line(Tup), element(2, Tup)).

%% Entry points compatible to old erl_parse.
%% These really suck and are only here until Calle gets multiple
%% entry points working.

-spec parse_form(Tokens) -> {ok, AbsForm} | {error, ErrorInfo} when
      Tokens :: [token()],
      AbsForm :: abstract_form(),
      ErrorInfo :: error_info().
parse_form([{'-',L1},{atom,L2,spec}|Tokens]) ->
    parse([{'-',L1},{'spec',L2}|Tokens]);
parse_form([{'-',L1},{atom,L2,callback}|Tokens]) ->
    parse([{'-',L1},{'callback',L2}|Tokens]);
parse_form(Tokens) ->
    parse(Tokens).

-spec parse_exprs(Tokens) -> {ok, ExprList} | {error, ErrorInfo} when
      Tokens :: [token()],
      ExprList :: [abstract_expr()],
      ErrorInfo :: error_info().
parse_exprs(Tokens) ->
    case parse([{atom,0,f},{'(',0},{')',0},{'->',0}|Tokens]) of
	{ok,{function,_Lf,f,0,[{clause,_Lc,[],[],Exprs}]}} ->
	    {ok,Exprs};
	{error,_} = Err -> Err
    end.

-spec parse_term(Tokens) -> {ok, Term} | {error, ErrorInfo} when
      Tokens :: [token()],
      Term :: term(),
      ErrorInfo :: error_info().
parse_term(Tokens) ->
    case parse([{atom,0,f},{'(',0},{')',0},{'->',0}|Tokens]) of
	{ok,{function,_Lf,f,0,[{clause,_Lc,[],[],[Expr]}]}} ->
	    try normalise(Expr) of
		Term -> {ok,Term}
	    catch
		_:_R -> {error,{?line(Expr),?MODULE,"bad term"}}
	    end;
	{ok,{function,_Lf,f,0,[{clause,_Lc,[],[],[_E1,E2|_Es]}]}} ->
	    {error,{?line(E2),?MODULE,"bad term"}};
	{error,_} = Err -> Err
    end.

%%  Convert between the abstract form of a term and a term.

-spec normalise(AbsTerm) -> Data when
      AbsTerm :: abstract_expr(),
      Data :: term().
normalise({char,_,C}) -> C;
normalise({integer,_,I}) -> I;
normalise({float,_,F}) -> F;
normalise({atom,_,A}) -> A;
normalise({string,_,S}) -> S;
normalise({nil,_}) -> [];
normalise({bin,_,Fs}) ->
    {value, B, _} =
	eval_bits:expr_grp(Fs, [],
			   fun(E, _) ->
				   {value, normalise(E), []}
			   end, [], true),
    B;
normalise({cons,_,Head,Tail}) ->
    [normalise(Head)|normalise(Tail)];
normalise({tuple,_,Args}) ->
    list_to_tuple(normalise_list(Args));
%% Atom dot-notation, as in 'foo.bar.baz'
%% Special case for unary +/-.
normalise({op,_,'+',{char,_,I}}) -> I;
normalise({op,_,'+',{integer,_,I}}) -> I;
normalise({op,_,'+',{float,_,F}}) -> F;
normalise({op,_,'-',{char,_,I}}) -> -I;		%Weird, but compatible!
normalise({op,_,'-',{integer,_,I}}) -> -I;
normalise({op,_,'-',{float,_,F}}) -> -F;
normalise(X) -> erlang:error({badarg, X}).

normalise_list([H|T]) ->
    [normalise(H)|normalise_list(T)];
normalise_list([]) ->
    [].

%%  Generate a list of tokens representing the abstract term.

-spec tokens(AbsTerm) -> Tokens when
      AbsTerm :: abstract_expr(),
      Tokens :: [token()].
tokens(Abs) ->
    tokens(Abs, []).

-spec tokens(AbsTerm, MoreTokens) -> Tokens when
      AbsTerm :: abstract_expr(),
      MoreTokens :: [token()],
      Tokens :: [token()].
tokens({char,L,C}, More) -> [{char,L,C}|More];
tokens({integer,L,N}, More) -> [{integer,L,N}|More];
tokens({float,L,F}, More) -> [{float,L,F}|More];
tokens({atom,L,A}, More) -> [{atom,L,A}|More];
tokens({var,L,V}, More) -> [{var,L,V}|More];
tokens({string,L,S}, More) -> [{string,L,S}|More];
tokens({nil,L}, More) -> [{'[',L},{']',L}|More];
tokens({cons,L,Head,Tail}, More) ->
    [{'[',L}|tokens(Head, tokens_tail(Tail, More))];
tokens({tuple,L,[]}, More) ->
    [{'{',L},{'}',L}|More];
tokens({tuple,L,[E|Es]}, More) ->
    [{'{',L}|tokens(E, tokens_tuple(Es, ?line(E), More))].

tokens_tail({cons,L,Head,Tail}, More) ->
    [{',',L}|tokens(Head, tokens_tail(Tail, More))];
tokens_tail({nil,L}, More) ->
    [{']',L}|More];
tokens_tail(Other, More) ->
    L = ?line(Other),
    [{'|',L}|tokens(Other, [{']',L}|More])].

tokens_tuple([E|Es], Line, More) ->
    [{',',Line}|tokens(E, tokens_tuple(Es, ?line(E), More))];
tokens_tuple([], Line, More) ->
    [{'}',Line}|More].

%% Give the relative precedences of operators.

inop_prec('=') -> {150,100,100};
inop_prec('!') -> {150,100,100};
inop_prec('orelse') -> {160,150,150};
inop_prec('andalso') -> {200,160,160};
inop_prec('==') -> {300,200,300};
inop_prec('/=') -> {300,200,300};
inop_prec('=<') -> {300,200,300};
inop_prec('<') -> {300,200,300};
inop_prec('>=') -> {300,200,300};
inop_prec('>') -> {300,200,300};
inop_prec('=:=') -> {300,200,300};
inop_prec('=/=') -> {300,200,300};
inop_prec('++') -> {400,300,300};
inop_prec('--') -> {400,300,300};
inop_prec('+') -> {400,400,500};
inop_prec('-') -> {400,400,500};
inop_prec('bor') -> {400,400,500};
inop_prec('bxor') -> {400,400,500};
inop_prec('bsl') -> {400,400,500};
inop_prec('bsr') -> {400,400,500};
inop_prec('or') -> {400,400,500};
inop_prec('xor') -> {400,400,500};
inop_prec('*') -> {500,500,600};
inop_prec('/') -> {500,500,600};
inop_prec('div') -> {500,500,600};
inop_prec('rem') -> {500,500,600};
inop_prec('band') -> {500,500,600};
inop_prec('and') -> {500,500,600};
inop_prec('#') -> {800,700,800};
inop_prec(':') -> {900,800,900};
inop_prec('.') -> {900,900,1000}.

-type pre_op() :: 'catch' | '+' | '-' | 'bnot' | 'not' | '#'.

-spec preop_prec(pre_op()) -> {0 | 600 | 700, 100 | 700 | 800}.

preop_prec('catch') -> {0,100};
preop_prec('+') -> {600,700};
preop_prec('-') -> {600,700};
preop_prec('bnot') -> {600,700};
preop_prec('not') -> {600,700};
preop_prec('#') -> {700,800}.

-spec func_prec() -> {800,700}.

func_prec() -> {800,700}.

-spec max_prec() -> 1000.

max_prec() -> 1000.

parse(T) ->
    bar:foo(T).