genwrappers.cc 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
linphone
Copyright (C) 2013 Belledonne Communications SARL
Simon Morlat (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 20
*/

21 22 23
#include "software-desc.hh"
#include "generator.hh"

24 25 26 27 28
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <string.h>
#include <cstdio>
#include <iostream>
29
#include <sstream>
30

31

32 33 34 35 36 37 38 39

static bool isSpace(const char *str){
	for(;*str!='\0';++str){
		if (!isspace(*str)) return false;
	}
	return true;
}

40
//Convenient class for examining node recursively
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
class XmlNode{
public:
	XmlNode(const xmlNode *node=NULL) : mNode(node){
	}
	XmlNode getChild(const string &name)const{
		if (mNode==NULL) return XmlNode();
		xmlNode *it;
		for(it=mNode->children;it!=NULL;it=it->next){
			if (xmlStrcmp(it->name,(const xmlChar*)name.c_str())==0)
				return XmlNode(it);
		}
		return XmlNode();
	}
	XmlNode getChildRecursive(const string &name)const{
		if (mNode==NULL) return XmlNode();
		xmlNode *it;
		//find in direct children
		for(it=mNode->children;it!=NULL;it=it->next){
			if (xmlStrcmp(it->name,(const xmlChar*)name.c_str())==0)
				return XmlNode(it);
		}
		//recurse into children
		for(it=mNode->children;it!=NULL;it=it->next){
			XmlNode res=XmlNode(it).getChildRecursive(name);
			if (!res.isNull()) return res;
		}
		return XmlNode();
68
	}
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	list<XmlNode> getChildren(const string &name)const{
		xmlNode *it;
		list<XmlNode> nodes;
		
		if (mNode==NULL) return nodes;
		for(it=mNode->children;it!=NULL;it=it->next){
			if (xmlStrcmp(it->name,(const xmlChar*)name.c_str())==0)
				nodes.push_back(XmlNode(it));
		}
		if (nodes.empty()) cerr<<"getChildren() no "<<name<<" found"<<endl;
		return nodes;
	}
	string getText()const{
		if (mNode==NULL) return "";
		XmlNode node=getChild("text");
		if (!node.isNull()) {
			const char *text=(const char*)node.mNode->content;
			if (!isSpace(text)) return string(text);
		}
		return "";
	}
	string getProp(const string &propname)const{
		if (mNode==NULL) return "";
		xmlChar *value;
		value=xmlGetProp((xmlNode*)mNode,(const xmlChar*)propname.c_str());
		if (value) return string((const char*)value);
		return "";
	}
	bool isNull()const{
		return mNode==NULL;
	}
private:
	const xmlNode *mNode;
};
103

104 105
static Argument *parseArgument(XmlNode node, bool isReturn){
	string name=node.getChild("declname").getText();
106
	Type *type=NULL;
107
	string typecontent=node.getChild("type").getText();
108 109
	bool isConst=false;
	bool isPointer=false;
110
	
111
	//find documented type if any
112
	string tname=node.getChild("type").getChild("ref").getText();
113 114 115 116 117 118 119 120 121 122 123
	if (!tname.empty()){
		type=Type::getType(tname);
	}else type=Type::getType(typecontent);
	
	//find const attribute if any
	if (typecontent.find("const")!=string::npos)
		isConst=true;
	
	if (typecontent.find("*")!=string::npos)
		isPointer=true;
	
124 125 126
	if (type==NULL) {
		return NULL;
	}
127
	//cout<<"Parsed argument "<<name<<" with type "<<type->getBasicType()<<" "<<type->getName()<<endl;
128
	return new Argument(type,!isReturn ? name : "",isConst,isPointer);
129 130 131
}

static string classNameToPrefix(const std::string &classname){
132
	char *tmp = new char[classname.size()*2];
133
	char *w=tmp;
134
	size_t i;
135 136 137 138 139 140 141 142 143 144 145
	
	for(i=0;i<classname.size();i++){
		char p=classname[i];
		if (isupper(p)){
			if (i!=0){
				*w++='_';
			}
			*w++=tolower(p);
		}else *w++=p;
	}
	*w++='\0';
146 147 148
	string ret(tmp);
	delete[] tmp;
	return ret;
149 150 151
}

static string makeMethodName(const string & suffix){
152
	char *tmp = new char[suffix.size()];
153
	char *w=tmp;
154
	size_t i;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	bool useUpper=false;
	
	for(i=0;i<suffix.size();i++){
		char p=suffix[i];
		
		if (p=='_'){
			if (i>0)
				useUpper=true;
		}else{
			if (useUpper)
				*w++=toupper(p);
			else 
				*w++=p;
			useUpper=false;
		}
	}
	*w++='\0';
172 173 174
	string ret(tmp);
	delete[] tmp;
	return ret;
175 176 177 178 179 180 181 182 183 184
}

static string extractMethodName(const string &c_name, const std::string& class_name){
	string prefix=classNameToPrefix(class_name);
	if (c_name.find(prefix)==0){
		return makeMethodName(c_name.substr(prefix.size(),string::npos));
	}
	return "";
}

185 186 187 188 189 190 191 192 193 194 195
static string getHelpBody(XmlNode myNode){
	ostringstream result;
	XmlNode brief=myNode.getChild("briefdescription");
	XmlNode detailed=myNode.getChild("detaileddescription");
	
	result<<brief.getText();
	result<<detailed.getChild("para").getText();
	//cout<<"getHelpBody():"<<result.str();
	return result.str();
}

196 197 198 199 200
static void parseFunction(Project *proj, xmlNode *node){
	string name;
	Argument *first_arg=NULL;
	string className;
	string methodName;
201 202 203
	Argument *retarg=NULL;
	list<Argument*> args;
	string help;
204 205 206 207 208 209 210 211 212 213 214
	XmlNode funcnode(node);
	XmlNode parameterlist;
	list<XmlNode> params;
	list<XmlNode> paramsHelp;
	list<XmlNode>::iterator it,helpit;
	
	name=funcnode.getChild("name").getText();
	params=funcnode.getChildren("param");
	parameterlist=funcnode.getChild("detaileddescription").getChildRecursive("parameterlist");
	if (parameterlist.isNull()) cerr<<"parameterlist not found"<<endl;
	paramsHelp=parameterlist.getChildren("parameteritem");
215
	
216 217 218 219 220 221 222 223 224 225 226 227
	for (it=params.begin(),helpit=paramsHelp.begin();it!=params.end();++it){
		Argument *a=parseArgument(*it,false);
		if (a){
			//add argument help
			if (!args.empty()){
				if (helpit!=paramsHelp.end()){
					XmlNode item=*helpit;
					a->setHelp(item.getChild("parameterdescription").getChild("para").getText());
				}else cerr<<"Undocumented parameter "<<a->getName()<<" in function "<<name<<endl;
			}
			args.push_back(a);
			if (helpit!=paramsHelp.end()) ++helpit;
228
		}
229
		else return;
230
	}
231 232 233
	help=getHelpBody(funcnode);

	retarg=parseArgument(funcnode,true);
234 235 236 237 238 239
	if (!retarg){
		cerr<<"Could not parse return argument of function "<<name<<endl;
		return;
	}
	
	if (!args.empty()) first_arg=args.front();
240
	if (!first_arg){
241
		cerr<<"Could not determine first argument of "<<name<<endl;
242 243
		return;
	}
244
	if (first_arg->getType()->getBasicType()!=Type::Class) return;
245 246
	className=first_arg->getType()->getName();
	methodName=extractMethodName(name,className);
247
	if (!methodName.empty() && methodName!="destroy"){
248
		//cout<<"Found "<<className<<"."<<methodName<<"()"<<endl;
249 250 251 252 253 254 255 256
		args.pop_front();
		Method *method=new Method("",retarg,methodName,args,first_arg->isConst(),false);
		method->setHelp(help);
		proj->getClass(className)->addMethod(method);
		delete first_arg;
	}
}

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
static string findCommon(const string &c1, const string & c2){
	size_t i;
	ostringstream res;
	for(i=0;i<c1.size() && i<c2.size();++i){
		if (tolower(c1[i])==tolower(c2[i]))
			res<<(char)c1[i];
		else break;
	}
	return res.str();
}

static string extractCallbackName(const string &c_name, const string & classname){
	string prefix=findCommon(c_name,classname);
	string res=c_name.substr(prefix.size(),string::npos);
	res[0]=tolower(res[0]);
	size_t pos=res.find("Cb");
	if (pos!=string::npos) res=res.substr(0,pos);
	return res;
}


static void parseCallback(Project *proj, XmlNode node){
	string argsstring=node.getChild("argsstring").getText();
	string name=node.getChild("name").getText();
	list<XmlNode> params=node.getChildRecursive("parameterlist").getChildren("parameteritem");
	list<XmlNode>::iterator it=params.begin();
	string rettype=node.getChild("type").getText();
	argsstring=argsstring.substr(argsstring.find('(')+1,string::npos);
	bool cont=true;
	list<Argument*> args;
	Type *firstArgType=NULL;
	
	rettype=rettype.substr(0,rettype.find('('));
	Argument *retarg=new Argument(Type::getType(rettype),"",false,rettype.find('*')!=string::npos);
	
	do{
		size_t comma=argsstring.find(',');
		size_t end=argsstring.find(')');
		if (comma!=string::npos && comma<end) end=comma;
		else cont=false;
		string arg=argsstring.substr(0,end);
		bool isConst=false;
		bool isPointer=false;
		
		size_t endtype=arg.find('*');
		if (endtype==string::npos) endtype=arg.rfind(' ');
		else isPointer=true;
		string typestring=arg.substr(0,endtype+1);
		Type *type=Type::getType(typestring);
		
		if (type==NULL) return;
		
		if (firstArgType==NULL) firstArgType=type;
		
		//find const attribute if any
		if (typestring.find("const")!=string::npos)
			isConst=true;
		
		string argname=arg.substr(endtype+1,end);
		argsstring=argsstring.substr(end+1,string::npos);
		Argument *argobj=new Argument(type,makeMethodName(argname),isConst,isPointer);
		if (it!=params.end()){
			argobj->setHelp((*it).getChild("parameterdescription").getChild("para").getText());
			++it;
		}
		args.push_back(argobj);
	}while(cont);
	
	if (firstArgType->getBasicType()!=Type::Class) return;
	Class *klass=proj->getClass(firstArgType->getName());
	Method *callback=new Method("", retarg, extractCallbackName(name,klass->getName()), args, false, false, true);
	//cout<<"Found callback "<<callback->getName()<<" with "<<args.size()<<" arguments."<<endl;
	callback->setHelp(node.getChild("detaileddescription").getChild("para").getText());
	klass->addMethod(callback);
	
	
}

Simon Morlat's avatar
Simon Morlat committed
335 336 337 338 339 340 341
static void parseEnum(Project *proj, XmlNode node){
	string name=node.getChild("name").getText();
	if (name[0]=='_') name.erase(0,1);
	Class *klass=proj->getClass(name);
	klass->setHelp(node.getChild("detaileddescription").getChild("para").getText());
	list<XmlNode> enumValues=node.getChildren("enumvalue");
	list<XmlNode>::iterator it;
342
	int value = 0;
Simon Morlat's avatar
Simon Morlat committed
343
	for (it=enumValues.begin();it!=enumValues.end();++it){
344
		string initializer = (*it).getChild("initializer").getText();
345 346 347 348 349 350 351 352 353 354 355 356 357
		if ((initializer.length() > 1) && (initializer.at(0) == '=')) {
			std::stringstream ss;
			if ((initializer.length() > 2) && (initializer.at(1) == '0')) {
				if ((initializer.length() > 3) && (initializer.at(2) == 'x')) {
					ss << std::hex << initializer.substr(3);
				} else {
					ss << std::oct << initializer.substr(2);
				}
			} else {
				ss << std::dec << initializer.substr(1);
			}
			ss >> value;
		}
358
		ConstField *cf=new ConstField(Type::getType("int"),(*it).getChild("name").getText(),value);
Simon Morlat's avatar
Simon Morlat committed
359 360
		cf->setHelp((*it).getChild("detaileddescription").getChild("para").getText());
		klass->addConstField(cf);
361
		value++;
Simon Morlat's avatar
Simon Morlat committed
362 363 364 365
	}
	
}

366
static void parseTypedef(Project *proj, xmlNode *node){
367 368 369
	XmlNode tdef(node);
	string typecontent=tdef.getChild("type").getText();
	string name=tdef.getChild("name").getText();
370 371
	if (typecontent.find("enum")==0){
		Type::addType(Type::Enum,name);
372 373
	}else if (typecontent.find("(*")!=string::npos){
		parseCallback(proj,node);
374 375
	}else
		proj->getClass(name)->setHelp(getHelpBody(node));
376 377 378
}

static void parseMemberDef(Project *proj, xmlNode *node){
379
	XmlNode member(node);
380 381
	string brief;
	string detailed;
382
	string kind;
383
	
384 385
	if (member.getChild("briefdescription").getText().empty() && 
		member.getChild("detaileddescription").getChild("para").getText().empty())
386
		return;
387 388 389 390
	if (member.getProp("id").find("group__")!=0)
		return;
	if (member.getChild("detaileddescription").getChildRecursive("xreftitle").getText()=="Deprecated")
		return;
391
	
392 393 394 395 396
	kind=member.getProp("kind");
	if (kind=="function"){
		parseFunction(proj,node);
	}else if (kind=="typedef"){
		parseTypedef(proj,node);
Simon Morlat's avatar
Simon Morlat committed
397 398
	}else if (kind=="enum"){
		parseEnum(proj,node);
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	}
}

static void inspectNode(Project *proj, xmlNode *a_node){
	xmlNode *cur_node;
	
	for (cur_node = a_node; cur_node != NULL ; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			//printf("node type: Element, name: %s\n", cur_node->name);
			if (strcmp((const char*)cur_node->name,"memberdef")==0 ){
				//cout<<"Found memberdef"<<endl;
				parseMemberDef(proj,cur_node);
			}
		}
		if (cur_node->children) inspectNode(proj,cur_node->children);
	}
}

