Skip to content

[AST] Enhance Struct Decl #117

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

Open
wants to merge 2 commits 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
21 changes: 18 additions & 3 deletions include/soll/ADT/STLExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
namespace soll {

template <typename T, typename... Args>
std::vector<std::unique_ptr<T>> make_unique_vector(Args &&... args) {
std::vector<std::unique_ptr<T>> make_unique_vector(Args &&... As) {
std::vector<std::unique_ptr<T>> result;
result.reserve(sizeof...(args));
(result.emplace_back(std::forward<Args>(args)), ...);
result.reserve(sizeof...(As));
(result.emplace_back(std::forward<Args>(As)), ...);
return result;
}

Expand All @@ -26,4 +26,19 @@ template <bool Const, class Type> struct cond_const {

template <class Type> struct cond_const<false, Type> { typedef Type type; };

/// Concatenate the contents of a container onto a vector
template <class T, class U>
std::vector<T> &operator+=(std::vector<T> &A, U &B) {
for (auto const &I : B) {
A.push_back(T(I));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have {} in a loop statement.

}
return A;
}
/// Concatenate the contents of a container onto a vector, move variant.
template <class T, class U>
std::vector<T> &operator+=(std::vector<T> &A, U &&B) {
std::move(B.begin(), B.end(), std::back_inserter(A));
return A;
}

} // namespace soll
6 changes: 0 additions & 6 deletions include/soll/AST/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,3 @@
#include "soll/AST/StmtAsm.h"
#include "soll/AST/StmtVisitor.h"
#include "soll/AST/Type.h"

namespace soll {

class AST {};

} // namespace soll
14 changes: 14 additions & 0 deletions include/soll/AST/ASTBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once

namespace soll {

class ASTNode {
public:
enum class ASTNodeType { DECL, STMT };
ASTNode() = default;
virtual ~ASTNode() = default;
virtual ASTNodeType getASTType() = 0;
};

} // namespace soll
1 change: 1 addition & 0 deletions include/soll/AST/ASTForward.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace soll {

class AST;
class Decl;
class InheritanceSpecifier;
class FunctionDecl;
Expand Down
61 changes: 57 additions & 4 deletions include/soll/AST/Decl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once

#include "soll/AST/ASTBase.h"
#include "soll/AST/ASTForward.h"
#include "soll/AST/DeclVisitor.h"
#include "soll/AST/Expr.h"
Expand All @@ -14,25 +15,32 @@ namespace soll {

class ASTContext;

class Decl {
class Decl : public ASTNode {
public:
enum class Visibility { Default, Private, Internal, Public, External };
virtual ~Decl() noexcept {}

ASTNodeType getASTType() override { return ASTNode::ASTNodeType::DECL; }

private:
SourceRange Location;
std::string Name;
Visibility Vis;
std::string UniqueName;
const ASTNode *Parent;

protected:
friend class ASTReader;

protected:
Decl(SourceRange L,
llvm::StringRef Name = llvm::StringRef::withNullAsEmpty(nullptr),
Visibility vis = Visibility::Default)
: Location(L), Name(Name.str()), Vis(vis), UniqueName(Name.str()) {}
Visibility Vis = Visibility::Default)
: Location(L), Name(Name.str()), Vis(Vis), UniqueName(Name.str()),
Parent(nullptr) {}

Decl(SourceRange L, std::string Name, Visibility Vis = Visibility::Default)
: Location(L), Name(Name), Vis(Vis), UniqueName(Name), Parent(nullptr) {}

public:
virtual void accept(DeclVisitor &visitor) = 0;
Expand All @@ -42,6 +50,38 @@ class Decl {
llvm::StringRef getUniqueName() const { return UniqueName; }
void setUniqueName(llvm::StringRef NewName) { UniqueName = NewName.str(); }
Visibility getVisibility() const { return Vis; }

void setScope(const ASTNode *D) { Parent = D; }
/// @returns the scope this declaration resides in. Can be nullptr if it is
/// the global scope. Available only after name and type resolution step.
const ASTNode *scope() const { return Parent; }

bool isStructMember() const;

bool isVisibleAsUnqualifiedName() const;
};

/**
* Pseudo AST node that is used as declaration for "this", "msg", "tx", "block"
* and the global functions when such an identifier is encountered. Will never
* have a valid location in the source code
*/
class MagicVariableDecl : public Decl {
public:
MagicVariableDecl(int Id, std::string MagicName, TypePtr Type)
: Decl(SourceRange(), MagicName), Type(Type) {}

virtual void accept(DeclVisitor &visitor) override {
assert(false && "MagicVariable should used inside real AST");
};
virtual void accept(ConstDeclVisitor &visitor) const override {
assert(false && "MagicVariable should used inside real AST");
};

TypePtr getType() { return Type; }

private:
TypePtr Type;
};

class SourceUnit : public Decl {
Expand Down Expand Up @@ -373,17 +413,30 @@ class StructDecl : public Decl {
Token Tok;
TypePtr Ty;
TypePtr ConstructorTy;
std::vector<VarDeclBasePtr> Members;

public:
StructDecl(Token NameTok, SourceRange L, llvm::StringRef Name,
std::vector<TypePtr> &&ET, std::vector<std::string> &&EN);
std::vector<VarDeclBasePtr> &&Members);

void accept(DeclVisitor &Visitor) override;
void accept(ConstDeclVisitor &Visitor) const override;

Token getToken() const { return Tok; }
TypePtr getType() const { return Ty; }
TypePtr getConstructorType() const { return ConstructorTy; }
std::vector<Decl *> getMembers() {
std::vector<Decl *> Res;
for (auto &M : Members)
Res.push_back(M.get());
return Res;
}
std::vector<const Decl *> getMembers() const {
std::vector<const Decl *> Res;
for (auto &M : Members)
Res.push_back(M.get());
return Res;
}
};

class ModifierInvocation {
Expand Down
6 changes: 5 additions & 1 deletion include/soll/AST/Stmt.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once

#include "soll/AST/ASTBase.h"
#include "soll/AST/ASTForward.h"
#include "soll/AST/StmtVisitor.h"
#include "soll/Basic/SourceLocation.h"
Expand All @@ -9,13 +10,16 @@

namespace soll {

class Stmt {
class Stmt : public ASTNode {

SourceRange Location;

public:
explicit Stmt(SourceRange L) : Location(L) {}
virtual ~Stmt() noexcept {}

ASTNodeType getASTType() override { return ASTNode::ASTNodeType::DECL; }

virtual void accept(StmtVisitor &visitor) = 0;
virtual void accept(ConstStmtVisitor &visitor) const = 0;

Expand Down
12 changes: 6 additions & 6 deletions include/soll/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,12 @@ class ArrayType : public ReferenceType {
class FunctionType : public Type {
std::vector<std::reference_wrapper<const TypePtr>> ParamTypes;
std::vector<std::reference_wrapper<const TypePtr>> ReturnTypes;
std::shared_ptr<const std::vector<std::string>> ParamNames;
std::shared_ptr<const std::vector<llvm::StringRef>> ParamNames;

public:
FunctionType(std::vector<std::reference_wrapper<const TypePtr>> &&PTys,
std::vector<std::reference_wrapper<const TypePtr>> &&RTys,
std::shared_ptr<std::vector<std::string>> PNames = nullptr)
std::shared_ptr<std::vector<llvm::StringRef>> PNames = nullptr)
: ParamTypes(std::move(PTys)), ReturnTypes(std::move(RTys)),
ParamNames(PNames) {}

Expand All @@ -391,7 +391,7 @@ class FunctionType : public Type {
getReturnTypes() const {
return ReturnTypes;
}
std::shared_ptr<const std::vector<std::string>> getParamNames() const {
std::shared_ptr<const std::vector<llvm::StringRef>> getParamNames() const {
return ParamNames;
}

Expand Down Expand Up @@ -546,12 +546,12 @@ class ReturnTupleType : public TupleType {

class StructType : public TupleType {
StructDecl *D;
std::vector<std::string> ElementNames;
std::vector<llvm::StringRef> ElementNames;
llvm::StructType *Tp = nullptr;

public:
StructType(StructDecl *D, std::vector<TypePtr> &&ET,
std::vector<std::string> &&EN)
std::vector<llvm::StringRef> &&EN)
: TupleType(std::move(ET)), D(D), ElementNames(std::move(EN)) {}
Category getCategory() const override { return Category::Struct; }
StructDecl *getDecl() { return D; }
Expand All @@ -576,7 +576,7 @@ class StructType : public TupleType {
bool hasElement(std::string Name) const {
return getElementIndex(Name) < ElementNames.size();
}
const std::vector<std::string> &getElementNames() const {
const std::vector<llvm::StringRef> &getElementNames() const {
return ElementNames;
}
};
Expand Down
3 changes: 3 additions & 0 deletions include/soll/Basic/DiagnosticSemaKinds.inc
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ DIAG(err_visibility_not_match, CLASS_ERROR, (unsigned)diag::Severity::Error, "vi
DIAG(err_private_function_can_not_be_virtual, CLASS_ERROR, (unsigned)diag::Severity::Error, "private funtcion %0 can not be virtual", 0, false, 1)
DIAG(err_mutability_overriding_not_allow, CLASS_ERROR, (unsigned)diag::Severity::Error, "can not change function %0 mutability", 0, false, 1)
DIAG(err_func_to_var_overrided_unsoupport, CLASS_ERROR, (unsigned)diag::Severity::Error, "function to state variables override is unsupport", 0, false, 1)
DIAG(err_use_illegalnames, CLASS_WARNING, (unsigned)diag::Severity::Warning, "Use of illegal identifier name: '%0'", 0, false, 0)
DIAG(err_already_declared, CLASS_WARNING, (unsigned)diag::Severity::Warning, "identifier '%0' already declared", 0, false, 0)
DIAG(err_declaration_not_found, CLASS_ERROR, (unsigned)diag::Severity::Error, "identifier '%0' not found", 0, false, 0)
Loading