forked from ballerina-platform/nballerina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbols.bal
118 lines (106 loc) · 3.37 KB
/
symbols.bal
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
import wso2/nballerina.bir;
import wso2/nballerina.types as t;
import wso2/nballerina.front.syntax as s;
import wso2/nballerina.comm.err;
import wso2/nballerina.comm.diagnostic as d;
type ModuleDefns table<s:ModuleLevelDefn> key(name);
// This is the module-level symbol table.
type ModuleSymbols record {|
ModuleDefns defns = table [];
map<Import>[] partPrefixes = [];
t:Context tc;
boolean allowAllTypes = false;
ResolvedType[] deferredEmptinessChecks = [];
// We need only one to give an error
d:Location? emptyNonCyclicTypeLocation = ();
|};
type ResolvedType record {|
t:SemType semType;
s:ModuleLevelDefn modDefn;
s:TypeDesc td;
|};
type Import record {|
s:ImportDecl? decl = ();
bir:ModuleId moduleId;
ModuleExports defns;
// This is when we haven't implemented everything in the module.
boolean partial;
boolean used = false;
|};
public type ExportedDefn t:FunctionSignature|t:SemType|s:ResolvedConst;
public type ModuleExports readonly & map<ExportedDefn>;
function symbolToString(ModuleSymbols mod, int partIndex, bir:Symbol sym) returns string {
string prefix;
if sym is bir:InternalSymbol {
prefix = "";
}
else {
bir:ModuleId modId = sym.module;
string? importPrefix = getPrefixForModuleId(mod, partIndex, modId);
if importPrefix == () {
string? org = modId.org;
string orgString = org == () ? "" : org + "/";
prefix = "{" + orgString + ".".'join(...sym.module.names) + "}";
}
else {
prefix = importPrefix + ":";
}
}
return prefix + sym.identifier;
}
function getPrefixForModuleId(ModuleSymbols mod, int partIndex, bir:ModuleId id) returns string? {
foreach var [prefix, { moduleId }] in mod.partPrefixes[partIndex].entries() {
if moduleId == id {
return prefix;
}
}
return ();
}
function createExports(ModuleSymbols mod) returns ModuleExports {
map<ExportedDefn> exports = {};
foreach var defn in mod.defns {
ExportedDefn|false? export;
if defn.vis != "public" {
continue;
}
if defn is s:FunctionDefn {
export = defn.signature;
}
else if defn is s:ConstDefn {
export = defn.resolved;
}
else {
export = defn.semType;
}
exports[defn.name] = <ExportedDefn>export;
}
return exports.cloneReadOnly();
}
function moduleIdDefaultPrefix(bir:ModuleId id) returns string {
// JBUG #33301 Bad, sad without `names` variable
string[] names = id.names;
return names[names.length() - 1];
}
function moduleIdToString(bir:ModuleId id) returns string {
string m = ".".'join(...id.names);
if id.org != "" {
return id.org + "/" + m;
}
else {
return m;
}
}
function lookupPrefix(ModuleSymbols mod, s:ModuleLevelDefn modDefn, string prefix, bir:Position pos) returns Import|err:Semantic {
Import? implicitImport = autoImportPrefixes[prefix];
if implicitImport != () {
return implicitImport;
}
Import? imported = mod.partPrefixes[modDefn.part.partIndex][prefix];
if imported == () {
return err:semantic(`no import declaration for ${prefix}`, loc=s:locationInDefn(modDefn, pos), defnName=modDefn.name);
}
else {
imported.used = true;
return imported;
}
}