static int parse_file(Project *proj, const char *filename){
	xmlDoc *doc = NULL;
	xmlNode *root_element = NULL;


	/*parse the file and get the DOM */
423
	doc = xmlReadFile(filename, NULL, XML_PARSE_RECOVER);
424 425 426 427 428 429 430 431 432 433 434 435 436

	if (doc == NULL) {
		cerr<<"xmlReadFile failed."<<endl;
		return -1;
	}

	/*Get the root element node */
	root_element = xmlDocGetRootElement(doc);

	inspectNode(proj,root_element);

	/*free the document */
	xmlFreeDoc(doc);
437
	return 0;
438 439 440 441
}

int main(int argc, char *argv[]){
	int i;
Simon Morlat's avatar
Simon Morlat committed
442
	string projectName="wrapper";
443 444 445
	OutputGenerator *gen=NULL;
	list<string> files;
	list<string>::iterator it;
446 447 448 449 450
	
	LIBXML_TEST_VERSION
	
	for(i=1;i<argc;i++){
		if (strcmp(argv[i],"--help")==0){
Simon Morlat's avatar
Simon Morlat committed
451
			fprintf(stderr,"%s: [--help] --output (c++, javascript) --project <project name> file1 file2...\nParses xml files generated by doxygen to output wrappers in a specified language.\n",argv[0]);
452
			return -1;
Simon Morlat's avatar
Simon Morlat committed
453
		}else if (strcmp(argv[i],"--output")==0){
454 455 456 457 458 459
			i++;
			if (strcmp(argv[i],"c++")==0){
				gen=new CplusplusGenerator();
			}else if (strcmp(argv[i],"javascript")==0){
				gen=new JavascriptGenerator();
			}
Simon Morlat's avatar
Simon Morlat committed
460 461 462
		}else if (strcmp(argv[i],"--project")==0){
			i++;
			projectName=argv[i];
463
		}else{
464
			files.push_back(argv[i]);
465 466 467
		}
	}
	
468 469 470 471
	if (gen==NULL) {
		cerr<<"No output generator selected !"<<endl;
		return -1;
	}
Simon Morlat's avatar
Simon Morlat committed
472
	Project *proj=new Project(projectName);
473 474 475 476 477 478 479 480
	for(it=files.begin();it!=files.end();it++){
		if (parse_file(proj,(*it).c_str())==-1){
			cerr<<"Parsing aborted."<<endl;
			return -1;
		}
	}
	proj->analyse();
	gen->generate(proj);
481
	return 0;
482
}