Files
mercury/tests/valid/error.m
Zoltan Somogyi f2043fc9bd Replace the item list with more structured ASTs.
The parts of the compiler that run before the HLDS is constructed used to use
a raw list of items to represent source files (.m), interface files (.int0,
.int3, .int2 and .int) and optimization files (.opt, and .trans_opt).
These lists had structure, but this structure was implicit, not explicit,
and its invariants were never really documented.

This diff changes that. It replaces the item list with FIVE separate types.

Three of these each represent the unprocessed content of one file:

- parse_tree_int represents the contents of one interface file;
- parse_tree_opt represents the contents of one optimization file;
- parse_tree_src represents the contents of one source file.

Two of these each represent the processed contents of one or more files:

- raw_compilation_unit represents the contents of one module in a source file.
  (The source file may contain several nested modules; the compilation unit
  represents just one.)
- aug_compilation_unit represents the contents of one module in a source file,
  just like raw_compilation_unit, but it is augmented with the contents of the
  interface and optimization files of the other modules imported (directly or
  indirectly) by the original module.

These five separate concepts all used to be represented by the same type,
list(item), but different invariants applied to the structure of those lists.
The most important of those invariants at least are now explicit in the types.
I think it is entirely possible that there are other invariants I haven't
discovered and documented (for example, .int3 files must have stricter
invariants on what can appear in them than .int files), but discovering
and documenting these should be MUCH easier after this change.

I have marked many further opportunities for improvements with "XXX ITEM_LIST".
Some of these include moving code between modules, and the creation of new
modules. However, I have left acting on those XXXs until later, in order to
keep the size of this diff down as much as possible, for easier reviewing.

