hoshi-lang dev
Yet another programming language
Loading...
Searching...
No Matches
visitor.h
Go to the documentation of this file.
1//
2// Created by XIaokang00010 on 2024/9/6.
3//
4
5#ifndef HOSHI_LANG_VISITOR_H
6#define HOSHI_LANG_VISITOR_H
7
12#include "share/def.hpp"
13#include <compiler/ir/IR.h>
15#include <memory>
16#include <ranges>
17#include <stdexcept>
18
19namespace yoi {
20
21 class visitor {
24 bool isVariadic = false;
25 bool isVirtual = false;
26 bool isCastRequired = false;
28 std::shared_ptr<IRValueType> variadicElementType = nullptr;
29 std::shared_ptr<IRFunctionDefinition> function = nullptr;
30
31 bool found() const;
32 };
33
34 std::stack<std::pair<std::shared_ptr<yoi::moduleContext>, yoi::indexT>> moduleContextStack;
35
36 void pushModuleContext(yoi::indexT moduleIndex);
37
38 void popModuleContext();
39
40 public:
41 std::shared_ptr<yoi::moduleContext> moduleContext;
42 std::shared_ptr<yoi::IRModule> irModule;
44
45 visitor(const std::shared_ptr<yoi::moduleContext> &moduleContext, const std::shared_ptr<yoi::IRModule> &irModule, yoi::indexT moduleIndex);
46
47 std::shared_ptr<yoi::IRModule> visit();
48
56
57 yoi::indexT isModuleName(identifier *it, yoi::indexT currentModule) const;
58
67
68 bool isVisitingGlobalScope() const;
69
71
72 void emitBasicCastTo(const std::shared_ptr<IRValueType> &toType);
73
74 yoi::wstr getInterfaceNameStr(const std::pair<yoi::indexT, yoi::indexT> &interfaceSrc);
75
76 yoi::wstr getTypeSpecUniqueNameStr(const std::shared_ptr<IRValueType> &type);
77
78 yoi::wstr getFuncUniqueNameStr(const std::vector<std::shared_ptr<IRValueType>> &argumentTypes, bool whetherIgnoreFirstParam = false);
79
80 std::shared_ptr<IRValueType> getIncompleteType(const yoi::wstr &typeName) const;
81
83
85
87 specializeFunctionTemplate(yoi::funcDefStmt *astNode, const yoi::vec<std::shared_ptr<IRValueType>> &templateArgs, yoi::indexT moduleIndex);
88
90 const yoi::vec<std::shared_ptr<IRValueType>> &concreteTemplateArgs,
91 yoi::implStmt *pureTemplateImplAst,
92 yoi::indexT moduleIndex);
93
94 std::pair<yoi::indexT, yoi::wstr> specializeStructMethodDeclaration(IRTemplateBuilder &structTemplate,
95 yoi::structDefInnerPair *methodAstNode,
96 const yoi::wstr &specializedStructName,
97 const yoi::vec<std::shared_ptr<IRValueType>> &concreteTemplateArgs,
98 yoi::indexT moduleIndex);
99
101 const std::shared_ptr<IRStructDefinition> &specializedStruct,
102 yoi::implInnerPair *methodAstNode,
103 const yoi::wstr &specializedStructName,
104 const yoi::vec<std::shared_ptr<IRValueType>> &concreteTemplateArgs,
105 yoi::indexT moduleIndex);
106
109
110 yoi::indexT specializeStructMethodTemplate(const std::shared_ptr<IRStructDefinition> &structDef,
113 const yoi::wstr &baseMethodName,
114 const yoi::vec<std::shared_ptr<IRValueType>> &methodTemplateArgs,
115 yoi::indexT moduleIndex);
116
118 const yoi::wstr &baseMethodName,
119 const yoi::vec<std::shared_ptr<IRValueType>> &specializedArgTypes);
120
122 const yoi::vec<std::shared_ptr<IRValueType>> &concreteTemplateArgs,
123 yoi::indexT moduleIndex);
124
126 const std::shared_ptr<IRValueType> &concreteStructType,
127 const yoi::wstr &specializedStructName,
128 const yoi::vec<std::shared_ptr<IRValueType>> &concreteTemplateArgs,
129 yoi::indexT targetModule);
130
131 yoi::wstr getMangledTemplateName(const yoi::wstr &baseName, const yoi::vec<std::shared_ptr<IRValueType>> &templateArgs);
132
133 void tryCastTo(const std::shared_ptr<IRValueType> &toType);
134
135 bool canCastTo(const std::shared_ptr<IRValueType> &fromType, const std::shared_ptr<IRValueType> &toType);
136
137 std::set<IRFunctionDefinition::FunctionAttrs> getFunctionAttributes(const yoi::vec<lexer::token> &attrs);
138
139 yoi::indexT generateNullInterfaceImplementation(const std::shared_ptr<IRValueType> &structType);
140
147 void visit(yoi::hoshiModule *module);
148
150
152
153 yoi::indexT visit(yoi::identifier *identifier, bool isStoreOp = false);
154
155 yoi::indexT visitExtern(yoi::identifier *identifier, yoi::indexT targetModule, bool isStoreOp = false);
156
158
160
161 yoi::indexT visit(yoi::subscriptExpr *subscriptExpr, bool isStoreOp = false);
162
163 yoi::indexT visitExtern(yoi::subscriptExpr *subscriptExpr, yoi::indexT targetModule, bool isStoreOp = false);
164
165 yoi::indexT visit(yoi::memberExpr *memberExpr, bool isStoreOp = false);
166
167 yoi::indexT visit(yoi::primary *primary, bool isStoreOp = false);
168
169 yoi::indexT visit(yoi::abstractExpr *abstractExpr, bool isStoreOp = false);
170
171 yoi::indexT visit(yoi::uniqueExpr *uniqueExpr, bool isStoreOp = false);
172
174
176
178
180
182
184
186
188
190
192
194
196
198
200
202
203 void visit(yoi::codeBlock *codeBlock, bool notEmitNewBlockInstruction = false);
204
206
208
210
212
214
216
218
220
222
224
226
227 static yoi::wstr getInterfaceImplName(const std::pair<yoi::indexT, yoi::indexT> &interfaceSrc, const std::shared_ptr<IRValueType> &typeSrc);
228
229 std::pair<std::pair<yoi::indexT, yoi::indexT>, std::shared_ptr<IRInterfaceInstanceDefinition>>
231
233
235
237
239
241
243
245
246 void visit(yoi::typeAliasStmt *typeAlias);
247
249
251
253
255
257
259
261
263
265
267
269
271
273
275
283
294 const yoi::vec<std::shared_ptr<IRValueType>> &argTypes,
295 yoi::indexT targetModule,
296 const std::shared_ptr<IRInterfaceInstanceDefinition> &interfaceContext);
297
307 const yoi::vec<std::shared_ptr<IRValueType>> &argTypes,
308 yoi::indexT targetModule,
309 const std::shared_ptr<IRStructDefinition> &structContext = nullptr);
310
314 bool handleInvocationExtern(const yoi::wstr &baseName,
316 yoi::indexT targetModule,
317 const std::shared_ptr<IRValueType> &structContext = nullptr,
318 bool noThisCall = false,
319 yoi::templateArg *templateArgs = nullptr);
320
330 template <typename T> yoi::indexT handleBinaryOperatorOverload(const yoi::wstr &overloadName, T *rhsAST);
331
339
346
354 yoi::indexT createCallableInterface(const yoi::vec<std::shared_ptr<IRValueType>> &parameterTypes,
355 const std::shared_ptr<IRValueType> &returnType);
356
367 std::pair<yoi::indexT, std::pair<yoi::indexT, yoi::indexT>> createCallableImplementationForLambda(
368 const std::shared_ptr<IRStructDefinition> &lambda, yoi::indexT lambdaStructIndex, yoi::indexT moduleIndex);
369
380 std::pair<yoi::indexT, std::pair<yoi::indexT, yoi::indexT>>
381 createCallableImplementationForFunction(const std::shared_ptr<IRFunctionDefinition> &func, yoi::indexT funcIndex, yoi::indexT moduleIndex, bool hasThis);
382
392 void createCallableInstanceForFunction(yoi::indexT implIndex, std::pair<yoi::indexT, yoi::indexT> callableInterfaceIndex, yoi::indexT moduleIndex, bool hasThis);
393
401
410
418 void constructDataStruct(yoi::indexT datastructIndex, yoi::indexT moduleIndex, yoi::invocationArguments *args);
419
420 std::shared_ptr<IRValueType> mapEnumTypeToBasicType(yoi::indexT targetModule, yoi::indexT targetEnumType);
421
429 std::shared_ptr<IRValueType> getGeneratorContext(const yoi::wstr &funcName, const std::shared_ptr<IRValueType> &yieldType);
430
431 void setupTemporaryConceptEvaluationEnvironment(yoi::indexT moduleIndex, const yoi::wstr &conceptName, const std::vector<std::shared_ptr<IRValueType>> &args);
432
433 std::pair<std::shared_ptr<IRConcept>, templateArg *> parseConceptName(yoi::externModuleAccessExpression *conceptName);
434
435 void evaluateConstraint(yoi::conceptStmt *stmt, const IRDebugInfo &currentDebugInfo);
436
438
439 void checkConceptSatisfaction(yoi::externModuleAccessExpression *stmt, const yoi::wstr &paramName, const std::shared_ptr<IRValueType> &args);
440
442 };
443
444} // namespace yoi
445
446#endif // HOSHI_LANG_VISITOR_H
yoi::indexT handleBinaryOperatorOverload(const yoi::wstr &overloadName, T *rhsAST)
Generates a call to certain operator overload function when left hand side or right hand side owns a ...
Definition visitor.cpp:4373
yoi::indexT visitExtern(yoi::identifier *identifier, yoi::indexT targetModule, bool isStoreOp=false)
Definition visitor.cpp:3118
yoi::indexTable< yoi::wstr, IRTemplateBuilder::Argument > getTemplateArgs(const yoi::defTemplateArg &templateArgs)
Definition visitor.cpp:3168
void emitBasicCastTo(const std::shared_ptr< IRValueType > &toType)
Definition visitor.cpp:3022
yoi::wstr getInterfaceNameStr(const std::pair< yoi::indexT, yoi::indexT > &interfaceSrc)
Definition visitor.cpp:3032
yoi::wstr getTypeSpecUniqueNameStr(const std::shared_ptr< IRValueType > &type)
Definition visitor.cpp:3036
yoi::indexT specializeStructMethodTemplate(const std::shared_ptr< IRStructDefinition > &structDef, yoi::structDefInnerPair *decl, yoi::implInnerPair *def, const yoi::wstr &baseMethodName, const yoi::vec< std::shared_ptr< IRValueType > > &methodTemplateArgs, yoi::indexT moduleIndex)
Definition visitor.cpp:3407
std::shared_ptr< IRValueType > mapEnumTypeToBasicType(yoi::indexT targetModule, yoi::indexT targetEnumType)
Definition visitor.cpp:5243
bool handleInvocationExtern(const yoi::wstr &baseName, yoi::invocationArguments *args, yoi::indexT targetModule, const std::shared_ptr< IRValueType > &structContext=nullptr, bool noThisCall=false, yoi::templateArg *templateArgs=nullptr)
Orchestrates an external function/method invocation IR generation.
Definition visitor.cpp:4227
std::pair< yoi::indexT, std::pair< yoi::indexT, yoi::indexT > > createCallableImplementationForFunction(const std::shared_ptr< IRFunctionDefinition > &func, yoi::indexT funcIndex, yoi::indexT moduleIndex, bool hasThis)
Create a Callable Implementation For Function object also create a unnamed struct for the function an...
Definition visitor.cpp:5394
yoi::wstr getFuncUniqueNameStr(const std::vector< std::shared_ptr< IRValueType > > &argumentTypes, bool whetherIgnoreFirstParam=false)
Definition visitor.cpp:3102
yoi::indexT handleUnaryOperatorOverload(const yoi::wstr &overloadName)
Handles unary operator overload function when the operand owns a appropriate overloaded operator meth...
Definition visitor.cpp:4481
void specializeStructMethodDefinition(IRTemplateBuilder &structTemplate, const std::shared_ptr< IRStructDefinition > &specializedStruct, yoi::implInnerPair *methodAstNode, const yoi::wstr &specializedStructName, const yoi::vec< std::shared_ptr< IRValueType > > &concreteTemplateArgs, yoi::indexT moduleIndex)
Definition visitor.cpp:3630
std::shared_ptr< yoi::IRModule > visit()
Definition visitor.cpp:33
bool handleSubscript(yoi::vec< yoi::subscript * >::iterator &it, yoi::vec< yoi::subscript * >::iterator end, bool isStoreOp, bool isLastTerm)
Handle subscript.
Definition visitor.cpp:4811
std::pair< yoi::indexT, yoi::wstr > specializeStructMethodDeclaration(IRTemplateBuilder &structTemplate, yoi::structDefInnerPair *methodAstNode, const yoi::wstr &specializedStructName, const yoi::vec< std::shared_ptr< IRValueType > > &concreteTemplateArgs, yoi::indexT moduleIndex)
Definition visitor.cpp:3515
std::stack< std::pair< std::shared_ptr< yoi::moduleContext >, yoi::indexT > > moduleContextStack
Definition visitor.h:34
visitor::OverloadResult resolveOverloadInterface(const yoi::wstr &baseName, const yoi::vec< std::shared_ptr< IRValueType > > &argTypes, yoi::indexT targetModule, const std::shared_ptr< IRInterfaceInstanceDefinition > &interfaceContext)
Resolves an function overload within the interface context.
Definition visitor.cpp:4716
yoi::indexT isModuleName(identifierWithTemplateArg *it, yoi::indexT currentModule) const
check if the subscript expression is a module name
Definition visitor.cpp:2925
static yoi::wstr getInterfaceImplName(const std::pair< yoi::indexT, yoi::indexT > &interfaceSrc, const std::shared_ptr< IRValueType > &typeSrc)
Definition visitor.cpp:2898
std::pair< yoi::indexT, std::pair< yoi::indexT, yoi::indexT > > createCallableImplementationForLambda(const std::shared_ptr< IRStructDefinition > &lambda, yoi::indexT lambdaStructIndex, yoi::indexT moduleIndex)
Create a Callable Implementation For Lambda object also create a struct for the lambda and return its...
Definition visitor.cpp:5056
yoi::indexT specializeStructTemplate(const yoi::wstr &templateName, const yoi::vec< std::shared_ptr< IRValueType > > &concreteTemplateArgs, yoi::implStmt *pureTemplateImplAst, yoi::indexT moduleIndex)
Definition visitor.cpp:3304
yoi::indexT specializeInterfaceTemplate(const yoi::wstr &templateName, const yoi::vec< std::shared_ptr< IRValueType > > &concreteTemplateArgs, yoi::indexT moduleIndex)
Definition visitor.cpp:4534
yoi::indexT createLambdaUnnamedStruct(yoi::lambdaExpr *lambdaExpr)
Create a Lambda Unnamed Struct object.
Definition visitor.cpp:4943
std::pair< std::shared_ptr< IRConcept >, templateArg * > parseConceptName(yoi::externModuleAccessExpression *conceptName)
Definition visitor.cpp:5745
void popModuleContext()
Definition visitor.cpp:4802
yoi::wstr parseIdentifierWithTemplateArg(yoi::identifierWithTemplateArg *identifierWithTemplateArg)
Definition visitor.cpp:2884
void emitBasicCastInBasicArithOpByLhsAndRhs(yoi::indexT lhs, yoi::indexT rhs)
Definition visitor.cpp:2973
void pushModuleContext(yoi::indexT moduleIndex)
Definition visitor.cpp:4794
IRValueType parseTypeSpecExtern(yoi::identifier *identifier, yoi::indexT targetModule)
Definition visitor.cpp:2770
OverloadResult resolveOverloadExtern(const yoi::wstr &baseName, const yoi::vec< std::shared_ptr< IRValueType > > &argTypes, yoi::indexT targetModule, const std::shared_ptr< IRStructDefinition > &structContext=nullptr)
Resolves an external function overload within a target module.
Definition visitor.cpp:4104
yoi::wstr getMangledTemplateName(const yoi::wstr &baseName, const yoi::vec< std::shared_ptr< IRValueType > > &templateArgs)
Definition visitor.cpp:3262
bool checkMarcoSatisfaction(yoi::marcoDescriptor *desc)
Check whether the marco satisfies the condition.
Definition visitor.cpp:5093
std::shared_ptr< IRValueType > getIncompleteType(const yoi::wstr &typeName) const
Definition visitor.cpp:3155
std::set< IRFunctionDefinition::FunctionAttrs > getFunctionAttributes(const yoi::vec< lexer::token > &attrs)
Definition visitor.cpp:4025
void evaluateConstraint(yoi::conceptStmt *stmt, const IRDebugInfo &currentDebugInfo)
Definition visitor.cpp:5726
std::pair< std::pair< yoi::indexT, yoi::indexT >, std::shared_ptr< IRInterfaceInstanceDefinition > > parseInterfaceName(yoi::externModuleAccessExpression *structDef)
Definition visitor.cpp:2903
yoi::indexT specializeFunctionTemplate(yoi::funcDefStmt *astNode, const yoi::vec< std::shared_ptr< IRValueType > > &templateArgs, yoi::indexT moduleIndex)
Definition visitor.cpp:3186
void createCallableInstanceForFunction(yoi::indexT implIndex, std::pair< yoi::indexT, yoi::indexT > callableInterfaceIndex, yoi::indexT moduleIndex, bool hasThis)
Create a Callable Instance For Function object.
Definition visitor.cpp:5508
void setupTemporaryConceptEvaluationEnvironment(yoi::indexT moduleIndex, const yoi::wstr &conceptName, const std::vector< std::shared_ptr< IRValueType > > &args)
Definition visitor.cpp:5679
yoi::vec< yoi::wstr > extractTemplateParamsFromTypeArgs(yoi::templateArg *templateArgs)
Definition visitor.cpp:3274
bool canCastTo(const std::shared_ptr< IRValueType > &fromType, const std::shared_ptr< IRValueType > &toType)
Definition visitor.cpp:3922
void specializeInterfaceImplementation(yoi::implStmt *implAst, const std::shared_ptr< IRValueType > &concreteStructType, const yoi::wstr &specializedStructName, const yoi::vec< std::shared_ptr< IRValueType > > &concreteTemplateArgs, yoi::indexT targetModule)
Definition visitor.cpp:4603
bool isVisitingGlobalScope() const
Definition visitor.cpp:2969
std::shared_ptr< yoi::moduleContext > moduleContext
Definition visitor.h:41
yoi::indexT createCallableInterface(const yoi::vec< std::shared_ptr< IRValueType > > &parameterTypes, const std::shared_ptr< IRValueType > &returnType)
Create a Callable Interface object and return its index in the module's interface table.
Definition visitor.cpp:4912
yoi::wstr getSpecializedMangledMethodName(yoi::indexTable< yoi::wstr, IRTemplateBuilder::Argument > &templateArgs, const yoi::wstr &baseMethodName, const yoi::vec< std::shared_ptr< IRValueType > > &specializedArgTypes)
Definition visitor.cpp:3720
yoi::vec< std::shared_ptr< IRValueType > > evaluateArguments(yoi::invocationArguments *args)
Visits a list of argument expressions and returns their types.
Definition visitor.cpp:4095
yoi::vec< std::shared_ptr< IRValueType > > parseTemplateArgs(const yoi::templateArg &templateArgs)
Definition visitor.cpp:3178
yoi::IRExternEntry getExternEntry(yoi::indexT moduleIndex, const yoi::wstr &identifier) const
search and return extern entry by identifier in target module
Definition visitor.cpp:2933
void tryCastTo(const std::shared_ptr< IRValueType > &toType)
Definition visitor.cpp:3851
yoi::indexT currentModuleIndex
Definition visitor.h:43
void constructDataStruct(yoi::indexT datastructIndex, yoi::indexT moduleIndex, yoi::invocationArguments *args)
Construct data struct and initialize with given parameters.
Definition visitor.cpp:5554
std::shared_ptr< yoi::IRModule > irModule
Definition visitor.h:42
yoi::indexT generateNullInterfaceImplementation(const std::shared_ptr< IRValueType > &structType)
Definition visitor.cpp:4002
void ejectTemporaryConceptEvaluationEnvironment()
Definition visitor.cpp:5721
void checkConceptSatisfaction(yoi::externModuleAccessExpression *stmt, const yoi::wstr &paramName, const std::shared_ptr< IRValueType > &args)
Definition visitor.cpp:5784
IRValueType parseTypeSpec(yoi::identifier *identifier)
Definition visitor.cpp:1893
std::shared_ptr< IRValueType > getGeneratorContext(const yoi::wstr &funcName, const std::shared_ptr< IRValueType > &yieldType)
Get or fetch generator context type for specific function.
Definition visitor.cpp:5590
std::vector< t > vec
Definition def.hpp:53
std::wstring wstr
Definition def.hpp:48
uint64_t indexT
Definition def.hpp:51
std::shared_ptr< IRValueType > variadicElementType
Definition visitor.h:28
std::shared_ptr< IRFunctionDefinition > function
Definition visitor.h:29