Skip to content

Commit

Permalink
fix: fix the way to deal with overload function
Browse files Browse the repository at this point in the history
  • Loading branch information
dingzifan719 committed Aug 1, 2022
1 parent ea7262c commit 6b5d392
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 23 deletions.
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.mhoffrog.attached/org.eclipse.cdt.core -->
<dependency>
<groupId>com.github.mhoffrog.attached</groupId>
<artifactId>org.eclipse.cdt.core</artifactId>
<version>5.11.0</version>
</dependency>
<!-- &lt;!&ndash; https://mvnrepository.com/artifact/com.github.mhoffrog.attached/org.eclipse.cdt.core &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.github.mhoffrog.attached</groupId>-->
<!-- <artifactId>org.eclipse.cdt.core</artifactId>-->
<!-- <version>5.11.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void main(String[] args) throws Exception {
System.out.println("Entity found finish!");
processor.dependencyBuild();
processor.outputFile(projectName);
// System.out.println("Process finish!");
System.out.println("Process finish!");
}
long endTime = System.currentTimeMillis();
System.out.println("Consumed time: " + (float) ((endTime - startTime) / 1000.00) + " s, or "
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/cdt/CppVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,10 @@ public void FoundFunctionDeclaration(CPPASTFunctionDeclarator declarator, ICPPAS

@Override
public int leave(IASTDeclaration declaration) {

if (declaration instanceof IASTSimpleDeclaration) {
IASTSimpleDeclaration simpleDeclaration = (IASTSimpleDeclaration) declaration;
IASTDeclSpecifier declSpec = simpleDeclaration.getDeclSpecifier();
if (declSpec instanceof CPPASTCompositeTypeSpecifier) {

int type = ((CPPASTCompositeTypeSpecifier) declSpec).getKey();
if (type == 1 || type == 2 || type == 3) {
context.popScope();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/cdt/FileParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public FileParser(String filepath, EntityRepo entityrepo,HashMap<String,Integer>
* @throws:
*/
public void parse( ) throws Exception {
System.out.println("Parse file path: " + this.filepath);
if(exitFile(filepath)) {
if(isFileParse(filepath)) {
return ;
Expand Down
52 changes: 38 additions & 14 deletions src/main/java/cdt/HandlerContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public int[] findFunction(String functionName, List<String> parameterLists){
if(scope.getSymbol(name) != null){
if(i == names.length - 1){
FunctionSymbol symbol = (FunctionSymbol)scope.getSymbolByKind(name, Configure.Function);
if(symbol == null) continue;
if(symbol.isOverload()){
for(Integer id:symbol.getOverload_list()){
FunctionEntity getEntity = (FunctionEntity) entityRepo.getEntity(id);
Expand Down Expand Up @@ -179,19 +180,19 @@ public FunctionEntity foundFunctionDefine(String name, String returnType, Locati
if(this.currentScope.getSymbolByKind(functionEntity.getName(), Configure.Function) == null){
// TODO 情况1.1 存在完全一致的实体且当前作用域无相同名称实体
// TODO 这里需要替换成作用域解析符 处理过后的方法内容 这里直接用了API返回的名字 很有可能是ASTObject::ASTObject这种类型的
symbol = new FunctionSymbol(name, findFunctionID[1]);
symbol = new FunctionSymbol(functionEntity.getName(), findFunctionID[1]);
this.currentScope.define(symbol, Configure.Function);
}else{
// TODO 情况1.2 存在完全一致的实体 当前作用域内存在相同名称的函数,检测是否为Overload基类
FunctionSymbol functionSymbol = (FunctionSymbol) this.currentScope.getSymbolByKind(functionEntity.getName(), Configure.Function);
if(functionSymbol.isBaseOverload()){
// TODO 情况1.2.1 存在完全一致的实体 当前作用域内存在相同名称的函数, 是Overload基类
// TODO 检查是否有完全一致的函数实体存在
for(int overloadID:functionSymbol.getOverload_list()){
if(overloadID == findFunctionID[1]){
// TODO 存在完全一致的实体 需要使用该作用域
symbol = (FunctionSymbol) functionSymbol.getSymbolByKind(functionEntity.getNameWithSignature(), Configure.Function);
}
symbol = (FunctionSymbol) functionSymbol.getSymbolByKind(functionEntity.getNameWithSignature(), Configure.Function);
if(symbol == null){
symbol = new FunctionSymbol(functionEntity.getNameWithSignature(), findFunctionID[1]);
functionSymbol.define(symbol, Configure.Function);
functionSymbol.addOverload(findFunctionID[1]);
}
}else{
// TODO 情况1.2.2 存在完全一致的实体 当前作用域内存在相同名称的函数, 非overload基类,检测是否为同一个函数
Expand All @@ -206,15 +207,26 @@ public FunctionEntity foundFunctionDefine(String name, String returnType, Locati
// TODO 需要在出栈的时候确定一下 是否为baseOverload,
// TODO baseOverload function和Overload function可以被视作是一个整体 绑定起来
functionSymbol.setBaseOverload();
if(entityRepo.getEntity(functionSymbol.getEntityID()) instanceof FunctionEntity){
FunctionEntity baseOverload = (FunctionEntity)(entityRepo.getEntity((functionSymbol.getEntityID())));
FunctionSymbol baseOverloadSymbol = new FunctionSymbol(baseOverload.getNameWithSignature(),
baseOverload.getId());
functionSymbol.define(baseOverloadSymbol, Configure.Function);
baseOverload.setScope(baseOverloadSymbol);
}
symbol = new FunctionSymbol(functionEntity.getNameWithSignature(), findFunctionID[1]);
symbol.setOverload();
functionSymbol.define(symbol, Configure.Function);
functionSymbol.addOverload(findFunctionID[1]);
if(functionSymbol.getSymbolByKind(symbol.getName(), Configure.Function) == null){
functionSymbol.define(symbol, Configure.Function);
functionSymbol.addOverload(findFunctionID[1]);
}else{
symbol = (FunctionSymbol) functionSymbol.getSymbolByKind(symbol.getName(), Configure.Function);
}
}
}
}

}else{
}
else{
// 不存在已被声明过的函数实体
int id = entityRepo.generateId();
symbol = new FunctionSymbol(name, id);
Expand All @@ -232,10 +244,21 @@ public FunctionEntity foundFunctionDefine(String name, String returnType, Locati
FunctionSymbol functionSymbol = (FunctionSymbol) this.currentScope.getSymbolByKind(functionEntity.getName(), Configure.Function);
if(!functionSymbol.isBaseOverload()){
functionSymbol.setBaseOverload();
if(entityRepo.getEntity(functionSymbol.getEntityID()) instanceof FunctionEntity){
FunctionEntity baseOverload = (FunctionEntity)(entityRepo.getEntity((functionSymbol.getEntityID())));
FunctionSymbol baseOverloadSymbol = new FunctionSymbol(baseOverload.getNameWithSignature(),
baseOverload.getId());
functionSymbol.define(baseOverloadSymbol, Configure.Function);
baseOverload.setScope(baseOverloadSymbol);
}
}
symbol = new FunctionSymbol(functionEntity.getNameWithSignature(), findFunctionID[1]);
symbol.setOverload();
functionSymbol.define(symbol, Configure.Function);
if(functionSymbol.getSymbolByKind(symbol.getName(), Configure.Function) == null){
functionSymbol.define(symbol, Configure.Function);
}else{
symbol = (FunctionSymbol) functionSymbol.getSymbolByKind(symbol.getName(), Configure.Function);
}
functionSymbol.addOverload(id);
}
if(this.latestValidContainer() instanceof ClassEntity){
Expand Down Expand Up @@ -710,8 +733,9 @@ public void exitLastedEntity() {
* @return: void
*/
private void pushScope(Scope s) {
// if(s != null) System.out.println("push scope: " + s.getClass().toString() + " " + s.getName()+ " in " +this.currentFileEntity.getQualifiedName());
this.currentScope = s;
if(s != null){
this.currentScope = s;
}
}


Expand All @@ -731,7 +755,7 @@ public void popScope() {
}
}

public void showASTNode(IASTNode node,int i) {
public void showASTNode(IASTNode node, int i) {
if(node.getChildren()==null) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/entity/DataAggregateEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public boolean getIsSpecializationTemplate() {
return this.isTemplate & this.isSpecializationTemplate;
}
public void setScope(Scope symbol){this.scope = symbol;}
public Scope getScope(){return this.scope;}


public void setUsing(String using) {
Expand Down

0 comments on commit 6b5d392

Please sign in to comment.