Commit Graph

11122 Commits

Author SHA1 Message Date
Zoltan Somogyi
37cfcf3726 Make the structure of a predicate explicit. 2026-02-04 04:17:10 +11:00
Zoltan Somogyi
ad942553b7 Fix too-long lines. 2026-02-04 04:16:37 +11:00
Zoltan Somogyi
de16b3f10e Fix comment. 2026-02-02 05:16:41 +11:00
Zoltan Somogyi
4542c7f24f Fix indentation. 2026-02-02 00:27:24 +11:00
Zoltan Somogyi
291879c8bb Move checks for type_infos to add_foreign_proc.m.
This allows to perform those checks for *all* foreign_procs,
not just the ones that get added to the HLDS.

compiler/add_foreign_proc.m:
    Move the code that checks the bodies of foreign_procs for the
    presence of type_info variables for existentially quantified
    type variables here from typecheck.m and typecheck_errors.m.
    Change the diagnostic's wording to match our new phraseology.

    Record identifiers in a set, not a list, for faster membership tests,
    since we now do even more of them.

compiler/foreign.m:
    Provide a mechanism to return the identifiers not just in the
    non-comment parts of foreign_procs, but the comment parts as well,
    since add_foreign_proc.m now needs this functionality.

compiler/make_hlds_warn.m:
    Conform to the change in foreign.m.

compiler/typecheck.m:
compiler/typecheck_errors.m:
    Delete the code that was moved (in a modified form)
    to add_foreign_proc.m.

compiler/ml_foreign_proc_gen.m:
    Update a reference in a comment.

tests/invalid/exist_foreign_error.err_exp:
    Expect the updated wording of the affected diagnostics,
    and expect diagnostics for *all* the foreign_procs in the test,
    regardless of which language they are for.

tests/invalid/exist_foreign_error.err_exp2:
tests/invalid/exist_foreign_error.err_exp3:
    Delete these files, since the output they expect is now
    in the .err_exp file.

