Line data Source code
1 : #include "../../../inc/parsingAnalysis/ast/variables/ast_autoDecl.h"
2 : #include "../../../inc/parsingAnalysis/ast/variables/ast_typedDecl.h"
3 : #include "../../../inc/parsingAnalysis/ast/variables/ast_varCall.h"
4 : #include "../../../inc/visitors/typeAnalysis/typeAnalysis.h"
5 : #include <memory>
6 :
7 : namespace nicole {
8 :
9 : /*
10 : - comrpueba que el tipo de la expresion no sea void
11 : - fija en currentScope el tipo
12 : - retorna NoPropagate
13 : */
14 : std::expected<std::shared_ptr<Type>, Error>
15 0 : TypeAnalysis::visit(const AST_AUTO_DECL *node) const noexcept {
16 0 : if (!node) {
17 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_AUTO_DECL");
18 0 : }
19 0 : auto exprTypeRes = node->value()->accept(*this);
20 0 : if (!exprTypeRes)
21 0 : return createError(exprTypeRes.error());
22 0 : auto deducedType = exprTypeRes.value();
23 : /*
24 : if (insideDeclWithGenerics &&
25 : typeTable_->isGenericType(deducedType, currentGenericList_))
26 : deducedType = std::make_shared<PlaceHolder>(deducedType);
27 : */
28 0 : auto insertRes = currentScope_->setVariableType(node->id(), deducedType);
29 0 : if (!insertRes)
30 0 : return createError(insertRes.error());
31 0 : node->setReturnedFromAnalysis(typeTable_->noPropagateType());
32 0 : return typeTable_->noPropagateType();
33 0 : }
34 :
35 : /*
36 : - comrpueba que el tipo de la expresion no sea body
37 : - comrpueba que la expresion sea compatible
38 : - retorna NoPropagate
39 : */
40 : std::expected<std::shared_ptr<Type>, Error>
41 0 : TypeAnalysis::visit(const AST_VAR_TYPED_DECL *node) const noexcept {
42 0 : if (!node) {
43 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_VAR_TYPED_DECL");
44 0 : }
45 0 : auto declaredType = node->varType();
46 : /*
47 : if (insideDeclWithGenerics &&
48 : typeTable_->isGenericType(declaredType, currentGenericList_))
49 : declaredType = std::make_shared<PlaceHolder>(declaredType);
50 : */
51 0 : auto valueRes = node->value()->accept(*this);
52 0 : if (!valueRes)
53 0 : return createError(valueRes.error());
54 0 : auto valueType = valueRes.value();
55 :
56 0 : if (!typeTable_->canAssign(declaredType, valueType))
57 0 : return createError(
58 0 : ERROR_TYPE::TYPE,
59 0 : "assigned value type does not match declared variable type -> " +
60 0 : declaredType->toString() + " | " + valueType->toString());
61 :
62 0 : node->setReturnedFromAnalysis(typeTable_->noPropagateType());
63 0 : return typeTable_->noPropagateType();
64 0 : }
65 :
66 : /*
67 : - se apoya en currentScope y en en currenStruct para saber si es un atributo
68 : */
69 : std::expected<std::shared_ptr<Type>, Error>
70 0 : TypeAnalysis::visit(const AST_VAR_CALL *node) const noexcept {
71 0 : if (!node) {
72 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_VAR_CALL");
73 0 : }
74 :
75 0 : if (analyzingInsideClass && currentUserType_ &&
76 0 : currentUserType_->hasAttribute(node->id())) {
77 0 : auto attrExp = currentUserType_->getAttribute(node->id());
78 0 : if (!attrExp)
79 0 : return createError(attrExp.error());
80 0 : node->setReturnedFromAnalysis(attrExp.value().type());
81 0 : return attrExp.value().type();
82 0 : }
83 :
84 0 : if (!currentScope_->has(node->id()))
85 0 : return createError(ERROR_TYPE::VARIABLE,
86 0 : "variable: " + node->id() + " does not exist");
87 0 : auto varExp = currentScope_->getVariable(node->id());
88 0 : if (!varExp)
89 0 : return createError(varExp.error());
90 0 : currentType_ = varExp->type();
91 0 : node->setReturnedFromAnalysis(varExp->type());
92 0 : return varExp->type();
93 0 : }
94 :
95 : } // namespace nicole
|