@@ -74,13 +74,62 @@ static void printSingleBlockRegion(OpAsmPrinter &p, Operation *op,
7474 if (!region.getBlocks ().front ().empty ())
7575 p.printRegion (region);
7676}
77+ static llvm::LogicalResult isValidName (llvm::StringRef in, mlir::Operation *loc,
78+ const Twine &label) {
79+ if (in.empty ())
80+ return loc->emitError (" name of " ) << label << " is empty" ;
81+
82+ bool allowUnderscore = false ;
83+ for (auto &elem : in) {
84+ if (elem == ' _' ) {
85+ if (!allowUnderscore)
86+ return loc->emitError (" name of " )
87+ << label << " should not contain leading or double underscores" ;
88+ } else {
89+ if (!isalnum (elem))
90+ return loc->emitError (" name of " )
91+ << label
92+ << " must contain only lowercase letters, digits and "
93+ " underscores" ;
94+
95+ if (llvm::isUpper (elem))
96+ return loc->emitError (" name of " )
97+ << label << " should not contain uppercase letters" ;
98+ }
99+
100+ allowUnderscore = elem != ' _' ;
101+ }
102+
103+ return success ();
104+ }
77105
78106LogicalResult DialectOp::verify () {
79107 if (!Dialect::isValidNamespace (getName ()))
80108 return emitOpError (" invalid dialect name" );
109+ if (failed (isValidName (getSymName (), getOperation (), " dialect" )))
110+ return failure ();
111+
81112 return success ();
82113}
83114
115+ LogicalResult OperationOp::verify () {
116+ return isValidName (getSymName (), getOperation (), " operation" );
117+ }
118+
119+ LogicalResult TypeOp::verify () {
120+ auto symName = getSymName ();
121+ if (symName.front () == ' !' )
122+ symName = symName.substr (1 );
123+ return isValidName (symName, getOperation (), " type" );
124+ }
125+
126+ LogicalResult AttributeOp::verify () {
127+ auto symName = getSymName ();
128+ if (symName.front () == ' #' )
129+ symName = symName.substr (1 );
130+ return isValidName (symName, getOperation (), " attribute" );
131+ }
132+
84133LogicalResult OperationOp::verifyRegions () {
85134 // Stores pairs of value kinds and the list of names of values of this kind in
86135 // the operation.
@@ -133,18 +182,10 @@ static LogicalResult verifyNames(Operation *op, StringRef kindName,
133182 DenseMap<StringRef, size_t > nameMap;
134183 for (auto [i, name] : llvm::enumerate (names)) {
135184 StringRef nameRef = llvm::cast<StringAttr>(name).getValue ();
136- if (nameRef.empty ())
137- return op->emitOpError ()
138- << " name of " << kindName << " #" << i << " is empty" ;
139- if (!llvm::isAlpha (nameRef[0 ]) && nameRef[0 ] != ' _' )
140- return op->emitOpError ()
141- << " name of " << kindName << " #" << i
142- << " must start with either a letter or an underscore" ;
143- if (llvm::any_of (nameRef,
144- [](char c) { return !llvm::isAlnum (c) && c != ' _' ; }))
145- return op->emitOpError ()
146- << " name of " << kindName << " #" << i
147- << " must contain only letters, digits and underscores" ;
185+
186+ if (failed (isValidName (nameRef, op, Twine (kindName) + " #" + Twine (i))))
187+ return failure ();
188+
148189 if (nameMap.contains (nameRef))
149190 return op->emitOpError () << " name of " << kindName << " #" << i
150191 << " is a duplicate of the name of " << kindName
0 commit comments