Skip to content

Commit

Permalink
Replace ASTParams with llvm::SmallVector of ASTParam
Browse files Browse the repository at this point in the history
  • Loading branch information
fly-lang committed Apr 28, 2024
1 parent 4571758 commit 5451604
Show file tree
Hide file tree
Showing 24 changed files with 75 additions and 132 deletions.
2 changes: 0 additions & 2 deletions include/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

#include "llvm/ADT/StringMap.h"

#include <vector>

namespace fly {

class SourceLocation;
Expand Down
3 changes: 0 additions & 3 deletions include/AST/ASTFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
#include "ASTFunctionBase.h"
#include "CodeGen/CodeGenFunction.h"

#include <vector>

namespace fly {

class ASTNode;
class ASTParams;
class ASTType;
class ASTBlock;
class ASTScopes;
Expand Down
16 changes: 8 additions & 8 deletions include/AST/ASTFunctionBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@
#include "ASTStmt.h"
#include "ASTBase.h"

#include <vector>

namespace fly {

class ASTGroupExpr;
class ASTParams;
class ASTExpr;
class ASTType;
class ASTVarRef;
Expand Down Expand Up @@ -57,8 +54,9 @@ namespace fly {

ASTScopes * Scopes;

// Header contains parameters
ASTParams *Params = nullptr;
llvm::SmallVector<ASTParam *, 8> Params;

ASTParam* Ellipsis = nullptr;

// Body is the main BlockStmt
ASTBlock *Body = nullptr;
Expand All @@ -82,9 +80,11 @@ namespace fly {

void addParam(ASTParam *Param);

void setEllipsis(ASTParam *Param);
llvm::SmallVector<ASTParam *, 8> getParams() const;

const ASTParams *getParams() const;
ASTParam *getEllipsis() const;

void setEllipsis(ASTParam *Param);

const ASTBlock *getBody() const;

Expand Down Expand Up @@ -120,7 +120,7 @@ namespace fly {

ASTBlock *getBlock() const;

std::string str() const;
std::string str() const override;
};
}

Expand Down
33 changes: 3 additions & 30 deletions include/AST/ASTParams.h → include/AST/ASTParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//
//===--------------------------------------------------------------------------------------------------------------===//

#ifndef FLY_ASTPARAMS_H
#define FLY_ASTPARAMS_H
#ifndef FLY_ASTPARAM_H
#define FLY_ASTPARAM_H

#include "ASTLocalVar.h"

Expand Down Expand Up @@ -39,33 +39,6 @@ namespace fly {

std::string str() const override;
};

/**
* All Parameters of a Function for Definition
* Ex.
* func(int param1, float param2, bool param3, ...)
*/
class ASTParams {

friend class ASTFunctionBase;

llvm::SmallVector<ASTParam *, 8> List;

ASTParam* Ellipsis = nullptr;

public:
uint64_t getSize() const;

ASTParam *at(unsigned long Index) const;

bool isEmpty() const;

const llvm::SmallVector<ASTParam *, 8> &getList() const;

const ASTParam* getEllipsis() const;

std::string str() const;
};
}

#endif //FLY_ASTPARAMS_H
#endif //FLY_ASTPARAM_H
5 changes: 2 additions & 3 deletions include/CodeGen/CodeGenFunctionBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/SmallVector.h"
#include "AST/ASTParams.h"
#include "AST/ASTParam.h"

namespace llvm {
class Function;
Expand All @@ -26,7 +26,6 @@ namespace llvm {
namespace fly {

class ASTFunctionBase;
class ASTParams;
class ASTType;
class CodeGenModule;

Expand Down Expand Up @@ -56,7 +55,7 @@ namespace fly {

void GenReturnType();

void GenParamTypes(CodeGenModule * CGM, SmallVector<llvm::Type *, 8> &Types, const ASTParams *Params);
void GenParamTypes(CodeGenModule * CGM, SmallVector<llvm::Type *, 8> &Types, llvm::SmallVector<ASTParam *, 8> Params);

ASTFunctionBase *getAST();

Expand Down
14 changes: 7 additions & 7 deletions include/Sema/SemaBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,14 @@ namespace fly {
if (StrMapIt != Functions.end()) {

// Search by Number of Parameters
const auto IntMapIt = StrMapIt->second.find(NewFunction->Params->getSize());
const auto IntMapIt = StrMapIt->second.find(NewFunction->getParams().size());

// Search by Type of Parameters
llvm::SmallVector<T *, 4> VectorFunctions = IntMapIt->second;
for (auto &Function: VectorFunctions) {

// Check if NewFunction have no params
if (NewFunction->getParams()->isEmpty() && Function->getParams()->isEmpty()) {
if (NewFunction->getParams().empty() && Function->getParams().empty()) {
return true;
}

Expand All @@ -496,7 +496,7 @@ namespace fly {

// add to std::map
std::map<uint64_t, llvm::SmallVector<T *, 4>> IntMap;
IntMap.insert(std::make_pair(NewFunction->Params->getSize(), Vect));
IntMap.insert(std::make_pair(NewFunction->getParams().size(), Vect));

// add to llvm::StringMap
return Functions.insert(std::make_pair(NewFunction->getName(), IntMap)).second;
Expand All @@ -509,7 +509,7 @@ namespace fly {
bool InsertFunction(std::map<uint64_t, llvm::SmallVector<T *, 4>> &Functions, T *NewFunction) {

// This Node contains a Function with this Function->Name
const auto &IntMapIt = Functions.find(NewFunction->Params->getSize());
const auto &IntMapIt = Functions.find(NewFunction->getParams().size());
if (IntMapIt == Functions.end()) { // but not have the same number of Params

// add to llvm::SmallVector
Expand All @@ -518,15 +518,15 @@ namespace fly {

// add to std::map
std::pair<uint64_t, SmallVector<T *, 4>> IntMapPair = std::make_pair(
NewFunction->Params->getSize(), VectorFunctions);
NewFunction->getParams().size(), VectorFunctions);

return Functions.insert(std::make_pair(NewFunction->Params->getSize(), VectorFunctions)).second;
return Functions.insert(std::make_pair(NewFunction->getParams().size(), VectorFunctions)).second;
} else { // This Node contains a Function with this Function->Name and same number of Params
llvm::SmallVector<T *, 4> VectorFunctions = IntMapIt->second;
for (auto &Function: VectorFunctions) {

// check no params duplicates
if (NewFunction->getParams()->isEmpty() && Function->getParams()->isEmpty()) {
if (NewFunction->getParams().empty() && Function->getParams().empty()) {
// Error:
return false;
}
Expand Down
11 changes: 5 additions & 6 deletions include/Sema/SemaValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define FLY_SEMA_VALIDATOR_H

#include "AST/ASTType.h"
#include "AST/ASTParams.h"
#include "AST/ASTParam.h"
#include "AST/ASTClassType.h"

namespace fly {
Expand All @@ -25,7 +25,6 @@ namespace fly {
class ASTNode;
class ASTImport;
class ASTExpr;
class ASTParams;
class ASTParam;
class ASTType;
class SourceLocation;
Expand All @@ -42,14 +41,14 @@ namespace fly {

bool DiagEnabled = true;

bool CheckDuplicateParams(ASTParams *Params, ASTParam *Param);
bool CheckDuplicateParams(llvm::SmallVector<ASTParam *, 8> Params, ASTParam *Param);

bool CheckDuplicateLocalVars(ASTStmt *Stmt, llvm::StringRef VarName);

static bool CheckParams(const ASTParams *Params, const ASTParams *CheckParams) {
static bool CheckParams(llvm::SmallVector<ASTParam *, 8> Params, llvm::SmallVector<ASTParam *, 8> CheckParams) {
// Types will be checked on Resolve()
for (ASTParam *Param : Params->getList()) {
for (ASTParam *CheckParam: CheckParams->getList()) {
for (ASTParam *Param : Params) {
for (ASTParam *CheckParam : CheckParams) {
if (CheckEqualTypes(Param->getType(), CheckParam->getType())) {
return false;
}
Expand Down
25 changes: 14 additions & 11 deletions src/AST/ASTFunctionBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@

#include "AST/ASTFunctionBase.h"
#include "AST/ASTBlock.h"
#include "AST/ASTParams.h"
#include "AST/ASTParam.h"

using namespace fly;

ASTFunctionBase::ASTFunctionBase(const SourceLocation &Loc, ASTFunctionKind Kind, ASTType *ReturnType,
llvm::StringRef Name, ASTScopes * Scopes) :
ASTBase(Loc), Kind(Kind), ReturnType(ReturnType),
Params(new ASTParams()), Name(Name), Scopes(Scopes) {
ASTBase(Loc), Kind(Kind), ReturnType(ReturnType), Name(Name), Scopes(Scopes) {

}

Expand All @@ -29,11 +28,15 @@ ASTScopes *ASTFunctionBase::getScopes() const {
}

void ASTFunctionBase::addParam(ASTParam *Param) {
Params->List.push_back(Param);
Params.push_back(Param);
}

llvm::SmallVector<ASTParam *, 8> ASTFunctionBase::getParams() const {
return Params;
}

void ASTFunctionBase::setEllipsis(ASTParam *Param) {
Params->Ellipsis = Param;
Ellipsis = Param;
}

const ASTBlock *ASTFunctionBase::getBody() const {
Expand All @@ -48,10 +51,6 @@ ASTParam *ASTFunctionBase::getErrorHandler() {
return ErrorHandler;
}

const ASTParams *ASTFunctionBase::getParams() const {
return Params;
}

ASTFunctionKind ASTFunctionBase::getKind() {
return Kind;
}
Expand All @@ -61,18 +60,22 @@ ASTType *ASTFunctionBase::getType() const {
}

bool ASTFunctionBase::isVarArg() {
return Params->getEllipsis();
return Ellipsis != nullptr;
}

std::string ASTFunctionBase::str() const {
return Logger("ASTFunctionBase").
Super(ASTBase::str()).
Attr("Name", Name).
Attr("Params", Params).
AttrList("Params", Params).
Attr("ReturnType", ReturnType).
End();
}

ASTParam *ASTFunctionBase::getEllipsis() const {
return Ellipsis;
}

ASTReturnStmt::ASTReturnStmt(ASTBlock *Parent, const SourceLocation &Loc) :
ASTStmt(Parent, Loc, ASTStmtKind::STMT_RETURN) {

Expand Down
30 changes: 2 additions & 28 deletions src/AST/ASTParams.cpp → src/AST/ASTParam.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//===--------------------------------------------------------------------------------------------------------------===//
// src/AST/ASTParams.cpp - AST Params implementation
// src/AST/ASTParam.cpp - AST Param implementation
//
// Part of the Fly Project https://flylang.org
// Under the Apache License v2.0 see LICENSE for details.
// Thank you to LLVM Project https://llvm.org/
//
//===--------------------------------------------------------------------------------------------------------------===//

#include "AST/ASTParams.h"
#include "AST/ASTParam.h"

using namespace fly;

Expand All @@ -33,29 +33,3 @@ std::string ASTParam::str() const {
Super(ASTLocalVar::str()).
End();
}

uint64_t ASTParams::getSize() const {
return List.size();
}

ASTParam *ASTParams::at(unsigned long Index) const {
return List[Index];
}

bool ASTParams::isEmpty() const {
return List.empty() && Ellipsis == nullptr;
}

const llvm::SmallVector<ASTParam *, 8> &ASTParams::getList() const {
return List;
}

const ASTParam *ASTParams::getEllipsis() const {
return Ellipsis;
}

std::string ASTParams::str() const {
return Logger("ASTParams").
AttrList("List", List).
End();
}
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ add_library(flyLib
AST/ASTLocalVar.cpp
AST/ASTNameSpace.cpp
AST/ASTNode.cpp
AST/ASTParams.cpp
AST/ASTParam.cpp
AST/ASTScopes.cpp
AST/ASTStmt.cpp
AST/ASTSwitchBlock.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/CodeGen/CodeGenClassFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ CodeGenClassFunction::CodeGenClassFunction(CodeGenModule *CGM, ASTClassFunction
GenParamTypes(CGM, ParamTypes, AST->getParams());

// Set LLVM Function Name %MODULE_CLASS_METHOD (if MODULE == default is empty)
FnType = llvm::FunctionType::get(RetType, ParamTypes, AST->getParams()->getEllipsis() != nullptr);
FnType = llvm::FunctionType::get(RetType, ParamTypes, AST->getEllipsis() != nullptr);

std::string Name = CodeGen::toIdentifier(getAST()->getName(), Class->getNameSpace()->getName(), Class->getName());
Fn = llvm::Function::Create(FnType, llvm::GlobalValue::ExternalLinkage, Name, CGM->getModule());
Expand Down
2 changes: 1 addition & 1 deletion src/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ CodeGenFunction::CodeGenFunction(CodeGenModule *CGM, ASTFunction *AST, bool isEx
GenParamTypes(CGM, ParamTypes, AST->getParams());

// Create LLVM Function
FnType = llvm::FunctionType::get(RetType, ParamTypes, AST->getParams()->getEllipsis() != nullptr);
FnType = llvm::FunctionType::get(RetType, ParamTypes, AST->getEllipsis() != nullptr);
Fn = llvm::Function::Create(FnType, llvm::GlobalValue::ExternalLinkage, "", CGM->getModule());

// Set Name
Expand Down
Loading

0 comments on commit 5451604

Please sign in to comment.