[cfe-dev] Rewriting question about renaming type

Vincent R. forumer at smartmobili.com
Wed Jul 31 15:05:16 PDT 2013


Hi,

When working with rewriting I would like to transform the variable 
declaration and convert them into .net types :

let's consider the following declaration:

@interface GSNamedColor : NSColor
{
   NSString* _catalog_name;
   NSString *_color_name;
   NSString *_cached_name_space;
   NSColor *_cached_color;
   BOOL _aBoolVar;
   NSInteger _anIntVar;
}

I am transforming it into this for now :

public class GSNamedColor : NSColor
{
    protected NSString *_catalog_name;
    protected NSString *_color_name;
    protected NSString *_cached_name_space;
    protected NSColor *_cached_color;
    protected BOOL _aBoolVar;
    protected NSInteger _anIntVar;
}

But of course what I would like to obtain is

public class GSNamedColor : NSColor
{
    protected NSString _catalog_name;
    protected NSString _color_name;
    protected NSString _cached_name_space;
    protected NSColor _cached_color;
    protected bool _aBoolVar;
    protected int _anIntVar;
}

It means that I have to remove * when it's an object and replace some 
type by corresponding ones

The code used for this transformation is :

void RewriteModernObjC::RewriteObjCFieldDecl(FieldDecl *fieldDecl,
                                              std::string &Result) {
   QualType Type = fieldDecl->getType();
   std::string Name = fieldDecl->getNameAsString();

   Result += "protected ";
   bool EleboratedType = RewriteObjCFieldDeclType(Type, Result);
   if (!EleboratedType)
     Type.getAsStringInternal(Name, Context->getPrintingPolicy());

   Result += Name;
  ...
}


so now my problem is to know what is the best option to modify the way 
variable are written, I have a simple option
that consists in parsing the result of Type.getAsStringInternal and I 
have already done something like that
when there is a call to getAsString

std::string 
RewriteModernObjC::ConvertObjcStringTypeIntoCsharp(std::string ObjcType)
{
	std::string CSType = ObjcType;

     //convert objc types into .net ones
     if (ObjcType == "BOOL")
		CSType = "bool";
     else if (ObjcType == "NSInteger")
		CSType = "int";
	else if (ObjcType == "NSUInteger")
		CSType = "unsigned int";
	else if (ObjcType == "CGFloat")
		CSType = "double";
     else if (*ObjcType.rbegin() == '*')
		CSType = ObjcType.substr(0, ObjcType.size()-1);

	return CSType;
}

but in the case of getAsStringInternal the method doesn't work because 
it returns the type followed by the name so it would mean I had to write 
another
method that almost do the same but that use a startWith algorithm 
instead of ==, and that split in function of space to check if there is 
a * just after
the type and remove it, ...

SO I suppose another option would be to modify the getAsString and 
getAsStringInternal but I don't know exactly how to do it.
There seem to be a printing policy but that's all I know for the 
moment.


My question is : what is the best approach and how would you do it ?











More information about the cfe-dev mailing list