FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/src/ast/ast_export.cpp at master · rburkholder/daScript · GitHub
rburkholder
/
daScript
Public
forked from
GaijinEntertainment/daScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
daScript
/
src
/
ast
/
ast_export.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
337 lines (325 loc) · 12.3 KB
Breadcrumbs
daScript
/
src
/
ast
/
ast_export.cpp
Copy path
File metadata and controls
337 lines (325 loc) · 12.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
daScript/misc/platform.h
"
#
include
"
daScript/ast/ast.h
"
#
include
"
daScript/ast/ast_visitor.h
"
namespace
das
{
class
ClearUnusedSymbols
:
public
Visitor
{
public:
virtual
bool
canVisitStructureFieldInit
( Structure * )
override
{
return
false
; }
virtual
bool
canVisitArgumentInit
( Function *,
const
VariablePtr &, Expression * )
override
{
return
false
; }
virtual
void
preVisit
(ExprAddr * expr)
override
{
Visitor::preVisit
(expr);
if
( expr->
func
&& !expr->
func
->
used
&& !expr->
func
->
builtIn
) {
expr->
func
=
nullptr
;
}
}
virtual
void
preVisitExpression
(Expression * expr)
override
{
Visitor::preVisitExpression
(expr);
if
( expr->
rtti_isCallFunc
() ) {
auto
cfe =
static_cast
<ExprCallFunc *>(expr);
if
( cfe->
func
&& !cfe->
func
->
used
&& !cfe->
func
->
builtIn
) {
cfe->
func
=
nullptr
;
}
}
}
};
class
MarkSymbolUse
:
public
Visitor
{
public:
MarkSymbolUse
(
bool
bid ) : builtInDependencies(bid) {
}
__forceinline
void
push
(
const
VariablePtr & pvar ) {
if
( !tw )
return
;
*tw <<
string
(logTab,
'
\t
'
) << pvar->
getMangledName
() <<
"
\n
"
;
logTab ++;
}
__forceinline
void
push
(
const
FunctionPtr & pfun ) {
if
( !tw )
return
;
*tw <<
string
(logTab,
'
\t
'
) << pfun->
getMangledName
() <<
"
\n
"
;
logTab ++;
}
__forceinline
void
pop
() {
if
( !tw )
return
;
logTab --;
}
void
propageteVarUse
(
const
VariablePtr & var) {
assert
(var);
if
(var->
used
)
return
;
push
(var);
var->
used
=
true
;
for
(
const
auto
& gv : var->
useGlobalVariables
) {
propageteVarUse
(gv);
}
for
(
const
auto
& it : var->
useFunctions
) {
propagateFunctionUse
(it);
}
pop
();
}
void
propagateFunctionUse
(
const
FunctionPtr & fn) {
assert
(fn);
if
(fn->
used
)
return
;
if
(fn->
builtIn
)
return
;
push
(fn);
fn->
used
=
true
;
for
(
const
auto
& gv : fn->
useGlobalVariables
) {
propageteVarUse
(gv);
}
for
(
const
auto
& it : fn->
useFunctions
) {
propagateFunctionUse
(it);
}
pop
();
}
void
markVarsUsed
( ModuleLibrary & lib,
bool
forceAll ){
lib.
foreach
([&](Module * pm) {
for
(
auto
& var : pm->
globals
.
each
() ) {
if
( forceAll || var->
used
) {
var->
used
=
false
;
propageteVarUse
(var);
}
}
return
true
;
},
"
*
"
);
}
void
markUsedFunctions
( ModuleLibrary & lib,
bool
forceAll,
bool
initThis, Module * macroModule ) {
lib.
foreach
([&](Module * pm) {
if
( initThis && macroModule && pm!=macroModule )
return
true
;
for
(
auto
& fn : pm->
functions
.
each
() ) {
if
( (forceAll && !fn->
macroInit
) || fn->
exports
|| fn->
init
|| fn->
shutdown
|| (fn->
macroInit
&& initThis) ) {
propagateFunctionUse
(fn);
}
}
return
true
;
},
"
*
"
);
}
void
markModuleVarsUsed
( ModuleLibrary &, Module * inWhichModule ) {
for
(
auto
& var : inWhichModule->
globals
.
each
() ) {
var->
used
=
false
;
propageteVarUse
(var);
}
}
void
markModuleUsedFunctions
( ModuleLibrary &, Module * inWhichModule ) {
for
(
auto
& fn : inWhichModule->
functions
.
each
() ) {
if
( fn->
builtIn
|| fn->
macroInit
|| fn->
macroFunction
)
continue
;
if
( fn->
privateFunction
&& fn->
generated
&& fn->
fromGeneric
)
continue
;
//
instances of templates are never roots
if
( fn->
isClassMethod
&& fn->
classParent
->
macroInterface
)
continue
;
//
methods of macro interfaces
propagateFunctionUse
(fn);
}
}
void
RemoveUnusedSymbols
( Module & mod ) {
auto
functions =
das::move
(mod.
functions
);
auto
globals =
das::move
(mod.
globals
);
mod.
functionsByName
.
clear
();
//
mod.globals.clear();
for
(
auto
& fn : functions.
each
() ) {
if
( fn->
used
) {
if
( !mod.
addFunction
(fn,
true
) ) {
program->
error
(
"
internal error, failed to add function
"
+ fn->
name
,
"
"
,
"
"
, fn->
at
);
}
}
}
for
(
auto
& var : globals.
each
() ) {
if
( var->
used
) {
if
( !mod.
addVariable
(var,
true
) ) {
program->
error
(
"
internal error, failed to add variable
"
+ var->
name
,
"
"
,
"
"
, var->
at
);
}
}
}
}
protected:
ProgramPtr program;
FunctionPtr func;
Variable *
gVar
=
nullptr
;
bool
builtInDependencies;
int
logTab =
0
;
public:
TextWriter * tw =
nullptr
;
protected:
virtual
bool
canVisitStructureFieldInit
( Structure * )
override
{
return
false
; }
virtual
bool
canVisitArgumentInit
( Function *,
const
VariablePtr &, Expression * )
override
{
return
false
; }
//
global variable declaration
virtual
void
preVisitGlobalLet
(
const
VariablePtr & var)
override
{
Visitor::preVisitGlobalLet
(var);
gVar
= var.
get
();
var->
useFunctions
.
clear
();
var->
useGlobalVariables
.
clear
();
var->
used
=
false
;
}
virtual
VariablePtr
visitGlobalLet
(
const
VariablePtr & var)
override
{
gVar
=
nullptr
;
return
Visitor::visitGlobalLet
(var);
}
//
function
virtual
void
preVisit
(Function * f)
override
{
Visitor::preVisit
(f);
func = f;
func->
useFunctions
.
clear
();
func->
useGlobalVariables
.
clear
();
func->
used
=
false
;
DAS_ASSERTF
(!func->
builtIn
,
"
visitor should never call 'visit' on builtin function at top level.
"
);
}
virtual
FunctionPtr
visit
(Function * that)
override
{
func.
reset
();
return
Visitor::visit
(that);
}
//
variable
virtual
void
preVisit
(ExprVar * expr)
override
{
Visitor::preVisit
(expr);
if
(!expr->
variable
)
return
;
if
(!expr->
local
&& !expr->
argument
&& !expr->
block
) {
if
(func) {
func->
useGlobalVariables
.
insert
(expr->
variable
.
get
());
}
else
if
(
gVar
) {
gVar
->
useGlobalVariables
.
insert
(expr->
variable
.
get
());
}
}
}
//
function address
virtual
void
preVisit
(ExprAddr * addr)
override
{
Visitor::preVisit
(addr);
if
(builtInDependencies || (addr->
func
&& !addr->
func
->
builtIn
)) {
assert
(addr->
func
);
if
(func) {
func->
useFunctions
.
insert
(addr->
func
);
}
else
if
(
gVar
) {
gVar
->
useFunctions
.
insert
(addr->
func
);
}
}
}
//
function call
virtual
void
preVisit
(ExprCall * call)
override
{
Visitor::preVisit
(call);
if
( !call->
func
)
return
;
if
(builtInDependencies || !call->
func
->
builtIn
) {
if
(func) {
func->
useFunctions
.
insert
(call->
func
);
}
else
if
(
gVar
) {
gVar
->
useFunctions
.
insert
(call->
func
);
}
}
}
//
new
virtual
void
preVisit
(ExprNew * call)
override
{
Visitor::preVisit
(call);
if
( call->
initializer
) {
if
(builtInDependencies || !call->
func
->
builtIn
) {
assert
(call->
func
);
if
(func) {
func->
useFunctions
.
insert
(call->
func
);
}
else
if
(
gVar
) {
gVar
->
useFunctions
.
insert
(call->
func
);
}
}
}
}
//
Op1
virtual
void
preVisit
(ExprOp1 * expr)
override
{
Visitor::preVisit
(expr);
if
(builtInDependencies || !expr->
func
->
builtIn
) {
assert
(expr->
func
);
if
(func) {
func->
useFunctions
.
insert
(expr->
func
);
}
else
if
(
gVar
) {
gVar
->
useFunctions
.
insert
(expr->
func
);
}
}
}
//
Op2
virtual
void
preVisit
(ExprOp2 * expr)
override
{
Visitor::preVisit
(expr);
if
(builtInDependencies || !expr->
func
->
builtIn
) {
assert
(expr->
func
);
if
(func) {
func->
useFunctions
.
insert
(expr->
func
);
}
else
if
(
gVar
) {
gVar
->
useFunctions
.
insert
(expr->
func
);
}
}
}
//
Op3
virtual
void
preVisit
(ExprOp3 * expr)
override
{
Visitor::preVisit
(expr);
if
( expr->
func
&& (builtInDependencies || !expr->
func
->
builtIn
) ) {
assert
(expr->
func
);
if
(func) {
func->
useFunctions
.
insert
(expr->
func
);
}
else
if
(
gVar
) {
gVar
->
useFunctions
.
insert
(expr->
func
);
}
}
}
};
void
Program::clearSymbolUse
() {
for
(
auto
& pm : library.
modules
) {
for
(
auto
& var : pm->
globals
.
each
() ) {
var->
used
=
false
;
}
for
(
auto
& fn : pm->
functions
.
each
() ) {
fn->
used
=
false
;
}
}
}
void
Program::markModuleSymbolUse
(TextWriter * logs) {
//
this module public, this module export, this module init\shutdown
clearSymbolUse
();
MarkSymbolUse
vis
(
false
);
vis.
tw
= logs;
visit
(vis);
vis.
markModuleUsedFunctions
(library, thisModule.
get
());
vis.
markModuleVarsUsed
(library, thisModule.
get
());
}
void
Program::markMacroSymbolUse
(TextWriter * logs) {
//
this module macro init
clearSymbolUse
();
MarkSymbolUse
vis
(
false
);
vis.
tw
= logs;
visit
(vis);
vis.
markUsedFunctions
(library,
false
,
true
, thisModule.
get
());
vis.
markVarsUsed
(library,
false
);
}
void
Program::markExecutableSymbolUse
(TextWriter * logs) {
clearSymbolUse
();
MarkSymbolUse
vis
(
false
);
vis.
tw
= logs;
visit
(vis);
vis.
markUsedFunctions
(library,
false
,
false
,
nullptr
);
vis.
markVarsUsed
(library,
false
);
}
void
Program::markFoldingSymbolUse
(TextWriter * logs) {
clearSymbolUse
();
MarkSymbolUse
vis
(
false
);
vis.
tw
= logs;
visit
(vis);
vis.
markUsedFunctions
(library,
true
,
true
,
nullptr
);
vis.
markVarsUsed
(library,
true
);
}
void
Program::markSymbolUse
(
bool
builtInSym,
bool
forceAll,
bool
initThis, Module * macroModule, TextWriter * logs) {
clearSymbolUse
();
MarkSymbolUse
vis
(builtInSym);
vis.
tw
= logs;
visit
(vis);
vis.
markUsedFunctions
(library, forceAll, initThis, macroModule);
vis.
markVarsUsed
(library, forceAll);
}
void
Program::removeUnusedSymbols
() {
if
( options.
getBoolOption
(
"
remove_unused_symbols
"
,
true
) ) {
ClearUnusedSymbols cvis;
visit
(cvis);
MarkSymbolUse
vis
(
false
);
vis.
RemoveUnusedSymbols
(*thisModule);
}
}
void
Program::dumpSymbolUse
(TextWriter & logs) {
logs <<
"
USED SYMBOLS ARE:
\n
"
;
for
(
auto
& pm : library.
modules
) {
for
(
auto
& var : pm->
globals
.
each
() ) {
if
( var->
used
) {
logs <<
"
let
"
<< var->
module
->
name
<<
"
::
"
<< var->
name
<<
"
:
"
<< var->
type
->
describe
() <<
"
\n
"
;
}
}
for
(
auto
& func : pm->
functions
.
each
() ) {
if
( func->
used
) {
logs << func->
module
->
name
<<
"
::
"
<< func->
describe
() <<
"
\n
"
;
}
}
}
logs <<
"
\n\n
"
;
}
}
Back
|
FazBrowse Home
|
New Git URL