Skip to content

Commit

Permalink
Fix SemaResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
fly-lang committed May 14, 2024
1 parent c5b0bd5 commit 0cc283b
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 179 deletions.
3 changes: 3 additions & 0 deletions include/AST/ASTDeleteStmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ namespace fly {
*/
class ASTDeleteStmt : public ASTStmt {

friend class SemaBuilder;
friend class SemaResolver;

ASTVarRef *VarRef = nullptr;

public:
Expand Down
1 change: 1 addition & 0 deletions include/AST/ASTFailStmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace fly {
class ASTFailStmt : public ASTStmt {

friend class SemaBuilder;
friend class SemaResolver;

ASTExpr *Expr = nullptr;

Expand Down
5 changes: 1 addition & 4 deletions include/AST/ASTFunctionBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,16 @@ namespace fly {
class ASTReturnStmt : public ASTStmt {

friend class SemaBuilder;
friend class SemaResolver;

ASTExpr *Expr = nullptr;

ASTBlockStmt *Block = nullptr;

ASTReturnStmt(const SourceLocation &Loc);

public:

ASTExpr *getExpr() const;

ASTBlockStmt *getBlock() const;

std::string str() const override;
};
}
Expand Down
2 changes: 2 additions & 0 deletions include/Sema/SemaBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ namespace fly {

bool AddCallArg(ASTCall *Call, ASTExpr *Expr);

bool AddLocalVar(ASTBlockStmt *BlockStmt, ASTLocalVar *LocalVar);

// Add Stmt
bool AddStmt(ASTStmt *Parent, ASTStmt *Stmt);

Expand Down
5 changes: 3 additions & 2 deletions include/Sema/SemaResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace fly {
class ASTIdentityType;
class ASTVar;
class ASTIdentity;
class ASTLoopInStmt;

class SemaResolver {

Expand Down Expand Up @@ -88,6 +89,8 @@ namespace fly {

bool ResolveLoopBlock(ASTLoopStmt *LoopStmt);

bool ResolveLoopInBlock(ASTLoopInStmt *LoopInStmt);

bool ResolveParentIdentifier(ASTStmt *Stmt, ASTIdentifier *&Identifier);

bool ResolveIdentityType(ASTModule *Module, ASTIdentityType *IdentityType);
Expand Down Expand Up @@ -120,8 +123,6 @@ namespace fly {

bool ResolveExpr(ASTStmt *Stmt, ASTExpr *Expr);

bool ResolveValueExpr(ASTValueExpr *pExpr);

ASTNameSpace *FindNameSpace(llvm::StringRef Name) const;

ASTModule *FindModule(ASTFunctionBase *FunctionBase) const;
Expand Down
4 changes: 0 additions & 4 deletions src/AST/ASTFunctionBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ ASTExpr *ASTReturnStmt::getExpr() const {
return Expr;
}

ASTBlockStmt *ASTReturnStmt::getBlock() const {
return Block;
}

std::string ASTReturnStmt::str() const {
return Logger("ASTReturn").
Attr("Kind", (uint64_t) Kind).
Expand Down
118 changes: 72 additions & 46 deletions src/Sema/SemaBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,73 @@ ASTValueExpr *SemaBuilder::CreateExpr(ASTValue *Value) {
FLY_DEBUG_MESSAGE("SemaBuilder", "CreateExpr",
Logger().Attr("Value", Value).End());
assert(Value && "Create ASTValueExpr by ASTValue");
ASTValueExpr *ValueExpr = new ASTValueExpr(Value);
return ValueExpr;
ASTValueExpr *Expr = new ASTValueExpr(Value);
const SourceLocation &Loc = Value->getLocation();

switch (Value->getTypeKind()) {

case ASTTypeKind::TYPE_BOOL:
Expr->Type = CreateBoolType(Loc);
break;

case ASTTypeKind::TYPE_INTEGER: {
ASTIntegerValue *Integer = ((ASTIntegerValue *) Expr->Value);

if (Integer->Negative) { // Integer is negative (Ex. -2)

if (Integer->Value > MIN_LONG) { // Negative Integer overflow min value
S.Diag(Expr->getLocation(), diag::err_sema_int_min_overflow);
return Expr;
}

if (Integer->Value > MIN_INT) {
Expr->Type = CreateLongType(Loc);
} else if (Integer->Value > MIN_SHORT) {
Expr->Type = CreateIntType(Loc);
} else {
Expr->Type = CreateShortType(Loc);
}
} else { // Positive Integer

if (Integer->Value > MAX_LONG) { // Positive Integer overflow max value
S.Diag(Expr->getLocation(), diag::err_sema_int_max_overflow);
return Expr;
}

if (Integer->Value > MAX_INT) {
Expr->Type = CreateLongType(Loc);
} else if (Integer->Value > MAX_SHORT) {
Expr->Type = CreateIntType(Loc);
} else if (Integer->Value > MAX_BYTE) {
Expr->Type = CreateShortType(Loc);
} else {
Expr->Type = CreateByteType(Loc);
}
}
break;
}

case ASTTypeKind::TYPE_FLOATING_POINT:
// Creating as Float on first but transform in Double if is contained into a Binary Expr with a Double Type
Expr->Type = CreateDoubleType(Loc);
break;

case ASTTypeKind::TYPE_STRING:
Expr->Type = CreateStringType(Loc);
break;

case ASTTypeKind::TYPE_ARRAY:
// TODO
break;
case ASTTypeKind::TYPE_IDENTITY:
// TODO
break;
case ASTTypeKind::TYPE_VOID:
break;
case ASTTypeKind::TYPE_ERROR:
break;
}
return Expr;
}

ASTCallExpr *SemaBuilder::CreateExpr(ASTCall *Call) {
Expand Down Expand Up @@ -1255,6 +1320,10 @@ SemaBuilder::AddCallArg(ASTCall *Call, ASTExpr *Expr) {
return true;
}

bool SemaBuilder::AddLocalVar(ASTBlockStmt *BlockStmt, ASTLocalVar *LocalVar) {
return BlockStmt->LocalVars.insert(std::make_pair(LocalVar->getName(), LocalVar)).second;
}

/**
* Add ExprStmt to Content
* @param ExprStmt
Expand All @@ -1263,51 +1332,8 @@ SemaBuilder::AddCallArg(ASTCall *Call, ASTExpr *Expr) {
bool
SemaBuilder::AddStmt(ASTStmt *Parent, ASTStmt *Stmt) {
FLY_DEBUG_MESSAGE("SemaBuilder", "AddStmt", Logger().Attr("Stmt", Stmt).End());

bool Success = false;
Stmt->Parent = Parent;
switch (Stmt->getKind()) {

case ASTStmtKind::STMT_VAR: {
ASTVarStmt *VarDefine = (ASTVarStmt *) Stmt;

ASTVar *Var = VarDefine->getVarRef()->getDef();

// Collects all LocalVars in the hierarchy Block
if (Var->getVarKind() == ASTVarKind::VAR_LOCAL) {
ASTLocalVar *LocalVar = (ASTLocalVar *) Var;

// Statements have an assignment so is initialized
if (VarDefine->getExpr() != nullptr)
LocalVar->setInitialization(VarDefine);

// Add to the Parent Block
const auto &Pair = std::pair<std::string, ASTLocalVar *>(LocalVar->getName(), LocalVar);

// Useful for Alloca into CodeGen
Parent->Function->Body->LocalVars.insert(Pair);
Success = true;
}
break;
}

case ASTStmtKind::STMT_BLOCK:
case ASTStmtKind::STMT_EXPR:
case ASTStmtKind::STMT_BREAK:
case ASTStmtKind::STMT_CONTINUE:
case ASTStmtKind::STMT_DELETE:
case ASTStmtKind::STMT_RETURN:
case ASTStmtKind::STMT_FAIL:
case ASTStmtKind::STMT_HANDLE:
case ASTStmtKind::STMT_IF:
case ASTStmtKind::STMT_SWITCH:
case ASTStmtKind::STMT_LOOP:
case ASTStmtKind::STMT_LOOP_IN:
Success = true;
break;
}

return Success;
return true;
}

bool SemaBuilder::AddElsif(ASTIfStmt *IfStmt, ASTExpr *Condition, ASTBlockStmt *Block) {
Expand Down
Loading

0 comments on commit 0cc283b

Please sign in to comment.