Skip to content

c_import: no_methods opt-out option (§16.2a) #416

Closed

Description

@ehartford

Summary

Implement the spec'd no_methods opt-out option on c_import: no_methods: "Type" disables auto-method generation for one imported type, no_methods: true disables it globally for that import. Today the option is not parsed at all — use c_import("lib.h", no_methods: true) is the "unknown c_import option" parse error — so users who hit a bad prefix-claim or want flat C style have no escape hatch.

Spec

§16.2a Auto-Method Generation — Opt-out:

  • "Per-type: use c_import("lib.h", no_methods: "Type"). Global: use c_import("lib.h", no_methods: true). Flat C functions are always available regardless."

Requirements: 16.2.2.29 (per-type), 16.2.2.30 (global).

Current state

  • c_import option parsing (src/Parser.w:2273-2337) recognizes exactly two keys: link (2286) and allow_untranslated (2301); any other key → emit_error("unknown c_import option") (2327). Options are stored as extras on NK_C_IMPORT with counts packed in d2 via pack_c_import_counts(link_count, allow_count) (src/Ast.w:299-306, node layout documented at Ast.w:1453).
  • Auto-method generation happens during header translation in src/CImport.w:1223-1305: snake_case prefixes are precomputed per struct (ci_compute_snake_prefix, CImport.w:1308), and matching imported functions get thin wrapper method sugar emitted (first-param self match ~1255-1281). Flat functions are always emitted regardless (req 16.2.2.31 verdict: ok).
  • The translation layer has no concept of an opt-out: nothing in CImport.w consumes a per-import option besides links/allow-list, which reach it via the frontend (Zcu.c_import_cache_key_frontend src/compiler/Frontend.w:352-374 and the allow-check helpers Frontend.w:473-499).

Implementation plan

  1. Parser (src/Parser.w option loop at 2273-2331): add else if key == "no_methods": accepting:
    • true/false keyword literal → global flag;
    • one string literal or [ "A", "B" ] array → per-type list (mirror the allow_untranslated parsing shape at 2301-2325).
      Store per-type syms as a third extras group after links and allow-list; extend the packed counts — d2 currently packs two counts (pack_c_import_counts, Ast.w:305); repack as three bitfields plus a global-flag bit (e.g. 12 bits link / 12 bits allow / 7 bits no_methods_count / 1 bit no_methods_all), adding c_import_no_methods_count/c_import_no_methods_all accessors next to Ast.w:299-306. Update the node-layout comment at Ast.w:1453 and every consumer of the packed counts: src/render.w:207-208, src/Resolve.w:337, src/compiler/Frontend.w:356-357, 479-481.
  2. Plumb to translation: pass the no_methods set/flag from the frontend's c_import evaluation into the CImport translation entry (same path the header spec and links travel). In src/CImport.w:1223-1236, skip prefix computation entirely when the global flag is set; when per-type, skip emitting wrappers for structs whose name is in the set (compare against the imported struct name before struct_prefixes.push). Constructor sugar (TypeName(args) → .new, req 16.2.2.10) must also be suppressed for opted-out types since it derives from the generated .new wrapper.
  3. Cache key: append a \n#no-methods: line with the flag and sorted type list in Zcu.c_import_cache_key_frontend (Frontend.w:352-374), exactly like #allow-untranslated: (361-364) — otherwise toggling the option reuses stale generated bindings.
  4. Diagnostics: unknown key error text stays for typos; a per-type name that matches no imported struct should be a warning in the import diagnostics ("no_methods: 'GHashTabel' matches no imported type") so misspellings don't silently no-op — emit where the type set is consumed in CImport.w.
  5. Fmt/render: src/render.w:207-208 renders c_import decls for formatting/printing; teach it the new option so with fmt round-trips.
  6. No rt/Link.w impact; parser/frontend/CImport only.

Test plan

(Use inline-header imports, pattern of test/compile_errors/err_c_import_str_to_c_char_requires_contract.w, declaring typedef struct Counter Counter; Counter *counter_new(void); void counter_incr(Counter *c); int counter_get(Counter *c);.)

  • test/behavior/behav_c_import_no_methods_global.w: use c_import(..., no_methods: true); flat calls counter_incr(c) work; method form is an unknown-method compile error — split the negative half into test/compile_errors/err_c_import_no_methods_global.w with //! expect-error: on c.incr().
  • test/compile_errors/err_c_import_no_methods_type.w: no_methods: "Counter"; c.incr() errors while a second type in the same header keeps its methods (positive half asserted in a behavior test behav_c_import_no_methods_type.w).
  • test/behavior/behav_c_import_no_methods_flat.w: with both opt-out forms, all flat functions remain callable (req 16.2.2.31).
  • Constructor sugar: with no_methods: "Counter", Counter(...) call form errors; counter_new() works.
  • Cache-key regression: same header, toggled option across two imports/compiles produces different surfaces (internals test or driver test relying on WITH_CIMPORT_CACHE_EPOCH hygiene).
  • Warning case: no_methods: "Typo" emits the matches-no-type warning.

Acceptance criteria

  • no_methods: true, no_methods: "Type", and no_methods: ["A", "B"] parse; anything else under that key is a clear parse error.
  • Opted-out types get no auto methods and no constructor-call sugar; flat functions unaffected; other types in the same import unaffected.
  • Option participates in the c_import cache key.
  • with fmt preserves the option.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 16.2.2.29, 16.2.2.30.

Activity

  1. ehartford commented on Jun 16, 2026

    @ehartford
    ContributorAuthor

    Implemented in 538e208. c_import now parses no_methods: true | "Type" | ["A","B"] (anything else is a parse error). Per-type names are stored as a third extras group; the NK_C_IMPORT packing gained a no_methods count + global-flag bit. The frontend applies the set before generation; ci_detect_member_functions emits no auto-methods/constructor sugar for opted-out types (or globally), while flat C functions remain. The option participates in the c_import cache key (format v12) and round-trips through with fmt. Tests: behav_c_import_no_methods_flat, behav_c_import_auto_method_baseline, err_c_import_no_methods_global, err_c_import_no_methods_type. build + fixpoint + compile-error/spec/behavior suites pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions