@@ -129,6 +129,7 @@ class ConstantTokenNone;
129129class GlobalValue ;
130130class GlobalObject ;
131131class GlobalIFunc ;
132+ class GlobalVariable ;
132133class Context ;
133134class Function ;
134135class Instruction ;
@@ -334,6 +335,7 @@ class Value {
334335 friend class DSOLocalEquivalent ; // For `Val`.
335336 friend class GlobalObject ; // For `Val`.
336337 friend class GlobalIFunc ; // For `Val`.
338+ friend class GlobalVariable ; // For `Val`.
337339
338340 // / All values point to the context.
339341 Context &Ctx;
@@ -1370,6 +1372,162 @@ class GlobalIFunc final
13701372#endif
13711373};
13721374
1375+ class GlobalVariable final
1376+ : public GlobalWithNodeAPI<GlobalVariable, llvm::GlobalVariable,
1377+ GlobalObject, llvm::GlobalObject> {
1378+ GlobalVariable (llvm::GlobalObject *C, Context &Ctx)
1379+ : GlobalWithNodeAPI(ClassID::GlobalVariable, C, Ctx) {}
1380+ friend class Context ; // For constructor.
1381+
1382+ // / Helper for mapped_iterator.
1383+ struct LLVMGVToGV {
1384+ Context &Ctx;
1385+ LLVMGVToGV (Context &Ctx) : Ctx(Ctx) {}
1386+ GlobalVariable &operator ()(llvm::GlobalVariable &LLVMGV) const ;
1387+ };
1388+
1389+ public:
1390+ // / For isa/dyn_cast.
1391+ static bool classof (const sandboxir::Value *From) {
1392+ return From->getSubclassID () == ClassID::GlobalVariable;
1393+ }
1394+
1395+ // / Definitions have initializers, declarations don't.
1396+ // /
1397+ inline bool hasInitializer () const {
1398+ return cast<llvm::GlobalVariable>(Val)->hasInitializer ();
1399+ }
1400+
1401+ // / hasDefinitiveInitializer - Whether the global variable has an initializer,
1402+ // / and any other instances of the global (this can happen due to weak
1403+ // / linkage) are guaranteed to have the same initializer.
1404+ // /
1405+ // / Note that if you want to transform a global, you must use
1406+ // / hasUniqueInitializer() instead, because of the *_odr linkage type.
1407+ // /
1408+ // / Example:
1409+ // /
1410+ // / @a = global SomeType* null - Initializer is both definitive and unique.
1411+ // /
1412+ // / @b = global weak SomeType* null - Initializer is neither definitive nor
1413+ // / unique.
1414+ // /
1415+ // / @c = global weak_odr SomeType* null - Initializer is definitive, but not
1416+ // / unique.
1417+ inline bool hasDefinitiveInitializer () const {
1418+ return cast<llvm::GlobalVariable>(Val)->hasDefinitiveInitializer ();
1419+ }
1420+
1421+ // / hasUniqueInitializer - Whether the global variable has an initializer, and
1422+ // / any changes made to the initializer will turn up in the final executable.
1423+ inline bool hasUniqueInitializer () const {
1424+ return cast<llvm::GlobalVariable>(Val)->hasUniqueInitializer ();
1425+ }
1426+
1427+ // / getInitializer - Return the initializer for this global variable. It is
1428+ // / illegal to call this method if the global is external, because we cannot
1429+ // / tell what the value is initialized to!
1430+ // /
1431+ Constant *getInitializer () const ;
1432+ // / setInitializer - Sets the initializer for this global variable, removing
1433+ // / any existing initializer if InitVal==NULL. The initializer must have the
1434+ // / type getValueType().
1435+ void setInitializer (Constant *InitVal);
1436+
1437+ // TODO: Add missing replaceInitializer(). Requires special tracker
1438+
1439+ // / If the value is a global constant, its value is immutable throughout the
1440+ // / runtime execution of the program. Assigning a value into the constant
1441+ // / leads to undefined behavior.
1442+ // /
1443+ bool isConstant () const {
1444+ return cast<llvm::GlobalVariable>(Val)->isConstant ();
1445+ }
1446+ void setConstant (bool V);
1447+
1448+ bool isExternallyInitialized () const {
1449+ return cast<llvm::GlobalVariable>(Val)->isExternallyInitialized ();
1450+ }
1451+ void setExternallyInitialized (bool Val);
1452+
1453+ // TODO: Missing copyAttributesFrom()
1454+
1455+ // TODO: Missing removeFromParent(), eraseFromParent(), dropAllReferences()
1456+
1457+ // TODO: Missing addDebugInfo(), getDebugInfo()
1458+
1459+ // TODO: Missing attribute setter functions: addAttribute(), setAttributes().
1460+ // There seems to be no removeAttribute() so we can't undo them.
1461+
1462+ // / Return true if the attribute exists.
1463+ bool hasAttribute (Attribute::AttrKind Kind) const {
1464+ return cast<llvm::GlobalVariable>(Val)->hasAttribute (Kind);
1465+ }
1466+
1467+ // / Return true if the attribute exists.
1468+ bool hasAttribute (StringRef Kind) const {
1469+ return cast<llvm::GlobalVariable>(Val)->hasAttribute (Kind);
1470+ }
1471+
1472+ // / Return true if any attributes exist.
1473+ bool hasAttributes () const {
1474+ return cast<llvm::GlobalVariable>(Val)->hasAttributes ();
1475+ }
1476+
1477+ // / Return the attribute object.
1478+ Attribute getAttribute (Attribute::AttrKind Kind) const {
1479+ return cast<llvm::GlobalVariable>(Val)->getAttribute (Kind);
1480+ }
1481+
1482+ // / Return the attribute object.
1483+ Attribute getAttribute (StringRef Kind) const {
1484+ return cast<llvm::GlobalVariable>(Val)->getAttribute (Kind);
1485+ }
1486+
1487+ // / Return the attribute set for this global
1488+ AttributeSet getAttributes () const {
1489+ return cast<llvm::GlobalVariable>(Val)->getAttributes ();
1490+ }
1491+
1492+ // / Return attribute set as list with index.
1493+ // / FIXME: This may not be required once ValueEnumerators
1494+ // / in bitcode-writer can enumerate attribute-set.
1495+ AttributeList getAttributesAsList (unsigned Index) const {
1496+ return cast<llvm::GlobalVariable>(Val)->getAttributesAsList (Index);
1497+ }
1498+
1499+ // / Check if section name is present
1500+ bool hasImplicitSection () const {
1501+ return cast<llvm::GlobalVariable>(Val)->hasImplicitSection ();
1502+ }
1503+
1504+ // / Get the custom code model raw value of this global.
1505+ // /
1506+ unsigned getCodeModelRaw () const {
1507+ return cast<llvm::GlobalVariable>(Val)->getCodeModelRaw ();
1508+ }
1509+
1510+ // / Get the custom code model of this global if it has one.
1511+ // /
1512+ // / If this global does not have a custom code model, the empty instance
1513+ // / will be returned.
1514+ std::optional<CodeModel::Model> getCodeModel () const {
1515+ return cast<llvm::GlobalVariable>(Val)->getCodeModel ();
1516+ }
1517+
1518+ // TODO: Missing setCodeModel(). Requires custom tracker.
1519+
1520+ #ifndef NDEBUG
1521+ void verify () const override {
1522+ assert (isa<llvm::GlobalVariable>(Val) && " Expected a GlobalVariable!" );
1523+ }
1524+ void dumpOS (raw_ostream &OS) const override {
1525+ dumpCommonPrefix (OS);
1526+ dumpCommonSuffix (OS);
1527+ }
1528+ #endif
1529+ };
1530+
13731531class BlockAddress final : public Constant {
13741532 BlockAddress (llvm::BlockAddress *C, Context &Ctx)
13751533 : Constant(ClassID::BlockAddress, C, Ctx) {}
0 commit comments