source: doc/proposals/modules-gen-hdr/README@ b28ce93

Last change on this file since b28ce93 was b28ce93, checked in by Michael Brooks <mlbrooks@…>, 3 months ago

Add POC illustrating generating headers from compile units

  • Property mode set to 100644
File size: 7.7 KB
Line 
1cfa-cppp - CForall pre-preprocessor
2
3POC deriving header files from a compile unit's source.
4
5Input: Aet of interdependent *.src.c files, each written as if header files are not a consideration (Java-style, definitions only), annotated with both the language additions and POC scaffolding listed below.
6
7Key Intermediate: For each %.src.c input, %.tdcl.h, %.defn.h, %.impl.c, containing relevant true-cpp #include directives of each other, such that the resulting classic-C build of a self-sufficient set of *.impl.c files gives the demo's Output.
8
9Output: Linked and running program
10
11Terminology
12- shred: "Shredding foo.src.c" means producing the "foo" Key Intermediates from the "foo" Input.
13- vicious: cyclic dependency that cannot be bootstrapped with the tools under POC
14
15
16To run:
17 if grep -q 'cfa-cppp' Makefile; then echo ok; else echo Wrong folder; fi;
18 make # expect success
19 hello/a.out # expect log of fcn calls and glb-var vals
20 coop/a.out # expect log of chickens hatching from eggs
21 akwd-val-trans/a.out # expect four coop-like logs of a/b calls
22 make err-vicious/a.out # expect failure after shredding done
23 make clean # expect success
24 make hello/a.out CFLAGS=-DERR1 # expect failure after shredding done
25 grep -rE 'ERR[0-9]*' --include=*.src.c # repeat prev -DERR act / do "manual" steps
26 # on resulting grep hits
27
28
29Demos are
30- (`err-` means "build is expected to fail")
31- hello: valid hello-world scenario with transitive dependency
32 - linear (bottom-up) build order would be fine
33- coop (chickens and eggs): valid circular dependency, resolved with `import auto &`
34 - "as much mutual recursion as I can cram, without going vicious"
35- vicious: the bridge too far, that coop resisted crossing, a:tdefs <-> b:tdefs
36 - requires an out-of-scope user's recourse, like multiple manual header fragments
37 - there does not exist a C-valid order of each module's offerings obtainable by ordering based on only offering sort (e.g. type definition, value declaration) and compile unit
38 - would never arise as real C code, without a split like one compile unit implementing two headers
39 - key difficulty: each side has a type definition that embeds a type defined on the opposite side
40- typeof: valid cicular dependency, its poential hiding in typeof
41 - b:vdefs -> a:tdefs -> b:vdecls
42- vicious-typeof: a vicious-cycle potential hiding in typeof, a:vdecls <-> b:vdecls
43 - comments under 'vicious' apply
44 - (including) expect recourse like multiple manual header fragments to resolve
45- vicious*/recourse-classic
46- vicious*/recourse-proposed
47
48
49In scope
50- module-level export vs private
51- automatically handle circular dependency (demos: valid=coop invalid=err-vicious)
52- ordered dependency with transitive "re-export" (demo: hello)
53- separate compilation
54- structs, functions and global variables
55
56Out of Scope
57- (though believed not deal breakers for this proposal)
58- quality error messages
59- private struct fields, friends
60- illustrating how to set up a build (current Makefile is "get it to work")
61- crawling imports ("first-time gcc -MMD"), determining a build order
62- interaction of these modules with preexsting CFA features (current demos are C)
63- extracting shred-relevant information from C source (mocked up with scaffolding below)
64- C preprocessor integration: exporting a macro, ifdef'ing a #import
65- user's recourse for situations where headers are not inferrable
66- typedefs
67- controling visibility of types (equiv. choosing to put a struct in *.h vs *.c)
68- proffering implementation (equiv. function bodies in the header, like for static-inline)
69- rejecting attempt to offer something public that can't be understood without seeing something private
70- relaxing declare before use
71- legacy integration (demo uses printf/exit as loose declarations, main as "just define it")
72- linker control (demo dumps all exports into one linker namespace)
73
74
75Language additions modelled
76- (The proposal is to include nicer versions of these placeholders to CFA)
77- module imports (#import), where `foo` corresponds with foo.c; one of:
78- `#import static foo`: this implementation depends on foo's interface
79 - most common, equivaluent of #include in *.c
80- `#import auto & foo`: above, plus this interface mentions types from foo's interface without relying on size information
81 - relevant limitation is you can't nest those types inside types defined here
82 - it's a recourse for breaking a "normal" include cycle
83 - syntax parallels `forall T &` emphasizing you get enough to define functions that take imported types by reference, but not by value
84 - uncommon, equiv. replacing #include with a type forward declaration or giving *.fwd.h)
85- `#import auto foo`: above, plus this interface has unlimited access to foo's interface (quite common, equiv. #include in *.h)
86
87Scaffolding for the POC
88- (The proposal is to have better compile-time analysis, making these elements unnecessary)
89- No single-line definitions allowed
90- The first line of every user-given definition is a valid declaration, with its semicolon removed and an open-curly added
91- Every user-given definition is preceded, on the adjacent line, by a `//#` directive, which is the appropriate one of:
92 - `@` for an exported type
93 - `$f` for an exported function
94 - `$v` for an exported variable
95 - `-` for anything static
96- Generally, abide with the shredder being brittle on whitespace
97
98Choice: semantics of transitive import
99- Option A: To import+export a depended-upon module means re-exporting both its types and its values (functions, variables)
100 - Benefit: the typical basic application of C headers works this way
101- Option B: To import+export a depended-upon module means re-exporting only its types
102 - I'll use those types types in my exported declarations
103 - by importing me, you'll get those types so that you can use my declarations
104 - if you also want its values, you'll need to import them yourself
105 - Benefit: gives importers a tidier symbol table; you get implicitly only what you actually need
106- Incomplete Option A used in demo: You re-export values only with `import auto M`, but not with `import auto & M`.
107 - This association is unnecessary mental complexity for a user.
108 - To do either a clean option A or B requires
109 - adding a third header flavour (a further split of .defn.h)
110 - furthermore, option A probably needs the rules for desugaring #import into #include to become transitive
111 - Left KISS / "common denominator" here
112 - Present-state workaround to achieve full Option A: Extra import in demo `akwd-val-trans/over_a.src.c`
113 - No option-B mockup available from present state because decision "`import auto M` re-exports values" means present state does exports that option B does not want
114
115Eventual implementation remark
116- To extract shred-relevant information from C source...
117- For %.c -> %.tdcl.h
118 - Ignore imports
119 - Need a limp-mode parser that does not differentiate types from identifiers
120 - It only has to produce the list of type names (non-recursively) exported, i.e. infer the `//# @` annotations
121- For %.c -> {%.defn.h, %.impl.c}
122 - On seeing `import auto foo` (or `import auto & foo`), pre-load the parser's type/id table as if `import auto & foo`, i.e. recursively consult imports, in limp mode, stopping upon a cycle (like #pragma once).
123 - It's not necessary to know information in foo.defn.h to shred % accurately.
124 - Today's CFA parser works and is sufficient (though overkill)
125 - Parsing here only needs to enable extracting a declaration from its definition.
126 - User errors, like, "You tried to pass an incomplete type by value," can come out later, while compiling %.impl.c.
Note: See TracBrowser for help on using the repository browser.