Commit Graph

471 Commits

Author SHA1 Message Date
Zoltan Somogyi
36e8833145 Add a new option, --warn-unused-types.
When set, this option tells the compiler to generate warnings
about locally-defined types that are neither used in the module
nor exported to any other module.

compiler/options.m:
    Add the new option.

compiler/unused_types.m:
    New module to implement the new option.

compiler/mercury_compile_front_end.m:
    Invoke the new module, unless the presence of previous errors
    would make its warnings just a distraction.

compiler/check_hlds.m:
    Include the new module.

compiler/notes/compiler_design.html:
    Document the new module.

compiler/typecheck_error_wrong_type.m:
    Simplify some code.

browser/declarative_tree.m:
compiler/accumulator.m:
compiler/du_type_layout.m:
compiler/intermod.m:
compiler/mode_errors.m:
compiler/parse_inst_mode_defn.m:
compiler/polyhedron.m:
compiler/split_parse_tree_src.m:
compiler/tag_switch_util.m:
compiler/typecheck_error_unify.m:
compiler/unneeded_code.m:
deep_profiler/mdprof_test.m:
library/getopt.m:
library/getopt_io.m:
    Delete unused types reported by the new option.

library/rtti_implementation.m:
    Comment out unused type reported by the new option. This type was exported
    to both Java and C#, but this diff comments it out because neither language
    the Java or the C# runtime system seems to use the exported versions
    either. (Bootcheck in both java and csharp grades worked, with the
    same number of test case failures as before.) We do not delete it,
    because it may be useful in the future.

tests/warnings/help_text.err_exp:
    Expect the documentation of the new option.

tests/invalid_nodepend/Mmakefile:
    Specify --warn-unused-types for two test cases to test that the compiler
    does NOT generate warnings about unused types in the presence of previous
    errors.

tests/warnings/abstract_type_decl.err_exp:
tests/warnings/bug412.err_exp:
tests/warnings/warn_dead_procs.err_exp:
    Expect the new warnings for unused types.

tests/warnings/Mmakefile:
    Specify --warn-unused-types for the three test cases listed above.
