Add the namespace table to the context
This commit is contained in:
parent
c1c1421531
commit
66cecc77f3
|
@ -26,19 +26,18 @@
|
||||||
#define SERENE_CONTEXT_H
|
#define SERENE_CONTEXT_H
|
||||||
|
|
||||||
#include "serene/environment.h"
|
#include "serene/environment.h"
|
||||||
|
#include "serene/namespace.h"
|
||||||
#include "llvm/ADT/StringRef.h"
|
#include "llvm/ADT/StringRef.h"
|
||||||
|
|
||||||
namespace serene {
|
namespace serene {
|
||||||
|
|
||||||
class Namespace;
|
|
||||||
|
|
||||||
namespace exprs {
|
namespace exprs {
|
||||||
class Expression;
|
class Expression;
|
||||||
using Node = std::shared_ptr<Expression>;
|
using Node = std::shared_ptr<Expression>;
|
||||||
} // namespace exprs
|
} // namespace exprs
|
||||||
|
|
||||||
struct SereneContext {
|
struct SereneContext {
|
||||||
// llvm::DenseMap<llvm::StringRef, Namespace> namespaces;
|
llvm::DenseMap<llvm::StringRef, std::unique_ptr<Namespace>> namespaces;
|
||||||
|
|
||||||
Environment<llvm::StringRef, exprs::Node> semanticEnv;
|
Environment<llvm::StringRef, exprs::Node> semanticEnv;
|
||||||
SereneContext(){};
|
SereneContext(){};
|
||||||
|
|
|
@ -25,43 +25,40 @@
|
||||||
#ifndef NAMESPACE_H
|
#ifndef NAMESPACE_H
|
||||||
#define NAMESPACE_H
|
#define NAMESPACE_H
|
||||||
|
|
||||||
#include "mlir/IR/BuiltinOps.h"
|
#include "serene/environment.h"
|
||||||
#include "mlir/IR/Value.h"
|
#include <llvm/ADT/StringRef.h>
|
||||||
#include "serene/exprs/expression.h"
|
|
||||||
#include "serene/llvm/IR/Value.h"
|
|
||||||
#include "llvm/ADT/DenseMap.h"
|
|
||||||
#include <llvm/IR/Module.h>
|
#include <llvm/IR/Module.h>
|
||||||
|
#include <mlir/Support/LogicalResult.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#define NAMESPACE_LOG(...) \
|
#define NAMESPACE_LOG(...) \
|
||||||
DEBUG_WITH_TYPE("NAMESPACE", llvm::dbgs() << __VA_ARGS__ << "\n");
|
DEBUG_WITH_TYPE("NAMESPACE", llvm::dbgs() << __VA_ARGS__ << "\n");
|
||||||
|
|
||||||
using ScopeMap = llvm::DenseMap<llvm::StringRef, mlir::Value>;
|
|
||||||
using PairT = std::pair<llvm::StringRef, mlir::Value>;
|
|
||||||
|
|
||||||
namespace serene {
|
namespace serene {
|
||||||
class AExpr;
|
namespace exprs {
|
||||||
|
class Expression;
|
||||||
|
using Node = std::shared_ptr<Expression>;
|
||||||
|
using Ast = std::vector<Node>;
|
||||||
|
} // namespace exprs
|
||||||
|
|
||||||
class Namespace {
|
class Namespace {
|
||||||
private:
|
private:
|
||||||
exprs::Ast tree{};
|
|
||||||
bool initialized = false;
|
bool initialized = false;
|
||||||
|
exprs::Ast tree;
|
||||||
ScopeMap rootScope;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
llvm::Optional<llvm::StringRef> filename;
|
|
||||||
mlir::StringRef name;
|
mlir::StringRef name;
|
||||||
|
llvm::Optional<llvm::StringRef> filename;
|
||||||
|
|
||||||
|
/// The root environment of the namespace on the semantic analysis phase.
|
||||||
|
/// Which is a mapping from names to AST nodes ( no evaluation ).
|
||||||
|
Environment<llvm::StringRef, exprs::Node> semanticEnv;
|
||||||
|
|
||||||
Namespace(llvm::StringRef ns_name, llvm::Optional<llvm::StringRef> filename);
|
Namespace(llvm::StringRef ns_name, llvm::Optional<llvm::StringRef> filename);
|
||||||
|
|
||||||
exprs::Ast &Tree();
|
exprs::Ast &Tree();
|
||||||
mlir::LogicalResult setTree(exprs::Ast &);
|
mlir::LogicalResult setTree(exprs::Ast &);
|
||||||
// TODO: Fix it to return llvm::Optional<mlir::Value> instead
|
|
||||||
llvm::Optional<mlir::Value> lookup(llvm::StringRef name);
|
|
||||||
mlir::LogicalResult insert_symbol(llvm::StringRef name, mlir::Value v);
|
|
||||||
|
|
||||||
void print_scope();
|
|
||||||
~Namespace();
|
~Namespace();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -43,14 +43,6 @@ Namespace::Namespace(llvm::StringRef ns_name,
|
||||||
|
|
||||||
exprs::Ast &Namespace::Tree() { return this->tree; }
|
exprs::Ast &Namespace::Tree() { return this->tree; }
|
||||||
|
|
||||||
llvm::Optional<mlir::Value> Namespace::lookup(llvm::StringRef name) {
|
|
||||||
if (auto value = rootScope.lookup(name)) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return llvm::None;
|
|
||||||
};
|
|
||||||
|
|
||||||
mlir::LogicalResult Namespace::setTree(exprs::Ast &t) {
|
mlir::LogicalResult Namespace::setTree(exprs::Ast &t) {
|
||||||
if (initialized) {
|
if (initialized) {
|
||||||
return mlir::failure();
|
return mlir::failure();
|
||||||
|
@ -60,15 +52,6 @@ mlir::LogicalResult Namespace::setTree(exprs::Ast &t) {
|
||||||
return mlir::success();
|
return mlir::success();
|
||||||
}
|
}
|
||||||
|
|
||||||
mlir::LogicalResult Namespace::insert_symbol(mlir::StringRef name,
|
|
||||||
mlir::Value v) {
|
|
||||||
|
|
||||||
rootScope.insert(PairT(name, v));
|
|
||||||
return mlir::success();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Namespace::print_scope(){};
|
|
||||||
|
|
||||||
Namespace::~Namespace() {}
|
Namespace::~Namespace() {}
|
||||||
|
|
||||||
} // namespace serene
|
} // namespace serene
|
||||||
|
|
Loading…
Reference in New Issue