This repository was archived by the owner on Jan 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathScopeScanner.cpp
More file actions
137 lines (113 loc) · 4.33 KB
/
ScopeScanner.cpp
File metadata and controls
137 lines (113 loc) · 4.33 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
#include "Sema/ScopeScanner.h"
void ScopeScanner::visit(const SharedPtr<ModuleNode> &module) {
SharedPtr<TopLevelScope> topLevelScope = TopLevelScope::create();
module->internalScope = topLevelScope;
topLevelScope->host = module;
pushScope(topLevelScope);
ASTVisitor::visit(module);
popScope();
}
void ScopeScanner::visit(const SharedPtr<VarDeclNode> &varDecl) {
varDecl->scope = getCurrentScope();
}
void ScopeScanner::visit(const SharedPtr<ParmVarDeclNode> ¶mVarDecl) {
paramVarDecl->scope = getCurrentScope();
}
void ScopeScanner::visit(const SharedPtr<FunctionDeclNode> &funcDecl) {
SharedPtr<TopLevelScope> topLevelScope = dynPtrCast<TopLevelScope>(getCurrentScope());
if (topLevelScope) {
funcDecl->scope = topLevelScope;
} else {
reportSemanticError("Nested functions are not allowed");
}
// 函数参数作用域: 在外层作作用域与内层作用域之间的辅助作用域
SharedPtr<LocalScope> paramScope = LocalScope::create(topLevelScope);
funcDecl->internalScope = paramScope;
paramScope->host = funcDecl;
pushScope(paramScope);
ASTVisitor::visit(funcDecl);
popScope();
}
void ScopeScanner::visit(const SharedPtr<BooleanLiteralExprNode> &boolLiteralExpr) {
boolLiteralExpr->scope = getCurrentScope();
}
void ScopeScanner::visit(const SharedPtr<IntegerLiteralExprNode> &intLiteralExpr) {
intLiteralExpr->scope = getCurrentScope();
}
void ScopeScanner::visit(const SharedPtr<FloatLiteralExprNode> &floatLiteralExpr) {
floatLiteralExpr->scope = getCurrentScope();
}
void ScopeScanner::visit(const SharedPtr<StringLiteralExprNode> &strLiteralExpr) {
strLiteralExpr->scope = getCurrentScope();
}
void ScopeScanner::visit(const SharedPtr<ArrayLiteralExprNode> &arrayLiteralExpr) {
arrayLiteralExpr->scope = getCurrentScope();
ASTVisitor::visit(arrayLiteralExpr);
}
void ScopeScanner::visit(const SharedPtr<IdentifierExprNode> &varExpr) {
varExpr->scope = getCurrentScope();
}
void ScopeScanner::visit(const SharedPtr<CallExprNode> &callExpr) {
callExpr->scope = getCurrentScope();
ASTVisitor::visit(callExpr);
}
void ScopeScanner::visit(const SharedPtr<UnaryOperatorExprNode> &uopExpr) {
uopExpr->scope = getCurrentScope();
ASTVisitor::visit(uopExpr);
}
void ScopeScanner::visit(const SharedPtr<BinaryOperatorExprNode> &bopExpr) {
bopExpr->scope = getCurrentScope();
ASTVisitor::visit(bopExpr);
}
void ScopeScanner::visit(const SharedPtr<ArraySubscriptExprNode> &asExpr) {
asExpr->scope = getCurrentScope();
ASTVisitor::visit(asExpr);
}
void ScopeScanner::visit(const SharedPtr<ExprStmtNode> &exprStmt) {
exprStmt->scope = getCurrentScope();
ASTVisitor::visit(exprStmt);
}
void ScopeScanner::visit(const SharedPtr<CompoundStmtNode> &compStmt) {
compStmt->scope = getCurrentScope();
SharedPtr<LocalScope> blockScope = LocalScope::create(getCurrentScope());
compStmt->internalScope = blockScope;
blockScope->host = compStmt;
pushScope(blockScope);
ASTVisitor::visit(compStmt);
popScope();
}
void ScopeScanner::visit(const SharedPtr<VarDeclStmtNode> &varDeclStmt) {
varDeclStmt->scope = getCurrentScope();
ASTVisitor::visit(varDeclStmt);
}
void ScopeScanner::visit(const SharedPtr<FunctionDeclStmtNode> &funcDeclStmt) {
funcDeclStmt->scope = getCurrentScope();
ASTVisitor::visit(funcDeclStmt);
}
void ScopeScanner::visit(const SharedPtr<IfStmtNode> &ifStmt) {
ifStmt->scope = getCurrentScope();
ASTVisitor::visit(ifStmt);
}
void ScopeScanner::visit(const SharedPtr<WhileStmtNode> &whileStmt) {
whileStmt->scope = getCurrentScope();
ASTVisitor::visit(whileStmt);
}
void ScopeScanner::visit(const SharedPtr<ForStmtNode> &forStmt) {
forStmt->scope = getCurrentScope();
SharedPtr<LocalScope> forScope = LocalScope::create(getCurrentScope());
forStmt->internalScope = forScope;
forScope->host = forStmt;
pushScope(forScope);
ASTVisitor::visit(forStmt);
popScope();
}
void ScopeScanner::visit(const SharedPtr<ContinueStmtNode> &continueStmt) {
continueStmt->scope = getCurrentScope();
}
void ScopeScanner::visit(const SharedPtr<BreakStmtNode> &breakStmt) {
breakStmt->scope = getCurrentScope();
}
void ScopeScanner::visit(const SharedPtr<ReturnStmtNode> &returnStmt) {
returnStmt->scope = getCurrentScope();
ASTVisitor::visit(returnStmt);
}