2026-02-15 11:26:34 +11:00
Peter Wang
9f84fec4f5 Fix bug with direct-arg ctors and intermodule optimisation.
If the compiler decides that a du type should use the direct-arg
representation for some of its constructors, it must include information
about that into the .opt file of the module defining the type, in the
form of `where direct_arg is' clauses, which will be used by modules
opt-importing that module and that type. That information was not being
included for du types defined in the *interface* section of a module.

Also fix a related issue that was uncovered: a word_aligned_pointer
assertion on a foreign_type definition would have no effect if there is
a no-tag du type definition for the same type constructor.

compiler/intermod.m:
compler/intermod_decide.m:
    Make should_opt_export_type_defn and some_type_needs_to_be_written
    succeed for `status_exported' du type definitions with direct-arg
    constructors. While `status_exported' suggests those type
    definitions would be redundant in .opt files, the information about
    the direct-arg constructors is not redundant.

compiler/du_type_layout.m:
    Add a is_word_aligned_ptr() value to the ComponentTypeMap if a
    no-tag du type also has a foreign_type definition for the current
    target language with a word_aligned_pointer assertion. Previously,
    this was only being done for single ctor NON no-tag du types.

    Add a XXX mentioning that we silently ignore word_aligned_pointer
    assertions in other cases.

tests/hard_coded/Mercury.options:
tests/hard_coded/Mmakefile:
tests/hard_coded/direct_arg_opt.m:
tests/hard_coded/direct_arg_opt_helper_1.m:
tests/hard_coded/direct_arg_opt_helper_1.direct_arg_opt_helper_2.m:
tests/hard_coded/direct_arg_opt.exp:
    Add a test case.
2026-01-12 11:33:56 +11:00
Zoltan Somogyi
ee9c7d3a84 Speed up bound vs ground inst checks.
The code that checks whether a bound inst wrapped around
a list of bound_functors matched the ground inst did several things
in a suboptimal fashion.

- It looked up the definition of the type constructor of the relevant type
  (the type of the variable the inst is for) more than once. (This was
  not easily visible because the lookups were in different predicates.)
  This diff factors these out, not for the immesurably small speedup,
  but to make possible the fixes for the next two issues.

- To simplify the "is there a bound_functor for each constructor in the type"
  check, it sorted the constructors of the type by name and arity. (Lists of
  bound_functors are always sorted by name and arity.) Given that most
  modules contain more than one bound inst for any given type constructor,
  any sorting after the first was unnecessarily repeated work. This diff
  therefore extends the representation of du types, which until now has
  include only a list of the data constructors in the type definition
  in definition order, with a list of those exact same data constructors
  in name/arity order.

- Even if a list of bound_functors lists all the constructors of a type,
  the bound inst containing them is not equivalent to ground if the inst
  of some argument of some bound_inst is not equivalent to ground.
  This means that we need to know the actual argument of each constructor.
  The du type definition lists argument types that refer to the type
  constructor's type parameters; we need the instances of these argument types
  that apply to type of the variable at hand, which usually binds concrete
  types to those type parameters.

  We used to apply the type-parameter-to-actual-type substitution to
  each argument of each data constructor in the type before we compared
  the resulting filled-in data constructor descriptions against the list of
  bound_functors. However, in cases where the comparison fails, the
  substitution applications to arguments beyond the point of failure
  are all wasted work. This diff therefore applies the substitution
  only when its result is about to be needed.

This diff leads to a speedup of about 3.5% on tools/speedtest,
and about 38% (yes, more than a third) when compiling options.m.

compiler/hlds_data.m:
    Add the new field to the representation of du types.

    Add a utility predicate that helps construct that field, since it is
    now needed by two modules (add_type.m and equiv_type_hlds.m).

    Delete two functions that were used only by det_check_switch.m,
    which this diff moves to that module (in modified form).

compiler/inst_match.m:
    Implement the first and third changes listed above, and take advantage
    of the second.

    The old call to all_du_ctor_arg_types, which this diff replaces,
    effectively lied about the list of constructors it returned,
    by simply not returning any constructors containing existentially
    quantified  types, on the grounds that they "were not handled yet".
    We now fail explicitly when we find any such constructors.

    Perform the check for one-to-one match between bound_functors and
    constructors with less argument passing.

compiler/det_check_switch.m:
    Move the code deleted from hlds_data.m here, and simplify it,
    taking advantage of the new field in du types.

compiler/Mercury.options:
    Specify --optimize-constructor-last-call for det_check_switch.m
    to optimize the updated moved code.

compiler/add_foreign_enum.m:
compiler/add_special_pred.m:
compiler/add_type.m:
compiler/check_typeclass.m:
compiler/code_info.m:
compiler/dead_proc_elim.m:
compiler/direct_arg_in_out.m:
compiler/du_type_layout.m:
compiler/equiv_type_hlds.m:
compiler/hlds_out_type_table.m:
compiler/inst_check.m:
compiler/intermod.m:
compiler/intermod_decide.m:
compiler/lookup_switch_util.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen_test.m:
compiler/ml_unify_gen_util.m:
compiler/mlds.m:
compiler/post_term_analysis.m:
compiler/recompilation.usage.m:
compiler/resolve_unify_functor.m:
compiler/simplify_goal_ite.m:
compiler/table_gen.m:
compiler/tag_switch_util.m:
compiler/term_norm.m:
compiler/type_ctor_info.m:
compiler/type_util.m:
compiler/typecheck_coerce.m:
compiler/unify_proc.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
    Conform to the changes above. This mostly means handling
    the new field in du types (usually by ignoring it).
2025-11-19 22:09:04 +11:00
Zoltan Somogyi
c643291d1f Add cons_list and snoc_list to cord.m.
library/cord.m:
    Add those two new predicates.

NEWS.md:
    Announce the additions.

compiler/*.m:
    Use snoc_list in many places.

tests/hard_coded/test_cord_2.{m,exp}:
    Extend this test case to test cons_list.
2025-10-10 18:48:26 +11:00
Zoltan Somogyi
5194de7d96 Require fundeps' domains and ranges to be nonempty.
compiler/prog_data.m:
    Encode the above invariant in the type of functional dependencies.

compiler/hlds_class.m:
    Document the same invariant in the HLDS representation of fundeps.

compiler/parse_class.m:
    Enforce the invariant when parsing fundeps.

compiler/parse_util.m:
    Add a new version of existing predicate for use by new code in
    parse_class.m.

compiler/add_class.m:
compiler/intermod.m:
compiler/parse_tree_out_item.m:
    Conform to the changes above.
2025-09-21 18:08:59 +10:00
Zoltan Somogyi
1296ec11e5 Partially clarify and partially fix d_file_deps.
Before this diff,

- the .d filed generated by "mmc --generate-dependencies mer_std.m", and
- the .d files generated by compiling all the modules in the library

had *two*-way differences: the first set of .d files contained dependencies
that the second did not, and *vice versa*. The latter differences were bugs.
Now, as far as I can tell from the deluge of data,
"mmc --generate-dependencies" generates a superset of the dependencies
generated by code generating compiler invocations, which is a safe
overapproximation of the actual dependencies.

compiler/generate_mmakefile_fragments.m:
    Flatten the d_file_deps structure by effectively inlining the std_deps
    structure inside it, and putting the resulting fields into a meaningful
    order. Document the resulting structure as well as I can.

    Use notag types to record the intended semantics of some fields
    into their values, with the intention of making them harder to misuse.
    They were misused before this diff; in particular, a missing module
    name in one of these fields would reliably lead to the failure
    of the hard_coded/foreign_import_module test case in hlc grades,
    though only with a sequential invocation of mmake. (The bug was
    that after "mmc --generate-dependencies foreign_import_module.m",
    foreign_import_module.d was missing the dependency of
    foreign_import_module.o on foreign_import_module_helper_1.mh,
    and therefore the compilation foreign_import_module.c got a gcc abort
    complaining about this .mh file being missing. The reason for
    the bug being intermittent was that the mmc invocation that created
    foreign_import_module.c would add the missing dependency, so *later*
    mmake invocations, though not existing ones, would know
    to build that .mh file.)

    Generate rules for object files depending on .mh files that
    account for both foreign_import_module declarations, and for
    foreign type definitions. Add an XXX about the absence of the
    information we need to properly handle the latter in the presence
    of intermodule optimization.

    Generate just one rule for object files depending on .mh files,
    instead of generating two (one for the dependencies that exist
    without intermodule optimization, and for one those that exist only
    with intermodule optimization.)

    Flatten the intermod_deps structure as well. It used to have two
    components that encoded the outcomes of two *almost* identical tests,
    but a change to d_file_deps.m below makes them actually identical,
    so they do not need to be recorded twice.

    Inline the old (and misnamed) gather_foreign_import_deps predicate
    at its only call site, for greater clarity.

    Simplify the code constructing some actions.

    Use more specifically descriptive predicate and variable names.

compiler/d_file_deps.m:
    Stop requiring the caller of construct_d_file_deps_gendep to pass
    an aug_compilation_unit; get it to pass instead two of its components,
    the only ones that are meaningful and also the only ones we actually need:
    the parse_tree of the module in question and its baggage.

    Eliminate some unnecessary differences between compute_d_file_deps_gendep
    and compute_d_file_deps_hlds by replacing some existing tests for just
    --intermod-opt with tests for *either* --intermod-opt *or* --use-opt-files,
    which is the test used elsewhere for the same purpose in the rest of this
    module. The two tests are equivalent for now, because handle_options.m
    force-sets --no-use-opt-files. And even when we undo that force-set,
    the new test will actually be what we want.

    Completely redo the code that computes the dependencies that
    generate_mmakefile_fragments.m converts into the dependencies
    of object files on .mh files.

    Give some predicates more meaningful names.

compiler/mmakefiles.m:
    Add a mechanism to allow the simplification of some codes
    creating actions.

compiler/module_deps_graph.m:
    Give a type a more descriptive name.

    Document a predicate.

    Add an XXX.

compiler/make.program_target.m:
compiler/write_deps_file.m:
    Conform to the changes above.

compiler/intermod.m:
    Fix comment rot.

compiler/parse_tree_out_item.m:
    Generate more readable output for foreign_enum pragmas.

library/digraph.m:
    Put the predicates/functions of this file into meaningful groups.

    We have several operations that have two names, one old and short,
    and one newer, longer and more descriptive. Implement the short named
    versions in terms of the longer named versions, not vice versa.

    Give some internal predicates more meaningful names.

    There are no algorithmic changes in this module.

browser/Mmakefile:
    Update a comment.

scripts/mmake.in:
    Allow $MMAKE_DEBUG to specify what debug flags to invoke gmake with.
2025-08-11 10:51:29 +02:00
Zoltan Somogyi
64ad7d9db4 Rename a function symbol to avoid an ambiguity. 2025-07-17 00:51:12 +02:00
Zoltan Somogyi
60386407ab Rename the "type_order_switch" pragma ...
... to "require_switch_arms_in_type_order".

compiler/prog_item.m:
compiler/hlds_markers.m:
    Update the names of the parse tree and the HLDS representations
    of this pragma.

compiler/parse_pragma.m:
    Update the code that parses the pragma.

compiler/det_check_switch.m:
    Update the name of the bespoke type control its operation.

    s/cases/arms/ in the text of the warning message.

compiler/add_pragma.m:
compiler/convert_parse_tree.m:
compiler/det_check_proc.m:
compiler/intermod.m:
compiler/item_util.m:
compiler/parse_tree_out_pragma.m:
compiler/table_gen.m:
    Conform to the changes above.

tests/warnings/bad_type_order_switch.m:
    Update the pragma name.

tests/warnings/bad_type_order_switch.err_exp:
    Expect arms, not cases.
2025-06-18 08:26:04 +02:00
Zoltan Somogyi
fbed5b6760 Add new pred marker pragma "type_order_switch".
compiler/prog_item.m:
    Add a new impl_marker pragma, type_order_switch. Its meaning is
    to ask the compiler to check all the switches in the predicate
    or function named in the pragma to see whether

    - the order of function symbols in the definition of type being
      switched on (actually, in the definition of the top type constructor
      of that type)

    - is matched by the order in the text of the cases of the switch.

    The intention is to use this on the optdb predicate in options.m.

compiler/hlds_markers.m:
    Add a new predicate marker, whose presence indicates this pragma.

compiler/parse_pragma.m:
compiler/add_pragma.m:
    Parse the new pragma, and convert it to the new marker.

compiler/det_check_proc.m:
    Expand the existing that check whether we need to need to look for
    warnings for the switches in a procedure body.

compiler/det_check_switch.m:
    Implement the new pragma.

    In the process, put the arguments of the affected predicates
    into the order required by our coding style.

compiler/error_spec.m:
    To help implement the new pragma, add a predicate for constructing
    diffs between two string sequences. This factors out two existing
    pieces of code. (New code in det_check_switch.m would have contained
    a third copy.)

compiler/add_type.m:
compiler/style_checks.m:
    Use the new predicate to replace those two old copies, simplifing
    existing code.

compiler/convert_parse_tree.m:
compiler/intermod.m:
compiler/item_util.m:
compiler/parse_tree_out_pragma.m:
compiler/table_gen.m:
    Conform to the changes above.

tests/warnings/bad_type_order_switch.{m,err_exp}:
    A test case for the new pragma.

tests/warnings/Mmakefile:
    Enable the new test case.
2025-06-17 15:35:57 +02:00
Zoltan Somogyi
4f57c70ff7 Tweak and announce the unneeded statevar options.
compiler/pre_typecheck.m:
    Add a heuristic: do NOT generate a warning about unused final statevars
    if all of a predicate's clauses are facts. In such cases, the warnings
    are more noise than helpful.

compiler/add_clause.m:
    Record for each clause whether it is a fact.

compiler/hlds_clauses.m:
    Add a slot for this new fact_or_not flag.

compiler/add_foreign_proc.m:
compiler/add_pragma_type_spec.m:
compiler/add_pred.m:
compiler/hlds_out_pred.m:
compiler/instance_method_clauses.m:
compiler/intermod.m:
compiler/typecheck.m:
compiler/typecheck_clauses.m:
compiler/unify_proc.m:
compiler/var_origins.m:
    Conform to the changes above.

compiler/options.m:
    Turn the options on by default. Their documentation already assumes this.

NEWS.md:
    Announce the new options warning about unneeded state variables.

tests/invalid/Mercury.options:
tests/invalid_purity/Mercury.options:
tests/invalid_nodepend/Mercury.options:
    Turn the new warn options off for test cases that

    - would otherwise get them, but
    - for which this is irrelevant to what they want to test for.

tests/invalid/state_vars_test_5.err_exp:
tests/invalid/try_io_else.err_exp:
tests/warnings/unify_x_f_x.err_exp:
    For these other cases, expect the new warnings.
2025-05-20 03:05:10 +10:00
Zoltan Somogyi
f70b5d6de7 Implement options to warn about unused state vars.
The new --warn-unneeded-initial-statevar option asks the compiler
to warn about code such as

    pred_a(!.X, ...) :-
        ... code that uses !.X, but does not update it ...

In this case, the preferred fix is to just replace all occurrences
of !.X with X.

The new --warn-unneeded-final-statevar option asks the compiler
to warn about code such as

    pred_a(!X, ...) :-
        ... code that maybe uses !.X, but does not update it ...

In this case, the preferred fix also involves replacing all occurrences
of !.X with X, but it also involves either deleting the argument
containing !:X (the best option), or, if there is some reason why
the predicate's signature must stay unchanged, to replace !:X with X as well.
And if the clause body does not actually refer to either !.X or !:X, then
*both* arguments represented by !X should be deleted.

The first option is a style warning; the second option, due to the
signature change it may call for, is a non-style warning.

Both options have a version whose name adds a "-lambda" suffix, and which
does the same warnings for the heads of lambda expressions, not clauses.

Note that several of the modules below, including some that help to implement
the warnings, also contain code changes that result from *acting* on
the new warnings, e.g. by deleting unneeded statevar arguments.
Other, similar changes will also come after this diff is committed.

compiler/options.m:
doc/user_guide.texi:
    Document the new options.

compiler/state_var.m:
    Gather the information needed to decide what code merits the new warnings.
    Do so in three stages:

    - when processing the head of a clause or of a lambda expression,
    - when processing the body goal of that clause or lambda expression,
    - when finishing up the processing of the clause or lambda expression.

    Add a predicate to generate the warnings for lambda expressions.

    Do not generate the warnings for clauses. This is because whether or not
    we want to warn about state vars in some clauses depends on the properties
    of *other* clauses of the same predicate, and state_var.m has access
    to only one clause at a time. Instead,

    - return the info needed by the warning-generating code in pre_typecheck.m
      (one of the first passes we execute after adding all clauses
      to the HLDS), and

    - we export some functionality for use by that code.

    Switch to a convention for naming the program variables corresponding
    to the middle (non-initial, non-final) versions of state variables
    whose output is affected by changes in the code of the clause body goal
    only if they involve that specific state variable.

    Give some predicates more descriptive names.

compiler/make_hlds.m:
    Make state_var.m and its new functionality visible from outside
    the make_hlds package.

compiler/add_clause.m:
    Record the information gathered by state_var.m in each clause.

compiler/hlds_clauses.m:
    Add a slot to each clause for this information.

    Give some predicates more descriptive names.

compiler/headvar_names.m:
    Use the contents of the new slots to detect whether any clauses
    have unused state vars, and if so, return the chosen consensus names
    of the head vars to the code of pre_typecheck.m, which uses this info
    as part of the implementation of the new warnings.

compiler/pre_typecheck.m:
    Implement the new warnings.

compiler/mercury_compile_front_end.m:
    Record the warnings that pre_typecheck.m can now return.

compiler/error_spec.m:
compiler/write_error_spec.m:
    Add unsigned versions of the format pieces involving ints, for use
    by the new code in pre_typecheck.m, and implement them.

compiler/hlds_out_util.m:
compiler/maybe_util.m:
    Move two related types from hlds_out_util.m to maybe_util.m,
    in order to allow pre_typecheck.m to use one of them.

compiler/hlds_args.m:
    Add a new utility function for use by the new code above.

compiler/foreign.m:
    Act on the new warnings by deleting the long-unused predicates
    being warned about.

compiler/post_typecheck.m:
    Speed up this traversal. (I originally thought to implement
    the new warnings in this pass.)

compiler/add_foreign_proc.m:
compiler/add_pragma.m:
compiler/add_pragma_tabling.m:
compiler/add_pragma_type_spec.m:
compiler/add_pred.m:
compiler/add_type.m:
compiler/build_mode_constraints.m:
compiler/call_gen.m:
compiler/check_typeclass.m:
compiler/clause_to_proc.m:
compiler/code_loc_dep.m:
compiler/delay_info.m:
compiler/delay_partial_inst.m:
compiler/dense_switch.m:
compiler/det_check_goal.m:
compiler/det_infer_goal.m:
compiler/disj_gen.m:
compiler/du_type_layout.m:
compiler/format_call.m:
compiler/goal_expr_to_goal.m:
compiler/hlds_dependency_graph.m:
compiler/hlds_out_pred.m:
compiler/hlds_pred.m:
compiler/hlds_rtti.m:
compiler/inst_merge.m:
compiler/instance_method_clauses.m:
compiler/intermod.m:
compiler/interval.m:
compiler/ite_gen.m:
compiler/lookup_switch.m:
compiler/make_hlds_passes.m:
compiler/mark_tail_calls.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mode_errors.m:
compiler/parse_string_format.m:
compiler/passes_aux.m:
compiler/polymorphism.m:
compiler/polymorphism_info.m:
compiler/polymorphism_type_info.m:
compiler/pragma_c_gen.m:
compiler/prop_mode_constraints.m:
compiler/purity.m:
compiler/quantification.m:
compiler/simplify_goal_call.m:
compiler/simplify_goal_conj.m:
compiler/string_switch.m:
compiler/superhomogeneous.m:
compiler/switch_gen.m:
compiler/tag_switch.m:
compiler/type_constraints.m:
compiler/typecheck.m:
compiler/typecheck_clauses.m:
compiler/typecheck_coerce.m:
compiler/typecheck_error_unify.m:
compiler/unify_gen_deconstruct.m:
compiler/unify_proc.m:
compiler/var_origins.m:
    Conform to the changes above, and/or act on the new warnings.

browser/diff.m:
library/bit_buffer.m:
library/getopt.m:
library/getopt_io.m:
library/io.error_util.m:
library/io.file.m:
library/mercury_term_lexer.m:
library/parsing_utils.m:
library/pretty_printer.m:
library/robdd.m:
library/rtti_implementation.m:
library/string.builder.m:
library/string.parse_runtime.m:
mdbcomp/feedback.m:
    Act on the new warnings.

tests/hard_coded/sv_nested_closures.m:
    Change this test's code to avoid the new warnings, since
    (if --halt-at-warn is ever enabled) the warnings would interfere
    with its job .

tests/invalid/bug197.err_exp:
tests/invalid/bug487.err_exp:
tests/invalid/nullary_ho_func_error.err_exp:
tests/invalid/try_detism.err_exp:
tests/warnings/singleton_test_state_var.err_exp:
    Expect variable names for the middle versions of state vars
    using the new naming scheme.
2025-05-18 06:43:24 +10:00
Zoltan Somogyi
807d1d8671 Test for what HLDS parts to dump using bool flags.
compiler/hlds_out_util.m:
    When setting up hlds_out_info, the structure threaded through
    all the code that does HLDS dumps, include in it not the raw value
    of the --hlds-dump-options option, but a structure derived from it,
    which contains a vector of boolean flags.

compiler/hlds_out_goal.m:
compiler/hlds_out_module.m:
compiler/hlds_out_pred.m:
compiler/hlds_out_type_table.m:
compiler/hlds_out_typeclass_table.m:
    Instead of testing whether a particular character occurs in the value
    of --hlds-dump-options, test the boolean flag that corresponds to that
    character. Selecting that boolean flag using its field name makes
    this test more readable, since e.g. "DumpOptions ^ dump_predicates = yes"
    is a lot clearer than "string.contains_char(DumpOptions, 'x')".
    It should also be very slightly faster.

compiler/intermod.m:
    Conform to the changes above.

doc/user_guide.texi:
    Fix some minor issues in the documentation of the --hlds-dump-options
    option that were revealed by work on the changes above.
2025-03-30 08:33:34 +11:00
Zoltan Somogyi
0326634b10 Move pred markers and goal features to a new module.
compiler/hlds_markers.m:
    This is that new module.

compiler/hlds.m:
compiler/notes/compiler_design.html:
    Include and document the new module.

compiler/hlds_pred.m:
compiler/hlds_goal.m:
    Delete the moved code.

compiler/*.m:
    Conform to the changes above. Roughly a third of the modules
    that import hlds_pred.m or hlds_goal.m import the new module.
2025-01-12 22:10:28 +11:00
Zoltan Somogyi
c3f58711fc Move pred markers and goal features to a new module.
compiler/hlds_markers.m:
    This is that new module.

compiler/hlds.m:
compiler/notes/compiler_design.html:
    Include and document the new module.

compiler/hlds_pred.m:
compiler/hlds_goal.m:
    Delete the moved code.

compiler/*.m:
    Conform to the changes above. Roughly a third of the modules
    that import hlds_pred.m or hlds_goal.m import the new module.
2025-01-12 22:06:06 +11:00
Zoltan Somogyi
79bfb1247f Carve prog_parse_tree.m out of prog_item.m.
compiler/prog_item.m:
compiler/prog_parse_tree.m:
    Split prog_item.m into two modules, with the new module prog_parse_tree.m
    containing the definitions of the file-kind-specific parse trees,
    and prog_item.m continuing to contain the definitions of the items
    that occur in those parse trees. Specialize the top-of-module comment
    to the current contents of each module.

compiler/parse_tree.m:
compiler/notes/compiler_design.html:
    Include and document the new module.

compiler/*.m:
    Conform to the changes above.
2024-12-19 01:27:00 +11:00
Zoltan Somogyi
5a45d1afb8 Replace the cons/3 cons_id ...
... with the du_data_ctor/1 cons_id, which contains a du_ctor/3.
Putting all the info about discriminated union data constructors into a
separate type allows code that works only with the cons_ids of du types
to stop having to worry about all the other kinds of cons_ids.

compiler/prog_data.m:
    As above.

    Delete the cons_ctor type from this module after moving it to
    recompilation.usage.m, which is the only part of the compiler
    that uses it.

compiler/add_foreign_enum.m:
    Replace cons_id_to_tag_map with du_ctor_to_tag_map, since the only
    cons_ids the map handles are du_ctors.

compiler/hlds_cons.m:
    Replace cons_ids with du_ctors in the cons_table and in the
    info we keep about du types' fields.

compiler/hlds_data.m:
    Replace cons_ids with du_ctors in cheaper_tag_tests.

    Provide two forms of a utility function. Both return du_ctors,
    but one wraps them up as cons_ids.

compiler/type_assign.m:
    Replace cons_ids with du_ctors in cons_type_info_sources.

compiler/type_util.m:
    Switch many of the utility predicates and functions in this module
    to operate on du_ctors instead of cons_ids. Split some of the others
    to have both du_ctor and cons_id versions.

    Replace a predicate that returned a set of solutions one at a time
    on backtracking with a predicate that returns all the solutions
    at once in a list.

    Reduce unnecessary variability in variable names.

    Add some XXXs for code with unclear motivations.

compiler/typecheck_error_undef.m:
    Delete a function argument that was used only in a sanity check,
    because the code at its only call site derived that argument using code
    that made it impossible for the sanity check to fail.

    Factor out some common code.

compiler/parse_tree_out_cons_id.m:
    For three functions that operate on cons_ids, add versions
    that do the same job on du_ctors.

compiler/inst_match.m:
    Conform to the changes above. This diff rewrites from scratch
    the algorithm for testing whether a list of bound insts covers
    *all* the du_ctors of a type, because the old code was both inefficient
    and very opaque.

compiler/float_regs.m:
    Conform to the changes above, and delete a conditionally enabled abort
    that shouldn't ever be enabled.

compiler/inst_util.m:
    Conform to the changes above, and rename a predicate to avoid
    an ambiguity.

compiler/mode_errors.m:
    Conform to the changes above, and switch to printing the cons_ids
    in some error messages using the standard mechanisms of
    write_error_spec.m.

compiler/resolve_unify_functor.m:
    Conform to the changes above. Factor out repeated tests against
    du_data_ctor.

compiler/term_norm.m:
    Conform to the changes above. Add XXXs for some bugs.

compiler/add_type.m:
compiler/assertion.m:
compiler/builtin_lib_types.m:
compiler/comp_unit_interface.m:
compiler/complexity.m:
compiler/const_struct.m:
compiler/cse_detection.m:
compiler/ctgc.selector.m:
compiler/dead_proc_elim.m:
compiler/deep_profiling.m:
compiler/delay_partial_inst.m:
compiler/direct_arg_in_out.m:
compiler/distance_granularity.m:
compiler/du_type_layout.m:
compiler/error_msg_inst.m:
compiler/field_access.m:
compiler/format_call.m:
compiler/goal_expr_to_goal.m:
compiler/goal_util.m:
compiler/hhf.m:
compiler/higher_order.specialize_calls.m:
compiler/higher_order.specialize_unify_compare.m:
compiler/hlds_code_util.m:
compiler/hlds_dependency_graph.m:
compiler/hlds_out_goal.m:
compiler/hlds_out_type_table.m:
compiler/hlds_out_util.m:
compiler/implementation_defined_literals.m:
compiler/inst_abstract_unify.m:
compiler/inst_check.m:
compiler/inst_merge.m:
compiler/inst_mode_type_prop.m:
compiler/inst_test.m:
compiler/instmap.m:
compiler/intermod.m:
compiler/intermod_decide.m:
compiler/lco.m:
compiler/ml_global_data.m:
compiler/ml_unify_gen_construct.m:
compiler/ml_unify_gen_deconstruct.m:
compiler/ml_unify_gen_util.m:
compiler/mode_top_functor.m:
compiler/mode_util.m:
compiler/modecheck_coerce.m:
compiler/modecheck_goal.m:
compiler/modecheck_unify.m:
compiler/modecheck_util.m:
compiler/module_qual.qualify_items.m:
compiler/old_type_constraints.m:
compiler/parse_inst_mode_name.m:
compiler/parse_tree_out_item.m:
compiler/parse_tree_to_term.m:
compiler/polymorphism_goal.m:
compiler/polymorphism_lambda.m:
compiler/pre_typecheck.m:
compiler/prog_ctgc.m:
compiler/prog_mode.m:
compiler/prog_rep.m:
compiler/prog_type.m:
compiler/prog_util.m:
compiler/purity.m:
compiler/qual_info.m:
compiler/rbmm.execution_path.m:
compiler/rbmm.region_transformation.m:
compiler/recompilation.usage.m:
compiler/recompilation.used_file.m:
compiler/recompute_instmap_deltas.m:
compiler/simplify_goal.m:
compiler/simplify_goal_call.m:
compiler/simplify_goal_switch.m:
compiler/simplify_goal_unify.m:
compiler/size_prof.m:
compiler/ssdebug.m:
compiler/stack_opt.m:
compiler/structure_reuse.direct_choose_reuse.m:
compiler/structure_reuse.direct_detect_garbage.m:
compiler/superhomogeneous.m:
compiler/table_gen.m:
compiler/term_constr_build.m:
compiler/typecheck_clauses.m:
compiler/typecheck_error_util.m:
compiler/typecheck_errors.m:
compiler/unify_gen_test.m:
compiler/unify_gen_util.m:
compiler/unify_proc.m:
compiler/untupling.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
    Conform to the changes above.

tests/invalid/coerce_int.err_exp:
tests/invalid/coerce_mode_error.err_exp:
tests/invalid/coerce_mode_error2.err_exp:
tests/invalid/coerce_recursive_inst.err_exp:
tests/invalid/coerce_recursive_type.err_exp:
    Expect diagnostics generated using the standard error_spec representations
    of cons_ids.
2024-06-30 21:36:03 +10:00
Zoltan Somogyi
ca932da67e Delete the lambda_eval_method type and its uses.
compiler/prog_data.m:
    The lambda_eval_method type has only ever been used by the Aditi backend,
    and it has been a dummy type since that backend has been deleted.
    Delete this type, and the lambda_eval_method fields from closure_cons
    cons_ids, from the corresponding closure_tag cons_tag, and from
    the representation of higher order types, in order to avoid having to
    maintain code that sets or copies fields that have no purpose.

    The lambda_eval_method type is (now was) separate from the eval method
    stored in proc_infos, which does have alternatives to the normal
    eval method. All of these alternatives are forms of tabling, but
    lambdas cannot be tabled on their own.

compiler/hlds_goal.m:
    Delete the lambda_eval_method field from rhs_lambda_goals.

compiler/*.m:
    Conform to the changes above.
2024-04-23 20:00:06 +10:00
Zoltan Somogyi
319e6e3cfa Rename type_spec_info as type_spec_tables.
compiler/hlds_module.m:
    Rename the type_spec_info type to type_spec_tables to avoid
    confusion (especially in variable names) with the item representing
    type_spec pragmas.

compiler/add_pragma_type_spec.m:
compiler/dead_proc_elim.m:
compiler/higher_order.specialize_calls.m:
compiler/higher_order.specialize_in_module.m:
compiler/intermod.m:
compiler/intermod_analysis.m:
compiler/intermod_decide.m:
compiler/mercury_compile_middle_passes.m:
compiler/recompilation.usage.m:
compiler/term_constr_initial.m:
compiler/unused_args.m:
    Conform to the change above.
2024-04-15 21:52:26 +10:00
Julien Fischer
4cd5c17f61 Fix more copyright notices ...
... and other minor fixes.

library/*.m:
library/LIB_FLAGS.in:
compiler/*.m:
mdbcomp/*.m:
    Fix and update copyright notices.

    Fix spelling.

    Delete trailing whitespace.
2024-02-20 17:18:52 +11:00
Julien Fischer
f5e71b1e90 Fix copyright notices in recently modified files.
compiler/*.m:
library/*.m:
mdbcomp/*.m:
runtime/*.[ch]:
    As above.

    Fix spelling in some spots.
2024-02-20 15:09:17 +11:00
Zoltan Somogyi
bfd61d55e6 Delete old ZZZs. 2024-02-20 04:08:13 +11:00
Zoltan Somogyi
8b5cc5125c Use string builders for .*opt files.
compiler/intermod.m:
compiler/intermod_analysis.m:
    Instead of writing stuff out to .*opt files, add them to string builders
    supplied by the callers.

compiler/mercury_compile_front_end.m:
compiler/mercury_compile_middle_passes.m:
    Use the new code in intermod*.m to handle .*opt files the same way
    we now handle .int* files: by

    - converting the new contents we want for the file to a string,
    - reading in the old version of the file as a string,
    - comparing the two strings,
    - writing out the new version only if it not the same as the old version.
2024-02-18 02:53:19 +11:00
Zoltan Somogyi
afecbafca4 Finish sentence in comment. 2024-02-10 00:43:44 +11:00
Julien Fischer
5da9b454e2 Fix some comments.
compiler/intermod.m:
    As above.
2024-02-09 23:53:58 +11:00
Zoltan Somogyi
5a594d512c Update a module comment. 2024-02-09 21:12:49 +11:00
Zoltan Somogyi
4d0ce5c35a Carve parse_tree_out_item.m out of parse_tree_out.m.
compiler/parse_tree_out.m:
compiler/parse_tree_out_item.m:
    Move the parts of the old parse_tree_out.m that deal with the output
    of individual items, or item components, to the new module
    parse_tree_out_item.m.

compiler/parse_tree.m:
compiler/notes/compiler_design.html:
    Add and document the new module.

compiler/comp_unit_interface.m:
compiler/hlds_out_type_table.m:
compiler/hlds_out_typeclass_table.m:
compiler/intermod.m:
compiler/intermod_analysis.m:
    Import the new module instead of the old.

compiler/indent.m:
compiler/mlds_to_target_util.m:
compiler/parse_tree_out_misc.m:
    Sort imports.
2024-02-08 16:56:47 +11:00
Zoltan Somogyi
d9f5ab5f60 Implement type_spec_constrained_preds pragmas.
This pragma, which has the form shown by this example,

    :- pragma type_spec_constrained_preds(
        [stream.line_oriented(Stream, State),
            stream.unboxed_reader(Stream, char, State, Error),
            stream.putback(Stream, char, State, Error)],
        apply_to_superclasses,
        [subst([Stream => io.text_input_stream,
            State => io.state, Error => io.error])]).

allows programmers to ask the compiler to create type-specialized versions
of all predicates and functions, in a module and its submodules, that have
one or more of the specified typeclass constraints.

The first argument specifies these constraints, of which the example above
has three. The second argument specifies whether the request also applies
to predicates and functions whose signatures include superclasses of the
type classes involved (or *their* superclasses, and so on), not the named
type classes themselves. The third argument specifies the requested list
of one or more specializations, each of which consists of a substitution
that maps one or more type variables to a type. (A type that should not
itself be a type variable.) This example requests just one specialization
for each match.

compiler/prog_item.m:
    Add a new kind of decl_pragma item to represent the new pragma.
    Define the new types it needs.

    Add some documentation of the invariants of existing item kinds.

compiler/prog_data.m:
    Define ground_type, a subtype of mer_type, for use by prog_item.m.

compiler/prog_type_test.m:
    Add a utility predicate for converting a type to a ground_type,
    if it is indeed ground.

compiler/prog_data_pragma.m:
    Change the definition of the type_subst type to use a purpose-specific
    function symbol instead of the pair function symbol.

compiler/parse_pragma.m:
    Add code to parse the new pragma, and to check the parsed form
    to see whether it violates the requirements upon it.

    Modify the code that parses plain old type_spec pragma to also allow
    subst([State = io.state, Error = io.error]) syntax to be used
    to specify type substitutions, as discussed on m-rev.

    Modify the code that parses the old substitution syntax to use
    the infrastructure of the new syntax, which can generate better
    error messages.

    Change this now-common code to allow the two sides of a substitution
    to be separated by either an equal sign (such as "State = io.state")
    or an arrow (such as "Error => io.error").

    Improve the wording of some error messages.

    Delete the code that accepted and then ignored a third argument
    in type_spec pragmas. This code was needed only until a change committed
    in July 2022 finished bootstrapping.

vim/syntax/mercury.vim:
    Add the new pragma's name to the list of pragma keywords.

compiler/add_pragma.m:
compiler/add_pragma_type_spec.m:
    Add code to process the new pragma. Add code to debug the processing
    of the new pragma.

    Change the code that processes the old type_spec pragma,

    - to avoid using mode-specific clauses if the specialization applies
      to the whole predicate and not just one procedure of it (this avoids
      a bug that I reported on m-rev on 2024 feb 2), and

    - to set the context of type-specialized predicates to match the original
      predicates, instead of leaving them set to the default context, which
      is a dummy context (this helps avoid error messages whose lack of context
      can make it hard to figure out what exactly they are complaining about).

compiler/options.m:
doc/user_guide.texi:
    Add an option that add_pragma_type_spec.m now consults to decide
    whether to report the type_spec pragmas generated to implement each
    type_spec_constrained_preds pragma. The documentation of this option
    is commented out. It should be made visible to users once we have
    (a) gathered sufficient experience with the new pragma to have confidence
    in it, and (b) documented the pragma itself.

    Add a way to check whether this diff exists in the compiler.

compiler/handle_options.m:
    Automatically disable the new option for all invocations other than
    those that generate code or check errors, since the output they generate
    would be more distracting than useful e.g. when making .intN files.

compiler/parse_class.m:
    Restructure the code that selects superclass constraints (constraints
    on typeclass declarations) out of the whole set of constraints that
    a predicate that is designed to parse constraints on *predicate*
    declarations has parsed. The two use cases are different, because
    neither inst constraints, nor typeclass constraints involving
    partially-specified types, are allowed in typeclass declarations.
    The restructure allows us to improve the error messages we generate
    if and when any such disallowed constraints are found.

compiler/parse_tree_out_pragma.m:
    Add code to output the new pragma.

    Add code to try to avoid putting redundant parentheses around
    name/arity pairs. It needs new code to be enabled in
    parse_tree_out_sym_name.m to work.

compiler/parse_tree_out_type.m:
    Generalize (and rename) an existing function.

compiler/parse_tree_out_sym_name.m:
    Add code (commented out for now) that can avoid putting redundant
    parentheses around name/arity pairs.

compiler/maybe_error.m:
    Define maybeN for N = 6, to join N = {1,2,3,4,5}, for use by new code
    above.

mdbcomp/sym_name.m:
    Fix a misleading predicate name.

compiler/hlds_class.m:
    Document an invariant.

compiler/hlds_module.m:
    Replace a multi_map with a one_or_more_map, and give a name to the type.

compiler/parse_item.m:
    Fix comments.

compiler/parse_tree_out_inst.m:
    Add a new utility function needed by other code in this diff.

compiler/hlds_out_typeclass_table.m:
    Clarify the typeclass table part of HLDS dumps.

compiler/check_import_accessibility.m:
compiler/check_typeclass.m:
compiler/convert_parse_tree.m:
compiler/equiv_type.m:
compiler/intermod.m:
compiler/item_util.m:
compiler/make_hlds_passes.m:
compiler/make_hlds_separate_items.m:
compiler/module_qual.qual_errors.m:
compiler/module_qual.qualify_items.m:
compiler/parse_type_name.m:
compiler/pred_name.m:
compiler/prog_item_stats.m:
compiler/recompilation.usage.m:
compiler/recompilation.version.m:
mdbcomp/slice_and_dice.m:
    Conform to the changes above.

compiler/prog_type_unify.m:
    Fix indentation.

tests/hard_coded/type_spec_modes.m:
    Modify this test to see whether the code parsing type substitutions
    in type_spec pragmas using the new syntax works.

tests/invalid_nodepend/typeclass_test_3.err_exp:
    Expect the improved error message parse_class.m now generates.

tests/invalid_nodepend/bad_tscp.{m,exp}:
    Add this test case to check the error messages generated by parse_pragma.m
    to report the problems it detects in malformed type_spec_constrained_preds
    pragmas.

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

tests/warnings/test_tscp.{m,exp}:
    Add a test case that checks whether, given a list of the new pragmas,
    the compiler generates the expected set of type_spec pragmas.

tests/warnings/{Mmakefile,Mercury.options}:
    Enable the new test case.
2024-02-07 16:55:00 +11:00
Zoltan Somogyi
a9c76976b4 Make indents unsigned.
compiler/indent.m:
    Make the indent type a synonym for uint.

compiler/*.m:
    Conform to the change above.
2023-11-22 07:46:14 +11:00
Zoltan Somogyi
b4464a4ff7 Enforce either all types only, or all types/modes.
compiler/prog_item.m:
    Predicate declarations have a slot for describing the arguments
    of the predicate (or function). The type of this slot allowed
    the representation of an argument list in which some arguments
    contained only a type, but others contained both a type and a mode.

    Change this to a representation that enforces the invariant that
    - either all arguments contain only types
    - or all arguments contain both types and modes.

    Add a third alternative to this type, which describes predicate
    declarations that contain NO visible arguments. The correct handling
    of this kind of predicate is far from trivial, due to the possible
    presence of with_type and with_inst annotations; adding this alternative
    requires det code dealing with argument lists to include a switch arm
    dealing with this situation specifically.

    Move this types here from prog_data.m, since it is used only during
    the initial construction of the HLDS. Likewise, move a utility operation
    on this type here from prog_util.m.

    Add some other utility operations on this type. One of them,
    get_declared_types_and_maybe_modes, factors out what used to be
    repeated code in other modules. It contains a fix for what seem to be some
    not-tested-for bugs in the old code's handling of arity-zero predicates.
    This fix is commented out due to a limitation in the syntax rules
    of the language. Document this issue.

compiler/parse_item.m:
compiler/parse_type_name.m:
    Enforce the invariant when constructing item_pred_decl_infos.

    Add indentation to an error message (which the test suite does not test).

compiler/add_pred.m:
    Update the logic of when an item_pred_decl_info represents a predmode
    declaration, and thus requires adding a mode to the HLDS as well.

compiler/recompilation.version.m:
    Update the logic of when an item_pred_decl_info represents a predmode
    declaration, and thus requires checking whether the mode information
    has changed since the last version.

compiler/equiv_type.m:
    Update and simplify the code that checks with_type and with_inst
    annotations for consistency.

    Update the wording of an error message. Stop module qualifying
    the predicate name involved in the error, since it is implicit
    in the context of the error message, and therefore hurts more
    as clutter than it helps.

    Fix a misleading predicate name.

compiler/prog_mode.m:
    Change the interface of a predicate to generate an error message
    unconditionally, not conditionally.

    Shorten some predicate names.

compiler/prog_data.m:
compiler/prog_util.m:
    Delete the stuff moved to prog_item.m.

compiler/parse_tree_out_pred_decl.m:
    Fix a quoting issue that are arises if the fix for the handling of
    arity-zero predicates is *not* commented out.

compiler/add_class.m:
compiler/add_pragma_tabling.m:
compiler/add_solver.m:
compiler/canonicalize_interface.m:
compiler/handle_options.m:
compiler/intermod.m:
compiler/make_hlds_passes.m:
compiler/module_qual.qualify_items.m:
compiler/parse_tree_out.m:
compiler/prog_mutable.m:
compiler/recompilation.check.m:
compiler/recompilation.usage.m:
    Conform to the changes above.

tests/invalid_nodepend/test_with_type.err_exp:
    Expect the updated error message from equiv_type.m.

tests/invalid_nodepend/invalid_float_literal.err_exp:
    Expect the fix in declaration formatting from parse_tree_out_pred_decl.m.

tests/recompilation/two_module_debug:
    Update this debug script to help debug this diff.

tests/recompilation/unchanged_with_type_nr_2.m.2:
    Fix an old inadvertent white space change.
2023-11-18 22:20:39 +11:00
Zoltan Somogyi
ffb963b30f Add code to write parse trees to a string.
Traditionally, we always wrote out parse trees (of .intN files, for example)
to a file. However, we have also supported being able to write out *parts*
of parse trees to strings, because that ability is useful e.g.

- in error messages, printing the code that the error message is about,
- when debugging.

We are considering a use case which requires the ability to write out
the *whole* parse tree of a .intN file to a string. That use case is
comparing whether the old and new versions of a .intN file are identical
or not, because we want to update the actual .intN file only if they
differ. (Updating the .intN file if they are identical could trigger
the unnecessary recompilation of an unbounded number of other modules.)

Previously, we have done this comparison by writing out the new parse tree
to an .intN.tmp file, and compared it to the .intN file. It should be simpler
and quite possibly faster to

- read in the old .intN file as a string
- convert the new parse tree to a string
- compare the two strings
- write out the new string if and only if it differs from the old string.

This should be especially so if we can open the .intN file in read-write mode,
so the file would need to be opened just once, in step one, even if we do
need to write out the new parse tree in step four.

compiler/parse_tree_out.m:
    Add functions to convert parse_tree_int[0123]s to strings.

    To avoid having to reimplement all the code that currently writes
    out those parse trees, convert the current predicates that always do I/O
    into predicates that use the methods of the existing pt_output type class,
    which, depending on the selected instance, can either do I/O or can build
    up a string. This conversion has already been done for the constructs
    that make up some parts of those parse trees; this diff extends the
    conversion to every construct that is part of parse trees listed above.

    As part of our existing conventions, predicates that have been
    generalized in this way have the "output" or "write" in their names
    replaced with "format".

    We also perform this generalization for the predicates that write out
    parse_tree_srcs and parse_tree_module_srcs, because doing so requires
    almost no extra code.

compiler/parse_item.m:
compiler/parse_tree_out_clause.m:
compiler/parse_tree_out_info.m:
compiler/parse_tree_out_inst.m:
compiler/parse_tree_out_misc.m:
compiler/parse_tree_out_pragma.m:
compiler/parse_tree_out_pred_decl.m:
compiler/parse_tree_out_type_repn.m:
compiler/prog_ctgc.m:
    Perform the generalization discussed above, both on predicates
    that write out Mercury constructs, and on some auxiliary predicates.

    In a few cases, the generalized versions already existed but were private,
    in which case this diff just exports them.

    In a few cases, rename predicates to avoid ambiguities.

compiler/add_clause.m:
compiler/hlds_out_goal.m:
compiler/hlds_out_pred.m:
compiler/hlds_out_type_table.m:
compiler/hlds_out_typeclass_table.m:
compiler/intermod.m:
compiler/intermod_analysis.m:
    Conform to the changes above.
2023-11-01 19:53:40 +11:00
Zoltan Somogyi
a0967ee14c Carve var_table_hlds.m out of hlds_pred.m.
compiler/hlds_pred.m:
compiler/var_table_hlds.m:
    Move operations on var_tables from hlds_pred.m to the new module
    var_table_hlds.m. They can't go into var_table.m, because that module
    is in the parse_tree module, and cannot access the HLDS.

compiler/hlds.m:
    Add the new module to the HLDS package.

compiler/notes/compiler_design.html:
    Document the new module.

compiler/add_pred.m:
compiler/add_special_pred.m:
compiler/build_mode_constraints.m:
compiler/higher_order.m:
compiler/hlds_out_pred.m:
compiler/intermod.m:
compiler/intermod_analysis.m:
compiler/mode_constraints.m:
compiler/old_type_constraints.m:
compiler/post_typecheck.m:
compiler/prop_mode_constraints.m:
compiler/typecheck.m:
compiler/unify_proc.m:
    Conform to the changes above.
2023-09-01 14:18:24 +10:00
Zoltan Somogyi
b2ba2dd917 Extend and fix the code for dumping insts.
The motivation for this diff is something I saw when I fixed the bug in
equiv_type_hlds.m on aug 13. During the bootcheck to verify the bug fix,
I enabled the end-of-front-end HLDS dump, and I saw that one module,
deep_profiler/display_report.m, had some references to MISSING_INSTs
in its HLDS dump. Since these could be signs of a bug in mode analysis,
I tracked them down. It turns out that the cause was an incompatibility
between the code that existed in error_msg_inst.m before that fix,
and the new code there added by that fix. But ironically, to find that
incompatibility, I first had to extend error_msg_inst.m's functionality
still further, specifically to make it possible to use it to write out insts
in HLDS dumps.

The reason for this need is that that the old code for dumping out insts
left a lot to be desired. It ignored (as in, it never wrote out) some parts
of specific kinds insts, such as the types in typed insts, without which
some parts of HLDS dumps did not make sense. For example, the ground inst
table in the HLDS dump of display_report.m contained several keys whose
printed versions were identical, seemingly indicating a bug in the code
that added new entries to that table (since it is supposed to add a new entry
to the table if the relevant key does not yet exist in the table). It turns
out that there was no bug; the keys differed, but only in the types.

compiler/error_msg_inst.m:
    Fix the incompatibility, and document both it, and its solution.

    Besides error_msg_inst, export a new function error_msg_inst_name,
    which does the same thing for inst_names as error_msg_inst does for insts.

    Add a flag to both functions that specifies whether the intended use
    of the return value is in an error message (whose audience is usually
    an ordinary Mercury user) or a HLDS dump (whose audience is always
    a Mercury developer). Include details such as the types in typed insts,
    and the structure of the compiler-generated inst names generally,
    which involve concepts that users do not know about, in the output
    only if the flag says the audience is Mercury developers.

    When printing out type or inst variables, use their actual names
    if these are available. To make this possible, require the callers
    of error_msg_{inst,inst_name} to provide tvarsets and inst_varsets,
    instead of always using empty varsets. The caller may still pass
    empty varsets if cannot do better than that, but most callers can,
    and now do pass valid varsets.

compiler/hlds_out_goal.m:
    Since we now want to pass a valid tvarset to error_msg_inst.m
    when printing the insts in goals' instmap_deltas, we need to pass around
    the tvarset, as well as the inst_varset, of the procedure that the goal
    was taken from. Instead of adding yet another parameter to all the affected
    predicates, replace all the existing parameters that have the same role
    (of which there were already about half a dozen) with a parameter
    of a new type named hlds_out_info_goal, which contains the values of
    all these old parameters, and the new one.

compiler/hlds_out_inst_table.m:
    Use the same setting to govern whether we use error_msg_inst.m's
    facilities for writing out insts and inst names in both the keys
    and the values of the various inst tables.

compiler/simplify_info.m:
    Include tvarsets in simplify_infos, since dumping out goals during
    simplification pass can now use this information.

compiler/add_clause.m:
compiler/add_mutable_aux_preds.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/direct_arg_in_out.m:
compiler/format_call.m:
compiler/goal_expr_to_goal.m:
compiler/hlds_out_pred.m:
compiler/inst_abstract_unify.m:
compiler/inst_lookup.m:
compiler/intermod.m:
compiler/lco.m:
compiler/liveness.m:
compiler/make_hlds_warn.m:
compiler/mode_errors.m:
compiler/pd_debug.m:
compiler/prog_mode.m:
compiler/push_goals_together.m:
compiler/saved_vars.m:
compiler/simplify_goal.m:
compiler/simplify_goal_conj.m:
compiler/simplify_proc.m:
compiler/stack_opt.m:
compiler/superhomogeneous.m:
compiler/unneeded_code.m:
    Conform to the changes above.
2023-08-31 16:25:31 +10:00
Zoltan Somogyi
0922461444 Include type_ctors in some iftc error conditions.
compiler/hlds_inst_mode.m:
    Modify the set of function symbols in the inst_for_type_ctor (iftc) type,
    in a way that allows the semantics of each function symbol to be descrribed
    exactly.

    Some of the new function symbols preserve the identity of the type_ctor
    originally recorded by the programmer for the inst. This allows the fixing
    of bug in the computation of the set of imported modules used by
    the current module.

compiler/inst_check.m:
    Update the code setting up the value of the iftc slot in inst definitions.

compiler/unused_imports.m:
    Apply that fix.

compiler/add_mode.m:
compiler/intermod.m:
compiler/recompilation.usage.m:
    Conform to the changes in hlds_inst_mode.m.
2023-08-11 15:23:20 +02:00
Zoltan Somogyi
9d38b252bf Separate marker pragmas from other decl/impl pragmas.
compiler/prog_item.m:
    Previously, both decl and impl pragmas contained some pragma kinds
    that contained only the specification of a predicate or function.
    These served only to specify a marker to be applied to the named
    predicate or function.

    This diff separates out those kinds of pragmas from the types of
    both the decl pragmas and the impl pragmas (the difference is that
    decl pragmas may appear in module interfaces, while impl pragmas may not),
    and gives them two new representations: decl markers and impl markers.

    While in the old representation, each kind of marker had its own wrapper
    around the predicate/function specification, in the new representation,
    they are side-by-side, which allows simpler construction techniques
    and smaller code.

    Update the definition of parse_tree_module_src, parse_tree_plain_opt,
    parse_tree_int0 and parse_tree_int1 to include markers alongside
    pragmas of each kind. Use subtypes to restrict the kinds of markers
    that can appear in parse_tree_plain_opts to the set that we actually
    can put into them. (Source files of course can contain any markers,
    and .intN files either get put into them either all of the markers
    that occur in the source file in a given section, or none of them.)

    Delete the item_pragma_info type, which was a wrapper around
    the specific info of each pragma, and stored a context and an item
    sequence number alongside it. Move the context and the item sequence
    number into the representation of each pragma. This should reduce
    visual clutter in the source code at places that construct or deconstruct
    pragmas, and at runtime (with direct args) it should reduce both
    the number of memory cells we need to allocate, and the number
    of pointers we need to follow.

    Include decl vs impl in the names of some function symbols.

    Partly to counteract that, shorten some names to avoid excessive
    line lengths.

compiler/add_pragma.m:
    Add predicates to add decl and impl markers.

    Move the predicates looping over lists of pragma next to the
    predicates handling those pragmas.

compiler/make_hlds_passes.m:
    Add both decl and impl markers before adding foreign_procs.
    The ability to do this was the original motivation for this diff.
    Update the comments both about this issue, and about why we delay
    adding tabling pragmas to the HLDS.

compiler/check_module_interface.m:
    Conform to the changes above.

    Add an XXX about something fishy.

compiler/item_util.m:
    Delete aux functions that are no longer needed.

compiler/add_mutable_aux_preds.m:
compiler/add_pragma_tabling.m:
compiler/add_pragma_type_spec.m:
compiler/comp_unit_interface.m:
compiler/convert_parse_tree.m:
compiler/equiv_type.m:
compiler/get_dependencies.m:
compiler/grab_modules.m:
compiler/hlds_module.m:
compiler/intermod.m:
compiler/intermod_analysis.m:
compiler/make_hlds_separate_items.m:
compiler/mercury_compile_middle_passes.m:
compiler/module_qual.collect_mq_info.m:
compiler/module_qual.qual_errors.m:
compiler/module_qual.qualify_items.m:
compiler/parse_pragma.m:
compiler/parse_pragma_analysis.m:
compiler/parse_pragma_foreign.m:
compiler/parse_pragma_tabling.m:
compiler/parse_tree_out.m:
compiler/parse_tree_out_pragma.m:
compiler/prog_item_stats.m:
compiler/prog_mutable.m:
compiler/recompilation.check.m:
compiler/recompilation.usage.m:
compiler/recompilation.version.m:
compiler/unused_args.m:
    Conform to the changes above.
2023-08-06 12:33:55 +02:00
Zoltan Somogyi
155bc71d72 Make foreign_procs their own top-level item kind.
compiler/prog_item.m:
    Change foreign_procs from being one kind of impl_pragma item
    to being their own item kind. Because of this, the changes to
    some of the modules listed below delete "pragma" from the names
    of predicates and types referring to foreign_procs.

    Include foreign_proc items in parse_tree_module_srcs and
    parse_tree_plain_opts, the two kinds of parse trees that may contain
    foreign_procs.

compiler/make_hlds_separate_items.m:
    Gather foreign procs independently of impl pragmas.

compiler/make_hlds_passes.m:
    Add foreign_procs from the parse_tree_module_src and any
    parse_tree_plain_opts to the HLDS at the same time as we add
    foreign_procs generated by the compiler to implement solver types
    and mutables. Document the reason for this.

    Document also the reason why we should add all marker pragmas
    just before we do this. Document the reason why two tests will fail
    until that, or something similar, is done.

compiler/add_foreign_proc.m:
    Delete a test that was required only because we couldn't guarantee
    the relative order of adding foreign_procs and pragmas that mark
    predicates as external on one backend.

compiler/module_qual.qual_errors.m:
    Add foreign_procs as a possible context for errors during qualification.

compiler/status.m:
    Add a comment documented an old issue.

compiler/add_mutable_aux_preds.m:
compiler/add_pragma.m:
compiler/add_pragma_tabling.m:
compiler/add_solver.m:
compiler/check_module_interface.m:
compiler/comp_unit_interface.m:
compiler/convert_parse_tree.m:
compiler/coverage_profiling.m:
compiler/dep_par_conj.m:
compiler/det_analysis.m:
compiler/equiv_type.m:
compiler/foreign.m:
compiler/get_dependencies.m:
compiler/goal_util.m:
compiler/grab_modules.m:
compiler/hlds_goal.m:
compiler/intermod.m:
compiler/item_util.m:
compiler/ml_foreign_proc_gen.m:
compiler/module_qual.collect_mq_info.m:
compiler/module_qual.qualify_items.m:
compiler/parse_pragma_foreign.m:
compiler/parse_tree_out.m:
compiler/parse_tree_out_pragma.m:
compiler/pragma_c_gen.m:
compiler/prog_item_stats.m:
compiler/prog_mutable.m:
compiler/recompilation.version.m:
compiler/structure_sharing.domain.m:
compiler/table_gen.m:
compiler/tabling_analysis.m:
compiler/term_util.m:
compiler/termination.m:
compiler/trailing_analysis.m:
compiler/prog_data_foreign.m:
compiler/unify_proc.m:
    Conform to the changes above.
2023-08-04 11:42:46 +02:00
Zoltan Somogyi
d302a4e5ca Give some predicates/constructors more meaningful names.
compiler/module_cmds.m:
    Rename the update_interface_X predicates to copy_dot_tmp_to_base_file_X,
    because

    - this better describes what they do, and
    - some of the files they are invoked on are *not* interface files.

    Add an extra parameter to copy_dot_tmp_to_base_file_report_any_error
    that specifies what kind of file is being copied, and print *that*
    as part of any error message.

    Do a similar rename of update_interface_result type, and of its
    function symbols.

    Give more meaningful names to touch_interface_datestamp and
    touch_datestamp as well.

compiler/analysis.file.m:
compiler/analysis.m:
compiler/compile_target_code.m:
compiler/export.m:
compiler/intermod.m:
compiler/make.module_target.m:
compiler/mercury_compile_front_end.m:
compiler/mercury_compile_main.m:
compiler/mercury_compile_middle_passes.m:
compiler/mlds_to_c_file.m:
compiler/recompilation.check.m:
compiler/write_module_interface_files.m:
    Conform to the changes above.
2023-08-02 04:02:24 +02:00
Zoltan Somogyi
3e9a5114ce Carve four new modules out of intermod.m.
Each of the new modules, and intermod.m itself, has better cohesion
than the original intermod.m.

compiler/intermod_decide.m:
    New module that contains the code that decides what to put
    into .opt files.

compiler/intermod_info.m:
    New module that defines the intermod_info type, which records
    the results of those decisions.

compiler/intermod_mark_exported.m:
    New module that contains the code that updates the statuses of entitties
    that we *would* export if the compiler were asked to generate a .opt file,
    but its current task is something else.

compiler/intermod_status.m:
    New module that contains the code that decides whether an entity's
    status allows it to be opt-exported.

compiler/intermod.m:
    Delete the code moved to other modules.

compiler/transform_hlds.m:
    Include the new modules.

compiler/notes/compiler_design.html:
    Document the new modules.

compiler/mercury_compile_front_end.m:
    Conform to the changes above.

compiler/intermod_analysis.m:
    Fix a comment.
2023-07-28 12:43:33 +02:00
Zoltan Somogyi
1d3cd2d0b1 Delete invalid procs from their pred_info.
compiler/hlds_pred.m:
    This module used to maintain a distinction between valid and invalid
    procedures in a pred_info. The distinction was based on whether the
    proc_info field containing a list of mode_error_infos was empty
    (such procedures were valid) or nonempty (such procedures were invalid).

    This field was used only during the early phases of the compiler
    from mode analysis to unique mode analysis, but all later passes
    had to check whether the field was empty before processing the procedure.

    This diff deletes this field from proc_infos. The information that this
    field used to contain is now stored in *temporary* data structures
    maintained and used only by the mode and unique mode analysis phases.
    These phases use the code they share in modes.m to delete all invalid
    procedures from the HLDS before they hand over that HLDS to other phases.
    This means that outside these two compiler phases, *all* procedures in the
    HLDS will be valid.

    Delete all service predicates and functions that tested procedures
    for validity, since this has now become a meaningless test. In one case,
    where there was no non-validity-testing equivalent, make one.

compiler/mode_info.m:
    Define the proc_mode_error_map, which effectively replaces the fields
    deleted from proc_infos. Define the operations on it that we need.

compiler/modes.m:
    Initialize proc_mode_error_maps to empty, and then pass them through
    mode analysis as part of the mode_info, allowing mode analysis code
    to use it to check procedure validity.

    When a mode analysis phase (either ordinary or unique mode analysis)
    is done, delete the procedures that we have now detected are invalid.
    However, before we do, print any inference messages about them.

compiler/unique_modes.m:
    Use the new field in mode_info to check procedures' validity.

    Delete the unique_modes_check_proc predicate, because it had
    only one caller in modes.m, which called another predicate in modes.m
    through it.

compiler/modecheck_call.m:
compiler/modecheck_unify.m:
    Use the new field in mode_info to check procedures' validity.

compiler/try_expand.m:
    Conform to the changes above.

    When a mode check of a procedure repeated after try expansion finds
    an error, delete the now-detected-to-be-invalid procedure.

compiler/cse_detection.m:
    Conform to the changes above.

    When a mode check of a procedure repeated after common subexpression
    eliminate finds an error, don't bother to delete the now-detected-to-be-
    invalid procedure, because the code on that path throws an exception
    anyway.

    Fix an old incongruity: one trace goal created a new ProgressStream
    in a predicate that would have been given existing one if needed.

compiler/direct_arg_in_out.m:
    Conform to the changes above by deleting a validity test.

    Delete a predicate that had no job *except* that validity test.

compiler/style_checks.m:
    Use missing procedure ids to detect invalid procedures. Add an XXX
    about a limitation of this approach.

compiler/bytecode_gen.m:
compiler/dead_proc_elim.m:
compiler/deep_profiling.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/det_analysis.m:
compiler/distance_granularity.m:
compiler/exception_analysis.m:
compiler/float_regs.m:
compiler/goal_mode.m:
compiler/higher_order.m:
compiler/hlds_call_tree.m:
compiler/hlds_dependency_graph.m:
compiler/hlds_out_pred.m:
compiler/intermod.m:
compiler/intermod_analysis.m:
compiler/introduce_parallelism.m:
compiler/lambda.m:
compiler/liveness.m:
compiler/mark_tail_calls.m:
compiler/mercury_compile_front_end.m:
compiler/mercury_compile_llds_back_end.m:
compiler/ml_proc_gen.m:
compiler/passes_aux.m:
compiler/pd_util.m:
compiler/polymorphism.m:
compiler/polymorphism_goal.m:
compiler/proc_gen.m:
compiler/purity.m:
compiler/rbmm.condition_renaming.m:
compiler/rbmm.execution_path.m:
compiler/rbmm.live_region_analysis.m:
compiler/rbmm.live_variable_analysis.m:
compiler/rbmm.points_to_analysis.m:
compiler/rbmm.region_arguments.m:
compiler/rbmm.region_instruction.m:
compiler/rbmm.region_transformation.m:
compiler/ssdebug.m:
compiler/stm_expand.m:
compiler/stratify.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.direct.m:
compiler/structure_reuse.domain.m:
compiler/structure_sharing.analysis.m:
compiler/structure_sharing.domain.m:
compiler/switch_detection.m:
compiler/table_gen.m:
compiler/tabling_analysis.m:
compiler/term_constr_initial.m:
compiler/termination.m:
compiler/trailing_analysis.m:
compiler/untupling.m:
compiler/unused_args.m:
    Conform to the changes above, mostly by deleting validity tests.
2023-07-18 14:03:14 +02:00
Zoltan Somogyi
b6ec42a132 Make some arities into pred_form_arities.
compiler/hlds_pred.m:
    Replace the arity field in pred_infos with a pred_form_arity field.

    Move the pred_info's pred_or_func field to its usual position
    in predicate/function descriptions: at the front (pred/func name/arity).

compiler/hlds_pred.m:
    Change two utility operations to return pred_form_arities instead of
    just arities, since they get them from pred_infos.

compiler/inst_mode_type_prop.m:
compiler/llds.m:
compiler/rtti.m:
    Change some fields whose types used to be arity (or int) to be
    pred_form_arity.

    In llds.m, include a pred_or_func field in c_procedures,
    for use in procedure-start comments.

mdbcomp/prim_data.m:
mdbcomp/program_representation.m:
    Add notes about two possible future improvements along similar lines.

compiler/prog_data.m:
    Add a utility function to calculate the number of extra arguments
    added to predicates/functions by compiler passes such as polymorphism.

compiler/add_pragma.m:
    Conform to the changes above.

    Fix a bug in an error message about ":- external" pragmas:
    the message used the pred_form arity instead of the user arity.
    (See the diff to external2.err_exp below.)

compiler/hlds_defns.m:
    Conform to the changes above.

    Include pred/func prefixes before name/arity pairs in the output
    where relavnt. (The user guide does not need to be updated, because
    its wording permits both the old and the new behavior.)

    Fix two separate bugs that referred to functions in user-facing output
    with the predicate form of their arity.

compiler/table_gen.m:
compiler/unused_args.m:
    Conform to the changes above.

    Fix a bug in each module that referred to functions in user-facing output
    with the predicate form of their arity.

compiler/recompilation.usage.m:
compiler/xml_documentation.m:
    Conform to the changes above.

    Mark a probable bug in each module with an XXX.

compiler/direct_arg_in_out.m:
    Conform to the changes above.

    Improve the wording of an error message a bit.
    (See the diff to gh72_errors.err_exp below.)

compiler/accumulator.m:
compiler/bytecode_gen.m:
compiler/complexity.m:
compiler/default_func_mode.m:
compiler/det_report.m:
compiler/distance_granularity.m:
compiler/equiv_type_hlds.m:
compiler/exception_analysis.m:
compiler/higher_order.m:
compiler/hlds_defns.m:
compiler/hlds_error_util.m:
compiler/hlds_module.m:
compiler/intermod.m:
compiler/intermod_order_pred_info.m:
compiler/introduce_exists_casts.m:
compiler/introduce_parallelism.m:
compiler/llds_out_file.m:
compiler/mercury_compile_llds_back_end.m:
compiler/ml_accurate_gc.m:
compiler/ml_args_util.m:
compiler/mode_errors.m:
compiler/modecheck_util.m:
compiler/modes.m:
compiler/old_type_constraints.m:
compiler/optimize.m:
compiler/polymorphism.m:
compiler/polymorphism_goal.m:
compiler/post_typecheck.m:
compiler/pre_typecheck.m:
compiler/pred_table.m:
compiler/proc_gen.m:
compiler/rbmm.region_transformation.m:
compiler/recompilation.usage.m:
compiler/rtti_out.m:
compiler/rtti_to_mlds.m:
compiler/simplify_goal_call.m:
compiler/ssdebug.m:
compiler/table_gen.m:
compiler/tabling_analysis.m:
compiler/term_constr_initial.m:
compiler/termination.m:
compiler/trailing_analysis.m:
compiler/transform_llds.m:
compiler/tupling.m:
compiler/type_class_info.m:
compiler/typecheck.m:
compiler/typecheck_error_undef.m:
compiler/types_into_modes.m:
compiler/xml_documentation.m:
    Conform to the changes above.

compiler/recompilation.m:
    Add a note.

compiler/parse_tree_out_sym_name.m:
    Improve variable names.

tests/invalid/external2.err_exp:
    Expect the fixed arity in an error message.

tests/invalid/gh72_errors.err_exp:
    Expect the expanded text of an error message.
2023-07-03 01:57:10 +02:00
Zoltan Somogyi
7292ecb571 Don't generate duplicate instance declarations.
We used to include in the .int0 file the abstract form of all the instance
declarations in both the interface and the implementation sections.
When an instance is declared (in an already-abstract form) in the interface
section and defined in the implementation section, this resulted in the
abstract interface declaration being included in the .int0 file twice,
once in the interface section, and once in the implementation section.

compiler/comp_unit_interface.m:
    Fix this by including an abstract instance declaration in the
    implementation section of a .int0 file only if it does not also appear
    in the interface section.

    Conform to the changes in prog_item.m below.

compiler/parse_tree_out.m:
    To help implement the above test, add a function to return the string
    form of an abstract instance declaration.

    It is easy to make this change for *abstract* instance declarations,
    but not *concrete* instance definitions, because (in order to handle
    instances that define methods by clauses, instead of by pred/func names)
    the latter would require generalizing *all* the code for writing out
    clauses, with all the overhead associated with replacing first order calls
    with method calls.

    Another change (unrelated to the problem above) is to write out
    typeclass and instance definitions for typeclasses with no methods
    in a nicer form. Instead of looking like this:

        :- instance classname(argtypes) where [

        ].

    they now look like this:

        :- instance classname(argtypes) where [].

    Another formatting change unrelated to the above: don't put parentheses
    around typeclass names in instance declarations/definitions if the name
    is all alphanumeric and not an operator.

    Conform to the changes in prog_item.m below.

compiler/prog_data.m:
compiler/prog_item.m:
    To be able to use the new code to convert abstract instances to strings
    in comp_unit_interface, and to write out abstract instance declarations
    inside .int0 (and other .intN) files, it helps to know which instance
    items can only be abstract in these files. As it turns out, none can be
    concrete. So define a subtype of item_instance_info that can contain
    only abstract instance declarations, and use it to replace
    item_instance_info in parse_tree_intN for all N.

compiler/parse_tree_out_info.m:
    Add a utility function for new code in comp_unit_interface.m
    invoking new code in parse_tree_out.m.

compiler/convert_parse_tree.m:
    Conform to the changes in prog_item.m by insisting that instances
    read in from .intN files are all abstract.

    Fix some typos in some error messages (which people can see only if
    something has screwed up a .intN file).

compiler/equiv_type.m:
compiler/get_dependencies.m:
compiler/make_hlds_separate_items.m:
compiler/module_qual.collect_mq_info.m:
compiler/module_qual.qualify_items.m:
compiler/recompilation.version.m:
    Conform to the changes above.

compiler/intermod.m:
    Simplify some code.

tests/invalid/Mercury.options:
    Fix the failure of the instances_pc.instances_pc_helper_1 test case
    when intermodule optimization is enabled for a bootcheck by disabling
    intermodule optimization for this test. The old code to do so didn't
    work due to typos.

tests/invalid/instances_pc.instances_pc_helper_1.err_exp:
    Expect a reference to only ONE old instance declaration in
    instances_pc.int0. Expect it at a new line number, due to the first
    formatting change above.

tests/misc_tests/pretty_print_test.exp:
    Expect no unnecessary parentheses around a class name in an
    instance definition, due to the second formatting change.
2023-06-30 09:34:01 +02:00
Zoltan Somogyi
a47de48c4d s/input_stream/text_input_stream/ ...
... and the same for output streams.
2023-04-24 14:59:20 +10:00
Zoltan Somogyi
b6178ef723 Delete prog_out.m, moving its code to other modules.
compiler/parse_tree_out_cons_id.m:
    Move the predicates and functions in prog_out.m that deal with cons_ids
    to this module.

compiler/parse_tree_out_sym_name.m:
    Move the predicates and functions in prog_out.m that deal with sym_names
    and similar entities to this module.

compiler/parse_tree_out_type.m:
    Move the predicates and functions in prog_out.m that deal with types
    to this module.

compiler/parse_tree_out_misc.m:
    Move the predicates and functions in prog_out.m that deal with simple
    types to this module.

    Delete mercury_output_det and mercury_format_det, replacing all their
    uses with calls to mercury_det_to_string.

compiler/prog_out.m:
    Delete this module.

compiler/parse_tree.m:
    Delete prog_out from the parse_tree package.

compiler/Mercury.options:
compiler/notes/compiler_design.html:
    Delete references to prog_out.m.

compiler/*.m:
    Update imports and any explicit module qualifications to account
    for the moved code.

tools/filter_sort_imports:
    Automatically filter out any repeated imports. This can help with
    changes like this that redistribute the contents of one module to other
    modules. In this case, after a global replacement of prog_out's import
    with the import of parse_tree_out_misc, this updated script could
    remove this changed import from modules that already imported
    parse_tree_out_misc.
2023-04-09 16:23:13 +10:00
Zoltan Somogyi
e2a8a8cbfa Break up mercury_to_mercury.m.
compiler/mercury_to_mercury.m:
    Delete this module, and replace it with ...

compiler/parse_tree_out_cons_id.m:
compiler/parse_tree_out_sym_name.m:
compiler/parse_tree_out_type.m:
compiler/parse_tree_out_misc.m:
    ... these four modules. The first three write out the entities
    in their names: cons_ids, sym_names, and types. The fourth contains
    the rest of the old mercury_to_mercury.m, plus a few predicates
    moved there from prog_out.m that deal with indentation.

compiler/parse_tree.m:
    Include the four new modules, and stop including the deleted module.

compiler/notes/compiler_design.html:
    Document the new modules.

compiler/prog_out.m:
    Delete the code moved to parse_tree_out_misc.m.

compiler/*.m:
    Adjust the imports as needed. Most modules need only one, maybe two
    of mercury_to_mercury's four successor modules.
2023-04-06 15:32:48 +10:00
Zoltan Somogyi
18817d62d0 Record more than a pred_proc_id for each method.
Class and instance definitions both contain lists of methods,
predicates and/or functions, that each have one or more procedures.
Until now, we represented the methods in class and instance definitions
as lists of nothing more than pred_proc_ids. This fact complicated
several operations,

- partly because there was no simple way to tell which procedures
  were part of the same predicate or function, and

- partly because the order of the list is important (we identify
  each method procedure in our equivalent of vtables with a number,
  which is simply the procedure's position in this list), but there was
  absolutely no information about recorded about this.

This diff therefore replaces the lists of pred_proc_ids with lists of
method_infos. Each method_info contains

- the method procedure number, i.e. the vtable index,

- the pred_or_func, sym_name and user arity of the predicate or function
  that the method procedure is a part of, to make it simple to test
  whether two method_infos represent different modes of the same predicate
  or function, or not,

- the original pred_proc_id of the method procedure, which never changes,
  and

- the current pred_proc_id, which program transformations *can* change.

compiler/hlds_class.m:
    Make the change above in the representations of class and instance
    definitions.

    Put the fields of both types into a better order, by putting
    related fields next to each other.

    Put a notag wrapper around method procedure numbers to prevent
    accidentally mixing them up with plain integers.

    Add some utility functions.

compiler/prog_data.m:
    Replace three fields containing pred_or_func, sym_name and arity
    in the parse tree representation of instance methods with just one,
    which contains all three pieces of info. This makes it easier to operate
    on them as a unit.

    Change the representation of methods defined by clauses from a list
    of clauses to a cord of clauses, since this supports constant-time
    append.

compiler/hlds_goal.m:
    Switch from plain ints to the new notag representation of method
    procedure numbers in method call goals.

compiler/add_class.m:
    Simplify the code for adding new classes to the HLDS.

    Give some predicates better names.

compiler/check_typeclass.m:
    Significantly simplify the code for that generates the pred_infos and
    proc_infos implementing all the methods of an instances definition,
    and construct lists of method_infos instead of lists of pred_proc_ids.

    Give some predicates better names.

    Some error messages about problems in instance definitions started with

        In instance declaration for class/arity:

    while others started with

        In instance declaration for class(module_a.foo, module_b.bar):

    Replace both with

        In instance declaration for class(foo, bar):

    because it contains more useful information than the first, and less
    non-useful information than the second. Improve the wording of some
    error messages.

    Factor out some common code.

compiler/prog_mode.m:
compiler/prog_type.m:
compiler/prog_util.m:
    Generalize the existing predicates for stripping "builtin.m" module
    qualifiers from sym_names, cons_ids, insts, types and modes
    to allow also the stripping of *all* module qualifiers. This capability
    is now used when we print an instance's type vector as a context
    for diagnostics about problems inside instance definitions.

compiler/add_pred.m:
    Add a mechanism for returning the pred_id of a newly created pred_info,
    whether or not it was declared using a predmode declaration. This
    capability is now needed by add_class.m.

    Move the code creating an error message into its own function, and export
    that function for add_class.m.

compiler/polymorphism_type_info.m:
    Fix some comment rot.

compiler/base_typeclass_info.m:
compiler/call_gen.m:
compiler/dead_proc_elim.m:
compiler/deep_profiling.m:
compiler/direct_arg_in_out.m:
compiler/error_msg_inst.m:
compiler/float_regs.m:
compiler/get_dependencies.m:
compiler/higher_order.m:
compiler/hlds_error_util.m:
compiler/hlds_out_goal.m:
compiler/hlds_out_typeclass_table.m:
compiler/instance_method_clauses.m:
compiler/intermod.m:
compiler/make_hlds_error.m:
compiler/ml_call_gen.m:
compiler/mode_errors.m:
compiler/modes.m:
compiler/module_qual.qualify_items.m:
compiler/old_type_constraints.m:
compiler/parse_class.m:
compiler/parse_tree_out.m:
compiler/parse_tree_out_inst.m:
compiler/polymorphism_post_copy.m:
compiler/polymorphism_type_class_info.m:
compiler/prog_item.m:
compiler/prog_rep.m:
compiler/recompilation.usage.m:
compiler/state_var.m:
compiler/type_class_info.m:
compiler/typecheck_debug.m:
compiler/typecheck_error_type_assign.m:
compiler/typecheck_errors.m:
compiler/typecheck_msgs.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
    Conform to the changes above.

tests/invalid/bug476.err_exp:
tests/invalid/tc_err1.err_exp:
tests/invalid/tc_err2.err_exp:
tests/invalid/typeclass_bogus_method.err_exp:
tests/invalid/typeclass_missing_mode.err_exp:
tests/invalid/typeclass_missing_mode_2.err_exp:
tests/invalid/typeclass_mode.err_exp:
tests/invalid/typeclass_mode_2.err_exp:
tests/invalid/typeclass_mode_3.err_exp:
tests/invalid/typeclass_mode_4.err_exp:
tests/invalid/typeclass_test_10.err_exp:
tests/invalid/typeclass_test_3.err_exp:
tests/invalid/typeclass_test_4.err_exp:
tests/invalid/typeclass_test_5.err_exp:
tests/invalid/typeclass_test_9.err_exp:
    Expect the updated wording of some error messages.
2022-11-22 02:27:33 +11:00
Zoltan Somogyi
07f877bc3f Carve term_context.m out of term.m.
library/term.m:
library/term_context.m:
    As above.

    Rename the term.context type as term_context.term_context, with
    term.context now being defined as an equivalence type.

    Replace the context_init function and predicate and the dummy_context_init
    function with just one function: dummy_context. This name includes
    the important part (the fact that it return a *dummy* context) and deletes
    the nonimportant part (dummy contexts are just about never updated,
    so the function does not really "initialize" them).

    Reduce function/predicate pairs that do the same thing to just a function.

library/MODULES_DOC:
library/library.m:
    Add the new module to the list of standard library modules.

NEWS:
    Mention the new module, and the obsoleting of the moved predicates
    and functions in term.m.

compiler/*.m:
library/*.m:
    Conform to the changes above.
2022-08-23 12:56:37 +10:00
Zoltan Somogyi
3e2cde2c33 Don't abort compilation for unresolved overloading.
compiler/pred_table.m:
    When we find more than one match for a predicate or function signature,
    we used to generate an error message, print it, and then abort
    compilation. Fix this by returning the error message to be handled
    by our callers.

    The compiler now has to pick one of the matches to continue with.
    The compiler's pick (the first match) may not be the match intended
    by the programmer, and it is possible that the compiler will later generate
    some error messages that would not happen if we picked the match
    intended by the programmer. We therefore warn the programmer about
    this possibility.

compiler/purity.m:
compiler/resolve_unify_functor.m:
    Include the error messages now returned by pred_table.m among
    all the other error messages generated by the purity pass,
    which, amongst other things, does the last part of the job
    of the typechecker.

compiler/intermod.m:
    Ignore the errors caused by unresolved overloading when generating
    .opt files. They will be reported when the compiler tries to generate
    target language code.

library/list.m:
    Add three related utility predicates that we discussed earlier,
    each of which was needed by one version or another of the code
    for constructing the error message in pred_table.m.

NEWS:
    Announce the additions to list.m.

tests/invalid_submodules/unresolved_overloading.{m,err_exp}:
    Expect the updated error message, and the *absence* of a compiler abort.
2022-08-21 15:28:55 +10:00
Zoltan Somogyi
752bb66f5b Carve four modules out of term.m.
Most modules that imported the old term.m need only a small subset
of its functionality. After this diff, most modules that used to import
term.m will need to import just one more module, and will import many
fewer predicates and functions in total.

library/term_int.m:
    A new module carved out of term.m containing the predicates
    that recognize terms containing integers, and the functions
    that construct such terms.

    While this job has *some* similarity to the job of the existing
    term_conversion.m module, most modules in the compiler use only one
    of those two modules, so merging them would not be a good idea.

library/term_subst.m:
    A new module carved out of term.m containing code to do
    substitutions and renames of various kinds.

    Rename the occurs predicate as var_occurs_in_subst_term,
    and occurs_list as var_occurs_in_subst_terms, since the latter
    are much more descriptive. Change the argument order to match
    the new names (var, subst, term/terms), as the old one did not
    even have the term/terms and the substitution next to each other,
    even though neither makes sense without the other.

library/term_unify.m:
    A new module carved out of term.m containing code to do unifications.

    Give all the predicates more meaningful names:

    unify_term ->                   unify_terms
    unify_term_list ->              unify_term_lists
    unify_term_dont_bind ->         unify_terms_dont_bind
    unify_term_list_dont_bind ->    unify_term_lists_dont_bind
    list_subsumes ->                first_term_list_subsumes_second

library/term_vars.m:
    A new module carved out of term.m containing code that find variables
    in terms.

    Give all the predicates more meaningful names:

    vars ->                         vars_in_term
    vars_2 ->                       vars_in_term_acc
    vars_list ->                    vars_in_terms
    contains_var ->                 term_contains_var
    contains_var_list ->            terms_contain_var

    Don't move the function version of vars_2 to term_vars.m, effectively
    deleting it, since operations that update an accumulator are awkward
    for functions.

library/term.m:
    Keep the moved predicates and functions in term.m, but

    - change their implementation to simply call the moved copy, and
    - obsolete the original in favor of the moved copy.

    Eventually, after either the next release or the release after the next,
    we should delete the obsoleted predicates and functions.

library/MODULES_DOC:
library/library.m:
    Add the new modules as documented parts of the standard library.

browser/interactive_query.m:
compiler/analysis.file.m:
compiler/det_util.m:
compiler/hlds_out_goal.m:
compiler/hlds_out_pred.m:
compiler/hlds_out_util.m:
compiler/intermod.m:
compiler/make.module_dep_file.m:
compiler/make_hlds_passes.m:
compiler/parse_class.m:
compiler/parse_inst_mode_defn.m:
compiler/parse_item.m:
compiler/parse_mutable.m:
compiler/parse_pragma.m:
compiler/parse_pragma_analysis.m:
compiler/parse_sym_name.m:
compiler/parse_tree_to_term.m:
compiler/parse_type_defn.m:
compiler/parse_util.m:
compiler/prog_ctgc.m:
compiler/prog_util.m:
compiler/recompilation.used_file.m:
compiler/recompilation.version.m:
compiler/superhomogeneous.m:
compiler/switch_detection.m:
compiler/typecheck.m:
library/io.m:
library/term_conversion.m:
library/varset.m:
    Conform to the changes above.
2022-08-21 01:01:21 +10:00
Zoltan Somogyi
f7355f708b Move var_db and var_{name,type}_source to var_db.m.
In the process, restrict the use of these types to just those modules
that need to provide services both other modules that use var_tables,
and modules that do not. The latter are modules that either can,
or always do, execute before var_tables are set up.

In some cases, code that previously operated on e.g. var_name_sources
had all its callers converted to use var_tables. In those cases, replace
the use of var_name_sources with var_tables directly.

In other cases, code that previously operated on e.g. var_name_sources
still has some callers that use varsets. In those cases,

- provide versions using var_tables if most callers use var_tables,
  renaming predicates/functions to make this the version seem the default,

- leave the operation to work on var_name_sources if some its callers
  also have only var_name_sources,

- if there are no such callers, keep just the two versions operating
  on varsets and var_tables respectively.

compiler/var_db.m:
compiler/var_table.m:
    Move the part of var_table.m that contains the definitions
    of the above three types, and the operations on them, to the
    new module var_db.m. Only 25 modules currently need var_db.m,
    compared to 176 for var_table.m.

compiler/parse_tree.m:
compiler/notes/compiler_design.html:
    Add and document the new module.

compiler/parse_tree_out_term.m:
    Add a "_vs" suffix to the names of operations that print variables
    or terms using varsets, and reuse their old names for versions
    that use var_table arguments. For the operations that need var_name_source
    versions, make it just select between the varset and var_table versions.

compiler/hlds_out_goal.m:
compiler/instmap.m:
compiler/hlds_out_mode.m:
compiler/hlds_out_util.m:
compiler/hlds_pred.m:
compiler/pd_info.m:
    Change some predicates that used to operate on var_{name,type}_sources
    to operate on var_tables.

    In hlds_out_mode.m, delete some unused predicates, and stop exporting
    a predicate whose only caller is local.

compiler/*.m:
    Conform to the changes above.
2022-08-19 10:44:39 +10:00
Zoltan Somogyi
d787ee9355 Store var_tables in proc_infos.
This fixes the performance problem reported in Mantis bug #562.

compiler/hlds_pred.m:
    Instead of storing a varset and a vartypes in each proc_info,
    store just a var_table. Update the predicates that create
    or clone procedures accordingly.

    Where we had operations on proc_infos that had two versions,
    one operating on a varset/vartypes pair and one operating on var_table,
    keep only the latter, with the (shorter) name of the former.

    Delete the arity argument of proc_info_init, because the only
    valid value of that argument is the length of the list of the
    argument types. (In other words, this arg has been redundant
    all along.)

    Change the operations that create new variables in a procedure
    to get the caller to specify the (base) name of the new variable
    up front.

    Delete the unused predicate proc_info_ensure_unique_names.

compiler/type_util.m:
    Due to the change above, we now construct var_tables during the
    construction of the HLDS. The code that does that needs to fill in
    the field that says whether the type of each variable in the table
    is a dummy type or not. However, at this time, the pass that decides
    type representations has not been run yet. The code of is_type_a_dummy
    used to throw an exception in such situations.

    Change this so that in such situations, is_type_a_dummy returns
    a placeholder, not-guaranteed-to-be-correct value. Document why
    this is ok.

compiler/post_typecheck.m:
    Replace the placeholder values in vte_is_dummy fields in all
    the entries in the var_tables in all (valid) predicates with valid data.
    (If there are any invalid predicates, the compilation will fail anyway.)
    The clause_to_proc pass will copy these updated var_tables
    to be the initial var_tables in procedures.

compiler/make_goal.m:
    Change the operations that create new variables in a procedure
    to get the caller to specify the (base) name of the new variable
    up front. This is simpler than the old method, which created new
    variables without a name, and had the caller give them a name as
    a separate operation. And since var_tables need this info,
    get the caller to also specify whether the type is a dummy,
    if the type is not a builtin type which is known not to be a dummy.

compiler/var_table.m:
    Document the times when the types and is_dummy fields in var_table
    entries become meaningful.

    Fix a potential bug: when performing type substitutions in
    var_table entries, updating a variable's type may change whether
    that variable is a dummy or not, so recompute that info.
    It is quite possible that we *never* replace a nondummy type
    with a dummy type or vice versa, but in the absence of a convincing
    correctness argument for that proposition, better safe than sorry.

    Export the previously-private predicate transform_var_table
    to post_typecheck.

    Add code to implement the unused predicate deleted from hlds_pred.m:
    at the time I wrote it, I haven't yet realised that it was unused.
    The code I wrote here is therefore unused as well, so it is commented out.
    I did not delete it, because it may be useful later on.

compiler/direct_arg_in_out.m:
    Don't make and split var_tables, since it is no longer needed.

compiler/accumulator.m:
compiler/add_class.m:
compiler/add_clause.m:
compiler/add_heap_ops.m:
compiler/add_pred.m:
compiler/add_special_pred.m:
compiler/add_trail_ops.m:
compiler/arg_info.m:
compiler/build_mode_constraints.m:
compiler/bytecode_gen.m:
compiler/check_typeclass.m:
compiler/clause_to_proc.m:
compiler/closure_analysis.m:
compiler/code_gen.m:
compiler/code_loc_dep.m:
compiler/complexity.m:
compiler/continuation_info.m:
compiler/cse_detection.m:
compiler/ctgc.livedata.m:
compiler/deep_profiling.m:
compiler/default_func_mode.m:
compiler/deforest.m:
compiler/delay_construct.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/det_analysis.m:
compiler/det_report.m:
compiler/distance_granularity.m:
compiler/equiv_type_hlds.m:
compiler/exception_analysis.m:
compiler/float_regs.m:
compiler/follow_code.m:
compiler/goal_mode.m:
compiler/goal_path.m:
compiler/higher_order.m:
compiler/hlds_out_pred.m:
compiler/hlds_rtti.m:
compiler/hlds_statistics.m:
compiler/inlining.m:
compiler/intermod.m:
compiler/intermod_analysis.m:
compiler/introduce_exists_casts.m:
compiler/introduce_parallelism.m:
compiler/lambda.m:
compiler/lco.m:
compiler/live_vars.m:
compiler/liveness.m:
compiler/loop_inv.m:
compiler/mark_tail_calls.m:
compiler/ml_accurate_gc.m:
compiler/ml_args_util.m:
compiler/ml_closure_gen.m:
compiler/ml_gen_info.m:
compiler/ml_proc_gen.m:
compiler/mode_errors.m:
compiler/mode_info.m:
compiler/modecheck_goal.m:
compiler/par_loop_control.m:
compiler/pd_debug.m:
compiler/pd_info.m:
compiler/pd_util.m:
compiler/polymorphism_info.m:
compiler/post_typecheck.m:
compiler/proc_gen.m:
compiler/proc_requests.m:
compiler/purity.m:
compiler/push_goals_together.m:
compiler/quantification.m:
compiler/rbmm.add_rbmm_goal_infos.m:
compiler/rbmm.live_variable_analysis.m:
compiler/rbmm.points_to_analysis.m:
compiler/rbmm.points_to_graph.m:
compiler/rbmm.points_to_info.m:
compiler/rbmm.region_liveness_info.m:
compiler/rbmm.region_transformation.m:
compiler/recompute_instmap_deltas.m:
compiler/saved_vars.m:
compiler/simplify_goal_unify.m:
compiler/simplify_info.m:
compiler/simplify_proc.m:
compiler/size_prof.m:
compiler/ssdebug.m:
compiler/stack_alloc.m:
compiler/stack_layout.m:
compiler/stack_opt.m:
compiler/stm_expand.m:
compiler/store_alloc.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.direct.choose_reuse.m:
compiler/structure_reuse.direct.detect_garbage.m:
compiler/structure_reuse.domain.m:
compiler/structure_reuse.indirect.m:
compiler/structure_reuse.lbu.m:
compiler/structure_reuse.lfu.m:
compiler/structure_reuse.versions.m:
compiler/structure_sharing.analysis.m:
compiler/structure_sharing.domain.m:
compiler/switch_detection.m:
compiler/table_gen.m:
compiler/tabling_analysis.m:
compiler/term_constr_build.m:
compiler/term_constr_initial.m:
compiler/term_errors.m:
compiler/term_pass1.m:
compiler/term_pass2.m:
compiler/trace_gen.m:
compiler/trailing_analysis.m:
compiler/try_expand.m:
compiler/tupling.m:
compiler/unneeded_code.m:
compiler/untupling.m:
compiler/unused_args.m:
compiler/unused_imports.m:
    Conform to the changes above. Mostly this means

    - not passing a module_info to get a var_table out of a proc_info, but
    - having to pass a module_info to code that either constructs a var_table,
      or adds entries to a var_table (since we now need the type table
      to figure out whether variables' types are dummies).
2022-08-18 18:53:15 +10:00
Zoltan Somogyi
26be4bb8e3 Add --warn-ambiguous-pragmas.
The new option is on by default, and it generates a warning about pragmas
that specify a name/arity pair that matches both a predicate and a function,
but does not say which one it is intended to apply to.

compiler/options.m:
doc/user_guide.texi:
    Add the new option.

NEWS:
    Announce the new option.

compiler/add_pragma.m:
    Implement the new option.

    Move to one predicate, get_matching_pred_ids, the code for dealing with
    both the absence of any matches, and the presence of more than one match,
    when looking up name/arity pairs in the predicate table. This allows us
    to delete the mostly-duplicate code fragments that did the same thing
    in all of get_matching_pred_ids's callers.

    Simplify the handling of conflicts between marker pragmas.

compiler/hlds_pred.m:
    Separate out no_inline decisions made by the compiler itself from
    the similar decisions made by users, in order to allow that simplification.

    Move the two inline markers next to each other.

compiler/make_hlds_error.m:
    Change a predicate called from add_pragma*.m to take user arities
    in their semantic form, not their int form, which raises the level
    of the predicate's interface, and factors out duplicate code in its
    callers.

compiler/add_pragma_type_spec.m:
compiler/intermod.m:
compiler/table_gen.m:
    Conform to the changes above.

doc/reference_manual.texi:
    Document pred(...) and func(...) wrappers around name/arity pairs
    in pragmas.

    Delete the commented out section on reserve_tag pragmas, which were
    deleted a while ago.

tests/invalid/inline_conflict.m:
    Expand this test case to test the *absence* of a warning for an
    ambiguous pragma with --no-warn-ambiguous-pragma.

tests/invalid/inline_conflict_warn.{m,err_exp}:
tests/invalid/Mercury.options:
    Add a copy of the inline_conflict test case to test the presence of
    a warning for an actually ambiguous pragma.

tests/invalid_nodepend/fact_table_in_interface.err_exp:
    Expect the updated wording of a warning.
2022-07-31 18:17:02 +10:00