[lld] [LLD] Improve linker script handing in LLD (PR #106334)

Hongyu Chen via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 29 22:38:55 PDT 2024


================
@@ -74,6 +75,106 @@ struct ExprValue {
 // Later, we evaluate the expression by calling the function.
 using Expr = std::function<ExprValue()>;
 
+class ScriptExpr {
+public:
+  enum class ExprKind : uint8_t { Constant, Dynamic, Unary, Binary };
+
+private:
+  ExprKind kind_;
+
+protected:
+  explicit ScriptExpr(ExprKind kind) : kind_(kind) {}
+
+public:
+  ExprKind getKind() const { return kind_; }
+  std::function<ExprValue()> getExpr() const;
+  ExprValue getExprValue() const;
+};
+
+class ConstantExpr : public ScriptExpr {
+public:
+  ConstantExpr(uint64_t val) : ScriptExpr(ExprKind::Constant), val_(val) {}
+  ExprValue getExprValue() const { return ExprValue(val_); }
+
+private:
+  uint64_t val_;
+};
+
+class DynamicExpr : public ScriptExpr {
+public:
+  DynamicExpr(std::function<ExprValue()> impl)
+      : ScriptExpr(ExprKind::Dynamic), impl_(impl) {}
+  ExprValue getExprValue() const { return impl_(); }
+
+private:
+  std::function<ExprValue()> impl_;
+};
+
+class UnaryExpr : public ScriptExpr {
+public:
+  UnaryExpr(StringRef op, ScriptExpr *operand)
+      : ScriptExpr(ExprKind::Unary), op_(op), operand_(operand) {}
+  ExprValue getExprValue() const;
+
+private:
+  StringRef op_;
----------------
yugier wrote:

yes it would but I was also thinking if I should keep the same coding pattern like `BinaryExpr` to create `enum class opcode` for `UnaryExpr`. 

https://github.com/llvm/llvm-project/pull/106334


More information about the llvm-commits mailing list