Skip to content

Commit

Permalink
Use FunSignature to to Function resolution.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdtscp committed May 14, 2019
1 parent 70f7d94 commit 0f29fc9
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 41 deletions.
1 change: 0 additions & 1 deletion include/ast/FunCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class FunCall : public Expr, public atl::enable_shared_from_this<FunCall> {
FunCall(const atl::shared_ptr<Identifier> &p_funIdentifier,
const atl::vector<atl::shared_ptr<Expr>> &p_funArgs);


bool operator==(Expr &rhs) const override;
bool operator!=(Expr &rhs) const override;

Expand Down
2 changes: 1 addition & 1 deletion include/ast/FunDecl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FunDecl : public Decl,
const atl::shared_ptr<Type> &p_funType);

atl::shared_ptr<Identifier> getIdentifier() const override;
FunSignature getSignature() const;
const FunSignature getSignature() const;

bool operator==(Decl &rhs) const override;
bool operator!=(Decl &rhs) const override;
Expand Down
17 changes: 17 additions & 0 deletions include/ast/FunSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ class FunSignature {
: funReturnType(p_funReturnType), funIdentifier(p_funIdentifier),
funArgs(p_funArgs) {}

bool operator==(const FunSignature &rhs) const {
// TODO: Incorporate return type?

// Check args match.
for (int idx = 0; idx < funArgs.size(); ++idx)
if (*funArgs[idx] != *rhs.funArgs[idx])
return false;

// Check identifier.
if (*funIdentifier != *rhs.funIdentifier)
return false;

return true;
}

bool operator!=(const FunSignature &rhs) const { return !(*this == rhs); }

private:
const atl::shared_ptr<Type> funReturnType;
const atl::shared_ptr<Identifier> funIdentifier;
Expand Down
1 change: 1 addition & 0 deletions include/passes/SemanticAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class SemanticAnalysis : public ASTVisitor<atl::shared_ptr<Type>> {

/* Helpers */
atl::shared_ptr<Type> collapseReferenceTypes(atl::shared_ptr<Type> type);
// TODO: ~Create: Type preventCascade(const Type t);
};

} // namespace ACC
7 changes: 4 additions & 3 deletions src/ast/ClassTypeDef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,12 @@ ClassTypeDef::findFunDeclLocal(const FunSignature &funSignature,
const atl::shared_ptr<Decl> currDecl = classDecls[idx];
if (currDecl->astClass() != "FunDecl" && currDecl->astClass() != "FunDef")
continue;
if (currDecl.get() == exemptDecl.get())
continue;
const atl::shared_ptr<FunDecl> currFunDecl =
atl::static_pointer_cast<FunDecl>(currDecl);
// TODO: Compare the FunDecl with the FunSignature.
if (currFunDecl.get() == exemptDecl.get())
continue;
if (funSignature != currFunDecl->getSignature())
continue;

return currFunDecl;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ast/FunDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ atl::shared_ptr<Identifier> FunDecl::getIdentifier() const {
return funIdentifier;
}

FunSignature FunDecl::getSignature() const {
const FunSignature FunDecl::getSignature() const {
atl::vector<atl::shared_ptr<Type>> paramTypes;
for (int idx = 0; idx < funParams.size(); ++idx)
paramTypes.push_back(funParams[idx]->type);
Expand Down
3 changes: 2 additions & 1 deletion src/ast/Namespace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ Namespace::findFunDeclLocal(const FunSignature &funSignature,
atl::static_pointer_cast<FunDecl>(currDecl);
if (currFunDecl.get() == exemptDecl.get())
continue;
// TODO: Compare the funSignature with the currFunDecl.
if (funSignature != currFunDecl->getSignature())
continue;

return currFunDecl;
}
Expand Down
5 changes: 2 additions & 3 deletions src/ast/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ Program::findFunDeclLocal(const FunSignature &funSignature,
const atl::shared_ptr<Decl> currDecl = decls[idx];
if (currDecl->astClass() != "FunDecl" && currDecl->astClass() != "FunDef")
continue;
if (currDecl.get() == exemptDecl.get())
continue;
const atl::shared_ptr<FunDecl> currFunDecl =
atl::static_pointer_cast<FunDecl>(currDecl);
if (currFunDecl.get() == exemptDecl.get())
continue;
// TODO: Compare the funSignature with the currFunDecl.
if (funSignature != currFunDecl->getSignature())
continue;

return currFunDecl;
}
Expand Down
68 changes: 37 additions & 31 deletions src/passes/SemanticAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ SemanticAnalysis::error(const atl::string &errorType, const atl::string &error,
}

void SemanticAnalysis::printErrors() {
printf("Semantic Analysis Errors:\n");
for (unsigned int idx = 0; idx < errors.size(); ++idx)
const unsigned int num_errors = errors.size();
if (num_errors > 0)
printf("Semantic Analysis Errors:\n");
for (unsigned int idx = 0; idx < num_errors; ++idx)
printf("\t%s\n", errors[idx].c_str());
}

Expand Down Expand Up @@ -58,10 +60,7 @@ atl::shared_ptr<Type> SemanticAnalysis::visit(Assign &as) {
const atl::shared_ptr<Type> lhsType = as.lhs->accept(*this);
const atl::shared_ptr<Type> rhsType = as.rhs->accept(*this);
if (*lhsType != *rhsType)
return error("Type Analysis",
"Assignation has mismatched types. (" +
lhsType->getSignature() + " = " + rhsType->getSignature() +
")",
return error("Type Analysis", "Assignation has mismatched types.",
as.getptr());
return atl::shared_ptr<BaseType>(new BaseType(PrimitiveType::NULLPTR_T));
}
Expand All @@ -72,10 +71,7 @@ atl::shared_ptr<Type> SemanticAnalysis::visit(BinOp &bo) {
const atl::shared_ptr<Type> lhsType = bo.lhs->accept(*this);
const atl::shared_ptr<Type> rhsType = bo.rhs->accept(*this);
if (*lhsType != *rhsType)
return error("Type Analysis",
"Binary operation has mismatched types. (" +
lhsType->getSignature() + ", " + rhsType->getSignature() +
")",
return error("Type Analysis", "Binary operation has mismatched types.",
bo.getptr());

switch (bo.operation) {
Expand Down Expand Up @@ -245,12 +241,14 @@ atl::shared_ptr<Type> SemanticAnalysis::visit(For &f) {
return atl::shared_ptr<BaseType>(new BaseType(PrimitiveType::NULLPTR_T));
}
atl::shared_ptr<Type> SemanticAnalysis::visit(FunCall &fc) {
// Visit all parameters first.
atl::vector<atl::shared_ptr<Type>> funCallArgTypes;
for (unsigned int idx = 0; idx < fc.funArgs.size(); ++idx)
fc.funArgs[idx]->accept(*this);
funCallArgTypes.push_back(fc.funArgs[idx]->accept(*this));

const FunSignature funCallSignature(nullptr, fc.funIdentifier,
funCallArgTypes);
const atl::shared_ptr<FunDecl> funDecl =
currScope->findFunDecl(fc.getSignature());
currScope->findFunDecl(funCallSignature);
if (funDecl == nullptr)
return error("Type Analysis", "Attempted to call undeclared function.",
fc.getptr());
Expand Down Expand Up @@ -408,19 +406,20 @@ atl::shared_ptr<Type> SemanticAnalysis::visit(MemberCall &mc) {

/* Now Manually Visit the Member Call */
// Visit all parameters first.
atl::vector<atl::shared_ptr<Type>> funCallArgTypes;
for (unsigned int idx = 0; idx < mc.funCall->funArgs.size(); ++idx)
mc.funCall->funArgs[idx]->accept(*this);
funCallArgTypes.push_back(mc.funCall->funArgs[idx]->accept(*this));

// Check this ClassTypeDef contains the member.
const FunSignature funCallSignature(nullptr, mc.funCall->funIdentifier,
funCallArgTypes);
const atl::shared_ptr<FunDecl> memberFunDecl =
objClassTypeDef->findFunDeclLocal(mc.funCall->getSignature());
objClassTypeDef->findFunDeclLocal(funCallSignature);
if (memberFunDecl == nullptr)
return error("Type Error",
"Attempted to call a member function that does "
"not exist in the class definition.",
mc.funCall);

// return mc.funCall->accept(*this);
return memberFunDecl->funType;
}
atl::shared_ptr<Type> SemanticAnalysis::visit(Namespace &n) {
Expand Down Expand Up @@ -487,19 +486,22 @@ atl::shared_ptr<Type> SemanticAnalysis::visit(SubscriptOp &so) {
objClassType->typeDefinition;

const atl::shared_ptr<Type> indexType = so.index->accept(*this);
const atl::string operatorSignature =
objClassType->getSignature() + "::operator[](" +
objClassType->getSignature() + "*, " + indexType->getSignature() + ")";
const atl::shared_ptr<FunDecl> objSubscriptOpDecl =
objClassTypeDef->findFunDeclLocal(operatorSignature);
if (objSubscriptOpDecl == nullptr) {
return error("Type Error",
"No definiton for subscript operator[] for type: " +
objClassType->identifier->toString(),
so.variable);
}
so.operatorDecl = objSubscriptOpDecl;
return objSubscriptOpDecl->funType;
// TODO: Create FunSignature for SubscriptOp.
// const atl::string operatorSignature =
// objClassType->getSignature() + "::operator[](" +
// objClassType->getSignature() + "*, " + indexType->getSignature() +
// ")";
// const atl::shared_ptr<FunDecl> objSubscriptOpDecl =
// objClassTypeDef->findFunDeclLocal(operatorSignature);
// if (objSubscriptOpDecl == nullptr) {
// return error("Type Error",
// "No definiton for subscript operator[] for type: " +
// objClassType->identifier->toString(),
// so.variable);

// so.operatorDecl = objSubscriptOpDecl;
// return objSubscriptOpDecl->funType;
return nullptr;
} else {
return error("Type Error",
"Cannot perform subscript operator[] on type: " +
Expand Down Expand Up @@ -590,6 +592,10 @@ atl::shared_ptr<Type> SemanticAnalysis::visit(VarDef &vd) {
atl::static_pointer_cast<Decl>(vd.getptr()));

// Visit the value initialised.
const atl::shared_ptr<Type> valueType = vd.varValue->accept(*this);
if (*valueType != *vd.type)
return error("Type Analysis", "VarDef has mismatched types.",
atl::static_pointer_cast<Decl>(vd.getptr()));

return atl::shared_ptr<BaseType>(new BaseType(PrimitiveType::NULLPTR_T));
}
Expand All @@ -602,7 +608,7 @@ atl::shared_ptr<Type> SemanticAnalysis::visit(VarExpr &ve) {
ve.varIdentifier->toString(),
ve.getptr());
ve.varDecl = varDecl;
return varDecl->type->accept(*this);
return varDecl->type;
}
atl::shared_ptr<Type> SemanticAnalysis::visit(While &w) {
atl::shared_ptr<Type> conditionType = w.condition->accept(*this);
Expand Down

0 comments on commit 0f29fc9

Please sign in to comment.