-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcppfly.cppm
More file actions
210 lines (186 loc) · 7.69 KB
/
Copy pathcppfly.cppm
File metadata and controls
210 lines (186 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// mcpp.toolchain.cppfly — the `standard = "c++fly"` capability: per-compiler
// support & mapping for "latest -std= level + every enableable experimental
// gate (language + stdlib)". Pure data tables + query functions, the fourth
// sibling of CommandDialect / BmiTraits / ProviderCapabilities — and the
// first consumer of Toolchain::version for gating.
//
// Also owns the "latest level for this toolchain" table, which fixes
// standard = "c++latest" reaching the GNU driver as the invalid spelling
// -std=c++latest (design §11-Q5).
//
// Table facts pinned by on-machine probes (gcc 16.1.0, 2026-07-14):
// contracts are enabled by -std=c++26 alone (__cpp_contracts defined);
// reflection additionally needs -freflection (__cpp_impl_reflection).
// See .agents/docs/2026-07-14-std-features-experimental-gate-design.md.
export module mcpp.toolchain.cppfly;
import std;
import mcpp.toolchain.dialect;
import mcpp.toolchain.model;
export namespace mcpp::toolchain::cppfly {
struct FeatureState {
std::string name; // "reflection"
std::string paper; // "P2996" — summary/diagnostics
std::string flags; // enabled: extra flags ("" = enabled by std level alone)
std::string reason; // skipped: why
bool enabled = false;
};
struct Resolution {
std::string stdCanonical; // "c++26" / "c++2c" / "c++23"
int stdLevel = 26;
std::vector<std::string> flags; // union of enabled gate flags, deduped
std::vector<FeatureState> features; // enabled + skipped, declaration order
};
// Leading integer of Toolchain::version ("16.1.0" → 16; unparsable → 0).
int compiler_major(const Toolchain& tc);
// Latest -std= canonical the resolved toolchain accepts. Used for both
// c++fly and c++latest (the raw canonicals are not valid -std= spellings).
std::string latest_std_canonical(const Toolchain& tc, int* levelOut = nullptr);
// The full c++fly answer for a toolchain: latest level + every gate it
// supports (enabled) and every gate it lacks (skipped, with reason).
Resolution resolve(const Toolchain& tc);
// The dialect-complete std flag for any normalized standard, including
// c++latest/c++fly which resolve per-toolchain. Plain canonicals delegate
// to std_flag_for unchanged.
std::string std_flag(const Toolchain& tc, std::string_view canonical, int level);
// Graph-global dialect flags: manifest-declared ∪ (experimental ? fly gate
// flags : ∅), deduped, declaration order kept. The ONE merge both consumers
// (BuildPlan::dialectFlags and prepare's stdFlagAndDialect) go through so
// scan-time, prebuild-time and compile-time dialect provably agree.
std::vector<std::string> effective_dialect_flags(const Toolchain& tc,
bool experimental, std::vector<std::string> manifestDialectFlags);
} // namespace mcpp::toolchain::cppfly
namespace mcpp::toolchain::cppfly {
namespace {
// ── Table 1: family × min major → latest supported -std= canonical ──────
// First matching row wins (rows per family ordered newest-first).
struct LatestStdRule {
CompilerId family;
int minMajor;
std::string_view canonical;
int level;
};
constexpr LatestStdRule kLatestStd[] = {
{ CompilerId::GCC, 14, "c++26", 26 },
{ CompilerId::GCC, 0, "c++23", 23 },
{ CompilerId::Clang, 17, "c++2c", 26 },
{ CompilerId::Clang, 0, "c++23", 23 },
// MSVC has no /std:c++26 — level > 20 is spelled /std:c++latest by
// std_flag_for; the canonical here only feeds diagnostics.
{ CompilerId::MSVC, 0, "c++26", 26 },
};
// ── Table 2: experimental language-feature gates (version-gated) ────────
// A family with no row = unsupported. flags == "" = supported at the fly
// std level with no extra gate flag (still reported for the summary).
struct GateRule {
CompilerId family;
int minMajor;
std::string_view flags;
};
constexpr GateRule kReflectionRules[] = {
{ CompilerId::GCC, 16, "-freflection" },
};
constexpr GateRule kContractsRules[] = {
{ CompilerId::GCC, 16, "" },
};
struct Gate {
std::string_view name;
std::string_view paper;
std::span<const GateRule> rules;
};
constexpr Gate kGates[] = {
{ "reflection", "P2996", kReflectionRules },
{ "contracts", "P2900", kContractsRules },
};
// ── Table 3: stdlib experimental gates (stdlib dimension) ───────────────
struct StdlibGateRule {
std::string_view name;
std::string_view stdlibId;
std::string_view flags;
};
constexpr StdlibGateRule kStdlibGates[] = {
// libstdc++ and MSVC STL ship their unstable bits ungated; libc++ hides
// them behind -fexperimental-library.
{ "experimental-library", "libc++", "-fexperimental-library" },
};
void add_unique(std::vector<std::string>& v, std::string_view f) {
if (f.empty()) return;
if (std::find(v.begin(), v.end(), f) == v.end()) v.emplace_back(f);
}
} // namespace
int compiler_major(const Toolchain& tc) {
int major = 0;
bool any = false;
for (char c : tc.version) {
if (c < '0' || c > '9') break;
major = major * 10 + (c - '0');
any = true;
}
return any ? major : 0;
}
std::string latest_std_canonical(const Toolchain& tc, int* levelOut) {
const int major = compiler_major(tc);
for (auto& r : kLatestStd) {
if (r.family != tc.compiler) continue;
if (major >= r.minMajor) {
if (levelOut) *levelOut = r.level;
return std::string(r.canonical);
}
}
if (levelOut) *levelOut = 26; // unknown family: newest ratified level
return "c++26";
}
Resolution resolve(const Toolchain& tc) {
Resolution r;
r.stdCanonical = latest_std_canonical(tc, &r.stdLevel);
const int major = compiler_major(tc);
for (auto& g : kGates) {
FeatureState st;
st.name = std::string(g.name);
st.paper = std::string(g.paper);
const GateRule* hit = nullptr;
for (auto& rule : g.rules) {
if (rule.family == tc.compiler) { hit = &rule; break; }
}
if (!hit) {
st.reason = std::format("{}: unsupported", tc.compiler_name());
} else if (major < hit->minMajor) {
st.reason = std::format("{} {} < {}", tc.compiler_name(), major, hit->minMajor);
} else {
st.enabled = true;
st.flags = std::string(hit->flags);
add_unique(r.flags, hit->flags);
}
r.features.push_back(std::move(st));
}
for (auto& sg : kStdlibGates) {
FeatureState st;
st.name = std::string(sg.name);
st.paper = "stdlib";
if (tc.stdlibId == sg.stdlibId) {
st.enabled = true;
st.flags = std::string(sg.flags);
add_unique(r.flags, sg.flags);
} else {
st.reason = std::format("stdlib is {}, gate applies to {}",
tc.stdlibId.empty() ? "unknown" : tc.stdlibId, sg.stdlibId);
}
r.features.push_back(std::move(st));
}
return r;
}
std::string std_flag(const Toolchain& tc, std::string_view canonical, int level) {
std::string resolvedCanonical(canonical);
int resolvedLevel = level;
// c++latest (999) / c++fly (1000): resolve to the family's real latest
// level — the raw canonical is not a valid -std= spelling on GNU.
if (level >= 999) resolvedCanonical = latest_std_canonical(tc, &resolvedLevel);
return std_flag_for(dialect_for(tc), resolvedCanonical, resolvedLevel);
}
std::vector<std::string> effective_dialect_flags(const Toolchain& tc,
bool experimental, std::vector<std::string> manifestDialectFlags)
{
if (!experimental) return manifestDialectFlags;
for (auto& f : resolve(tc).flags) add_unique(manifestDialectFlags, f);
return manifestDialectFlags;
}
} // namespace mcpp::toolchain::cppfly