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
|
%% -*- erlang-indent-level: 2 -*-
%%-----------------------------------------------------------------------
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2006-2010. 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%
%%
%%%-------------------------------------------------------------------
%%% File : dialyzer_utils.erl
%%% Author : Tobias Lindahl <[email protected]>
%%% Description :
%%%
%%% Created : 5 Dec 2006 by Tobias Lindahl <[email protected]>
%%%-------------------------------------------------------------------
-module(dialyzer_utils).
-export([
format_sig/1,
format_sig/2,
get_abstract_code_from_beam/1,
get_abstract_code_from_src/1,
get_abstract_code_from_src/2,
get_core_from_abstract_code/1,
get_core_from_abstract_code/2,
get_core_from_src/1,
get_core_from_src/2,
get_record_and_type_info/1,
get_spec_info/3,
merge_records/2,
pp_hook/0,
process_record_remote_types/1,
src_compiler_opts/0
]).
-include("dialyzer.hrl").
%%-define(DEBUG, true).
-ifdef(DEBUG).
print_types(RecDict) ->
Keys = dict:fetch_keys(RecDict),
print_types1(Keys, RecDict).
print_types1([], _) ->
ok;
print_types1([{type, _Name} = Key|T], RecDict) ->
{ok, {_Mod, Form, _Args}} = dict:find(Key, RecDict),
io:format("\n~w: ~w\n", [Key, erl_types:t_from_form(Form, RecDict)]),
print_types1(T, RecDict);
print_types1([{opaque, _Name} = Key|T], RecDict) ->
{ok, {_Mod, Form, _Args}} = dict:find(Key, RecDict),
io:format("\n~w: ~w\n", [Key, erl_types:t_from_form(Form, RecDict)]),
print_types1(T, RecDict);
print_types1([{record, _Name} = Key|T], RecDict) ->
{ok, [{Arity, Fields} = AF]} = dict:find(Key, RecDict),
io:format("~w: ~w\n\n", [Key, AF]),
print_types1(T, RecDict).
-define(debug(D_), print_types(D_)).
-else.
-define(debug(D_), ok).
-endif.
%%
%% Types that need to be imported from somewhere else
%%
-type abstract_code() :: [tuple()]. %% XXX: refine
-type comp_options() :: [atom()]. %% XXX: only a resticted set of options used
%% ============================================================================
%%
%% Compilation utils
%%
%% ============================================================================
-spec get_abstract_code_from_src(atom() | file:filename()) ->
{'ok', abstract_code()} | {'error', [string()]}.
get_abstract_code_from_src(File) ->
get_abstract_code_from_src(File, src_compiler_opts()).
-spec get_abstract_code_from_src(atom() | file:filename(), comp_options()) ->
{'ok', abstract_code()} | {'error', [string()]}.
get_abstract_code_from_src(File, Opts) ->
case compile:file(File, [to_pp, binary|Opts]) of
error -> {error, []};
{error, Errors, _} -> {error, format_errors(Errors)};
{ok, _, AbstrCode} -> {ok, AbstrCode}
end.
-type get_core_from_src_ret() :: {'ok', cerl:c_module()} | {'error', string()}.
-spec get_core_from_src(file:filename()) -> get_core_from_src_ret().
get_core_from_src(File) ->
get_core_from_src(File, []).
-spec get_core_from_src(file:filename(), comp_options()) -> get_core_from_src_ret().
get_core_from_src(File, Opts) ->
case get_abstract_code_from_src(File, Opts) of
{error, _} = Error -> Error;
{ok, AbstrCode} ->
case get_core_from_abstract_code(AbstrCode, Opts) of
error -> {error, " Could not get Core Erlang code from abstract code"};
{ok, _Core} = C -> C
end
end.
-spec get_abstract_code_from_beam(file:filename()) -> 'error' | {'ok', abstract_code()}.
get_abstract_code_from_beam(File) ->
case beam_lib:chunks(File, [abstract_code]) of
{ok, {_, List}} ->
case lists:keyfind(abstract_code, 1, List) of
{abstract_code, {raw_abstract_v1, Abstr}} -> {ok, Abstr};
_ -> error
end;
_ ->
%% No or unsuitable abstract code.
error
end.
-type get_core_from_abs_ret() :: {'ok', cerl:c_module()} | 'error'.
-spec get_core_from_abstract_code(abstract_code()) -> get_core_from_abs_ret().
get_core_from_abstract_code(AbstrCode) ->
get_core_from_abstract_code(AbstrCode, []).
-spec get_core_from_abstract_code(abstract_code(), comp_options()) -> get_core_from_abs_ret().
get_core_from_abstract_code(AbstrCode, Opts) ->
%% We do not want the parse_transforms around since we already
%% performed them. In some cases we end up in trouble when
%% performing them again.
AbstrCode1 = cleanup_parse_transforms(AbstrCode),
try compile:forms(AbstrCode1, Opts ++ src_compiler_opts()) of
{ok, _, Core} -> {ok, Core};
_What -> error
catch
error:_ -> error
end.
%% ============================================================================
%%
%% Typed Records
%%
%% ============================================================================
-spec get_record_and_type_info(abstract_code()) ->
{'ok', dict()} | {'error', string()}.
get_record_and_type_info(AbstractCode) ->
Module = get_module(AbstractCode),
get_record_and_type_info(AbstractCode, Module, dict:new()).
-spec get_record_and_type_info(abstract_code(), atom(), dict()) ->
{'ok', dict()} | {'error', string()}.
get_record_and_type_info(AbstractCode, Module, RecDict) ->
get_record_and_type_info(AbstractCode, Module, [], RecDict).
get_record_and_type_info([{attribute, _, record, {Name, Fields0}}|Left],
Module, Records, RecDict) ->
{ok, Fields} = get_record_fields(Fields0, RecDict),
Arity = length(Fields),
NewRecDict = dict:store({record, Name}, [{Arity, Fields}], RecDict),
get_record_and_type_info(Left, Module, [{record, Name}|Records], NewRecDict);
get_record_and_type_info([{attribute, _, type, {{record, Name}, Fields0, []}}
|Left], Module, Records, RecDict) ->
%% This overrides the original record declaration.
{ok, Fields} = get_record_fields(Fields0, RecDict),
Arity = length(Fields),
NewRecDict = dict:store({record, Name}, [{Arity, Fields}], RecDict),
get_record_and_type_info(Left, Module, Records, NewRecDict);
get_record_and_type_info([{attribute, _, Attr, {Name, TypeForm}}|Left],
Module, Records, RecDict) when Attr =:= 'type';
Attr =:= 'opaque' ->
try
NewRecDict = add_new_type(Attr, Name, TypeForm, [], Module, RecDict),
get_record_and_type_info(Left, Module, Records, NewRecDict)
catch
throw:{error, _} = Error -> Error
end;
get_record_and_type_info([{attribute, _, Attr, {Name, TypeForm, Args}}|Left],
Module, Records, RecDict) when Attr =:= 'type';
Attr =:= 'opaque' ->
try
NewRecDict = add_new_type(Attr, Name, TypeForm, Args, Module, RecDict),
get_record_and_type_info(Left, Module, Records, NewRecDict)
catch
throw:{error, _} = Error -> Error
end;
get_record_and_type_info([_Other|Left], Module, Records, RecDict) ->
get_record_and_type_info(Left, Module, Records, RecDict);
get_record_and_type_info([], _Module, Records, RecDict) ->
case type_record_fields(lists:reverse(Records), RecDict) of
{ok, _NewRecDict} = Ok ->
?debug(NewRecDict),
Ok;
{Name, {error, Error}} ->
{error, lists:flatten(io_lib:format(" Error while parsing #~w{}: ~s\n",
[Name, Error]))}
end.
add_new_type(TypeOrOpaque, Name, TypeForm, ArgForms, Module, RecDict) ->
case erl_types:type_is_defined(TypeOrOpaque, Name, RecDict) of
true ->
throw({error, io_lib:format("Type already defined: ~w\n", [Name])});
false ->
ArgTypes = [erl_types:t_from_form(X) || X <- ArgForms],
case lists:all(fun erl_types:t_is_var/1, ArgTypes) of
true ->
ArgNames = [erl_types:t_var_name(X) || X <- ArgTypes],
dict:store({TypeOrOpaque, Name}, {Module, TypeForm, ArgNames}, RecDict);
false ->
throw({error, io_lib:format("Type declaration for ~w does not "
"have variables as parameters", [Name])})
end
end.
get_record_fields(Fields, RecDict) ->
get_record_fields(Fields, RecDict, []).
get_record_fields([{typed_record_field, OrdRecField, TypeForm}|Left],
RecDict, Acc) ->
Name =
case OrdRecField of
{record_field, _Line, Name0} -> erl_parse:normalise(Name0);
{record_field, _Line, Name0, _Init} -> erl_parse:normalise(Name0)
end,
get_record_fields(Left, RecDict, [{Name, TypeForm}|Acc]);
get_record_fields([{record_field, _Line, Name}|Left], RecDict, Acc) ->
NewAcc = [{erl_parse:normalise(Name), {var, -1, '_'}}|Acc],
get_record_fields(Left, RecDict, NewAcc);
get_record_fields([{record_field, _Line, Name, _Init}|Left], RecDict, Acc) ->
NewAcc = [{erl_parse:normalise(Name), {var, -1, '_'}}|Acc],
get_record_fields(Left, RecDict, NewAcc);
get_record_fields([], _RecDict, Acc) ->
{ok, lists:reverse(Acc)}.
type_record_fields([], RecDict) ->
{ok, RecDict};
type_record_fields([RecKey|Recs], RecDict) ->
{ok, [{Arity, Fields}]} = dict:find(RecKey, RecDict),
try
TypedFields =
[{FieldName, erl_types:t_from_form(FieldTypeForm, RecDict)}
|| {FieldName, FieldTypeForm} <- Fields],
RecDict1 = dict:store(RecKey, [{Arity, TypedFields}], RecDict),
Fun = fun(OldOrdDict) ->
orddict:store(Arity, TypedFields, OldOrdDict)
end,
RecDict2 = dict:update(RecKey, Fun, RecDict1),
type_record_fields(Recs, RecDict2)
catch
throw:{error, _} = Error ->
{record, Name} = RecKey,
{Name, Error}
end.
-spec process_record_remote_types(dialyzer_codeserver:codeserver()) -> dialyzer_codeserver:codeserver().
process_record_remote_types(CServer) ->
TempRecords = dialyzer_codeserver:get_temp_records(CServer),
RecordFun =
fun(Key, Value) ->
case Key of
{record, _Name} ->
FieldFun =
fun(_Arity, Fields) ->
[{Name, erl_types:t_solve_remote(Field, TempRecords)} || {Name, Field} <- Fields]
end,
orddict:map(FieldFun, Value);
_Other -> Value
end
end,
ModuleFun =
fun(_Module, Record) ->
dict:map(RecordFun, Record)
end,
NewRecords = dict:map(ModuleFun, TempRecords),
dialyzer_codeserver:finalize_records(NewRecords, CServer).
-spec merge_records(dict(), dict()) -> dict().
merge_records(NewRecords, OldRecords) ->
dict:merge(fun(_Key, NewVal, _OldVal) -> NewVal end, NewRecords, OldRecords).
%% ============================================================================
%%
%% Spec info
%%
%% ============================================================================
-spec get_spec_info(module(), abstract_code(), dict()) ->
{'ok', dict()} | {'error', string()}.
get_spec_info(ModName, AbstractCode, RecordsDict) ->
get_spec_info(AbstractCode, dict:new(), RecordsDict, ModName, "nofile").
%% TypeSpec is a list of conditional contracts for a function.
%% Each contract is of the form {[Argument], Range, [Constraint]} where
%% - Argument and Range are in erl_types:erl_type() format and
%% - Constraint is of the form {subtype, T1, T2} where T1 and T2
%% are erl_types:erl_type()
get_spec_info([{attribute, Ln, spec, {Id, TypeSpec}}|Left],
SpecDict, RecordsDict, ModName, File) when is_list(TypeSpec) ->
MFA = case Id of
{_, _, _} = T -> T;
{F, A} -> {ModName, F, A}
end,
try dict:find(MFA, SpecDict) of
error ->
NewSpecDict =
dialyzer_contracts:store_tmp_contract(MFA, {File, Ln}, TypeSpec,
SpecDict, RecordsDict),
get_spec_info(Left, NewSpecDict, RecordsDict, ModName, File);
{ok, {{OtherFile, L},_C}} ->
{Mod, Fun, Arity} = MFA,
Msg = io_lib:format(" Contract for function ~w:~w/~w "
"already defined in ~s:~w\n",
[Mod, Fun, Arity, OtherFile, L]),
throw({error, Msg})
catch
throw:{error, Error} ->
{error, lists:flatten(io_lib:format(" Error while parsing contract "
"in line ~w: ~s\n", [Ln, Error]))}
end;
get_spec_info([{attribute, _, file, {IncludeFile, _}}|Left],
SpecDict, RecordsDict, ModName, _File) ->
get_spec_info(Left, SpecDict, RecordsDict, ModName, IncludeFile);
get_spec_info([_Other|Left], SpecDict, RecordsDict, ModName, File) ->
get_spec_info(Left, SpecDict, RecordsDict, ModName, File);
get_spec_info([], SpecDict, _RecordsDict, _ModName, _File) ->
{ok, SpecDict}.
%% ============================================================================
%%
%% Util utils
%%
%% ============================================================================
-spec src_compiler_opts() -> comp_options().
src_compiler_opts() ->
[no_copt, to_core, binary, return_errors,
no_inline, strict_record_tests, strict_record_updates].
-spec get_module(abstract_code()) -> module().
get_module([{attribute, _, module, {M, _As}} | _]) -> M;
get_module([{attribute, _, module, M} | _]) -> M;
get_module([_ | Rest]) -> get_module(Rest).
-spec cleanup_parse_transforms(abstract_code()) -> abstract_code().
cleanup_parse_transforms([{attribute, _, compile, {parse_transform, _}}|Left]) ->
cleanup_parse_transforms(Left);
cleanup_parse_transforms([Other|Left]) ->
[Other|cleanup_parse_transforms(Left)];
cleanup_parse_transforms([]) ->
[].
-spec format_errors([{module(), string()}]) -> [string()].
format_errors([{Mod, Errors}|Left]) ->
FormatedError =
[io_lib:format("~s:~w: ~s\n", [Mod, Line, M:format_error(Desc)])
|| {Line, M, Desc} <- Errors],
[lists:flatten(FormatedError) | format_errors(Left)];
format_errors([]) ->
[].
-spec format_sig(erl_types:erl_type()) -> string().
format_sig(Type) ->
format_sig(Type, dict:new()).
-spec format_sig(erl_types:erl_type(), dict()) -> string().
format_sig(Type, RecDict) ->
"fun(" ++ Sig = lists:flatten(erl_types:t_to_string(Type, RecDict)),
")" ++ RevSig = lists:reverse(Sig),
lists:reverse(RevSig).
%%-------------------------------------------------------------------
%% Author : Per Gustafsson <[email protected]>
%% Description : Provides better printing of binaries.
%% Created : 5 March 2007
%%-------------------------------------------------------------------
pp_hook() ->
fun pp_hook/3.
-spec pp_hook() -> fun((cerl:cerl(), _, _) -> term()).
pp_hook(Node, Ctxt, Cont) ->
case cerl:type(Node) of
binary ->
pp_binary(Node, Ctxt, Cont);
bitstr ->
pp_segment(Node, Ctxt, Cont);
_ ->
Cont(Node, Ctxt)
end.
pp_binary(Node, Ctxt, Cont) ->
prettypr:beside(prettypr:text("<<"),
prettypr:beside(pp_segments(cerl:binary_segments(Node),
Ctxt, Cont),
prettypr:text(">>"))).
pp_segments([Seg], Ctxt, Cont) ->
pp_segment(Seg, Ctxt, Cont);
pp_segments([], _Ctxt, _Cont) ->
prettypr:text("");
pp_segments([Seg|Rest], Ctxt, Cont) ->
prettypr:beside(pp_segment(Seg, Ctxt, Cont),
prettypr:beside(prettypr:text(","),
pp_segments(Rest, Ctxt, Cont))).
pp_segment(Node, Ctxt, Cont) ->
Val = cerl:bitstr_val(Node),
Size = cerl:bitstr_size(Node),
Unit = cerl:bitstr_unit(Node),
Type = cerl:bitstr_type(Node),
Flags = cerl:bitstr_flags(Node),
prettypr:beside(Cont(Val, Ctxt),
prettypr:beside(pp_size(Size, Ctxt, Cont),
prettypr:beside(pp_opts(Type, Flags),
pp_unit(Unit, Ctxt, Cont)))).
pp_size(Size, Ctxt, Cont) ->
case cerl:is_c_atom(Size) of
true ->
prettypr:text("");
false ->
prettypr:beside(prettypr:text(":"), Cont(Size, Ctxt))
end.
pp_opts(Type, Flags) ->
FinalFlags =
case cerl:atom_val(Type) of
binary -> [];
float -> keep_endian(cerl:concrete(Flags));
integer -> keep_all(cerl:concrete(Flags));
utf8 -> [];
utf16 -> [];
utf32 -> []
end,
prettypr:beside(prettypr:text("/"),
prettypr:beside(pp_atom(Type),
pp_flags(FinalFlags))).
pp_flags([]) ->
prettypr:text("");
pp_flags([Flag|Flags]) ->
prettypr:beside(prettypr:text("-"),
prettypr:beside(pp_atom(Flag),
pp_flags(Flags))).
keep_endian(Flags) ->
[cerl:c_atom(X) || X <- Flags, (X =:= little) or (X =:= native)].
keep_all(Flags) ->
[cerl:c_atom(X) || X <- Flags,
(X =:= little) or (X =:= native) or (X =:= signed)].
pp_unit(Unit, Ctxt, Cont) ->
case cerl:concrete(Unit) of
N when is_integer(N) ->
prettypr:beside(prettypr:text("-"),
prettypr:beside(prettypr:text("unit:"),
Cont(Unit, Ctxt)));
_ -> % Other value: e.g. 'undefined' when UTF
prettypr:text("")
end.
pp_atom(Atom) ->
String = atom_to_list(cerl:atom_val(Atom)),
prettypr:text(String).
|