mirror of https://github.com/exaloop/codon.git
Refactor stmt.h
parent
9ddc3d1ce7
commit
6315dcc3c9
|
@ -0,0 +1,55 @@
|
|||
// Copyright (C) 2022-2024 Exaloop Inc. <https://exaloop.io>
|
||||
|
||||
#include "attr.h"
|
||||
|
||||
namespace codon::ast {
|
||||
|
||||
const std::string Attr::Module = "module";
|
||||
const std::string Attr::ParentClass = "parentClass";
|
||||
const std::string Attr::Bindings = "bindings";
|
||||
|
||||
const std::string Attr::LLVM = "llvm";
|
||||
const std::string Attr::Python = "python";
|
||||
const std::string Attr::Atomic = "atomic";
|
||||
const std::string Attr::Property = "property";
|
||||
const std::string Attr::StaticMethod = "staticmethod";
|
||||
const std::string Attr::Attribute = "__attribute__";
|
||||
const std::string Attr::C = "C";
|
||||
|
||||
const std::string Attr::Internal = "__internal__";
|
||||
const std::string Attr::HiddenFromUser = "__hidden__";
|
||||
const std::string Attr::ForceRealize = "__force__";
|
||||
const std::string Attr::RealizeWithoutSelf =
|
||||
"std.internal.attributes.realize_without_self.0:0";
|
||||
|
||||
const std::string Attr::CVarArg = ".__vararg__";
|
||||
const std::string Attr::Method = ".__method__";
|
||||
const std::string Attr::Capture = ".__capture__";
|
||||
const std::string Attr::HasSelf = ".__hasself__";
|
||||
const std::string Attr::IsGenerator = ".__generator__";
|
||||
|
||||
const std::string Attr::Extend = "extend";
|
||||
const std::string Attr::Tuple = "tuple";
|
||||
const std::string Attr::ClassDeduce = "deduce";
|
||||
const std::string Attr::ClassNoTuple = "__notuple__";
|
||||
|
||||
const std::string Attr::Test = "std.internal.attributes.test.0:0";
|
||||
const std::string Attr::Overload = "overload:0";
|
||||
const std::string Attr::Export = "std.internal.attributes.export.0:0";
|
||||
|
||||
const std::string Attr::ClassMagic = "classMagic";
|
||||
const std::string Attr::ExprSequenceItem = "exprSequenceItem";
|
||||
const std::string Attr::ExprStarSequenceItem = "exprStarSequenceItem";
|
||||
const std::string Attr::ExprList = "exprList";
|
||||
const std::string Attr::ExprSet = "exprSet";
|
||||
const std::string Attr::ExprDict = "exprDict";
|
||||
const std::string Attr::ExprPartial = "exprPartial";
|
||||
const std::string Attr::ExprDominated = "exprDominated";
|
||||
const std::string Attr::ExprStarArgument = "exprStarArgument";
|
||||
const std::string Attr::ExprKwStarArgument = "exprKwStarArgument";
|
||||
const std::string Attr::ExprOrderedCall = "exprOrderedCall";
|
||||
const std::string Attr::ExprExternVar = "exprExternVar";
|
||||
const std::string Attr::ExprDominatedUndefCheck = "exprDominatedUndefCheck";
|
||||
const std::string Attr::ExprDominatedUsed = "exprDominatedUsed";
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
// Copyright (C) 2022-2024 Exaloop Inc. <https://exaloop.io>
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace codon::ast {
|
||||
|
||||
const int INDENT_SIZE = 2;
|
||||
|
||||
struct Attr {
|
||||
// Function attributes
|
||||
const static std::string Module;
|
||||
const static std::string ParentClass;
|
||||
const static std::string Bindings;
|
||||
// Toplevel attributes
|
||||
const static std::string LLVM;
|
||||
const static std::string Python;
|
||||
const static std::string Atomic;
|
||||
const static std::string Property;
|
||||
const static std::string StaticMethod;
|
||||
const static std::string Attribute;
|
||||
const static std::string C;
|
||||
// Internal attributes
|
||||
const static std::string Internal;
|
||||
const static std::string HiddenFromUser;
|
||||
const static std::string ForceRealize;
|
||||
const static std::string RealizeWithoutSelf; // not internal
|
||||
// Compiler-generated attributes
|
||||
const static std::string CVarArg;
|
||||
const static std::string Method;
|
||||
const static std::string Capture;
|
||||
const static std::string HasSelf;
|
||||
const static std::string IsGenerator;
|
||||
// Class attributes
|
||||
const static std::string Extend;
|
||||
const static std::string Tuple;
|
||||
const static std::string ClassDeduce;
|
||||
const static std::string ClassNoTuple;
|
||||
// Standard library attributes
|
||||
const static std::string Test;
|
||||
const static std::string Overload;
|
||||
const static std::string Export;
|
||||
// Expression-related attributes
|
||||
const static std::string ClassMagic;
|
||||
const static std::string ExprSequenceItem;
|
||||
const static std::string ExprStarSequenceItem;
|
||||
const static std::string ExprList;
|
||||
const static std::string ExprSet;
|
||||
const static std::string ExprDict;
|
||||
const static std::string ExprPartial;
|
||||
const static std::string ExprDominated;
|
||||
const static std::string ExprStarArgument;
|
||||
const static std::string ExprKwStarArgument;
|
||||
const static std::string ExprOrderedCall;
|
||||
const static std::string ExprExternVar;
|
||||
const static std::string ExprDominatedUndefCheck;
|
||||
const static std::string ExprDominatedUsed;
|
||||
};
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
// Copyright (C) 2022-2024 Exaloop Inc. <https://exaloop.io>
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
#include "codon/cir/base.h"
|
||||
|
||||
namespace codon::ast {
|
||||
|
||||
using ir::cast;
|
||||
|
||||
// Forward declarations
|
||||
struct Cache;
|
||||
struct ASTVisitor;
|
||||
|
||||
struct ASTNode : public ir::Node {
|
||||
static const char NodeId;
|
||||
using ir::Node::Node;
|
||||
|
||||
/// See LLVM documentation.
|
||||
static const void *nodeId() { return &NodeId; }
|
||||
const void *dynamicNodeId() const override { return &NodeId; }
|
||||
/// See LLVM documentation.
|
||||
virtual bool isConvertible(const void *other) const override {
|
||||
return other == nodeId() || ir::Node::isConvertible(other);
|
||||
}
|
||||
|
||||
Cache *cache;
|
||||
|
||||
ASTNode() = default;
|
||||
ASTNode(const ASTNode &);
|
||||
virtual ~ASTNode() = default;
|
||||
|
||||
/// Convert a node to an S-expression.
|
||||
virtual std::string toString(int) const = 0;
|
||||
virtual std::string toString() const { return toString(-1); }
|
||||
|
||||
/// Deep copy a node.
|
||||
virtual ASTNode *clone(bool clean) const = 0;
|
||||
ASTNode *clone() const { return clone(false); }
|
||||
|
||||
/// Accept an AST visitor.
|
||||
virtual void accept(ASTVisitor &visitor) = 0;
|
||||
|
||||
/// Allow pretty-printing to C++ streams.
|
||||
friend std::ostream &operator<<(std::ostream &out, const ASTNode &expr) {
|
||||
return out << expr.toString();
|
||||
}
|
||||
|
||||
void setAttribute(const std::string &key, std::unique_ptr<ir::Attribute> value) {
|
||||
attributes[key] = std::move(value);
|
||||
}
|
||||
void setAttribute(const std::string &key, const std::string &value) {
|
||||
attributes[key] = std::make_unique<ir::StringValueAttribute>(value);
|
||||
}
|
||||
void setAttribute(const std::string &key) {
|
||||
attributes[key] = std::make_unique<ir::Attribute>();
|
||||
}
|
||||
|
||||
inline decltype(auto) members() {
|
||||
int a = 0;
|
||||
return std::tie(a);
|
||||
}
|
||||
};
|
||||
|
||||
template <class... TA> void E(error::Error e, ASTNode *o, const TA &...args) {
|
||||
E(e, o->getSrcInfo(), args...);
|
||||
}
|
||||
template <class... TA> void E(error::Error e, const ASTNode &o, const TA &...args) {
|
||||
E(e, o.getSrcInfo(), args...);
|
||||
}
|
||||
|
||||
template <typename Derived, typename Parent> class AcceptorExtend : public Parent {
|
||||
public:
|
||||
using Parent::Parent;
|
||||
|
||||
/// See LLVM documentation.
|
||||
static const void *nodeId() { return &Derived::NodeId; }
|
||||
const void *dynamicNodeId() const override { return &Derived::NodeId; }
|
||||
/// See LLVM documentation.
|
||||
virtual bool isConvertible(const void *other) const override {
|
||||
return other == nodeId() || Parent::isConvertible(other);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T> struct Items {
|
||||
Items(std::vector<T> items) : items(std::move(items)) {}
|
||||
const T &operator[](int i) const { return items[i]; }
|
||||
T &operator[](int i) { return items[i]; }
|
||||
auto begin() { return items.begin(); }
|
||||
auto end() { return items.end(); }
|
||||
auto begin() const { return items.begin(); }
|
||||
auto end() const { return items.end(); }
|
||||
auto size() const { return items.size(); }
|
||||
bool empty() const { return items.empty(); }
|
||||
const T &front() const { return items.front(); }
|
||||
const T &back() const { return items.back(); }
|
||||
T &front() { return items.front(); }
|
||||
T &back() { return items.back(); }
|
||||
|
||||
protected:
|
||||
std::vector<T> items;
|
||||
};
|
||||
|
||||
} // namespace codon::ast
|
||||
|
||||
template <typename T>
|
||||
struct fmt::formatter<
|
||||
T, std::enable_if_t<std::is_base_of<codon::ast::ASTNode, T>::value, char>>
|
||||
: fmt::ostream_formatter {};
|
Loading…
Reference in New Issue