-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBmodEmitter.h
More file actions
84 lines (71 loc) · 4 KB
/
Copy pathBmodEmitter.h
File metadata and controls
84 lines (71 loc) · 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
#ifndef BLANG_BMOD_EMITTER_H_
#define BLANG_BMOD_EMITTER_H_
#include "BmodFormat.h"
#include <set>
#include <string>
#include <vector>
#include <ostream>
#include "Type.h"
namespace QLang
{
class BmodEmitter
{
public:
// The .bmod interface format version (see BmodFormat.h for the history
// and the bump rule). Aliased here so emitter code reads naturally.
static const int kFormatVersion = BlangBmod::kFormatVersion;
// Emit a .bmod interface file from one or more parsed modules.
// Only pub symbols are emitted. `scope` (may be null) is used to resolve
// FOREIGN types referenced in exported signatures — a type defined in another
// module — so U5 can emit `// foreign-type:` headers carrying that type's
// defining-module identity (digest) + human name, letting this interface parse
// standalone and mangle the foreign type with the DEFINING module's identity.
static void emit( const std::vector<Module*> &modules, std::ostream &out,
Scope *scope = nullptr );
private:
static void emitFunction( FunctionDefinition *func, std::ostream &out );
static void emitStruct( StructDefinition *structDef, std::ostream &out,
const std::set<std::string> &exportedProtocols );
// Non-generic structs ship an `impl` block of bodyless init/method
// SIGNATURES — the interface that makes an imported type constructible and
// callable (P8). The init signature doubles as the struct's factory record:
// its presence tells a consumer to construct through the library-emitted
// factory instead of allocating locally.
static void emitStructInterface( StructDefinition *structDef, std::ostream &out );
// True when a non-generic struct has anything worth an interface block:
// any `pub` member, or an `init` of either visibility (a private init is
// still declared so a consumer can be told the constructor is private
// rather than absent).
static bool structHasInterfaceMembers( StructDefinition *structDef );
// A `table` or `@json` struct whose SHAPE is its data contract (DB columns,
// JSON keys, D15). Its field declarations must cross the `.bmod` boundary as
// compiler-facing metadata — and editing one must move the interface hash.
// Every OTHER non-generic struct drops its fields (format 4): a private
// field's type is not part of the interface, so a consumer cannot name it
// and internal changes stop rebuilding downstream. Generic structs are
// exempt from both rules (they ship full layout + bodies for monomorphization).
static bool isDataContractStruct( StructDefinition *structDef );
// Protocol conformance records (`impl Protocol for Type { }`, D16), emitted
// after the interface block so the conformance check sees the methods.
//
// `exportedProtocols` is the set of protocol names a consumer will be able to
// resolve from this file: the protocols this .bmod itself declares, plus the
// always-in-scope builtins. A record naming anything else would be a dangling
// reference that makes the whole interface unparseable, so it is skipped.
static void emitConformances( StructDefinition *structDef, std::ostream &out,
const std::set<std::string> &exportedProtocols );
static void emitEnum( EnumDefinition *enumDef, std::ostream &out );
static void emitProtocol( ProtocolDefinition *protoDef, std::ostream &out );
static void emitAnnotations( const std::vector<AnnotationNode> &annotations, std::ostream &out );
static void emitGenericParams( const std::vector<GenericParam> ¶ms, std::ostream &out );
static void emitType( Type *type, std::ostream &out );
// Cross-module generics: a generic definition's BODY is shipped in the
// .bmod (verbatim source slice) so consumers can monomorphize it — a
// signature alone would leave every downstream instantiation a linker
// error. Returns the source text from the start of the definition's line
// through its brace-matched closing '}', or empty on failure (caller falls
// back to signature-only emission).
static std::string sliceDefinitionSource( const SourceLocation &loc );
};
} // namespace QLang
#endif // BLANG_BMOD_EMITTER_H_