compiler/prog_item.m:
    Define the five new AST types described above, and utility predicates
    that operate on them.

    In the rest of this change, I tried, as much as possible, to change
    predicates that used to take item lists as arguments to make them change
    one of these types instead. In many cases, this required putting
    the argument lists of those predicates into a more consistent order.
    (Often, predicates that operated on the contents of the module
    took the name of the module and the list of items in the module
    not just as separate arguments, but as separate arguments that
    weren't even next to each other.)

    Define types that identify the different kinds of interface and
    optimization files (.int, .int2 etc). These replace the string suffixes
    we used to use to identify file types. Predicates that used to take strings
    representing suffixes as arguments now have to specify whether they can
    handle all these file types (source, interface and optimization),
    or just (e.g.) all interface file types.

    We used to have items corresponding to `:- module' and `:- end_module'.
    Delete these; this information is now implicit in the structure of the
    relevant AST. The parser handles the corresponding terms as markers,
    not items; these markers are live only during parsing.

    We used to have module_defns corresponding to `:- interface' and
    `:- implementation'. Delete these; this information is now also implicit
    in the structure of the relevant AST. Delete also, for the same reason,
    the module_defns used to mark the starts of sublists in the overall lists
    of items whose items came from the interface files or optimization files
    of other modules. The former are now markers during parsing. The latter
    are never parsed, but are created directly, after parsing has been done.

    Delete the pragma type for `:- pragma source_file'. This is never
    needed later; it is now a marker during parsing.

    Change the internal representation of `:- import' and `:- use'.
    It used to store a list of module names, but that list was an actual list
    only during parsing; after that, it always had exactly one element.
    It now stores one module name, and the parser has a mechanism to convert
    one read-in term to more than one item, for use with terms such as
    `:- import_module a, b'.

    Delete the internal representation of `:- export', which was never
    implemented, since if it IS ever implemented, it will almost certainly
    be in a different form, which will need different support.

    Document some further opportunities for simplification, later.
    (This diff is already more than big enough.)

compiler/prog_io_item.m:
    Rewrite the top-level part of this module. Instead of returning an item
    for every parsed term, distinguish between parsing items that end up
    in item lists inside ASTs, and parsing markers that end up creating
    the STRUCTURE of those ASTs.

compiler/prog_io.m:
    Rewrite the meat of this module. Instead of reading in a simple item list,
    we now have to read in three different parse trees with three different
    grammars, each of which is more complex than a simple list.

compiler/read_modules.m:
    We used to have a map that mapped file names to the contents of those
    files. We now need three separate maps, for interface files, optimization
    files and source files, due to their separate types.
    (We don't actually use the map for optimization files, which seems
    to be a potential performance bug. The root cause of that problem
    us that while intermod.m and the grab_*modules part of modules.m do
    similar jobs, they don't use the same mechanisms.)

    Replace the read_module predicate with the predicates read_module_src
    and read_module_int, since these now return different types.

    To avoid having to create AST-type-specialized variants of
    read_module_ignore_errors and read_module_if_changed, give each of
    read_module_{src,int} arguments that optionally tell them to ignore errors
    and/or to read the module only if changed (though the "and" part of
    "and/or" should not be needed.) These options already existed, but
    they weren't exported.

compiler/timestamp.m:
    Define the type we use for this option in read_modules.

compiler/status.m:
    New module, containing mostly

    - stuff carved out of hlds_pred.m, which defines the import_status type,
      and the predicates that operate on it;
    - stuff carved out of make_hlds_passes.m, which defines the item_status
      type and the predicates that operate on that; and
    - stuff carved out prog_data.m, which defines the section (now
      module_section) and import_locn types.

    It also contains the new section kinds we now use to represent item blocks
    that were imported from interface and optimization files.

compiler/parse_tree.m:
compiler/notes/compiler_design.html:
    Add status.m to the parse_tree package.

compiler/hlds_pred.m:
compiler/prog_data.m:
    Remove the stuff now in status.m.

compiler/error_util.m:
    Provide a mechanism to control the order of messages with respect to
    ALL other messages, not just those that also specify ordering.

compiler/mercury_to_mercury.m:
    Provide predicates for printing out parse_tree_* and *_compilation_unit,
    since printing out a simple item list is no longer enough for debugging.

    Pretty-print type definitions nicely.

    Replace a boolean with a purpose-specific enum.

compiler/modules.m:
    Rewrite virtually all this module to make it work on the new AST
    representations. Generate more detailed error messages for duplicate
    module inclusions. Note lots of possibilities for further improvements,
    including in the documentation. Mark places I am still not sure about,
    especially places where I am not sure *why* the code is doing
    what it is doing.

compiler/module_imports.m:
    This module stores the data structure in which we accumulate the stuff
    imported into a compilation unit, i.e. it is in these data structures
    that a raw_compilation_unit becomes an aug_compilation_unit. Modify
    the data structure and the predicates that operate on it to work on the
    new AST representations, not on an (apparently) simple list of items.

    Avoid ambiguities by adding a prefix to field names.

    Add some convenience predicates.

compiler/module_qual.m:
    Perform module qualification on both raw lists of items (for use when
    generating .int3 files) but also on item blocks (for use pretty much
    in every other situation).

    Generate warnings about module imports that are unnecessarily in the
    module interface using the module's context (the context of the `:- module'
    declaration), not line 1 of the relevant file.

compiler/prog_io_error.m:
    Split some error categories more finely, since some error kinds here
    actually used to be reported for more than one distinct situation.

compiler/prog_io_util.m:
    Provide utility predicates that operate on nonempty lists.

compiler/recompilation.version.m:
    Make the comparison of the old and new contents of the interface file
    work on two parse_tree_ints, not on two raw sequences of items.

    Delete a boolean option that was always `yes', never 'no'.

compiler/recompilation.m:
    Turn some functions into predicates to allow the use of state variable
    notation.

    Avoid ambiguities by adding a prefix to field names.

compiler/write_module_interface_files.m:
    Besides updating the code in this module to work on the new parse tree
    representations, also use cords instead of reversed lists in several cases.
    Note many possibilities for further improvements.

library/list.m:
    Move the type one_or_more here from the compiler directory, since
    we now use it in more than one compiler module, and this is its natural
    home.

mdbcomp/sym_name.m:
    Rename "match_sym_name" to "partial_sym_name_matches_full", since this
    better describes its job.

    Add a det version of sym_name_get_module_name.

compiler/equiv_type.m:
    Rename some types to make them more expressive.

compiler/accumulator.m:
compiler/add_class.m:
compiler/add_foreign_enum.m:
compiler/add_foreign_proc.m:
compiler/add_mode.m:
compiler/add_pragma.m:
compiler/add_pragma_tabling.m:
compiler/add_pred.m:
compiler/add_solver.m:
compiler/add_special_pred.m:
compiler/add_type.m:
compiler/assertion.m:
compiler/base_typeclass_info.m:
compiler/check_typeclass.m:
compiler/ctgc.util.m:
compiler/dead_proc_elim.m:
compiler/dep_par_conj.m:
compiler/dependency_graph.m:
compiler/deps_map.m:
compiler/det_report.m:
compiler/elds_to_erlang.m:
compiler/equiv_type_hlds.m:
compiler/erl_code_gen.m:
compiler/export.m:
compiler/format_call.m:
compiler/higher_order.m:
compiler/hlds_data.m:
compiler/hlds_module.m:
compiler/hlds_out_pred.m:
compiler/inst_check.m:
compiler/intermod.m:
compiler/item_util.m:
compiler/lambda.m:
compiler/lco.m:
compiler/make.module_dep_file.m:
compiler/make_hlds.m:
compiler/make_hlds_error.m:
compiler/make_hlds_passes.m:
compiler/make_tags.m:
compiler/mercury_compile.m:
compiler/ml_proc_gen.m:
compiler/ml_type_gen.m:
compiler/mode_errors.m:
compiler/oisu_check.m:
compiler/par_loop_control.m:
compiler/polymorphism.m:
compiler/post_term_analysis.m:
compiler/post_typecheck.m:
compiler/pred_table.m:
compiler/prog_io_dcg.m:
compiler/prog_io_find.m:
compiler/prog_io_pragma.m:
compiler/prog_io_sym_name.m:
compiler/prog_io_type_defn.m:
compiler/prog_io_typeclass.m:
compiler/prop_mode_constraints.m:
compiler/push_goals_together.m:
compiler/qual_info.m:
compiler/recompilation.check.m:
compiler/recompilation.usage.m:
compiler/simplify_proc.m:
compiler/smm_common.m:
compiler/special_pred.m:
compiler/ssdebug.m:
compiler/stm_expand.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.direct.m:
compiler/structure_reuse.indirect.m:
compiler/structure_reuse.versions.m:
compiler/structure_sharing.analysis.m:
compiler/structure_sharing.domain.m:
compiler/table_gen.m:
compiler/term_constr_initial.m:
compiler/term_constr_main.m:
compiler/termination.m:
compiler/trace_params.m:
compiler/trans_opt.m:
compiler/type_class_info.m:
compiler/type_ctor_info.m:
compiler/typecheck.m:
compiler/typecheck_errors.m:
compiler/typecheck_info.m:
compiler/unify_proc.m:
compiler/untupling.m:
compiler/unused_args.m:
compiler/unused_imports.m:
compiler/write_deps_file.m:
compiler/xml_documentation.m:
    Conform to the changes above.

tests/hard_coded/higher_order_func_test.m:
tests/hard_coded/higher_order_syntax.m:
    Avoid a warning about importing a module in the interface, not the
    implementation.

tests/invalid/after_end_module.err_exp:
tests/invalid/any_mode.err_exp:
tests/invalid/bad_end_module.err_exp:
tests/invalid/bigtest.err_exp:
tests/invalid/bug113.err_exp:
tests/invalid/duplicate_modes.err_exp:
tests/invalid/errors.err_exp:
tests/invalid/errors1.err_exp:
tests/invalid/errors2.err_exp:
tests/invalid/funcs_as_preds.err_exp:
tests/invalid/inst_list_dup.err_exp:
tests/invalid/invalid_main.err_exp:
tests/invalid/missing_interface_import2.err_exp:
tests/invalid/no_exports.err_exp:
tests/invalid/occurs.err_exp:
tests/invalid/predmode.err_exp:
tests/invalid/prog_io_erroneous.err_exp:
tests/invalid/type_inf_loop.err_exp:
tests/invalid/typeclass_missing_det_3.err_exp:
tests/invalid/typeclass_test_11.err_exp:
tests/invalid/types.err_exp:
tests/invalid/undef_inst.err_exp:
tests/invalid/undef_mode.err_exp:
tests/invalid/undef_type.err_exp:
tests/invalid/unicode1.err_exp:
tests/invalid/unicode2.err_exp:
tests/invalid/vars_in_wrong_places.err_exp:
tests/warnings/unused_import.exp:
tests/warnings/unused_interface_import.exp:
    Update the expected outputs in the invalid and warnings directories
    to account for one or more of the following five changes.

    Error messages that warn about a module not exporting anything
    used to always refer to line 1 of the module's source file.
    Now expect these messages to refer to the actual context of the module,
    which is the context of its `:- module' declaration.

    Expect a similarly updated context for messages that warn about
    unnecessarily importing modules in the interface, not in the
    implementation.

    Expect a similarly updated context for messages that warn about
    importing a module via both `:- import_module' and `:- use_module'.

    For the modules that follow the `:- module' declaration directly with code,
    also expect an error message about the missing section marker.

    For modules that have terms after the `:- end_module' declaration,
    replace "end_module" with "`:- end_module'" in the error message.

tests/invalid/func_class.{m,err_exp}:
    New test case. It is a copy of the old tests/valid/func_class.m, which
    is missing more than one module marker. The expected output is what I think
    we should generate. The test case currently fails, because we currently
    print only a subset of the expected errors. I am pretty sure the reason
    for that is that old code I have not modified simply throws away the
    missing error messages. Fixing this is work for the near future.

tests/invalid/Mmakefile:
    Enable the new test case.

tests/misc_tests/pretty_print_test.exp:
    Expect the pretty-printed output to use four-space indentation,
    per our current style guide, since the compiler now generates such output.

tests/misc_tests/pretty_print_test.m:
    Clean up the source code of the test as well.

tests/valid/complicated_unify.m:
tests/valid/det_switch.m:
tests/valid/easy_nondet_test.m:
tests/valid/error.m:
tests/valid/func_class.m:
tests/valid/func_int_bug_main.m:
tests/valid/higher_order.m:
tests/valid/higher_order2.m:
tests/valid/implied_mode.m:
tests/valid/indexing.m:
tests/valid/multidet_test.m:
tests/valid/nasty_func_test.m:
tests/valid/semidet_disj.m:
tests/valid/stack_alloc.m:
tests/valid/switches.m:
    Add missing section markers to these modules. They used to follow
    the `:- module' declaration directly with code.
2015-07-21 04:06:52 +10:00

13 lines
304 B
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
:- module error.
:- implementation.
:- import_module require.
:- pred t(int::out) is det.
t(_X) :-
error("").