11use crate :: syntax:: symbol:: Segment ;
22use crate :: syntax:: { Lifetimes , NamedType , Pair , Symbol } ;
33use proc_macro2:: { Ident , Span } ;
4+ use std:: collections:: HashSet ;
45use std:: fmt:: { self , Display } ;
56use std:: iter;
7+ use std:: sync:: LazyLock ;
68use syn:: ext:: IdentExt ;
79use syn:: parse:: { Error , Parser , Result } ;
810use syn:: punctuated:: Punctuated ;
@@ -37,8 +39,13 @@ impl NamedType {
3739
3840impl ForeignName {
3941 pub ( crate ) fn parse ( text : & str , span : Span ) -> Result < Self > {
40- // TODO: support C++ names containing whitespace (`unsigned int`) or
41- // non-alphanumeric characters (`operator++`).
42+ if ForeignName :: is_valid_operator_name ( text) {
43+ return Ok ( ForeignName {
44+ text : text. to_string ( ) ,
45+ span,
46+ } ) ;
47+ }
48+
4249 match Ident :: parse_any. parse_str ( text) {
4350 Ok ( ident) => {
4451 let text = ident. to_string ( ) ;
@@ -55,6 +62,26 @@ impl ForeignName {
5562 pub ( crate ) fn span ( & self ) -> Span {
5663 self . span
5764 }
65+
66+ pub ( crate ) fn is_valid_operator_name ( name : & str ) -> bool {
67+ #[ rustfmt:: skip]
68+ static CPP_OPERATORS : LazyLock < HashSet < & ' static str > > = LazyLock :: new ( || {
69+ // Based on `llvm/llvm-project/clang/include/clang/Basic/OperatorKinds.def`.
70+ // Excluding `?` because it is not overridable.
71+ //
72+ // TODO: Consider also allowing `operator <type>`
73+ // (see https://en.cppreference.com/w/cpp/language/cast_operator.html).
74+ [
75+ " new" , " delete" , " new[]" , " delete[]" , " co_await" ,
76+ "+" , "-" , "*" , "/" , "%" , "^" , "&" , "|" , "~" , "!" , "=" , "<" , ">" ,
77+ "+=" , "-=" , "*=" , "/=" , "%=" , "^=" , "&=" , "|=" ,
78+ "<<" , ">>" , "<<=" , ">>=" , "==" , "!=" , "<=" , ">=" , "<=>" ,
79+ "&&" , "||" , "++" , "--" , "," , "->*" , "->" , "()" , "[]" ,
80+ ] . into_iter ( ) . collect ( )
81+ } ) ;
82+ name. strip_prefix ( "operator" )
83+ . is_some_and ( |suffix| CPP_OPERATORS . contains ( suffix) )
84+ }
5885}
5986
6087impl Display for ForeignName {
0 commit comments