Skip to content

Commit

Permalink
[AST] move TypePtr to ASTContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinkela committed Jun 16, 2021
1 parent 36c7426 commit d1659e9
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 175 deletions.
13 changes: 13 additions & 0 deletions include/soll/AST/ASTContext.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once
#include "soll/AST/Type.h"
#include "soll/Frontend/FrontendOptions.h"
#include <llvm/ADT/APInt.h>
#include <llvm/ADT/IntrusiveRefCntPtr.h>
Expand All @@ -12,6 +13,18 @@ class ASTContext : public llvm::RefCountedBase<ASTContext> {
const llvm::StringMap<llvm::APInt> LibrariesAddressMap;

public:
const TypePtr IntegerTypeU256Ptr;
const TypePtr IntegerTypeI256Ptr;
const TypePtr ContractTypePtr;
const TypePtr FixedBytesTypeB32Ptr;
const TypePtr FixedBytesTypeB20Ptr;
const TypePtr FixedBytesTypeB4Ptr;
const TypePtr FixedBytesTypeB1Ptr;
const TypePtr BooleanTypePtr;
const TypePtr StringTypePtr;
const TypePtr BytesTypePtr;
const TypePtr AddressTypeNonPayablePtr;
const TypePtr AddressTypePayablePtr;
ASTContext(InputKind Language,
const std::vector<std::string> &LibrariesAddressInfo);

Expand Down
20 changes: 0 additions & 20 deletions include/soll/AST/TypePtr.h

This file was deleted.

2 changes: 2 additions & 0 deletions include/soll/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class Sema {
return LibrariesAddressMap;
}

ASTContext &getContext() { return Context; }

// Decl
std::unique_ptr<FunctionDecl> CreateFunctionDecl(
SourceRange L, llvm::StringRef name, FunctionDecl::Visibility visibility,
Expand Down
22 changes: 21 additions & 1 deletion lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,25 @@ extractLibraries(const std::vector<std::string> &LibrariesAddressInfo) {
ASTContext::ASTContext(InputKind Language,
const std::vector<std::string> &LibrariesAddressInfo)
: Language(Language),
LibrariesAddressMap(extractLibraries(LibrariesAddressInfo)) {}
LibrariesAddressMap(extractLibraries(LibrariesAddressInfo)),
IntegerTypeU256Ptr(
std::make_shared<IntegerType>(IntegerType::IntKind::U256)),
IntegerTypeI256Ptr(
std::make_shared<IntegerType>(IntegerType::IntKind::I256)),
ContractTypePtr(std::make_shared<ContractType>()),
FixedBytesTypeB32Ptr(
std::make_shared<FixedBytesType>(FixedBytesType::ByteKind::B32)),
FixedBytesTypeB20Ptr(
std::make_shared<FixedBytesType>(FixedBytesType::ByteKind::B20)),
FixedBytesTypeB4Ptr(
std::make_shared<FixedBytesType>(FixedBytesType::ByteKind::B4)),
FixedBytesTypeB1Ptr(
std::make_shared<FixedBytesType>(FixedBytesType::ByteKind::B1)),
BooleanTypePtr(std::make_shared<BooleanType>()),
StringTypePtr(std::make_shared<StringType>()),
BytesTypePtr(std::make_shared<BytesType>()),
AddressTypeNonPayablePtr(
std::make_shared<AddressType>(StateMutability::NonPayable)),
AddressTypePayablePtr(
std::make_shared<AddressType>(StateMutability::Payable)) {}
} // namespace soll
1 change: 0 additions & 1 deletion lib/AST/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ add_llvm_library(sollAST
StmtAsm.cpp
StmtVisitor.cpp
Type.cpp
TypePtr.cpp
LINK_LIBS
SHA3
)
25 changes: 0 additions & 25 deletions lib/AST/TypePtr.cpp

This file was deleted.

61 changes: 32 additions & 29 deletions lib/Sema/Sema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "soll/Sema/Sema.h"
#include "soll/AST/AST.h"
#include "soll/AST/ASTConsumer.h"
#include "soll/AST/TypePtr.h"
#include "soll/Basic/DiagnosticSema.h"
#include "soll/Lex/Lexer.h"
#include "soll/Sema/Scope.h"
Expand Down Expand Up @@ -115,76 +114,80 @@ std::unique_ptr<Identifier> Sema::CreateIdentifier(const Token &Tok) {
case Identifier::SpecialIdentifier::msg:
case Identifier::SpecialIdentifier::tx:
case Identifier::SpecialIdentifier::abi:
Ty = ContractTypePtr;
Ty = Context.ContractTypePtr;
break;
case Identifier::SpecialIdentifier::now:
Ty = IntegerTypeU256Ptr;
Ty = Context.IntegerTypeU256Ptr;
break;
case Identifier::SpecialIdentifier::blockhash:
Ty = std::make_shared<FunctionType>(
std::vector<std::reference_wrapper<const TypePtr>>{
std::cref(IntegerTypeU256Ptr)},
std::cref(Context.IntegerTypeU256Ptr)},
std::vector<std::reference_wrapper<const TypePtr>>{
std::cref(FixedBytesTypeB32Ptr)});
std::cref(Context.FixedBytesTypeB32Ptr)});
break;
case Identifier::SpecialIdentifier::gasleft:
Ty = std::make_shared<FunctionType>(
std::vector<std::reference_wrapper<const TypePtr>>{},
std::vector<std::reference_wrapper<const TypePtr>>{
std::cref(IntegerTypeU256Ptr)});
std::cref(Context.IntegerTypeU256Ptr)});
break;
case Identifier::SpecialIdentifier::assert_:
Ty = std::make_shared<FunctionType>(
std::vector<std::reference_wrapper<const TypePtr>>{
std::cref(BooleanTypePtr)},
std::cref(Context.BooleanTypePtr)},
std::vector<std::reference_wrapper<const TypePtr>>{});
break;
case Identifier::SpecialIdentifier::require:
Ty = std::make_shared<FunctionType>(
std::vector<std::reference_wrapper<const TypePtr>>{
std::cref(BooleanTypePtr), std::cref(StringTypePtr)},
std::cref(Context.BooleanTypePtr),
std::cref(Context.StringTypePtr)},
std::vector<std::reference_wrapper<const TypePtr>>{});
break;
case Identifier::SpecialIdentifier::revert:
Ty = std::make_shared<FunctionType>(
std::vector<std::reference_wrapper<const TypePtr>>{
std::cref(StringTypePtr)},
std::cref(Context.StringTypePtr)},
std::vector<std::reference_wrapper<const TypePtr>>{});
break;
case Identifier::SpecialIdentifier::addmod:
case Identifier::SpecialIdentifier::mulmod:
Ty = std::make_shared<FunctionType>(
std::vector<std::reference_wrapper<const TypePtr>>{
std::cref(IntegerTypeU256Ptr), std::cref(IntegerTypeU256Ptr),
std::cref(IntegerTypeU256Ptr)},
std::cref(Context.IntegerTypeU256Ptr),
std::cref(Context.IntegerTypeU256Ptr),
std::cref(Context.IntegerTypeU256Ptr)},
std::vector<std::reference_wrapper<const TypePtr>>{
std::cref(IntegerTypeU256Ptr)});
std::cref(Context.IntegerTypeU256Ptr)});
break;
case Identifier::SpecialIdentifier::keccak256:
case Identifier::SpecialIdentifier::sha256:
Ty = std::make_shared<FunctionType>(
std::vector<std::reference_wrapper<const TypePtr>>{
std::cref(BytesTypePtr)},
std::cref(Context.BytesTypePtr)},
std::vector<std::reference_wrapper<const TypePtr>>{
std::cref(FixedBytesTypeB32Ptr)});
std::cref(Context.FixedBytesTypeB32Ptr)});
break;
case Identifier::SpecialIdentifier::ripemd160:
Ty = std::make_shared<FunctionType>(
std::vector<std::reference_wrapper<const TypePtr>>{
std::cref(BytesTypePtr)},
std::cref(Context.BytesTypePtr)},
std::vector<std::reference_wrapper<const TypePtr>>{
std::cref(FixedBytesTypeB20Ptr)});
std::cref(Context.FixedBytesTypeB20Ptr)});
break;
case Identifier::SpecialIdentifier::ecrecover:
Ty = std::make_shared<FunctionType>(
std::vector<std::reference_wrapper<const TypePtr>>{
std::cref(FixedBytesTypeB32Ptr), std::cref(IntegerTypeU256Ptr),
std::cref(FixedBytesTypeB32Ptr), std::cref(FixedBytesTypeB32Ptr)},
std::cref(Context.FixedBytesTypeB32Ptr),
std::cref(Context.IntegerTypeU256Ptr),
std::cref(Context.FixedBytesTypeB32Ptr),
std::cref(Context.FixedBytesTypeB32Ptr)},
std::vector<std::reference_wrapper<const TypePtr>>{
std::cref(AddressTypeNonPayablePtr)});
std::cref(Context.AddressTypeNonPayablePtr)});
break;
case Identifier::SpecialIdentifier::this_:
Ty = ContractTypePtr;
Ty = Context.ContractTypePtr;
break;
default:
assert(false && "unknown special identifier");
Expand Down Expand Up @@ -247,7 +250,7 @@ Sema::CreateMemberExpr(std::unique_ptr<Expr> &&BaseExpr, Token Tok) {
case Identifier::SpecialIdentifier::abi_encode:
case Identifier::SpecialIdentifier::abi_encodeWithSelector:
case Identifier::SpecialIdentifier::abi_encodeWithSignature:
Ty = BytesTypePtr;
Ty = Context.BytesTypePtr;
break;
case Identifier::SpecialIdentifier::abi_decode:
Diag(Tok.getLocation(), diag::err_unimplemented_identifier)
Expand All @@ -262,13 +265,13 @@ Sema::CreateMemberExpr(std::unique_ptr<Expr> &&BaseExpr, Token Tok) {
case Identifier::SpecialIdentifier::block:
switch (Iter->second) {
case Identifier::SpecialIdentifier::block_coinbase:
Ty = AddressTypePayablePtr;
Ty = Context.AddressTypePayablePtr;
break;
case Identifier::SpecialIdentifier::block_difficulty:
case Identifier::SpecialIdentifier::block_gaslimit:
case Identifier::SpecialIdentifier::block_number:
case Identifier::SpecialIdentifier::block_timestamp:
Ty = IntegerTypeU256Ptr;
Ty = Context.IntegerTypeU256Ptr;
break;
default:
assert(false && "unknown member");
Expand All @@ -278,16 +281,16 @@ Sema::CreateMemberExpr(std::unique_ptr<Expr> &&BaseExpr, Token Tok) {
case Identifier::SpecialIdentifier::msg:
switch (Iter->second) {
case Identifier::SpecialIdentifier::msg_data:
Ty = BytesTypePtr;
Ty = Context.BytesTypePtr;
break;
case Identifier::SpecialIdentifier::msg_sender:
Ty = AddressTypePayablePtr;
Ty = Context.AddressTypePayablePtr;
break;
case Identifier::SpecialIdentifier::msg_sig:
Ty = FixedBytesTypeB4Ptr;
Ty = Context.FixedBytesTypeB4Ptr;
break;
case Identifier::SpecialIdentifier::msg_value:
Ty = IntegerTypeU256Ptr;
Ty = Context.IntegerTypeU256Ptr;
break;
default:
assert(false && "unknown member");
Expand All @@ -297,10 +300,10 @@ Sema::CreateMemberExpr(std::unique_ptr<Expr> &&BaseExpr, Token Tok) {
case Identifier::SpecialIdentifier::tx:
switch (Iter->second) {
case Identifier::SpecialIdentifier::tx_gasprice:
Ty = IntegerTypeU256Ptr;
Ty = Context.IntegerTypeU256Ptr;
break;
case Identifier::SpecialIdentifier::tx_origin:
Ty = AddressTypePayablePtr;
Ty = Context.AddressTypePayablePtr;
break;
default:
assert(false && "unknown member");
Expand Down
Loading

0 comments on commit d1659e9

Please sign in to comment.