-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBmodEmitter.cpp
More file actions
685 lines (613 loc) · 21.4 KB
/
Copy pathBmodEmitter.cpp
File metadata and controls
685 lines (613 loc) · 21.4 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
#include "BmodEmitter.h"
#include "Expression.h"
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
using namespace QLang;
using namespace std;
// Cache of source files read for definition slicing (one read per file).
static map<string, vector<string>> gSourceLineCache;
static const vector<string> *sourceLines( const string &path )
{
auto it = gSourceLineCache.find( path );
if ( it != gSourceLineCache.end() )
return &it->second;
ifstream in( path );
if ( !in.is_open() )
return nullptr;
vector<string> lines;
string line;
while ( getline( in, line ) )
lines.push_back( line );
auto res = gSourceLineCache.emplace( path, std::move( lines ) );
return &res.first->second;
}
string BmodEmitter::sliceDefinitionSource( const SourceLocation &loc )
{
if ( loc.file.empty() || loc.line == 0 )
return "";
const vector<string> *lines = sourceLines( loc.file );
if ( lines == nullptr || (size_t)loc.line > lines->size() )
return "";
// Scan from the start of the definition's line, brace-matching to the
// definition's closing '}' while skipping strings, chars, and comments.
ostringstream out;
int depth = 0;
bool sawOpen = false;
bool inLineComment = false, inBlockComment = false;
bool inString = false, inChar = false;
for ( size_t li = (size_t)loc.line - 1; li < lines->size(); li++ )
{
const string &l = ( *lines )[li];
inLineComment = false;
for ( size_t ci = 0; ci < l.size(); ci++ )
{
char c = l[ci];
char next = ( ci + 1 < l.size() ) ? l[ci + 1] : '\0';
if ( inLineComment )
continue;
if ( inBlockComment )
{
if ( c == '*' && next == '/' )
{
inBlockComment = false;
ci++;
}
continue;
}
if ( inString )
{
if ( c == '\\' )
ci++;
else if ( c == '"' )
inString = false;
continue;
}
if ( inChar )
{
if ( c == '\\' )
ci++;
else if ( c == '\'' )
inChar = false;
continue;
}
if ( c == '/' && next == '/' ) { inLineComment = true; continue; }
if ( c == '/' && next == '*' ) { inBlockComment = true; ci++; continue; }
if ( c == '"' ) { inString = true; continue; }
if ( c == '\'' ) { inChar = true; continue; }
if ( c == '{' ) { depth++; sawOpen = true; }
else if ( c == '}' )
{
depth--;
if ( sawOpen && depth == 0 )
{
// Emit through this closing brace and stop.
out << l.substr( 0, ci + 1 ) << "\n";
return out.str();
}
}
}
out << l << "\n";
}
return ""; // ran off the end without closing — malformed, fall back
}
// Helper to get a non-const Type* from various const SmartPtr contexts.
// The BmodEmitter only reads from types, never modifies them.
static Type *nc( const Type *t ) { return const_cast<Type*>( t ); }
bool BmodEmitter::isDataContractStruct( StructDefinition *structDef )
{
// KEEP IN SYNC with Sema::isExportedDataContract (Sema.cpp): the emitter and
// the P9 field-type check must agree on which structs' field types cross the
// boundary, or a struct could ship a field the exporter never validated.
if ( structDef->isTable() )
return true;
for ( const auto &ann : structDef->getAnnotations() )
if ( ann.mName == "json" )
return true;
return false;
}
void BmodEmitter::emitType( Type *type, ostream &out )
{
out << type->getName();
if ( type->getNumTypeParams() > 0 )
{
out << "<";
for ( int i = 0; i < type->getNumTypeParams(); i++ )
{
if ( i > 0 )
out << ", ";
emitType( type->getTypeParam( i ), out );
}
out << ">";
}
}
void BmodEmitter::emitAnnotations( const vector<AnnotationNode> &annotations, ostream &out )
{
for ( const auto &ann : annotations )
{
out << "@" << ann.mName;
if ( !ann.mArgs.empty() )
{
out << "(";
for ( size_t i = 0; i < ann.mArgs.size(); i++ )
{
if ( i > 0 )
out << ", ";
out << "\"" << ann.mArgs[i] << "\"";
}
out << ")";
}
out << endl;
}
}
void BmodEmitter::emitGenericParams( const vector<GenericParam> ¶ms, ostream &out )
{
if ( params.empty() )
return;
out << "<";
for ( size_t i = 0; i < params.size(); i++ )
{
if ( i > 0 )
out << ", ";
out << params[i].mName;
if ( !params[i].mConstraint.empty() )
out << ": " << params[i].mConstraint;
}
out << ">";
}
void BmodEmitter::emitFunction( FunctionDefinition *func, ostream &out )
{
if ( !func->isPublic() )
return;
emitAnnotations( func->getAnnotations(), out );
// A GENERIC function ships its full definition (verbatim source): the
// consumer must monomorphize the body per instantiation — a signature
// alone would leave every downstream use a linker error. Monomorphized
// instances are emitted linkonce_odr, so a lib and its consumers
// instantiating the same specialization dedup at link time.
if ( func->isGeneric() )
{
string src = sliceDefinitionSource( func->getLocation() );
if ( !src.empty() )
{
out << src;
return;
}
// fall through to signature-only if the source is unavailable
}
out << "pub fn " << func->getName();
emitGenericParams( func->getGenericParams(), out );
out << "(";
for ( int i = 0; i < func->getNumberParams(); i++ )
{
if ( i > 0 )
out << ", ";
VariableDefinition *param = func->getParam( i );
emitType( nc( param->getVariableType() ), out );
out << " " << param->getName();
}
if ( func->isVariadic() )
{
if ( func->getNumberParams() > 0 )
out << ", ";
out << "...";
}
out << ")";
if ( func->getReturnType() != nullptr )
{
out << " -> ";
emitType( func->getReturnType(), out );
}
out << ";" << endl;
}
void BmodEmitter::emitStruct( StructDefinition *structDef, ostream &out,
const std::set<std::string> &exportedProtocols )
{
if ( !structDef->isPublic() )
return;
emitAnnotations( structDef->getAnnotations(), out );
// Source order is `pub table struct Name` — the visibility modifier first.
// This emitted the inverse (`table pub struct`) until U2; it round-tripped
// only through parser leniency, and U5's D15 metadata work makes table
// structs load-bearing across the boundary, so it has to be right first.
out << "pub ";
if ( structDef->isTable() )
out << "table ";
out << "struct " << structDef->getName();
emitGenericParams( structDef->getGenericParams(), out );
out << " {" << endl;
// FIELD LAYOUT (format 4, D15). A field's type reaches the consumer only when
// it MUST — for a generic struct (consumers monomorphize from full layout,
// A6) or a data-contract struct (its shape is its DB/JSON contract). Every
// other non-generic `pub struct` emits an EMPTY body: the consumer constructs
// through the library-emitted factory (which needs no layout, U1) and calls
// `pub` methods, and it can no longer NAME a field. The retained fields of a
// data-contract struct are compiler-facing metadata — present so query/@json
// codegen and Sema's field validation can read them, un-nameable from source
// by a resolution rule (Sema, U5). This is what drops a private field's type
// out of the interface hash so internal edits stop rebuilding downstream.
if ( !structDef->getGenericParams().empty() || isDataContractStruct( structDef ) )
{
for ( const auto &field : structDef->getFields() )
{
out << "\t";
emitType( nc( field->getVariableType() ), out );
out << " " << field->getName() << ";" << endl;
}
}
out << "}" << endl;
// A GENERIC struct also ships its method bodies (an impl block of verbatim
// source slices): instantiating Box<int> in a consumer monomorphizes the
// methods, which requires bodies. Non-generic struct methods stay out of
// the .bmod — they are ordinary symbols linked from the library archive.
if ( !structDef->getGenericParams().empty() &&
!structDef->getMethods().empty() )
{
ostringstream methods;
bool allSliced = true;
for ( const auto &msp : structDef->getMethods() )
{
FunctionDefinition *m = const_cast<FunctionDefinition*>(
(const FunctionDefinition*)msp );
string src = sliceDefinitionSource( m->getLocation() );
if ( src.empty() )
{
allSliced = false;
break;
}
methods << src;
}
if ( allSliced )
{
out << "impl " << structDef->getName() << " {" << endl;
out << methods.str();
out << "}" << endl;
}
// Conformance records apply to generic structs too: D16 names generic
// CONSTRAINT checking (`sort<T: Comparable>` with a foreign T) as one of
// the things that needs them, so returning early here would leave exactly
// that case unserved.
emitConformances( structDef, out, exportedProtocols );
return;
}
// A NON-GENERIC struct ships its init and method SIGNATURES (no bodies) —
// this is what makes an imported type constructible and callable at all
// (design record P8). The bodies stay in the library archive and link from
// the .a; codegen turns each bodyless signature into an LLVM `declare`.
//
// The `init` signature is also the struct's factory record: a consumer that
// sees it constructs through the library-emitted factory symbol (derived
// from the struct name by mangleStructFactoryName) rather than allocating
// locally. The factory is deliberately NOT emitted as a free `pub fn` — it
// must not be nameable from source, or `Counter(5)` would gain a second
// spelling and the one-external-form rule (D9) would be broken.
//
// Interim semantics: every method ships until `pub` exists on impl members;
// the unit that adds `pub` flips this to pub-only.
if ( structHasInterfaceMembers( structDef ) )
emitStructInterface( structDef, out );
emitConformances( structDef, out, exportedProtocols );
}
void BmodEmitter::emitConformances( StructDefinition *structDef, ostream &out,
const std::set<std::string> &exportedProtocols )
{
// Protocol conformance records (design record D16). Emitted as EMPTY impl
// blocks: the method signatures are already in the struct's interface block
// above, and repeating them here would give the struct two copies of every
// conforming method. An empty body still satisfies the conformance check,
// which validates against the struct's accumulated methods rather than the
// impl block's own members (QImplBlock.cpp:156-157) — so this must be
// emitted AFTER the interface block, never before.
//
// Without these records a consumer cannot dispatch `print("{}", x)` through
// Printable on an imported type, and a foreign type cannot satisfy a generic
// constraint.
for ( const auto &proto : structDef->getConformedProtocols() )
{
// Only emit a record a consumer can actually resolve. A non-`pub`
// protocol is not emitted into this .bmod, so a record naming it would
// dangle and take the whole interface down with it. (Rejecting that
// combination at the LIBRARY build is P9 enforcement, which U3 owns —
// until then U2's job is simply not to emit an unreadable file.)
if ( exportedProtocols.count( proto ) == 0 )
continue;
out << "impl " << proto << " for " << structDef->getName() << " {" << endl
<< "}" << endl;
}
}
bool BmodEmitter::structHasInterfaceMembers( StructDefinition *structDef )
{
for ( const auto &msp : structDef->getMethods() )
{
FunctionDefinition *m = const_cast<FunctionDefinition*>(
(const FunctionDefinition*)msp );
if ( m->isGeneric() )
continue;
// A PRIVATE init is still emitted (as a bare `init`) — see below.
if ( m->isPublic() || m->isInit() )
return true;
}
return false;
}
void BmodEmitter::emitStructInterface( StructDefinition *structDef, ostream &out )
{
out << "impl " << structDef->getName() << " {" << endl;
// A struct accumulates methods from every impl block that targets it — its
// own, plus one per `impl Protocol for Struct`. Emitting the list verbatim
// would repeat any method that satisfies a protocol, giving the re-parsed
// struct two copies of it. BLang has no overloading, so the name alone is
// the identity.
std::vector<std::string> emitted;
for ( const auto &msp : structDef->getMethods() )
{
FunctionDefinition *method = const_cast<FunctionDefinition*>(
(const FunctionDefinition*)msp );
bool already = false;
for ( const auto &n : emitted )
if ( n == method->getName() )
already = true;
if ( already )
continue;
emitted.push_back( method->getName() );
// Generic methods on a non-generic struct would need their body shipped
// to be monomorphized; that is out of this unit's scope, so skip them
// rather than emit an unusable signature.
if ( method->isGeneric() )
continue;
// VISIBILITY FILTER (D9, format 3+). Private by default: an unmarked
// method is module-visible only and does not belong in the interface.
//
// `init` is the one exception, and it is deliberate. A PRIVATE init is
// still emitted, as a bare `init(...)` with no `pub`, so a consumer can
// tell "this type's constructor is private" from "this type has no
// constructor" and get the better diagnostic. Under format 3 the
// spellings mean:
// pub init(...) -> externally constructible
// init(...) -> declared, but private (module-only)
// (absent) -> no constructor at all
// A format-2 .bmod must NOT be read under this rule: there an unmarked
// init was EXPORTED, because `pub` could not yet be written.
if ( !method->isPublic() && !method->isInit() )
continue;
if ( method->isInit() )
out << ( method->isPublic() ? "\tpub init(" : "\tinit(" );
else
out << "\tpub fn " << method->getName() << "(";
bool first = true;
for ( int i = 0; i < method->getNumberParams(); i++ )
{
VariableDefinition *param = method->getParam( i );
bool isSelf = ( param->getVariableType() != nullptr &&
param->getVariableType()->getName() == "self" );
// `init` carries an implicit self that is re-created on parse, so
// it must not appear in the emitted signature.
if ( isSelf && method->isInit() )
continue;
if ( !first )
out << ", ";
first = false;
if ( isSelf )
out << "self";
else
{
emitType( nc( param->getVariableType() ), out );
out << " " << param->getName();
}
}
out << ")";
if ( !method->isInit() && method->getReturnType() != nullptr )
{
out << " -> ";
emitType( method->getReturnType(), out );
}
out << ";" << endl;
}
out << "}" << endl;
}
void BmodEmitter::emitEnum( EnumDefinition *enumDef, ostream &out )
{
if ( !enumDef->isPublic() )
return;
emitAnnotations( enumDef->getAnnotations(), out );
out << "pub enum " << enumDef->getName();
emitGenericParams( enumDef->getGenericParams(), out );
out << " {" << endl;
const auto &variants = enumDef->getVariants();
for ( size_t i = 0; i < variants.size(); i++ )
{
out << "\t" << variants[i].mName;
if ( !variants[i].mAssociatedTypes.empty() )
{
out << "(";
for ( size_t j = 0; j < variants[i].mAssociatedTypes.size(); j++ )
{
if ( j > 0 )
out << ", ";
emitType( nc( (const Type*)variants[i].mAssociatedTypes[j] ), out );
}
out << ")";
}
if ( i + 1 < variants.size() )
out << ",";
out << endl;
}
out << "}" << endl;
}
void BmodEmitter::emitProtocol( ProtocolDefinition *protoDef, ostream &out )
{
if ( !protoDef->isPublic() )
return;
out << "pub protocol " << protoDef->getName();
emitGenericParams( protoDef->getGenericParams(), out );
out << " {" << endl;
for ( const auto &sp : protoDef->getRequiredMethods() )
{
FunctionDefinition *method = const_cast<FunctionDefinition*>( (const FunctionDefinition*)sp );
out << "\tfn " << method->getName() << "(";
for ( int i = 0; i < method->getNumberParams(); i++ )
{
if ( i > 0 )
out << ", ";
VariableDefinition *param = method->getParam( i );
if ( param->getVariableType()->getName() == "self" )
out << "self";
else
{
emitType( nc( param->getVariableType() ), out );
out << " " << param->getName();
}
}
out << ")";
if ( method->getReturnType() != nullptr )
{
out << " -> ";
emitType( method->getReturnType(), out );
}
out << ";" << endl;
}
out << "}" << endl;
}
// Collect every type NAME referenced by a Type, recursing into generic args.
static void collectTypeNames( Type *t, std::set<std::string> &names )
{
if ( t == nullptr )
return;
names.insert( t->getName() );
for ( int i = 0; i < t->getNumTypeParams(); i++ )
collectTypeNames( t->getTypeParam( i ), names );
}
void BmodEmitter::emit( const vector<Module*> &modules, ostream &out, Scope *scope )
{
out << "// auto-generated .bmod interface file — do not edit" << endl;
// Format version, deliberately a COMMENT: a compiler that predates the
// marker still parses the file. A version mismatch should cost a cache miss
// and a rebuild, never a syntax error inside a generated file.
out << "// blang-bmod-format: " << kFormatVersion << endl;
// modules-v2-graph U5 (done-condition 7): emit FOREIGN-TYPE references. A type
// used in an exported signature but DEFINED in another module (this lib imports
// Q and returns Q's Box<int>) is recorded as
// // foreign-type: <name> <human-module-name> <identity-digest>
// so this interface (a) parses STANDALONE — the reader registers <name> before
// parsing signatures that use it — and (b) mangles the foreign type with its
// DEFINING module's identity digest, not the reader's. Reference-by-identity
// only — foreign bodies are NOT embedded; the transitive .bmod closure (bcc)
// supplies a generic's body for monomorphization.
if ( scope != nullptr )
{
std::set<std::string> ownTypes;
for ( auto *mod : modules )
{
for ( const auto &sp : mod->getStructList() )
ownTypes.insert( ( (const StructDefinition *)sp )->getName() );
for ( const auto &sp : mod->getEnumList() )
ownTypes.insert( ( (const EnumDefinition *)sp )->getName() );
}
std::set<std::string> referenced;
for ( auto *mod : modules )
{
for ( const auto &sp : mod->getFunctionList() )
{
FunctionDefinition *f = const_cast<FunctionDefinition *>(
(const FunctionDefinition *)sp );
if ( !f->isPublic() )
continue;
collectTypeNames( f->getReturnType(), referenced );
for ( int i = 0; i < f->getNumberParams(); i++ )
collectTypeNames( f->getParam( i )->getVariableType(), referenced );
}
for ( const auto &sp : mod->getStructList() )
{
StructDefinition *s = const_cast<StructDefinition *>(
(const StructDefinition *)sp );
if ( !s->isPublic() )
continue;
for ( const auto &msp : s->getMethods() )
{
FunctionDefinition *m = const_cast<FunctionDefinition *>(
(const FunctionDefinition *)msp );
if ( !m->isPublic() )
continue;
collectTypeNames( m->getReturnType(), referenced );
for ( int i = 0; i < m->getNumberParams(); i++ )
collectTypeNames( m->getParam( i )->getVariableType(), referenced );
}
}
}
for ( const auto &name : referenced )
{
if ( ownTypes.count( name ) )
continue;
Symbol *sym = scope->findSymbol( name );
StructDefinition *sd = dynamic_cast<StructDefinition *>( sym );
if ( sd == nullptr || sd->getModuleDigest().empty() )
continue;
std::string human = sd->getDefiningFile();
if ( human.empty() )
human = "?";
out << "// foreign-type: " << name << " " << human << " "
<< sd->getModuleDigest() << endl;
}
}
out << endl;
// Names a consumer of this file will be able to resolve: the protocols this
// .bmod declares, plus the builtins that are in scope everywhere.
std::set<std::string> exportedProtocols;
// KEEP IN SYNC with the builtin protocol registration in
// QModule.cpp (createGlobalScope, "Register Printable as a builtin
// protocol"). A builtin is resolvable in every scope without being declared
// in any .bmod, so it must be listed here or every conformance record naming
// it would be silently dropped (known-issues KI-15 is the same failure for
// protocols arriving via a dependency).
exportedProtocols.insert( "Printable" );
for ( auto *mod : modules )
for ( const auto &sp : mod->getProtocolList() )
{
ProtocolDefinition *p = const_cast<ProtocolDefinition*>( (const ProtocolDefinition*)sp );
if ( p->isPublic() )
exportedProtocols.insert( p->getName() );
}
for ( auto *mod : modules )
{
// PROTOCOLS FIRST. A struct's conformance record (`impl P for S { }`)
// names a protocol, and the impl-block parser resolves that name at the
// point of use — so emitting protocols after structs makes every record a
// forward reference and the whole interface unparseable:
//
// t.bmod:11:23: error: Unknown protocol 'Sizeable' in impl block
//
// It went unnoticed at first because the only conformance in the corpus
// was to `Printable`, the one builtin protocol pre-registered in every
// scope. Any user-defined `pub protocol` broke its library's interface.
//
// The "a record must follow its struct's interface block" constraint
// (conformance checking reads the struct's accumulated methods) concerns
// the STRUCT, not the protocol, so both orderings satisfy it.
for ( const auto &sp : mod->getProtocolList() )
{
ProtocolDefinition *protoDef = const_cast<ProtocolDefinition*>( (const ProtocolDefinition*)sp );
emitProtocol( protoDef, out );
out << endl;
}
// Emit structs
for ( const auto &sp : mod->getStructList() )
{
StructDefinition *structDef = const_cast<StructDefinition*>( (const StructDefinition*)sp );
emitStruct( structDef, out, exportedProtocols );
out << endl;
}
// Emit enums
for ( const auto &sp : mod->getEnumList() )
{
EnumDefinition *enumDef = const_cast<EnumDefinition*>( (const EnumDefinition*)sp );
emitEnum( enumDef, out );
out << endl;
}
// Emit functions
for ( const auto &sp : mod->getFunctionList() )
{
FunctionDefinition *func = const_cast<FunctionDefinition*>( (const FunctionDefinition*)sp );
emitFunction( func, out );
}
}
}