Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug preventing access to formal parameters in addition to variables #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions Chapter05/tinylang/include/tinylang/AST/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class Expr {
EK_Int,
EK_Bool,
EK_Var,
EK_Param,
EK_Const,
EK_Func,
};
Expand Down Expand Up @@ -303,13 +304,11 @@ class BooleanLiteral : public Expr {
};

class VariableAccess : public Expr {
Decl *Var;
VariableDeclaration *Var;

public:
VariableAccess(VariableDeclaration *Var)
: Expr(EK_Var, Var->getType(), false), Var(Var) {}
VariableAccess(FormalParameterDeclaration *Param)
: Expr(EK_Var, Param->getType(), false), Var(Param) {}

Decl *getDecl() { return Var; }

Expand All @@ -318,6 +317,20 @@ class VariableAccess : public Expr {
}
};

class FormalParameterAccess : public Expr {
FormalParameterDeclaration *Param;

public:
FormalParameterAccess(FormalParameterDeclaration *Param)
: Expr(EK_Param, Param->getType(), false), Param(Param) {}

Decl *getDecl() { return Param; }

static bool classof(const Expr *E) {
return E->getKind() == EK_Param;
}
};

class ConstantAccess : public Expr {
ConstantDeclaration *Const;

Expand Down Expand Up @@ -372,14 +385,14 @@ class Stmt {
};

class AssignmentStatement : public Stmt {
VariableDeclaration *Var;
Decl *D;
Expr *E;

public:
AssignmentStatement(VariableDeclaration *Var, Expr *E)
: Stmt(SK_Assign), Var(Var), E(E) {}
AssignmentStatement(Decl *D, Expr *E)
: Stmt(SK_Assign), D(D), E(E) {}

VariableDeclaration *getVar() { return Var; }
Decl *getDecl() { return D; }
Expr *getExpr() { return E; }

static bool classof(const Stmt *S) {
Expand Down Expand Up @@ -455,4 +468,4 @@ class ReturnStatement : public Stmt {
};

} // namespace tinylang
#endif
#endif
6 changes: 5 additions & 1 deletion Chapter05/tinylang/lib/CodeGen/CGProcedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ llvm::Value *CGProcedure::emitExpr(Expr *E) {
// With more languages features in place, here you need
// to add array and record support.
return readVariable(Curr, Decl);
} else if (auto *Var =
llvm::dyn_cast<FormalParameterAccess>(E)) {
auto *Decl = Var->getDecl();
return readVariable(Curr, Decl);
} else if (auto *Const =
llvm::dyn_cast<ConstantAccess>(E)) {
return emitExpr(Const->getDecl()->getExpr());
Expand All @@ -313,7 +317,7 @@ llvm::Value *CGProcedure::emitExpr(Expr *E) {

void CGProcedure::emitStmt(AssignmentStatement *Stmt) {
auto *Val = emitExpr(Stmt->getExpr());
writeVariable(Curr, Stmt->getVar(), Val);
writeVariable(Curr, Stmt->getDecl(), Val);
}

void CGProcedure::emitStmt(ProcedureCallStatement *Stmt) {
Expand Down
24 changes: 14 additions & 10 deletions Chapter05/tinylang/lib/Sema/Sema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void Sema::checkFormalAndActualParameters(
Loc,
diag::
err_type_of_formal_and_actual_parameter_not_compatible);
if (F->isVar() && isa<VariableAccess>(Arg))
if (F->isVar() && isa<FormalParameterAccess>(Arg))
Diags.report(Loc,
diag::err_var_parameter_requires_var);
}
Expand Down Expand Up @@ -195,16 +195,20 @@ void Sema::actOnProcedureDeclaration(

void Sema::actOnAssignment(StmtList &Stmts, SMLoc Loc,
Decl *D, Expr *E) {
TypeDeclaration *type;
if (auto Var = dyn_cast<VariableDeclaration>(D)) {
if (Var->getType() != E->getType()) {
Diags.report(
Loc, diag::err_types_for_operator_not_compatible,
tok::getPunctuatorSpelling(tok::colonequal));
}
Stmts.push_back(new AssignmentStatement(Var, E));
type = Var->getType();
} else if (auto Param = dyn_cast<FormalParameterDeclaration>(D)) {
type = Param->getType();
} else if (D) {
// TODO Emit error
}
if (type != E->getType()) {
Diags.report(
Loc, diag::err_types_for_operator_not_compatible,
tok::getPunctuatorSpelling(tok::colonequal));
}
Stmts.push_back(new AssignmentStatement(D, E));
}

void Sema::actOnProcCall(StmtList &Stmts, SMLoc Loc,
Expand Down Expand Up @@ -353,7 +357,7 @@ Expr *Sema::actOnPrefixExpression(Expr *E,
if (Op.getKind() == tok::minus) {
bool Ambiguous = true;
if (isa<IntegerLiteral>(E) || isa<VariableAccess>(E) ||
isa<ConstantAccess>(E))
isa<FormalParameterAccess>(E) || isa<ConstantAccess>(E))
Ambiguous = false;
else if (auto *Infix = dyn_cast<InfixExpression>(E)) {
tok::TokenKind Kind =
Expand Down Expand Up @@ -389,7 +393,7 @@ Expr *Sema::actOnVariable(Decl *D) {
if (auto *V = dyn_cast<VariableDeclaration>(D))
return new VariableAccess(V);
else if (auto *P = dyn_cast<FormalParameterDeclaration>(D))
return new VariableAccess(P);
return new FormalParameterAccess(P);
else if (auto *C = dyn_cast<ConstantDeclaration>(D)) {
if (C == TrueConst)
return TrueLiteral;
Expand Down Expand Up @@ -437,4 +441,4 @@ Decl *Sema::actOnQualIdentPart(Decl *Prev, SMLoc Loc,
}
Diags.report(Loc, diag::err_undeclared_name, Name);
return nullptr;
}
}
1 change: 1 addition & 0 deletions Chapter05/tinylang/tools/driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/TargetRegistry.h"
Expand Down