|
1 | 1 | #include "ModuleLifter.hpp" |
| 2 | +#include "AST/Exprs.hpp" |
2 | 3 | #include "DITypeLifter.hpp" |
3 | 4 | #include "GILGen/GILGen.hpp" |
| 5 | +#include "TypeLifter.hpp" |
4 | 6 | #include "llvm/IR/Function.h" |
5 | 7 |
|
6 | 8 | namespace glu::irdec { |
@@ -39,35 +41,67 @@ class ModuleLifter { |
39 | 41 |
|
40 | 42 | glu::ast::ModuleDecl *detectExternalFunctions() |
41 | 43 | { |
42 | | - DITypeLifter typeLifter(_astContext); |
| 44 | + DITypeLifter diTypeLifter(_astContext); |
| 45 | + TypeLifter typeLifter(_astContext); |
43 | 46 | std::vector<glu::ast::DeclBase *> decls; |
| 47 | + auto &astArena = _astContext.getASTMemoryArena(); |
44 | 48 |
|
45 | 49 | for (auto &func : _llvmModule->functions()) { |
46 | 50 | if (!func.isDeclaration() |
47 | 51 | && func.getLinkage() == llvm::Function::ExternalLinkage) { |
48 | | - auto subProgram = func.getSubprogram(); |
49 | | - if (!subProgram) |
| 52 | + types::Ty type; |
| 53 | + llvm::StringRef funcName = func.getName(); |
| 54 | + if (auto subprogram = func.getSubprogram()) { |
| 55 | + type = diTypeLifter.lift(subprogram->getType()); |
| 56 | + funcName = subprogram->getName(); |
| 57 | + } else { |
| 58 | + type = typeLifter.lift(func.getFunctionType()); |
| 59 | + } |
| 60 | + auto funcType |
| 61 | + = llvm::dyn_cast_if_present<glu::types::FunctionTy>(type); |
| 62 | + if (!funcType) |
50 | 63 | continue; |
51 | | - auto type = typeLifter.lift(subProgram->getType()); |
52 | | - if (auto funcType |
53 | | - = llvm::dyn_cast_if_present<glu::types::FunctionTy>(type)) { |
54 | | - auto funcDecl |
55 | | - = _astContext.getASTMemoryArena() |
56 | | - .create<glu::ast::FunctionDecl>( |
57 | | - SourceLocation::invalid, nullptr, |
58 | | - func.getName(), funcType, |
59 | | - generateParamsDecls(funcType->getParameters() |
60 | | - ), |
61 | | - nullptr, glu::ast::Visibility::Public |
62 | | - ); |
63 | | - decls.push_back(funcDecl); |
| 64 | + llvm::SmallVector<ast::Attribute *, 4> attrs; |
| 65 | + if (funcName != func.getName()) { |
| 66 | + auto *attr = astArena.create<ast::Attribute>( |
| 67 | + ast::AttributeKind::LinkageNameKind, |
| 68 | + SourceLocation::invalid, |
| 69 | + astArena.create<ast::LiteralExpr>( |
| 70 | + func.getName(), nullptr, SourceLocation::invalid |
| 71 | + ) |
| 72 | + ); |
| 73 | + attrs.push_back(attr); |
| 74 | + } else { |
| 75 | + auto *attr = astArena.create<ast::Attribute>( |
| 76 | + ast::AttributeKind::NoManglingKind, |
| 77 | + SourceLocation::invalid, nullptr |
| 78 | + ); |
| 79 | + attrs.push_back(attr); |
| 80 | + } |
| 81 | + if (func.isVarArg()) { |
| 82 | + auto *attr = astArena.create<ast::Attribute>( |
| 83 | + ast::AttributeKind::CVariadicKind, |
| 84 | + SourceLocation::invalid, nullptr |
| 85 | + ); |
| 86 | + attrs.push_back(attr); |
64 | 87 | } |
| 88 | + ast::AttributeList *attributes |
| 89 | + = astArena.create<ast::AttributeList>( |
| 90 | + attrs, SourceLocation::invalid |
| 91 | + ); |
| 92 | + auto funcDecl = astArena.create<glu::ast::FunctionDecl>( |
| 93 | + SourceLocation::invalid, nullptr, funcName, funcType, |
| 94 | + generateParamsDecls(funcType->getParameters()), nullptr, |
| 95 | + glu::ast::Visibility::Public, attributes |
| 96 | + ); |
| 97 | + decls.push_back(funcDecl); |
65 | 98 | } |
66 | 99 | } |
67 | 100 |
|
68 | | - for (auto decl : typeLifter.getDeclBindings()) { |
| 101 | + for (auto decl : diTypeLifter.getDeclBindings()) { |
69 | 102 | decls.push_back(decl.second); |
70 | 103 | } |
| 104 | + // FIXME: Do the same with typeLifter!!! |
71 | 105 | return _astContext.getASTMemoryArena().create<glu::ast::ModuleDecl>( |
72 | 106 | SourceLocation::invalid, std::move(decls), &_astContext |
73 | 107 | ); |
|
0 commit comments