--- cpp/src/slice2php/Main.cpp 2011-06-15 21:43:59.000000000 +0200 +++ Main.cpp 2013-01-23 12:25:47.056253338 +0100 @@ -86,11 +86,20 @@ // void writeDefaultValue(const TypePtr&); + void writeTypeDoc(const TypePtr& p); + + void writeDocCommentOp(const OperationPtr& p); + + void copyDocComment(const ContainedPtr& p); + + StringList splitComment(const ContainedPtr& p); + struct MemberInfo { string fixedName; bool inherited; DataMemberPtr dataMember; + TypePtr type; }; typedef list MemberInfoList; @@ -159,6 +168,105 @@ } } +/** Copied from slice2java/Gen.cpp */ +StringList +CodeVisitor::splitComment(const ContainedPtr& p) +{ + StringList result; + + string comment = p->comment(); + string::size_type pos = 0; + string::size_type nextPos; + while((nextPos = comment.find_first_of('\n', pos)) != string::npos) + { + result.push_back(string(comment, pos, nextPos - pos)); + pos = nextPos + 1; + } + string lastLine = string(comment, pos); + if(lastLine.find_first_not_of(" \t\n\r") != string::npos) + { + result.push_back(lastLine); + } + + return result; +} + +void +CodeVisitor::copyDocComment(const ContainedPtr& p) { + StringList lines = splitComment(p); + + _out << nl << "/**"; + + for(StringList::const_iterator i = lines.begin(); i != lines.end(); ++i) + { + _out << nl << " * " << (*i); + } + if(!lines.empty()) + { + _out << nl << " *"; + } +} + +void +CodeVisitor::writeDocCommentOp(const OperationPtr& p) +{ + ContainerPtr container = p->container(); + ContainedPtr contained = ContainedPtr::dynamicCast(container); + + copyDocComment(p); + + TypePtr retType = p->returnType(); + if (retType) { + string retTypeStr = retType->typeId(); + _out << nl << " * @returnType "; + writeTypeDoc(retType); + _out << nl << " * @returnIceType " << retType->typeId(); + } + ParamDeclList params = p->parameters(); + for(ParamDeclList::iterator q = params.begin(); q != params.end(); ++q) + { + _out << nl << " * @paramType " << fixIdent((*q)->name()) << " "; + writeTypeDoc((*q)->type()); + } + _out << nl << " */"; + +} + +void +CodeVisitor::writeTypeDoc(const TypePtr& p) { + + BuiltinPtr builtin = BuiltinPtr::dynamicCast(p); + if(builtin) { + _out << builtin->typeId(); + return; + } + + ProxyPtr prx = ProxyPtr::dynamicCast(p); + if(prx) + { + //_out << " (prxtype: "<< prx->_class()->containedType() << ") "; + _out << getAbsolute(prx->_class(), _ns, "", ""); + return; + } + + DictionaryPtr dict = DictionaryPtr::dynamicCast(p); + if(dict) { + _out << "dict"; + return; + } + + SequencePtr seq = SequencePtr::dynamicCast(p); + if(seq) { + writeTypeDoc(seq->type()); + _out << "[]"; + return; + } + + ContainedPtr cont = ContainedPtr::dynamicCast(p); + assert(cont); + _out << getAbsolute(cont, _ns, "", ""); +} + bool CodeVisitor::visitClassDefStart(const ClassDefPtr& p) { @@ -169,7 +277,7 @@ string prxName = getName(p, "Prx"); string prxType = getTypeVar(p, "Prx"); string prxAbs = getAbsolute(p, _ns, "", "Prx"); - ClassList bases = p->bases(); + ClassList bases = p->bases(); ClassDefPtr base; OperationList ops = p->operations(); OperationList::iterator oli; @@ -186,6 +294,10 @@ { _out << sp << nl << "if(!interface_exists('" << escapeName(abs) << "'))"; _out << sb; + copyDocComment(p); + _out << nl << " * @iceType " << scoped; + _out << nl << " * @iceKind interface"; + _out << nl << " */"; _out << nl << "interface " << name; if(!bases.empty()) { @@ -202,8 +314,10 @@ _out << sb; for(oli = ops.begin(); oli != ops.end(); ++oli) { - _out << nl << "public function " << fixIdent((*oli)->name()) << '('; - ParamDeclList params = (*oli)->parameters(); + writeDocCommentOp(*oli); + _out << nl << "public function " << fixIdent((*oli)->name()) << '('; + + ParamDeclList params = (*oli)->parameters(); for(ParamDeclList::iterator q = params.begin(); q != params.end(); ++q) { if(q != params.begin()) @@ -213,6 +327,7 @@ _out << '$' << fixIdent((*q)->name()); } _out << ");"; + _out << nl; } _out << eb; } @@ -220,6 +335,10 @@ { _out << sp << nl << "if(!class_exists('" << escapeName(abs) << "'))"; _out << sb; + copyDocComment(p); + _out << nl << " * @iceType " << scoped; + _out << nl << " * @iceKind class"; + _out << nl << " */"; _out << nl; if(isAbstract) { @@ -340,7 +459,11 @@ bool isProtected = p->hasMetaData("protected"); for(DataMemberList::iterator q = members.begin(); q != members.end(); ++q) { - _out << nl; + copyDocComment(*q); + _out << nl << " * @var "; + writeTypeDoc((*q)->type()); + _out << nl << " */"; + _out << nl; if(isProtected || (*q)->hasMetaData("protected")) { _out << "protected "; @@ -583,6 +706,10 @@ _out << sp << nl << "if(!class_exists('" << escapeName(abs) << "'))"; _out << sb; + copyDocComment(p); + _out << nl << " * @iceType " << scoped; + _out << nl << " * @iceKind exception"; + _out << nl << " */"; _out << nl << "class " << name << " extends "; ExceptionPtr base = p->base(); string baseName; @@ -662,6 +789,10 @@ _out << sp; for(dmli = members.begin(); dmli != members.end(); ++dmli) { + copyDocComment((*dmli)); + _out << nl << " * @var "; + writeTypeDoc((*dmli)->type()); + _out << nl << "*/"; _out << nl << "public $" << fixIdent((*dmli)->name()) << ";"; } } @@ -736,6 +867,7 @@ memberList.back().fixedName = fixIdent((*q)->name()); memberList.back().inherited = false; memberList.back().dataMember = *q; + memberList.back().type = (*q)->type(); } } @@ -744,6 +876,10 @@ _out << sp << nl << "if(!class_exists('" << escapeName(abs) << "'))"; _out << sb; + copyDocComment(p); + _out << nl << " * @iceType " << scoped; + _out << nl << " * @iceKind struct"; + _out << nl << " */"; _out << nl << "class " << name; _out << sb; _out << nl << "public function __construct("; @@ -770,6 +906,10 @@ _out << sp; for(r = memberList.begin(); r != memberList.end(); ++r) { + _out << nl << "/**"; + _out << nl << " * @var "; + writeTypeDoc(r->type); + _out << nl << " */"; _out << nl << "public $" << r->fixedName << ';'; } } @@ -901,6 +1041,10 @@ _out << sp << nl << "if(!class_exists('" << escapeName(abs) << "'))"; _out << sb; + _out << nl << "/**"; + _out << nl << " * @iceType " << scoped; + _out << nl << " * @iceKind enum"; + _out << nl << " */"; _out << nl << "class " << name; _out << sb; @@ -1679,7 +1823,7 @@ else { PreprocessorPtr icecpp = Preprocessor::create(argv[0], *i, cppArgs); - FILE* cppHandle = icecpp->preprocess(false); + FILE* cppHandle = icecpp->preprocess(true); if(cppHandle == 0) {