Skip to content

Commit

Permalink
[AST] Enhance Struct Decl
Browse files Browse the repository at this point in the history
  • Loading branch information
LFsWang committed Sep 9, 2021
1 parent 821b1f6 commit fcbb73d
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 45 deletions.
16 changes: 15 additions & 1 deletion include/soll/ADT/STLExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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 &&...args) {
std::vector<std::unique_ptr<T>> result;
result.reserve(sizeof...(args));
(result.emplace_back(std::forward<Args>(args)), ...);
Expand All @@ -26,4 +26,18 @@ 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));
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
13 changes: 13 additions & 0 deletions include/soll/AST/ASTBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once

namespace soll {

class ASTNode {
public:
enum class ASTNodeType { DECL, STMT };
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
38 changes: 34 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,11 +15,13 @@ 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;
Expand All @@ -31,8 +34,11 @@ class Decl {
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()) {}

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

public:
virtual void accept(DeclVisitor &visitor) = 0;
Expand All @@ -44,6 +50,29 @@ class Decl {
Visibility getVisibility() const { return Vis; }
};

/**
* 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 {
std::vector<DeclPtr> Nodes;

Expand Down Expand Up @@ -373,10 +402,11 @@ 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;
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
30 changes: 21 additions & 9 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ FunctionDecl::FunctionDecl(

std::vector<std::reference_wrapper<const TypePtr>> PTys;
std::vector<std::reference_wrapper<const TypePtr>> RTys;
auto PNames = std::make_shared<std::vector<std::string>>();
auto PNames = std::make_shared<std::vector<llvm::StringRef>>();
for (auto VD : this->getParams()->getParams()) {
PNames->emplace_back(VD->getName().str());
PNames->emplace_back(VD->getName());
PTys.emplace_back(std::cref(VD->getType()));
}
if (this->getReturnParams()->getParams().size())
Expand Down Expand Up @@ -332,18 +332,30 @@ EventDecl::EventDecl(SourceRange L, llvm::StringRef Name,
}

StructDecl::StructDecl(Token NameTok, SourceRange L, llvm::StringRef Name,
std::vector<TypePtr> &&ET, std::vector<std::string> &&EN)
: Decl(L, Name, Visibility::Default), Tok(NameTok),
Ty(std::make_shared<StructType>(this, std::move(ET), std::move(EN))) {
std::vector<VarDeclBasePtr> &&Members)
: Decl(L, Name, Visibility::Default), Tok(NameTok) {

std::vector<TypePtr> ElementTypes;
std::vector<llvm::StringRef> ElementNames;
for (auto &E : Members) {
ElementTypes.emplace_back(E->getType());
ElementNames.emplace_back(E->getName());
}

Ty = std::make_shared<StructType>(this, std::move(ElementTypes),
std::move(ElementNames));

std::vector<std::reference_wrapper<const TypePtr>> CElementTypes;
auto STy = dynamic_cast<const StructType *>(Ty.get());
std::vector<std::reference_wrapper<const TypePtr>> ElementTypes;

for (const auto &ETy : STy->getElementTypes()) {
ElementTypes.emplace_back(std::cref(ETy));
CElementTypes.emplace_back(std::cref(ETy));
}

ConstructorTy = std::make_shared<FunctionType>(
std::move(ElementTypes),
std::move(CElementTypes),
std::vector<std::reference_wrapper<const TypePtr>>{std::cref(Ty)},
std::make_shared<std::vector<std::string>>(STy->getElementNames()));
std::make_shared<std::vector<llvm::StringRef>>(STy->getElementNames()));
}

} // namespace soll
25 changes: 7 additions & 18 deletions lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,26 +801,15 @@ std::unique_ptr<StructDecl> Parser::parseStructDeclaration() {
return nullptr;
}
SourceLocation End = Tok.getEndLoc();
std::vector<TypePtr> ElementTypes;
std::vector<std::string> ElementNames;
while (Tok.isNot(tok::eof)) {
if (Tok.is(tok::r_brace)) {
End = Tok.getEndLoc();
ConsumeBrace();
break;
}
TypePtr T = parseTypeName(false);
std::string ElementName = Tok.getIdentifierInfo()->getName().str();
ConsumeToken();
if (ExpectAndConsumeSemi()) {
return nullptr;
}
ElementTypes.emplace_back(T);
ElementNames.emplace_back(ElementName);
std::vector<VarDeclBasePtr> Members;

while (Tok.is(tok::r_brace)) {
Members.push_back(parseVariableDeclaration());
ExpectAndConsumeSemi();
}

auto SD = std::make_unique<StructDecl>(NameTok, SourceRange(Begin, End), Name,
std::move(ElementTypes),
std::move(ElementNames));
std::move(Members));
return SD;
}

Expand Down

0 comments on commit fcbb73d

Please sign in to comment.