tests/invalid/fp_dup_bug.err_exp2:
tests/invalid/fp_dup_bug.err_exp3:
tests/invalid/gh72_errors.err_exp2:
tests/invalid/gh72_errors.err_exp3:
    Expect the updated wording of diagnostics affected by previous
    changes (which updated the .err_exp files for C, not these for
    Java and C#).
2026-01-31 16:31:04 +11:00
Zoltan Somogyi
8362e1ca66 Centralize control of add_foreign_proc.m diagnostics.
compiler/add_foreign_proc.m:
    Create a single predicate, check_foreign_proc to invoke all the
    other predicates that each generate one kind of diagnostic
    about foreign_procs. Inline in it all the other predicates
    that previously did part of its job.

    Do not let the lack of a proc_id stop the invocation of any
    of those predicates, except the single one that needs the proc_id
    for its work.

    Also, make this the only place that decides how the foreign_proc
    being for an imported predicate affects what we do.

    Simplify predicate argument lists as allowed by the changes above.
2026-01-30 23:10:00 +11:00
Zoltan Somogyi
f41e76cfbe Use the agreed wording in foreign_proc diagnostics.
compiler/add_foreign_proc.m:
    Use the agreed wording *consistently* in the diagnostics
    generated by this module.

    Also, delete a call to the test for builtins that I recently
    accidentally duplicated instead of just moving.

tests/invalid/foreign_proc_dup_var.err_exp:
tests/invalid/foreign_purity_mismatch.err_exp:
tests/invalid/fp_dup_bug.err_exp:
tests/invalid/pragma_qual_error.err_exp:
tests/warnings/foreign_singleton.err_exp:
tests/warnings/singleton_test.err_exp:
tests/warnings/warn_return.err_exp:
tests/warnings/warn_succ_ind.err_exp:
    Expect the updated wording for all those diagnostics.
2026-01-30 20:27:30 +11:00
Zoltan Somogyi
9dc69a6cce Move all diagnostics predicates to the end. 2026-01-30 17:41:02 +11:00
Zoltan Somogyi
738ed9a35e Move a predicate to its only calling module. 2026-01-30 16:43:17 +11:00
Zoltan Somogyi
af3a275563 Stop double-warning about foreign_proc singletons.
compiler/make_hlds_warn.m:
    We have long invoked the predicate that warns about singletons
    (and a few other kinds of errors) in foreign_procs

    - both when we add the foreign_proc to the HLDS,
    - and when we add Mercury a clause to the HLDS, and the clause
      contains a call_foreign_proc goal.

    However, until the inlining pass, Mercury clauses can *never* contain
    a call_foreign_proc goal, so the latter code never does anything.
    Delete it.
2026-01-30 16:01:31 +11:00
Zoltan Somogyi
325fb21cbb Check all foreign_procs for more kinds of issues.
compiler/add_foreign_proc.m:
    Perform several more kinds of checks on all foreign_procs,
    not just on the foreign_procs that we end up adding to the HLDS.
    These are

    - the checks for whether the foreign_proc is for a builtin operation;

    - the checks for repeated variables in the foreign_proc's argument list;
      and

    - the checks for purity mismatches between the foreign_proc
      and the predicate/function it is for.

    An unrelated change: simplify, and make much more direct, the logic
    of the is_foreign_proc_for_this_backend predicate.

tests/invalid/foreign_proc_dup_var.err_exp:
tests/invalid/foreign_purity_mismatch.err_exp:
    Expect error messages not just for C foreign_procs, but Java and C#
    foreign_procs as well.

tests/invalid/foreign_proc_dup_var.err_exp2:
tests/invalid/foreign_proc_dup_var.err_exp3:
tests/invalid/foreign_purity_mismatch.err_exp2:
tests/invalid/foreign_purity_mismatch.err_exp3:
    Delete these files, which used to hold the diagnostics for
    Java and C# foreign_procs respectively.
2026-01-30 05:18:33 +11:00
Zoltan Somogyi
881ccc718a Break up more predicates to allow the pieces ...
... to be rearranged.
2026-01-29 23:42:02 +11:00
Zoltan Somogyi
2448461ae1 Warn about singletons for *all* foreign procs.
compiler/add_foreign_proc.m:
    The existing code for adding foreign_procs

    - tests whether the foreign_proc is for an imported predicate,
      and if so, stops with an error message,

    - then tests whether the foreign_proc is for the current backend,
      and if it is not, ignores the foreign_proc,

    - and then both adds the foreign proc to the HLDS, and checks it
      for singletons.

    Reverse the order of the last two tests, so that we now test
    foreign_procs for singletons *even if* they are not for the current
    backend. (Though of course we do not add such foreign_procs to the HLDS.)

library/io.environment.m:
library/private_builtin.m:
library/rtti_implementation.m:
    Fix the warnings for now result for non-C foreign_procs even during
    bootchecks in C grades.

tests/warnings/foreign_singleton.err_exp:
    Expect warnings for Java and C# foreign_procs as well as C foreign_procs.

tests/warnings/singleton_test.{m,err_exp}:
tests/warnings/warn_return.{m,err_exp}:
tests/warnings/warn_succ_ind.{m,err_exp}:
    Make these test cases more readable. Delete any obsolete parts,
    as well as the causes of warnings that these test cases are
    not intended to test for. (The latter makes the tests' *outputs*
    more readable.)

    Expect warnings for Java and C# foreign_procs as well as C foreign_procs,
    and expect them with the new line numbers.

tests/warnings/foreign_singleton.err_exp2:
tests/warnings/foreign_singleton.err_exp3:
tests/warnings/singleton_test.err_exp2:
tests/warnings/singleton_test.err_exp3:
tests/warnings/warn_return.err_exp2:
tests/warnings/warn_return.err_exp3:
tests/warnings/warn_succ_ind.err_exp2:
tests/warnings/warn_succ_ind.err_exp3:
    Delete these Java- and C#-specific expected outputs, since the warnings
    they test for are now in the corresponding .err_exp files.
2026-01-29 22:09:58 +11:00
Zoltan Somogyi
3bd59d030d Break one more predicate. 2026-01-29 15:16:19 +11:00
Zoltan Somogyi
e997c4fe93 Break up another predicate. 2026-01-29 04:28:46 +11:00
Zoltan Somogyi
7a2acab33d Break up the add_foreign_proc predicate.
compiler/add_foreign_proc.m:
    Replace the ~220 line add_foreign_proc predicate with
    eight smaller predicates here (and one in sym_name.m),
    none of which exceed 55 lines.

mdbcomp/sym_name.m:
    Add that one predicate, which will probably be useful
    elsewhere as well.

    Fix bad existing documentation.
2026-01-29 03:27:10 +11:00
Zoltan Somogyi
d93429d17f Fix indentation. 2026-01-29 03:18:52 +11:00
Zoltan Somogyi
355b96ce06 Fix a comment. 2026-01-29 03:18:27 +11:00
Zoltan Somogyi
44a0c62d96 Include state var references in "did you mean" messages.
compiler/make_hlds_warn.m:
    When generating "did you mean" messages to add to warnings about
    singleton variables, consider state variables in two separate ways.

    - When figuring out what you could have meant with a singleton reference
      that is a variable such as PredInfo, consider that the intended reference
      *could* have been to a state variable (such as !.PredInfo).

    - When figuring out what you could have meant with a singleton reference
      that is a state variable reference as !.PredIfno or !:PredIfno, consider
      that the intended reference *could* have been to another state variable
      (such as !.PredInfo or !:PredInfo).

    Stop passing around a parameter that, for some predicates, was *always*
    set to the same value.

tests/invalid/undef_symbol.{m,err_exp}:
    Extend this test case with code that (except for the types)
    duplicates the motivating example for this change.

tests/invalid/bug278.err_exp:
tests/invalid/field_syntax_error.err_exp:
    Expect the extra dym messages that we now generate.
2026-01-29 01:20:54 +11:00
Zoltan Somogyi
62d9fcbe4b Ignore (mostly) comments inside foreign_procs.
compiler/foreign.m:
    When returning the list of identifiers inside a foreign_proc's code,
    ignore the contents of comments. This means that warnings about
    variable names that occur in the foreign_proc's argument list
    but do not occur in the foreign_proc's code cannot be shut up anymore
    by mention the variable name in comments.

    The "mostly" part is that typecheck.m still looks at the bodies
    of foreign_procs without knowing about comments. Fixing that
    will be part a future change.

NEWS.md:
    Announce this breaking change.

compiler/fact_table_gen.m:
    When we try to generate a foreign_proc's body for a fact table
    but cannot do so due to the fact table file not being readable,
    add a marker to the predicate that the fact table is for.

compiler/hlds_markers.m:
    Update the documentation of the existing predicate marker
    we use for this.

compiler/make_hlds_warn.m:
    Do not warn about singleton variables in foreign_proc bodies
    that are empty due to missing fact table files.

library/array.m:
library/io.file.m:
library/profiling_builtin.m:
library/stm_builtin.m:
library/table_builtin.m:
library/term_size_prof_builtin.m:
tests/invalid/erroneous_throw_promise.m:
tests/invalid/foreign_procs_exist_type.m:
tests/invalid/foreign_purity_mismatch.m:
tests/invalid/gh72_errors.m:
tests/invalid_purity/purity.m:
tests/valid/solv.m:
    Shut up warnings about singletons in foreign_procs by adding a _ prefix
    to the names of the relevant arguments, and delete the comments
    that can no longer do that job.

tests/invalid/foreign_purity_mismatch.err_exp:
tests/invalid/gh72_errors.err_exp:
tests/invalid/gh72_errors.err_exp2:
tests/invalid/gh72_errors.err_exp3:
tests/invalid_purity/purity.err_exp:
    Expect updated line numbers.

tests/warnings/warn_return.{m,err_exp}:
    Extend this test case to test that

    - we DO warn about return statements outside comments, but
    - we DO NOT warn about return statements inside comments.
2026-01-28 13:34:30 +11:00
Zoltan Somogyi
8d8090ad67 Rename and restructure c_code_to_name_list.
compiler/foreign.m:
    Rename c_code_to_name_list to foreign_code_to_identifiers,
    which describes its job much better. Make it take the language
    of the foreign code as input, though we ignore it for now.
    Also rename the predicates in its call tree accordingly.

    Restructure foreign_code_to_identifiers's call tree to ensure
    tail recursion in loops that may be long enough for that to matter.

compiler/make_hlds_warn.m:
    Conform to the changes above.
2026-01-27 16:57:49 +11:00
Zoltan Somogyi
8bafcae097 Move c_code_to_name_list to foreign.m. 2026-01-27 16:00:29 +11:00
Zoltan Somogyi
d4edaf5588 Move large blocks of code to separate predicates. 2026-01-27 14:57:59 +11:00
Julien Fischer
b9777da30f Fix some documentation errors in the compiler.
Fix some mispelled variable names.

compiler/accumulator.m:
compiler/check_import_accessibility.m:
compiler/decide_type_repn.m:
compiler/du_type_layout.m:
compiler/error_spec.m:
compiler/hlds_data.m:
compiler/hlds_goal.m:
compiler/mlds.m:
compiler/parse_item.m:
compiler/prog_data.m:
compiler/type_util.m:
    As above.
2026-01-24 01:19:51 +11:00
Julien Fischer
2fb788c403 Add a missing word to the coding standards.
compiler/notes/coding_standard.html:
    As above.
2026-01-21 20:16:07 +11:00
Julien Fischer
fd02945ecc Fix errors in usage message.
compiler/options.m:
    As above.

tests/warnings/help_text.err_exp:
    Conform to the above change.
2026-01-20 20:54:55 +11:00
Zoltan Somogyi
280db1b10e Describe the reason for the mode_error_info type. 2026-01-19 14:30:49 +11:00
Zoltan Somogyi
a45c8e7f33 Clarify some comments.
And delete an obsolete comment.
2026-01-19 14:30:16 +11:00
Zoltan Somogyi
c2e5b8aac1 Improve some variable names. 2026-01-19 14:29:36 +11:00
Zoltan Somogyi
f7a0e88d89 Minor improvements to compiler_design.html.
List the top modules of the make.m package first,
and the utility and data structure modules last.

Mention the deleted backends.

Improve wording in several places.

Fix some cut-and-paste errors.
2026-01-19 14:01:38 +11:00
Julien Fischer
ce26cc1928 Minor fix to compiler_design doc.
compiler/notes/compiler_design.html:
    As above.
2026-01-19 11:33:58 +11:00
Zoltan Somogyi
86b33fbd40 Gather statistics about switch search depth results.
compiler/switch_detection.m:
    Add conditionally-enabled code to gather statistics about
    what happens when a switch candidate has disjuncts left over,
    and we test whether these leftovers form a switch on another variable.
    (The results show that after 99.91% of switch candidates,
    there are *no* disjuncts left over.)

compiler/mercury_compile_main.m:
    Invoke the predicate that writes out any statistics we gathered.

tools/switch_depth:
    This new summarizes the results.
2026-01-15 10:45:35 +11:00
Zoltan Somogyi
5063ff4a98 Use the pred form of cord.snoc where appropriate. 2026-01-15 05:52:08 +11:00
Zoltan Somogyi
d13f25ae2f Compute the best candidate as we go along.
compiler/switch_detection.m:
    As above.

compiler/switch_candidates.m:
    Adapt the underlying infrastructure.
2026-01-15 05:00:52 +11:00
Zoltan Somogyi
5e4c35505d Add a utility function. 2026-01-15 04:59:20 +11:00
Zoltan Somogyi
6e68a47ad8 Requantify a procedure body only if needed.
compiler/switch_candidates.m:
    Some ways of transforming disjunctions into switches call for
    duplicating code. Any variables local to duplicated code must be
    renamed apart by quantification. We used to set a flag calling for
    the requantification of the procedure body when finding a switch
    candidate that calls for such code duplication. However, this is wrong:
    we only need to requantify the procedure body if such a switch candidate
    is *actually selected*. Fix this, mostly to clarify the code, though
    the avoiding unneeded requantification is also a (minor) performance win.

compiler/switch_detection.m:
    Record *with each switch candidate* whether it calls for requantifying
    the procedure body if it is selected.
2026-01-14 16:12:30 +11:00
Zoltan Somogyi
05f9dfcbd3 Update copyright year. 2026-01-14 13:01:34 +11:00
Julien Fischer
28fa62c5dc Fix doubled-up word.
compiler/builtin_ops.m:
    As above.
2026-01-13 17:51:22 +11:00
Zoltan Somogyi
3f58eabe9f Delete the allow_multi_arm_switches internal option.
The compiler has had no code to set it from its default value of "yes"
to "no" since the Erlang backend was deleted in October 2020.

compiler/options.m:
    Delete the option.

compiler/handle_options.m:
    Delete an (incorrect) reference to the option.

compiler/switch_detection.m:
    Stop paying attention to the option.

tests/warnings/help_text.err_exp:
    Stop expecting documentation of the option.
2026-01-13 15:22:12 +11:00
Julien Fischer
41328b42b2 Minor corrections.
compiler/hlds_goal.m:
    Fix spelling.

compiler/handle_options.m:
compiler/mercury_compile_front_end.m:
compiler/source_file_map.m:
compiler/unused_imports.m:
    Fix doubled-up words.
2026-01-12 23:36:24 +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
81cafc5928 Improve code readability. 2026-01-11 10:12:47 +11:00
Zoltan Somogyi
b071f84f81 Assign goal_ids in the scout disjunctions pass ...
... reducing the number of procedure body traversals by one,
and avoiding the unneeded construction of the containing goal map.
2026-01-11 07:26:49 +11:00
Zoltan Somogyi
635b2268fe Improve handling of from_ground_term_deconstruct scopes.
compiler/hlds_goal.m:
    Document that all goals inside from_ground_term_deconstruct scopes
    are not just unifications, but unifications whose right hand sides
    are rhs_functors.

compiler/constraint.m:
    Fix a potential problem, which is that pushing a constraint into
    from_ground_term_{deconstruct,other) scopes can invalidate
    their invariants.

compiler/goal_refs.m:
    Fix a comment.

compiler/lco.m:
    Qualify a call.

compiler/modecheck_goal.m:
    Fix a misleading predicate name, and typos.

compiler/try_expand.m:
    Exploit the newly-documented invariant about from_ground_term_deconstruct
    scopes.
2026-01-11 03:57:52 +11:00
Zoltan Somogyi
77017dcbcb Put related predicates together. 2026-01-10 14:57:24 +11:00
Zoltan Somogyi
17f0230447 Carve three new modules out of switch_detection.m.
Simplify the interfaces between the new modules and switch_detection.m.

compiler/scout_disjunctions.m:
    New module containing the part of the old switch_detection.m
    that scouts disjunctions ahead of the main pass in bulk.

compiler/find_bind_var.m:
    New module containing the part of the old switch_detection.m
    that scouts disjunctions as *part* of the main pass, piecemeal.

compiler/switch_candidates.m:
    New module containing the part of the old switch_detection.m
    that recognizes candidate switches, and chooses the best switch
    if there is more than one candidate.

compiler/switch_detection.m:
    Delete the moved code. Conform to the interface simplifications.

compiler/check_hlds.m:
compiler/notes/compiler_design.html:
    Add and document the new modules.

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

compiler/hlds_out_goal.m:
    Add the capability to format a case as a string.
2026-01-10 10:40:43 +11:00
Julien Fischer
f1cc066ab0 Remove workarounds for depend_ints on Windows.
The changes to file copying in the compiler in early 2024 mean that we no
longer encounter the same performance problems these workarounds were put
in place to avoid.

compiler/Mmakefile:
library/Mmakefile:
    As above.
2026-01-07 16:55:55 +11:00
Julien Fischer
0f2ee70edf Support wildcard argument expansion with MSVC.
In Unix expansion of wildcard arguments is done by the shell. On Windows,
this is not the case and programs are responsible for expanding wildcard
argument themselves. When using source file mapping, we typically used
wildcards to specify the source files to map. When native Windows Mercury
compilers are used in the Windows command prompt or PowerShell, this does
not work, which is inconvenient to say the least. (In environments like
Cygwin or MSYS2, the shell does do wildcard expansion.)

Microsoft's C runtime has an optional component that programs can
link against which will do wildcard expansion on the argument vector
before main is called. Set up builds with MSVC to link against this
component when building the Mercury compiler.

scripts/parse_ml_options.sh-subr.in:
scripts/ml.in:
    Add a new option --use-msvc-setargv-extension, which causes the the
    wsetargv.obj file that enables the extension to be passed the linker.
    (MSVC seems rather picky about where this is positioned. A a result
    we do it within the ml script where have we more control over the matter.)

compiler/Mmakefile:
    Pass --use-msvc-setargv-extension to the ml script if we are using MSVC.

    Unrelated: Fix the identity of a compiler in a comment.

    Unrelated: Add an XXX about something that looks suspicious.

util/mkinit.c:
    Add a pointer to scripts/ml.in, saying that if the Windows entry
    point (currently wmain) is updated, then the script may need to
    be updated.
2026-01-06 21:45:53 +11:00
Julien Fischer
318b21ae48 Fix a problem with reading .module_dep files.
The source file name that is used as the second argument of a .module_dep file
may be directory qualified (e.g. if we are using source file mapping to
include source files from multiple directories). On Windows, such directory
qualified source file names may use '\' as a path separator. We use the term
parser to read the contents of .module dep files and that requires '\' to be
escaped. However, the code that writes module_dep files does not currently
do the required escaping. Fix that.

compiler/make.module_dep_file.m:
    Escape the source file name before writing it to a .module_dep file.
2026-01-05 19:41:41 +11:00
Julien Fischer
ac642e9ef7 Update copyright notices for 2026.
LICENSE:
compiler/print_help.m:
doc/*.texi:
profiler/mercury_profile.m:
slice/mcov.m:
slice/mdice.m:
slice/mslice.m:
slice/mtc_diff.m:
slice/mtc_union.m:
trace/mercury_trace_Internal.c:
    As above.
2026-01-01 19:56:20 +11:00