Skip to content

Commit 5332508

Browse files
committed
[AST] Enhance Struct Decl
1 parent 6f5353e commit 5332508

File tree

9 files changed

+102
-45
lines changed

9 files changed

+102
-45
lines changed

include/soll/ADT/STLExtras.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace soll {
1111

1212
template <typename T, typename... Args>
13-
std::vector<std::unique_ptr<T>> make_unique_vector(Args &&... args) {
13+
std::vector<std::unique_ptr<T>> make_unique_vector(Args &&...args) {
1414
std::vector<std::unique_ptr<T>> result;
1515
result.reserve(sizeof...(args));
1616
(result.emplace_back(std::forward<Args>(args)), ...);
@@ -26,4 +26,18 @@ template <bool Const, class Type> struct cond_const {
2626

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

29+
/// Concatenate the contents of a container onto a vector
30+
template <class T, class U>
31+
std::vector<T> &operator+=(std::vector<T> &A, U &B) {
32+
for (auto const &I : B)
33+
A.push_back(T(I));
34+
return A;
35+
}
36+
/// Concatenate the contents of a container onto a vector, move variant.
37+
template <class T, class U>
38+
std::vector<T> &operator+=(std::vector<T> &A, U &&B) {
39+
std::move(B.begin(), B.end(), std::back_inserter(A));
40+
return A;
41+
}
42+
2943
} // namespace soll

include/soll/AST/AST.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,3 @@
1010
#include "soll/AST/StmtAsm.h"
1111
#include "soll/AST/StmtVisitor.h"
1212
#include "soll/AST/Type.h"
13-
14-
namespace soll {
15-
16-
class AST {};
17-
18-
} // namespace soll

include/soll/AST/ASTBase.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
#pragma once
3+
4+
namespace soll {
5+
6+
class ASTNode {
7+
public:
8+
enum class ASTNodeType { DECL, STMT };
9+
ASTNode() = default;
10+
virtual ASTNodeType getASTType() = 0;
11+
};
12+
13+
} // namespace soll

include/soll/AST/ASTForward.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace soll {
77

8+
class AST;
89
class Decl;
910
class InheritanceSpecifier;
1011
class FunctionDecl;

include/soll/AST/Decl.h

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
22
#pragma once
33

4+
#include "soll/AST/ASTBase.h"
45
#include "soll/AST/ASTForward.h"
56
#include "soll/AST/DeclVisitor.h"
67
#include "soll/AST/Expr.h"
@@ -14,11 +15,13 @@ namespace soll {
1415

1516
class ASTContext;
1617

17-
class Decl {
18+
class Decl : public ASTNode {
1819
public:
1920
enum class Visibility { Default, Private, Internal, Public, External };
2021
virtual ~Decl() noexcept {}
2122

23+
ASTNodeType getASTType() override { return ASTNode::ASTNodeType::DECL; }
24+
2225
private:
2326
SourceRange Location;
2427
std::string Name;
@@ -31,8 +34,11 @@ class Decl {
3134
protected:
3235
Decl(SourceRange L,
3336
llvm::StringRef Name = llvm::StringRef::withNullAsEmpty(nullptr),
34-
Visibility vis = Visibility::Default)
35-
: Location(L), Name(Name.str()), Vis(vis), UniqueName(Name.str()) {}
37+
Visibility Vis = Visibility::Default)
38+
: Location(L), Name(Name.str()), Vis(Vis), UniqueName(Name.str()) {}
39+
40+
Decl(SourceRange L, std::string Name, Visibility Vis = Visibility::Default)
41+
: Location(L), Name(Name), Vis(Vis), UniqueName(Name) {}
3642

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

53+
/**
54+
* Pseudo AST node that is used as declaration for "this", "msg", "tx", "block"
55+
* and the global functions when such an identifier is encountered. Will never
56+
* have a valid location in the source code
57+
*/
58+
class MagicVariableDecl : public Decl {
59+
public:
60+
MagicVariableDecl(int Id, std::string MagicName, TypePtr Type)
61+
: Decl(SourceRange(), MagicName), Type(Type) {}
62+
63+
virtual void accept(DeclVisitor &visitor) override {
64+
assert(false && "MagicVariable should used inside real AST");
65+
};
66+
virtual void accept(ConstDeclVisitor &visitor) const override {
67+
assert(false && "MagicVariable should used inside real AST");
68+
};
69+
70+
TypePtr getType() { return Type; }
71+
72+
private:
73+
TypePtr Type;
74+
};
75+
4776
class SourceUnit : public Decl {
4877
std::vector<DeclPtr> Nodes;
4978

@@ -373,10 +402,11 @@ class StructDecl : public Decl {
373402
Token Tok;
374403
TypePtr Ty;
375404
TypePtr ConstructorTy;
405+
std::vector<VarDeclBasePtr> Members;
376406

377407
public:
378408
StructDecl(Token NameTok, SourceRange L, llvm::StringRef Name,
379-
std::vector<TypePtr> &&ET, std::vector<std::string> &&EN);
409+
std::vector<VarDeclBasePtr> &&Members);
380410

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

include/soll/AST/Stmt.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
22
#pragma once
33

4+
#include "soll/AST/ASTBase.h"
45
#include "soll/AST/ASTForward.h"
56
#include "soll/AST/StmtVisitor.h"
67
#include "soll/Basic/SourceLocation.h"
@@ -9,13 +10,16 @@
910

1011
namespace soll {
1112

12-
class Stmt {
13+
class Stmt : public ASTNode {
14+
1315
SourceRange Location;
1416

1517
public:
1618
explicit Stmt(SourceRange L) : Location(L) {}
1719
virtual ~Stmt() noexcept {}
1820

21+
ASTNodeType getASTType() override { return ASTNode::ASTNodeType::DECL; }
22+
1923
virtual void accept(StmtVisitor &visitor) = 0;
2024
virtual void accept(ConstStmtVisitor &visitor) const = 0;
2125

include/soll/AST/Type.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,12 @@ class ArrayType : public ReferenceType {
374374
class FunctionType : public Type {
375375
std::vector<std::reference_wrapper<const TypePtr>> ParamTypes;
376376
std::vector<std::reference_wrapper<const TypePtr>> ReturnTypes;
377-
std::shared_ptr<const std::vector<std::string>> ParamNames;
377+
std::shared_ptr<const std::vector<llvm::StringRef>> ParamNames;
378378

379379
public:
380380
FunctionType(std::vector<std::reference_wrapper<const TypePtr>> &&PTys,
381381
std::vector<std::reference_wrapper<const TypePtr>> &&RTys,
382-
std::shared_ptr<std::vector<std::string>> PNames = nullptr)
382+
std::shared_ptr<std::vector<llvm::StringRef>> PNames = nullptr)
383383
: ParamTypes(std::move(PTys)), ReturnTypes(std::move(RTys)),
384384
ParamNames(PNames) {}
385385

@@ -391,7 +391,7 @@ class FunctionType : public Type {
391391
getReturnTypes() const {
392392
return ReturnTypes;
393393
}
394-
std::shared_ptr<const std::vector<std::string>> getParamNames() const {
394+
std::shared_ptr<const std::vector<llvm::StringRef>> getParamNames() const {
395395
return ParamNames;
396396
}
397397

@@ -546,12 +546,12 @@ class ReturnTupleType : public TupleType {
546546

547547
class StructType : public TupleType {
548548
StructDecl *D;
549-
std::vector<std::string> ElementNames;
549+
std::vector<llvm::StringRef> ElementNames;
550550
llvm::StructType *Tp = nullptr;
551551

552552
public:
553553
StructType(StructDecl *D, std::vector<TypePtr> &&ET,
554-
std::vector<std::string> &&EN)
554+
std::vector<llvm::StringRef> &&EN)
555555
: TupleType(std::move(ET)), D(D), ElementNames(std::move(EN)) {}
556556
Category getCategory() const override { return Category::Struct; }
557557
StructDecl *getDecl() { return D; }
@@ -576,7 +576,7 @@ class StructType : public TupleType {
576576
bool hasElement(std::string Name) const {
577577
return getElementIndex(Name) < ElementNames.size();
578578
}
579-
const std::vector<std::string> &getElementNames() const {
579+
const std::vector<llvm::StringRef> &getElementNames() const {
580580
return ElementNames;
581581
}
582582
};

lib/AST/Decl.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ FunctionDecl::FunctionDecl(
287287

288288
std::vector<std::reference_wrapper<const TypePtr>> PTys;
289289
std::vector<std::reference_wrapper<const TypePtr>> RTys;
290-
auto PNames = std::make_shared<std::vector<std::string>>();
290+
auto PNames = std::make_shared<std::vector<llvm::StringRef>>();
291291
for (auto VD : this->getParams()->getParams()) {
292-
PNames->emplace_back(VD->getName().str());
292+
PNames->emplace_back(VD->getName());
293293
PTys.emplace_back(std::cref(VD->getType()));
294294
}
295295
if (this->getReturnParams()->getParams().size())
@@ -332,18 +332,30 @@ EventDecl::EventDecl(SourceRange L, llvm::StringRef Name,
332332
}
333333

334334
StructDecl::StructDecl(Token NameTok, SourceRange L, llvm::StringRef Name,
335-
std::vector<TypePtr> &&ET, std::vector<std::string> &&EN)
336-
: Decl(L, Name, Visibility::Default), Tok(NameTok),
337-
Ty(std::make_shared<StructType>(this, std::move(ET), std::move(EN))) {
335+
std::vector<VarDeclBasePtr> &&Members)
336+
: Decl(L, Name, Visibility::Default), Tok(NameTok) {
337+
338+
std::vector<TypePtr> ElementTypes;
339+
std::vector<llvm::StringRef> ElementNames;
340+
for (auto &E : Members) {
341+
ElementTypes.emplace_back(E->getType());
342+
ElementNames.emplace_back(E->getName());
343+
}
344+
345+
Ty = std::make_shared<StructType>(this, std::move(ElementTypes),
346+
std::move(ElementNames));
347+
348+
std::vector<std::reference_wrapper<const TypePtr>> CElementTypes;
338349
auto STy = dynamic_cast<const StructType *>(Ty.get());
339-
std::vector<std::reference_wrapper<const TypePtr>> ElementTypes;
350+
340351
for (const auto &ETy : STy->getElementTypes()) {
341-
ElementTypes.emplace_back(std::cref(ETy));
352+
CElementTypes.emplace_back(std::cref(ETy));
342353
}
354+
343355
ConstructorTy = std::make_shared<FunctionType>(
344-
std::move(ElementTypes),
356+
std::move(CElementTypes),
345357
std::vector<std::reference_wrapper<const TypePtr>>{std::cref(Ty)},
346-
std::make_shared<std::vector<std::string>>(STy->getElementNames()));
358+
std::make_shared<std::vector<llvm::StringRef>>(STy->getElementNames()));
347359
}
348360

349361
} // namespace soll

lib/Parse/Parser.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -801,26 +801,15 @@ std::unique_ptr<StructDecl> Parser::parseStructDeclaration() {
801801
return nullptr;
802802
}
803803
SourceLocation End = Tok.getEndLoc();
804-
std::vector<TypePtr> ElementTypes;
805-
std::vector<std::string> ElementNames;
806-
while (Tok.isNot(tok::eof)) {
807-
if (Tok.is(tok::r_brace)) {
808-
End = Tok.getEndLoc();
809-
ConsumeBrace();
810-
break;
811-
}
812-
TypePtr T = parseTypeName(false);
813-
std::string ElementName = Tok.getIdentifierInfo()->getName().str();
814-
ConsumeToken();
815-
if (ExpectAndConsumeSemi()) {
816-
return nullptr;
817-
}
818-
ElementTypes.emplace_back(T);
819-
ElementNames.emplace_back(ElementName);
804+
std::vector<VarDeclBasePtr> Members;
805+
806+
while (Tok.is(tok::r_brace)) {
807+
Members.push_back(parseVariableDeclaration());
808+
ExpectAndConsumeSemi();
820809
}
810+
821811
auto SD = std::make_unique<StructDecl>(NameTok, SourceRange(Begin, End), Name,
822-
std::move(ElementTypes),
823-
std::move(ElementNames));
812+
std::move(Members));
824813
return SD;
825814
}
826815

0 commit comments

Comments
 (0)