@@ -1804,23 +1804,36 @@ void OpEmitter::genAttrGetters() {
1804
1804
}
1805
1805
1806
1806
void OpEmitter::genAttrSetters () {
1807
+ bool useProperties = op.getDialect ().usePropertiesForAttributes ();
1808
+
1809
+ // Generate the code to set an attribute.
1810
+ auto emitSetAttr = [&](Method *method, StringRef getterName,
1811
+ StringRef attrName, StringRef attrVar) {
1812
+ if (useProperties) {
1813
+ method->body () << formatv (" getProperties().{0} = {1};" , attrName,
1814
+ attrVar);
1815
+ } else {
1816
+ method->body () << formatv (" (*this)->setAttr({0}AttrName(), {1});" ,
1817
+ getterName, attrVar);
1818
+ }
1819
+ };
1820
+
1807
1821
// Generate raw named setter type. This is a wrapper class that allows setting
1808
1822
// to the attributes via setters instead of having to use the string interface
1809
1823
// for better compile time verification.
1810
1824
auto emitAttrWithStorageType = [&](StringRef setterName, StringRef getterName,
1811
- Attribute attr) {
1825
+ StringRef attrName, Attribute attr) {
1812
1826
auto *method =
1813
1827
opClass.addMethod (" void" , setterName + " Attr" ,
1814
1828
MethodParameter (attr.getStorageType (), " attr" ));
1815
1829
if (method)
1816
- method->body () << formatv (" (*this)->setAttr({0}AttrName(), attr);" ,
1817
- getterName);
1830
+ emitSetAttr (method, getterName, attrName, " attr" );
1818
1831
};
1819
1832
1820
1833
// Generate a setter that accepts the underlying C++ type as opposed to the
1821
1834
// attribute type.
1822
1835
auto emitAttrWithReturnType = [&](StringRef setterName, StringRef getterName,
1823
- Attribute attr) {
1836
+ StringRef attrName, Attribute attr) {
1824
1837
Attribute baseAttr = attr.getBaseAttr ();
1825
1838
if (!canUseUnwrappedRawValue (baseAttr))
1826
1839
return ;
@@ -1849,9 +1862,8 @@ void OpEmitter::genAttrSetters() {
1849
1862
1850
1863
// If the value isn't optional, just set it directly.
1851
1864
if (!isOptional) {
1852
- method->body () << formatv (
1853
- " (*this)->setAttr({0}AttrName(), {1});" , getterName,
1854
- constBuildAttrFromParam (attr, fctx, " attrValue" ));
1865
+ emitSetAttr (method, getterName, attrName,
1866
+ constBuildAttrFromParam (attr, fctx, " attrValue" ));
1855
1867
return ;
1856
1868
}
1857
1869
@@ -1862,22 +1874,36 @@ void OpEmitter::genAttrSetters() {
1862
1874
// optional but not in the same way as the others (i.e. it uses bool over
1863
1875
// std::optional<>).
1864
1876
StringRef paramStr = isUnitAttr ? " attrValue" : " *attrValue" ;
1865
- const char *optionalCodeBody = R"(
1877
+ if (!useProperties) {
1878
+ const char *optionalCodeBody = R"(
1866
1879
if (attrValue)
1867
1880
return (*this)->setAttr({0}AttrName(), {1});
1868
1881
(*this)->removeAttr({0}AttrName());)" ;
1869
- method->body () << formatv (
1870
- optionalCodeBody, getterName,
1871
- constBuildAttrFromParam (baseAttr, fctx, paramStr));
1882
+ method->body () << formatv (
1883
+ optionalCodeBody, getterName,
1884
+ constBuildAttrFromParam (baseAttr, fctx, paramStr));
1885
+ } else {
1886
+ const char *optionalCodeBody = R"(
1887
+ auto &odsProp = getProperties().{0};
1888
+ if (attrValue)
1889
+ odsProp = {1};
1890
+ else
1891
+ odsProp = nullptr;)" ;
1892
+ method->body () << formatv (
1893
+ optionalCodeBody, attrName,
1894
+ constBuildAttrFromParam (baseAttr, fctx, paramStr));
1895
+ }
1872
1896
};
1873
1897
1874
1898
for (const NamedAttribute &namedAttr : op.getAttributes ()) {
1875
1899
if (namedAttr.attr .isDerivedAttr ())
1876
1900
continue ;
1877
1901
std::string setterName = op.getSetterName (namedAttr.name );
1878
1902
std::string getterName = op.getGetterName (namedAttr.name );
1879
- emitAttrWithStorageType (setterName, getterName, namedAttr.attr );
1880
- emitAttrWithReturnType (setterName, getterName, namedAttr.attr );
1903
+ emitAttrWithStorageType (setterName, getterName, namedAttr.name ,
1904
+ namedAttr.attr );
1905
+ emitAttrWithReturnType (setterName, getterName, namedAttr.name ,
1906
+ namedAttr.attr );
1881
1907
}
1882
1908
}
1883
1909
0 commit comments