objp.php 126 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794
  1. <?php
  2. // Includes
  3. require_once("utilities.php");
  4. require_once("objp_base.php");
  5. require_once("docset.php");
  6. // Constants
  7. define("CAST_HANDLE", true);
  8. define("DONT_CAST_HANDLE", false);
  9. define("ACCESS_HANDLE_DIRECT", 1);
  10. define("ACCESS_HANDLE_FUNCTION", 2);
  11. define("REGISTER_SEL", true);
  12. define("DONT_REGISTER_SEL", false);
  13. define("USE_HANDLE", true);
  14. define("DONT_USE_HANDLE", false);
  15. class ObjectivePParser extends ObjectivePParserBase {
  16. /**
  17. * UTILITIES
  18. */
  19. // Skips blocks in the current file being parsed
  20. function SkipBlock ($line) {
  21. if ($line != "") {
  22. foreach ($this->skip_blocks as $key => $value) {
  23. if (@ereg($key, $line)) {
  24. $this->parser_skipping = true;
  25. }
  26. if (@ereg($value, $line)) {
  27. $this->parser_skipping = false;
  28. }
  29. }
  30. }
  31. return $this->parser_skipping;
  32. }
  33. function IsKeywordReserved($keyword) {
  34. $keyword = strtolower($keyword);
  35. if (in_array($keyword, $this->reserved_keywords)) return true;
  36. if (($this->current_class != null) &&
  37. ($this->dump["master"][$this->current_class["name"]] ["field_names"] != null) &&
  38. in_array($keyword, $this->dump["master"][$this->current_class["name"]] ["field_names"])) return true;
  39. }
  40. // Extracts the name from an Objective-C method definition
  41. function ExtractObjcMethodName ($method) {
  42. // cut out comments first
  43. $method = eregi_replace("(/\*.*\*/)", "", $method);
  44. $method = eregi_replace("//.*$", "", $method);
  45. $method = trim($method, " ");
  46. $params = explode(":", $method);
  47. $name = "";
  48. if (count($params) > 1) {
  49. foreach ($params as $value) {
  50. $value = trim($value, " ");
  51. if (eregi("([a-zA-Z0-9_]+)[[:space:]]*$", $value, $captures)) $name .= $captures[1].":";
  52. /*
  53. // paremeter with no label (or first parameter)
  54. if (eregi("\([a-zA-Z0-9_]+\)[[:space:]]*([a-zA-Z0-9]+)[[:space:]]*$", $value, $captures)) {
  55. if ($name != "") {
  56. $name .= ":";
  57. } else {
  58. $name .= $captures[1].":";
  59. }
  60. continue;
  61. }
  62. // parameter with label
  63. if (eregi("\([a-zA-Z0-9_]+\)[[:space:]]*[a-zA-Z0-9_]+[[:space:]]+([a-zA-Z0-9]+)$", $value, $captures)) $name .= $captures[1].":";
  64. */
  65. }
  66. } else {
  67. if (eregi("([a-zA-Z0-9_]+)[[:space:]]*(;)*$", $method, $captures)) $name = $captures[1];
  68. }
  69. return $name;
  70. }
  71. function MaybeRenameMethod(&$name, $isclassmethod) {
  72. if ($isclassmethod) {
  73. foreach ($this->replace_class_methods as $org_name => $replace_name) {
  74. if ($name == $org_name) {
  75. $name = $replace_name;
  76. break;
  77. }
  78. }
  79. } else {
  80. foreach ($this->replace_instance_methods as $org_name => $replace_name) {
  81. if ($name == $org_name) {
  82. $name = $replace_name;
  83. break;
  84. }
  85. }
  86. }
  87. }
  88. // in case of a non-function pointer and non-pointer type,
  89. // split a C field expression (e.g. "long afield" or
  90. // "long int :32") into its type and field name. Necessary
  91. // to recognise name that "long int :32" means "a field
  92. // without a name whose type/alignment is "long int" and that is
  93. // bitpacked
  94. function ExtractCFieldSimpleTypeAndSingleName($param) {
  95. // print("Converting field $param\n");
  96. // remove all "const" occurrences to simplify matching
  97. $clean_param = str_replace_word("const", "", $param);
  98. $got_type = false;
  99. // assume not a pointer type
  100. if (preg_match("!^\s*(unsigned|signed)\b!", $clean_param, $signedunsigned)) {
  101. $got_type = true;
  102. $clean_param = preg_replace("!^\s*(unsigned|signed)\b!", "", $clean_param);
  103. }
  104. if (preg_match("!^\s*(char|short|int|long\s+long\s+int|long\s+int|long\s+long|long)\b!", $clean_param, $inttype)) {
  105. $got_type = true;
  106. $clean_param = preg_replace("!^\s*(char|short|int|long\s+long\s+int|long\s+int|long\s+long|long)\b!", "", $clean_param);
  107. }
  108. if ($got_type) {
  109. // remove bitfield and array, are encoded later
  110. $result["name"] = trim(preg_replace("!(?::.*)?(?:\[[^]]*\])?!","",$clean_param));
  111. $result["type"] = trim(preg_replace("!\s+!", " ", $signedunsigned[0] . " " . $inttype[0]));
  112. } else {
  113. // remove "struct"
  114. $clean_param = str_replace_word("struct", "", $clean_param);
  115. // has to be "type fieldname"
  116. preg_match("!^\s*(\w+)\b\s*(\w+)(?:\[[0-9]*\])?\s*!", $clean_param, $matches);
  117. $result["name"] = $matches[2]; // can be empty
  118. $result["type"] = $matches[1];
  119. }
  120. // print("field conversion result: ".$result["name"].": ".$result["type"]."\n");
  121. return $result;
  122. }
  123. function ExtractCParaNameAndType($param) {
  124. // print("Converting $param\n");
  125. // remove all "const" occurrences to simplify matching
  126. $clean_param = str_replace_word("const", "", $param);
  127. // remove all spaces between multiple pointer modifiers
  128. $clean_param = trim(preg_replace("![*]\s+[*]!", "**", $clean_param));
  129. // 1) varargs
  130. if ($clean_param == "...") {
  131. $result["name"] = "";
  132. $result["pointermods"] = "";
  133. $result["type"] = "...";
  134. // 2) type is a function pointer (does not yet match arrays of function
  135. // pointers!)
  136. } elseif (preg_match($this->pregex_function_pointer_c_paratype, $clean_param, $matches)) {
  137. $result["name"] = $matches[3];
  138. // not correct; simply assume no "pointer to function pointer" types are
  139. // used
  140. $result["pointermods"] = "";
  141. $funcptrtype = $this->ParseFunctionDeclaration($matches[1], $matches[2], "", $matches[4], false, "");
  142. $result["type"] = $this->AddCallback($matches[3], $funcptrtype);
  143. // 3) any other pointer type (including arrays)
  144. } elseif (preg_match("!^([^*]*)([*]+)\s*(\w+(?:\[[0-9]*\])?)?\s*$!", $clean_param, $matches)){
  145. $result["name"] = $matches[3]; // can be empty
  146. $result["pointermods"] = $matches[2];
  147. $result["type"] = trim($matches[1]);
  148. // 4) basic C type (including arrays)
  149. } else {
  150. // definitely not a pointer type
  151. $result["pointermods"] = "";
  152. $got_type = false;
  153. if (preg_match("!^\s*(unsigned|signed)!", $clean_param, $signedunsigned)) {
  154. $got_type = true;
  155. $clean_param = preg_replace("!^\s*(unsigned|signed)!", "", $clean_param);
  156. }
  157. if (preg_match("!^\s*(char|short|int|long\s+long\s+int|long\s+int|long\s+long|long)!", $clean_param, $inttype)) {
  158. $got_type = true;
  159. $clean_param = preg_replace("!^\s*(char|short|int|long\s+long\s+int|long\s+int|long\s+long|long)!", "", $clean_param);
  160. }
  161. if ($got_type) {
  162. $result["name"] = trim($clean_param);
  163. $result["type"] = trim($signedunsigned[0] . " " . $inttype[0]);
  164. } else {
  165. // remove "struct"
  166. $clean_param = str_replace_word("struct", "", $clean_param);
  167. // has to be "type paraname", or just "type"
  168. preg_match("!^\s*(\w+)\b\s*(\w+(?:\[[0-9]*\])?)?\s*!", $clean_param, $matches);
  169. $result["name"] = $matches[2]; // can be empty
  170. $result["type"] = $matches[1];
  171. }
  172. }
  173. // print("param conversion result: ".$result["name"].": ".$result["pointermods"].$result["type"]."\n");
  174. return $result;
  175. }
  176. // Returns the parameter modifier string for a callback parameter type
  177. function GetCallbackParameterModifiers ($type, $name) {
  178. $modifiers = "";
  179. $type = trim($type, "*");
  180. // if the name starts with * it's a pointer
  181. // don't process framework classes since they're always pointers
  182. if (ereg("^[[:space:]]*\*", $name)) {
  183. if (!in_array($type, $this->cocoa_classes)) $modifiers = "var ";
  184. }
  185. // double ** are always var
  186. if (ereg("^[[:space:]]*\*\*", $name)) {
  187. $modifiers = "var ";
  188. }
  189. return $modifiers;
  190. }
  191. // Print string to output file
  192. function PrintOutput ($indent, $string) {
  193. for ($i=0; $i < $indent; $i++) {
  194. $indent_string .= " ";
  195. }
  196. if (($this->output) && (!$this->show)) fwrite($this->output, "$indent_string$string\n");
  197. if ($this->show) print("$indent_string$string\n");
  198. }
  199. // Returns a class hierarchy array
  200. function GetClassHierarchy ($class, &$hierarchy) {
  201. if (!$hierarchy) $hierarchy = array();
  202. $hierarchy[] = $class["name"];
  203. if ($class["super_class"]) {
  204. $hierarchy[] = $this->GetClassHierarchy($class["super_class"], $hierarchy);
  205. }
  206. return $class["name"];
  207. }
  208. // Returns all protected keywords in a class hierarchy
  209. function GetProtectedKeywords ($in_class) {
  210. $this->GetClassHierarchy($in_class, $hierarchy);
  211. $keywords = array();
  212. foreach ($hierarchy as $class) {
  213. if ($this->dump["master"][$class]["protected_keywords"]) {
  214. foreach ($this->dump["master"][$class]["protected_keywords"] as $keyword) $keywords[] = strtolower($keyword);
  215. }
  216. }
  217. return $keywords;
  218. }
  219. // Gets the preferred property name from attributes
  220. function GetPropertyName ($kind, $params, &$name) {
  221. foreach ($params as $value) {
  222. $pair = explode("=", $value);
  223. if ($pair[0] == $kind) {
  224. $name = $pair[1];
  225. return true;
  226. break;
  227. }
  228. }
  229. }
  230. // Checks if a line has a property and returns the parts
  231. function LineHasProperty ($line, &$parts) {
  232. if (eregi($this->regex_objc_property_attributes, $line, $captures)) {
  233. $parts = $captures;
  234. //print_r($parts);
  235. return true;
  236. } elseif (eregi($this->regex_objc_property, $line, $captures)) {
  237. $parts = $captures;
  238. //print_r($parts);
  239. return true;
  240. }
  241. }
  242. // Returns header a category should be moved to
  243. function FindCategoryHeader ($category) {
  244. foreach ($this->dump as $name => $header) {
  245. if ((@array_key_exists($category, $header["classes"])) && ($category != "NSObject")) {
  246. return $name;
  247. }
  248. }
  249. }
  250. // Checks if $class (array) contains $method (array)
  251. function ClassContainsMethod ($class, $method) {
  252. if ($class["methods"]) {
  253. foreach ($class["methods"] as $key) {
  254. if ($key["name"] == $method["name"]) return true;
  255. }
  256. }
  257. }
  258. function GetAnonBitFieldName() {
  259. $name = "_anonbitfield_".$this->current_header["name_clean"].$this->current_header["anonbitfields"];
  260. $this->current_header["anonbitfields"]++;
  261. return $name;
  262. }
  263. // create a variant record with a first anonymous field of type
  264. // first_bitpacked_type so that the compiler may correctly align it
  265. // (it does not do that currently, but this at least provides a
  266. // hint to the programmer)
  267. function BitPackedForceAlignment($first_bitpacked_type, $firstindent, $otherindents) {
  268. $result = $firstindent . "case byte of\n" . $otherindents ."0: (" . $this->GetAnonBitFieldName() . ": $first_bitpacked_type);\n" . $otherindents . "1: (";
  269. return $result;
  270. }
  271. function EncodePointerModifiers($type, $pointertype) {
  272. if ($pointertype[0] == "*") $type = $this->ReplacePointerType($type);
  273. if ($pointertype[1] == "*") {
  274. if ($type == "Pointer") {
  275. $type = "PPointer";
  276. } elseif ($type == "PChar") {
  277. $type = "PPChar";
  278. } elseif (in_array($type, $this->cocoa_classes)) {
  279. $type = $type.$this->class_pointer_suffix;
  280. } else {
  281. // to match MacOSAll types
  282. $type = $type."Ptr";
  283. }
  284. }
  285. return $type;
  286. }
  287. /**
  288. * DOCSETS UTILITIES
  289. */
  290. function FindDocumentationForMethod ($class, $name) {
  291. if ($this->docset) {
  292. $doc = $this->docset[$class][$name];
  293. if ($doc) return "{ $doc }";
  294. }
  295. }
  296. function FindDocumentationForType ($name) {
  297. if ($this->docset) {
  298. foreach ($this->docset as $class) {
  299. foreach ($class as $type => $text) {
  300. if ($type == $name) return "{ $text }";
  301. }
  302. }
  303. }
  304. }
  305. /**
  306. * ADDING LANGUAGE STRUCTURE UTILITIES
  307. */
  308. // Adds a method structure to a class and performs checks for overloaded methods
  309. function AddMethodToClass (&$method, &$class) {
  310. // ignore methods
  311. if (in_array($method["name"], $this->ignore_methods)) return false;
  312. // add comment to the method
  313. $method["comment"] = $this->InsertCurrentComment();
  314. if (@!in_array($method["name"], $class["declared_methods"])) {
  315. $class["all"][$method["name"]] = $method;
  316. $class["protected_keywords"][] = $method["name"];
  317. $class["declared_methods"][] = $method["name"];
  318. $this->dump["all_methods"][$class["name"]][] = $method["objc_method"];
  319. if ($this->show_added_messages) print(" @ Added ".$method["name"]." to ".$class["name"]."\n");
  320. $this->method_count ++;
  321. return true;
  322. } else {
  323. print(" ! ".$method["def"]." already exists in ".$class["name"]." defined as ".$class["all"][$method["name"]]["def"]."\n");
  324. }
  325. }
  326. // Adds a typedef to the header and handles organization to prevent order conflicts
  327. function AddTypeDef (&$header, $typedef) {
  328. //$this->AppendCurrentMacro($header["types"]["typedef"]);
  329. $this->AppendCurrentComment($header["types"]["typedef"]);
  330. if ($this->comment_terminated) $header["types"]["typedef"][] = $this->InsertCurrentComment();
  331. $header["types"]["typedef"][] = $typedef;
  332. }
  333. // adds a function callback type with name $name, and returns
  334. // the name of the callback type
  335. function AddCallback($name, $function_pointer) {
  336. if ($this->current_header) {
  337. $type = $this->current_header["name_clean"].ucwords($name);
  338. if (!@in_array($function_pointer, $this->current_header["types"]["callbacks"])) {
  339. $count = 0;
  340. while (@array_key_exists($type, $this->current_header["types"]["callbacks"])) {
  341. $count ++;
  342. $type = "$type$count";
  343. }
  344. // append the new type to the the current class
  345. $this->current_header["types"]["callbacks"][$type] = $function_pointer;
  346. } else {
  347. // Use the name of the existing callback of matching type
  348. $type = array_search($function_pointer, $this->current_header["types"]["callbacks"]);
  349. }
  350. return $type;
  351. }
  352. return "invalid_callback_type_because_no_current_header";
  353. }
  354. /**
  355. * OBJC FORMATTING UTILITIES
  356. */
  357. // Performs additional formatting on Objective-c type i.e. (out NSObject **)
  358. function FormatObjcType ($type, &$modifiers) {
  359. $modifiers = "";
  360. // toss out all const identifiers
  361. $type = istr_replace_word("const", "", $type);
  362. // replace inout paramaters
  363. $type = istr_replace_word("inout", "", $type);
  364. $type = istr_replace_word("out", "", $type);
  365. // Translate protocol which type conforms to (id <NSURLHandleClient>)
  366. $type = preg_replace("!id\s*<([^,>]*)>!", "$1Protocol", $type);
  367. // Remove other protocol types
  368. $type = trim(eregi_replace("<[^>]*>", "", $type));
  369. // Replace types before cleaning
  370. $type = $this->ReplaceObjcType($type);
  371. // Remove array brackets (NSUInteger[])p
  372. $type = trim(eregi_replace("\[[0-9]*\]", "", $type));
  373. // var params to non-object types (NSRange *)
  374. if (ereg("([a-zA-Z0-9_]+)[[:space:]]*[*]+$", $type, $captures)) {
  375. if (!in_array($captures[1], $this->cocoa_classes)) {
  376. $type = $this->ReplaceReferenceParameterType(trim($type,"* "));
  377. //$modifiers = "var ";
  378. }
  379. }
  380. // Handle NS*** pointers (NSError **)
  381. if (ereg("(NS[a-zA-Z0-9_]+)[[:space:]]*\*\*$", $type, $captures)) {
  382. if (in_array($captures[1], $this->cocoa_classes)) {
  383. $type = trim($type,"* ")."$this->class_pointer_suffix";
  384. }
  385. }
  386. // clean the type
  387. $type = trim($type, "* ");
  388. //print("$type\n");
  389. return $type;
  390. }
  391. // Performs additional formatting on Objective-c parameter types
  392. function FormatObjcParams ($string, &$variable_arguments) {
  393. $params = explode(":", $string);
  394. $string = "";
  395. $variable_arguments = false;
  396. // print_r($params);
  397. if (count($params) > 0) {
  398. foreach ($params as $value) {
  399. // parameter is varargs
  400. if (eregi("(.*),[[:space:]]*\.\.\.", $value)) {
  401. $string .= ":(id)$this->varargs_param_name";
  402. $variable_arguments = true;
  403. //print("$value\n");
  404. continue;
  405. }
  406. if (preg_match("!^[^(]*$!",$value)) {
  407. // parameter without type -> id
  408. $value = "(id)".$value;
  409. }
  410. // normal parameter
  411. if (ereg("\((.*)\)", $value, $captures)) {
  412. $new_value = $this->ReplaceObjcType($captures[1]);
  413. if ($new_value != $captures[1]) $value = ereg_replace("\((.*)\)", "($new_value)", $value);
  414. $string .= ":$value";
  415. continue;
  416. }
  417. }
  418. }
  419. $string = ltrim($string, ":");
  420. return $string;
  421. }
  422. /**
  423. * SYNTAX MAKING UTILITIES
  424. */
  425. // Makes a paramater list string with options to modify
  426. function MakeParamList ($param_array, $use_handle, $cast_handle, $direct, $register_selector) {
  427. $params = "";
  428. foreach ($param_array as $pair) {
  429. // register selector parameters
  430. if (($register_selector) && ($pair["type"] == "SEL")) {
  431. $params .= "sel_registerName(".$pair["name"]."), ";
  432. continue;
  433. }
  434. // use the object handle for NSObject descendants
  435. if ($use_handle) {
  436. if (in_array($pair["type"], $this->cocoa_classes)) {
  437. // cast the param to the original class type
  438. if ($cast_handle) {
  439. if ($direct == ACCESS_HANDLE_DIRECT) {
  440. $params .= $pair["type"]."(".$pair["name"].".Handle), ";
  441. } else {
  442. $params .= $pair["type"]."(GetHandle(".$pair["name"].")), ";
  443. }
  444. } else {
  445. if ($direct == ACCESS_HANDLE_DIRECT) {
  446. $params .= $pair["name"].".Handle, ";
  447. } else {
  448. $params .= "GetHandle(".$pair["name"]."), ";
  449. }
  450. }
  451. } else {
  452. if (($this->objects_are_wrappers) && ($pair["type"] == $this->objc_id)) { // id is always a wrapper
  453. if ($direct == ACCESS_HANDLE_DIRECT) {
  454. $params .= $pair["type"]."(".$pair["name"].".Handle), ";
  455. } else {
  456. $params .= $pair["type"]."(GetHandle(".$pair["name"].")), ";
  457. }
  458. } else {
  459. $params .= $pair["name"].", ";
  460. }
  461. }
  462. } else { // append without modification
  463. $params .= $pair["name"].", ";
  464. }
  465. }
  466. return trim($params, ", ");
  467. }
  468. // Makes a list of paramameter variables with NS*** class types cast to "id" or the original class
  469. function MakeObjcTypeParamList ($param_array, $objc_type) {
  470. $params = "";
  471. foreach ($param_array as $pair) {
  472. if (in_array($pair["type"], $this->cocoa_classes)) {
  473. if ($objc_type) {
  474. $params .= "$this->objc_id(".$pair["name"]."), ";
  475. } else {
  476. $params .= $pair["type"]."(".$pair["name"]."), ";
  477. }
  478. } else {
  479. $params .= $pair["name"].", ";
  480. }
  481. }
  482. return trim($params, ", ");
  483. }
  484. // Makes a struct field into an inline array (or returns field un-changed)
  485. function MakeFieldInlineArray ($io_field, $line, $name, $type) {
  486. if (eregi("\[([0-9]+)\];", $line, $array_size)) {
  487. $length = (int)$array_size[1] - 1;
  488. if ($length > 0) {
  489. $io_field = " $name: array[0..$length] of $type;";
  490. }
  491. }
  492. return $io_field;
  493. }
  494. // Makes a type bitpacked (or returns type un-changed)
  495. function MakeFieldBitPacked ($ioType, $field, &$bitpacked) {
  496. $bitpacked = false;
  497. if (preg_match("!:([0-9]+)\s*(?:__attribute__\(\([^)]*\)\))?\s*;$!", $field, $bitpack)) {
  498. $length = (int)$bitpack[1];
  499. if ($length > 1) {
  500. $ioType = "0..((1 shl $length)-1)";
  501. } else {
  502. $ioType = "0..$length";
  503. }
  504. $bitpacked = true;
  505. }
  506. return $ioType;
  507. }
  508. /**
  509. * REPLACEMENT UTILITIES
  510. */
  511. // Replace type with pointer equivalent
  512. function ReplacePointerType ($type) {
  513. $found = false;
  514. // all classes are implicit pointer types
  515. if (in_array($type, $this->cocoa_classes)) return $type;
  516. // function pointer types are sometimes also implicit
  517. // pointers
  518. if (in_array($type, $this->implicit_function_pointer_types)) return $type;
  519. // PPointer = ^Pointer
  520. if ($type == "Pointer") return "PPointer";
  521. if ($type == "PChar") return "PPChar";
  522. // use preferred pointer type
  523. foreach ($this->pointer_types as $objc_type => $replace_type) {
  524. if ($objc_type == $type) {
  525. $found = true;
  526. $type = $replace_type;
  527. break;
  528. }
  529. }
  530. //$type = "Pointer {".$type."}";
  531. // use generic pointer type
  532. if (!$found)
  533. $type = $type."Ptr";
  534. return $type;
  535. }
  536. // Replace objc type with preferred type
  537. function ReplaceObjcType ($type) {
  538. foreach ($this->replace_types as $objc_type => $replace_type) {
  539. if ($objc_type == $type) {
  540. $type = $replace_type;
  541. break;
  542. }
  543. }
  544. return $type;
  545. }
  546. // Replace garbage collector hints
  547. function ReplaceGarbageCollectorHints ($string, &$io_hint) {
  548. $io_hint = false;
  549. foreach ($this->garbage_collector_hints as $hint) {
  550. $out_string = str_ireplace($hint, "", $string);
  551. if ($out_string != $string) {
  552. $io_hint = $hint;
  553. $string = $out_string;
  554. }
  555. }
  556. return $string;
  557. }
  558. // Replace remote message passing modifiers
  559. function ReplaceRemoteMessagingModifiers ($string, &$io_modifier) {
  560. $io_hint = false;
  561. foreach ($this->remote_messaging_modifiers as $modifier) {
  562. $out_string = preg_replace("!\b".$modifier."\b!", "", $string);
  563. if ($out_string != $string) {
  564. $io_modifier = $modifier;
  565. $string = $out_string;
  566. }
  567. }
  568. return trim($string);
  569. }
  570. // Replace type of reference parameter with pointer
  571. function ReplaceReferenceParameterType ($type) {
  572. $type = $this->ReplaceObjcType($type);
  573. return $this->ReplacePointerType($type);
  574. }
  575. // Replace a framework class with generic id and comment hint
  576. function ReplaceFrameworkClassWithID ($string) {
  577. foreach ($this->cocoa_classes as $class) {
  578. $string = istr_replace_word($class, $this->objc_id, $string);
  579. }
  580. return $string;
  581. }
  582. /**
  583. * MACRO UTILITIES
  584. */
  585. // Insert macro blocks to replace c-style blocks
  586. function InsertMacroBlocks ($line, &$in_macro_block) {
  587. // only insert if we are in a block already.
  588. // NOTE: this does not handle nesting!
  589. if ($in_macro_block) {
  590. // macro else statment
  591. if (eregi("#else", $line)) {
  592. return "{\$else}";
  593. }
  594. // macro endif statment
  595. if (eregi("#endif", $line)) {
  596. $in_macro_block = false;
  597. return "{\$endif}";
  598. }
  599. }
  600. foreach ($this->macro_blocks as $key => $value) {
  601. if (eregi($key, $line, $captures)) {
  602. $in_macro_block = true;
  603. // replace the c-macro with a Pascal version
  604. if ($value == "*") {
  605. $captures[0] = trim($captures[0], "#");
  606. return "{\$".$captures[0]."}";
  607. } else {
  608. return "{".$value."}";
  609. }
  610. }
  611. }
  612. }
  613. // Appends the current macro to a recipient and resets it's reference
  614. function AppendCurrentMacro (&$recipient) {
  615. if ($this->in_macro_block) {
  616. $this->in_macro_block = false;
  617. $recipient[] = $this->macro_block;
  618. //if (is_array($recipient)) $recipient[] = $this->macro_block;
  619. //if (is_string($recipient)) $recipient .= $this->macro_block;
  620. }
  621. }
  622. // Returns the current macro and resets it's reference
  623. function InsertCurrentMacro () {
  624. if ($this->in_macro_block) {
  625. $this->in_macro_block = false;
  626. return $this->macro_block;
  627. } else {
  628. return null;
  629. }
  630. }
  631. // Build the current macro block
  632. function BuildMacroBlocks ($line) {
  633. // only insert if we are in a block already.
  634. // NOTE: this does not handle nesting!
  635. if ($this->in_macro_block) {
  636. // macro else statment
  637. if (eregi("#else", $line)) {
  638. $this->macro_block = "{\$else}";
  639. }
  640. // macro endif statment
  641. if (eregi("#endif", $line)) {
  642. $this->in_macro_block = false;
  643. $this->macro_block = "{\$endif}";
  644. }
  645. }
  646. foreach ($this->macro_blocks as $key => $value) {
  647. if (eregi($key, $line, $captures)) {
  648. $this->in_macro_block = true;
  649. // replace the c-macro with a Pascal version
  650. if ($value == "*") {
  651. $captures[0] = trim($captures[0], "#");
  652. $this->macro_block = "{\$".$captures[0]."}";
  653. } else {
  654. $this->macro_block = "{".$value."}";
  655. }
  656. }
  657. }
  658. }
  659. // Remove OS X versions macros from a line
  660. // NOTE: These should be re-inlcuded in Pascal
  661. function RemoveVersionMacros ($line, &$deprecatedmods) {
  662. if (preg_match($this->pregex_deprecated_macro,$line)) {
  663. $deprecatedmods = DeprecatedMacroToDirective($line);
  664. } else {
  665. $deprecatedmods = "";
  666. }
  667. foreach ($this->version_macros as $macro) {
  668. $line = eregi_replace($macro, "", $line);
  669. }
  670. return $line;
  671. }
  672. /**
  673. * CONVERTING UTILITIES
  674. */
  675. // Converts an Objective-c parameter string to Pascal
  676. function ConvertObjcParamsToPascal ($string, $protected_keywords) {
  677. $params = explode(":", $string);
  678. $list = array();
  679. $list["pairs"] = array();
  680. $param_list = array();
  681. //print_r($params);
  682. //print("$string\n");
  683. if (count($params) > 0) {
  684. foreach ($params as $value) {
  685. $value = trim($value);
  686. $valid = false;
  687. $modifiers = "";
  688. $value = $this->ReplaceRemoteMessagingModifiers($value, $null);
  689. // function pointer (callback)
  690. if (preg_match($this->pregex_function_pointer_objc_paratype, $value, $captures)) {
  691. $name = $captures[5];
  692. $function_pointer = $this->ParseFunctionDeclaration($captures[1], $captures[2], "", $captures[4], false, "");
  693. $type = $this->AddCallback($name, $function_pointer);
  694. $valid = true;
  695. } elseif ((eregi("\(([a-zA-Z_]+).*\)([a-zA-Z_]+).*\.\.\.", $value, $captures)) || (eregi("(.*),[[:space:]]*\.\.\.", $value, $captures))) { // variable arguments
  696. $name = $captures[2];
  697. $type = $captures[1];
  698. $valid = true;
  699. } elseif (eregi("\((.*)\)[[:space:]]*([a-zA-Z_]+)", $value, $captures)) { // standard parameter
  700. $captures[1] = trim($captures[1]);
  701. $type = $this->FormatObjcType($captures[1], $modifiers);
  702. $name = $captures[2];
  703. $valid = true;
  704. }
  705. if ($valid) {
  706. // protect reserved keywords
  707. if ($this->IsKeywordReserved($name)) $name .= "_";
  708. if (!in_array($type, $this->reserved_types)) {
  709. if ($this->IsKeywordReserved($type)) $type .= "_";
  710. }
  711. // can't have "boolean: boolean;" parameters
  712. if (preg_match("!\b$name\b!i",$type)) $name .= "_";
  713. // print("para name: \"$name\"\n");
  714. // print_r("protected: \"$protected_keywords\"\n");
  715. while (@in_array(strtolower($name), $protected_keywords)) $name .= "_";
  716. // case-insensitive in_array:
  717. // if( preg_match("/\b$name\b/i", join(" ", array_values($protected_keywords))) ) $name .= "_";
  718. // if (@in_array($type, $protected_keywords)) $type .= "_";
  719. // replace objc types
  720. $type = $this->ReplaceObjcType($type);
  721. // make sure we label duplicate params, which are allowed in Objective-C
  722. while (in_array($name, $param_list)) {
  723. $count ++;
  724. $name = "$name$count";
  725. }
  726. // id is always a wrapper
  727. if (($this->objects_are_wrappers) && ($type == $this->objc_id)) {
  728. $name_list = "$type(GetHandle($name))";
  729. } else {
  730. $name_list = $name;
  731. }
  732. // add modifiers to the name if there are any
  733. $name_with_modifiers = $modifiers.$name;
  734. // create pair array
  735. $pair["name"] = $name;
  736. $pair["type"] = $type;
  737. // append list
  738. $list["pairs"][] = $pair;
  739. $list["string_with_modifiers"] .= "$name_with_modifiers: $type; ";
  740. $list["string"] .= "$name: $type; ";
  741. $list["list"] .= "$name_list, ";
  742. $param_list[] = $name;
  743. }
  744. }
  745. }
  746. // clean up the strings
  747. $list["string"] = trim($list["string"], "; ");
  748. $list["string_with_modifiers"] = trim($list["string_with_modifiers"], "; ");
  749. $list["list"] = trim($list["list"], ", ");
  750. return $list;
  751. }
  752. // Converts an Objective-C method to Pascal format
  753. function ConvertObjcMethodToPascal ($class, $source, $parts, $protected_keywords, $has_params, $deprecatedmods) {
  754. //print("$source\n");
  755. //print_r($parts);
  756. // replace "hinted" params comment with hinted type
  757. if ($this->replace_hinted_params) {
  758. // param string
  759. if (eregi("(/\*[[:space:]]*(.*)[[:space:]]*\*/)", $parts[4], $captures)) {
  760. // ??? change the parameter to the hinted type
  761. //$parts[4] = eregi_replace("(/\*.*\*/)", $captures[2], $parts[4]);
  762. //$parts[4] = trim($parts[4], " ");
  763. }
  764. // return type
  765. if (eregi("(/\*[[:space:]]*(.*)[[:space:]]*\*/)", $parts[2], $captures)) $parts[2] = $this->ReplaceRemoteMessagingModifiers($captures[2], $null);
  766. //print_r($parts);
  767. } else { // remove comments from params and return type
  768. $parts[4] = eregi_replace("(/\*.*\*/)", "", $parts[4]);
  769. $parts[4] = trim($parts[4]);
  770. $parts[2] = eregi_replace("(/\*.*\*/)", "", $parts[2]);
  771. $parts[2] = $this->ReplaceRemoteMessagingModifiers($parts[2], $null);
  772. }
  773. $return_type_clean = $parts[2];
  774. $return_type_pointers = preg_replace("![^*]+!e", "", $return_type_clean);
  775. $return_type_clean = trim($return_type_clean,"* ");
  776. // perform preformatting before attempting to protect keywords
  777. $parts[2] = $this->FormatObjcType($parts[2], $modifiers);
  778. $parts[4] = $this->FormatObjcParams($parts[4], $variable_arguments);
  779. //print($parts[4]."\n");
  780. if ($has_params) {
  781. $name = $this->ConvertObjcMethodName($source);
  782. // merge default protected keywords for the class/category
  783. if ($this->default_protected["*"]) $protected_keywords = array_merge($this->default_protected["*"], $protected_keywords);
  784. if ($this->default_protected[$class]) $protected_keywords = array_merge($this->default_protected[$class], $protected_keywords);
  785. $param_array = $this->ConvertObjcParamsToPascal($parts[4], $protected_keywords);
  786. $params = "(".$param_array["string"].")";
  787. $params_with_modifiers = "(".$param_array["string_with_modifiers"].")";
  788. } else {
  789. $params = "";
  790. $params_with_modifiers = "";
  791. // no parameters -> definitely no underscore normally, but there are some
  792. // conflicts...
  793. $name = $parts[3];
  794. // clean it up
  795. if ($this->trailing_underscore) {
  796. if (in_array($source, $this->trailing_underscore_methods)) $name = $name . "_";
  797. }
  798. $param_array = null;
  799. $variable_arguments = false;
  800. }
  801. // rename method if required
  802. $this->MaybeRenameMethod($name,$parts[1]=="+");
  803. // protect method name from keywords
  804. if ($this->IsKeywordReserved($name)) $name .= "_";
  805. // replace objc type
  806. $return_type = $this->ConvertReturnType($return_type_clean,$return_type_pointers);
  807. $virtual = "";
  808. $class_prefix = "";
  809. // determine the type based on return value
  810. if (ereg($this->regex_procedure_type, $return_type_clean.$return_type_pointers)) {
  811. $kind = "procedure";
  812. } else {
  813. $kind = "function";
  814. }
  815. // determine if this is a class method
  816. if ($parts[1] == "+") {
  817. $class_prefix = "class ";
  818. // These methods probably return the an allocated instance of the class, a typical convenience method.
  819. // ??? Ack! $class may be the category or protocol name
  820. //if ($return_type == $this->objc_id) $return_type = $class;
  821. }
  822. // Replace SEL with the string equivalent
  823. if ($this->register_selectors) {
  824. $params_with_modifiers = str_replace_word("SEL", $this->sel_string, $params_with_modifiers);
  825. }
  826. // detect blocks (not yet supported)
  827. $has_blocks = strpos("$return_type $params_with_modifiers","^");
  828. // make method templates
  829. if ($kind != "function") {
  830. if ($variable_arguments) $modifier .= " varargs;";
  831. $method = "$class_prefix$kind $name$params_with_modifiers;$modifier$virtual";
  832. $method_template = "[KIND] [PREFIX]$name"."[PARAMS];$modifier";
  833. } else {
  834. if ($variable_arguments) $return_type = "$return_type; varargs";
  835. $method = $class_prefix."function $name$params_with_modifiers: $return_type;$modifier$virtual";
  836. $method_template = "[KIND] [PREFIX]$name"."[PARAMS]: [RETURN];$modifier";
  837. }
  838. $method_template_procedure = "procedure [PREFIX]$name"."[PARAMS];$modifier";
  839. $method_template_function = "function [PREFIX]$name"."[PARAMS]: [RETURN];$modifier";
  840. // build structure
  841. $struct["def"] = $method;
  842. $struct["template"] = $method_template;
  843. $struct["template_function"] = $method_template_function;
  844. $struct["template_procedure"] = $method_template_procedure;
  845. $struct["objc_method"] = $this->ExtractObjcMethodName($source);
  846. $struct["class_prefix"] = $class_prefix;
  847. if ($deprecatedmods != "") $struct["deprecated"] = $deprecatedmods.";";
  848. //$struct["def_objc"] = eregi("(.*);", $source, $captures[1]);
  849. if ($return_type == "void") $return_type = "";
  850. $struct["return"] = $return_type;
  851. if (in_array($return_type, $this->cocoa_classes)) $struct["returns_wrapper"] = true;
  852. $struct["param_string_clean"] = trim($params, "()");
  853. $struct["param_string_clean_with_modifiers"] = trim($params_with_modifiers, "()");
  854. $struct["param_string"] = $params;
  855. $struct["param_string_with_modifiers"] = $params_with_modifiers;
  856. $struct["param_array"] = $param_array["pairs"];
  857. $struct["param_list"] = $param_array["list"];
  858. $struct["class"] = $class;
  859. $struct["name"] = $name;
  860. $struct["kind"] = $kind;
  861. if ($has_blocks === false) $struct["blocks_disable_comment"] = "";
  862. else $struct["blocks_disable_comment"] = "// ";
  863. if ($struct["param_array"] != null) $struct["has_params"] = true;
  864. // FPC bug work around
  865. if (strlen($name) > $this->maximum_method_length) {
  866. $struct["can_override"] = false;
  867. print(" # WARNING: method $name can't override because the name is too long\n");
  868. $this->warning_count ++;
  869. }
  870. return $struct;
  871. }
  872. // Converts a C parameter string to Pascal
  873. function ConvertCParamsPascal ($string) {
  874. // print("params: $string\n");
  875. if ((trim($string) == "void") || (trim($string) == "")) return "";
  876. $params = explode(",", $string);
  877. $count = 0;
  878. $param_string = "";
  879. foreach ($params as $param) {
  880. $name_type = $this->ExtractCParaNameAndType($param);
  881. $type = $name_type["type"];
  882. $name = $name_type["name"];
  883. $pointertype = $name_type["pointermods"];
  884. // create name if none is specified
  885. if ($name == "") {
  886. $count ++;
  887. $name = "param$count";
  888. }
  889. // remove const keywords
  890. $type = str_replace_word("const", "", $type);
  891. $type = $this->ReplaceObjcType($type);
  892. // Remove array brackets (NSUInteger[])p
  893. if (eregi("\[[0-9]*\]", $name)) {
  894. $orgtype = $this->EncodePointerModifiers($type,$pointertype);
  895. $pointertype .= "*";
  896. $type = $this->EncodePointerModifiers($type,$pointertype)." {array of $orgtype}";
  897. $name = eregi_replace("\[[0-9]*\]", "", $name);
  898. } else {
  899. $type = $this->EncodePointerModifiers($type,$pointertype);
  900. }
  901. $modifier = "";
  902. if ($this->IsKeywordReserved($name)) $name .= "_";
  903. // multiple parameters
  904. if ($type == "...") {
  905. $param_string .= "varargs: array of const";
  906. break;
  907. }
  908. $param_string .= "$modifier$name: $type; ";
  909. }
  910. $param_string = trim($param_string, "; ");
  911. //print("$param_string\n");
  912. return $param_string;
  913. }
  914. // Converts an Objective-c method name to Pascal
  915. function ConvertObjcMethodName ($method) {
  916. $params = explode(":", $method);
  917. $name = "";
  918. $count = 0;
  919. if (count($params) > 1) {
  920. foreach ($params as $value) {
  921. if (eregi("([a-zA-Z0-9]+)$", $value, $captures)) $name .= $captures[1]."_";
  922. }
  923. } else {
  924. if (eregi("([a-zA-Z0-9]+)(;)*$", $params[0], $captures)) $name .= $captures[1]."_";
  925. }
  926. // clean it up
  927. if ($this->trailing_underscore) {
  928. if (!in_array($method, $this->trailing_underscore_methods)) $name = trim($name, "_");
  929. }
  930. $name = $this->ReplaceObjcType($name);
  931. return $name;
  932. }
  933. // Convert a method return type to Pascal
  934. function ConvertReturnType ($type, $pointertype) {
  935. $type = $this->ReplaceGarbageCollectorHints($type, $null);
  936. $type = $this->ReplaceRemoteMessagingModifiers($type, $null);
  937. // format the return type to make sure it's clean
  938. $type = $this->FormatObjcType($type, $null_modifier);
  939. // translate type to Pascal
  940. $type = $this->ReplaceObjcType($type);
  941. // incorportate pointer modifiers
  942. $type = $this->EncodePointerModifiers($type,$pointertype);
  943. return $type;
  944. }
  945. /**
  946. * USER PATCHES
  947. */
  948. function InsertPatches ($header) {
  949. $path = "$this->root/patches/".$header["name_clean"].".patch";
  950. if ($handle = @fopen($path, "r")) {
  951. $text = file_get_contents($path);
  952. $this->PrintOutput(0, $text);
  953. fclose($handle);
  954. }
  955. }
  956. function HeaderContainsPatch ($header) {
  957. if ($handle = @fopen("$this->root/patches/".$header["name_clean"].".patch", "r")) {
  958. fclose($handle);
  959. return true;
  960. }
  961. }
  962. /**
  963. * COMMENTS
  964. */
  965. // Trim a comment string
  966. function TrimComment ($comment, $preserve_line_endings) {
  967. // trim line breaks
  968. if (!$preserve_line_endings) $comment = trim($comment, "\n");
  969. // remove remains of C comments
  970. $comment = eregi_replace("^[!*/ ]+", "", $comment);
  971. $comment = eregi_replace("[!*/ ]+$", "", $comment);
  972. // remove all Pascal comment characters to prevent nesting
  973. $comment = str_replace(array("{", "}"), "", $comment);
  974. //print("$comment\n");
  975. return $comment;
  976. }
  977. // Builds comments from $line spaning multiple lines or at the end of lines
  978. // Returns a terminated comment string or a fragment in $fragment for futher processing
  979. function BuildComment ($line, $file) {
  980. // set the current comment header being parsed
  981. $this->comment_header = $file;
  982. // comment parsing is off, bail!
  983. if (!$this->parse_comments) return;
  984. // single-line comment at start of line
  985. if (eregi("^[[:space:]]*//(.*)", $line, $captures)) {
  986. //print("$captures[1]\n");
  987. // ignore comment
  988. foreach ($this->ignore_comments as $pattern) {
  989. if (eregi($pattern, $captures[1])) return;
  990. }
  991. $this->comment_terminated = $this->TrimComment($captures[1], false);
  992. return;
  993. } elseif (eregi("[[:space:]]+//(.*)", $line, $captures)) { // single-line comments at end of line
  994. //print("$captures[1]\n");
  995. // ignore comment
  996. foreach ($this->ignore_comments as $pattern) {
  997. if (eregi($pattern, $captures[1])) return;
  998. }
  999. $this->comment_eol = $captures[1];
  1000. return;
  1001. }
  1002. // multi-line comments terminated
  1003. if (eregi("/\*(.*)\*/", $line, $captures)) {
  1004. //print("$captures[1]\n");
  1005. // ignore comment
  1006. foreach ($this->ignore_comments as $pattern) {
  1007. if (eregi($pattern, $captures[1])) return;
  1008. }
  1009. $this->comment_terminated = $this->TrimComment($captures[1], false);
  1010. return;
  1011. }
  1012. // terminate comment fragment
  1013. if ($this->comment_fragment_open) {
  1014. if (eregi("(.*)\*/", $line, $captures)) {
  1015. // append fragment
  1016. $comment = $this->TrimComment($captures[1], false);
  1017. if ($comment) $this->comment_fragment .= $comment;
  1018. // closed comment block
  1019. if (!$captures[1]) {
  1020. $this->comment_block_closed = true;
  1021. }
  1022. // set terminated comment with fragment
  1023. $this->comment_terminated = $this->comment_fragment;
  1024. // add extra line break for comment blocks
  1025. if (($this->comment_block_open) && ($this->comment_block_closed)) {
  1026. // ??? the printing will cut this out if we add line endings!
  1027. $this->comment_terminated = "$this->comment_fragment";
  1028. }
  1029. $this->comment_fragment = null;
  1030. $this->comment_fragment_open = false;
  1031. $this->comment_block_open = false;
  1032. $this->comment_block_closed = false;
  1033. return;
  1034. } else {
  1035. // build the fragment
  1036. $comment = $this->TrimComment($line, true);
  1037. // ignore comment and stop building fragment
  1038. foreach ($this->ignore_comments as $pattern) {
  1039. if (eregi($pattern, $comment)) {
  1040. $this->comment_fragment = null;
  1041. $this->comment_fragment_open = false;
  1042. $this->comment_block_open = false;
  1043. $this->comment_block_closed = false;
  1044. return;
  1045. }
  1046. }
  1047. if (($this->comment_fragment_previous != $line) && ($comment)) $this->comment_fragment .= $comment."\n";
  1048. $this->comment_fragment_previous = $line;
  1049. return;
  1050. }
  1051. }
  1052. // start comment fragment
  1053. if (eregi("/\*(.*)", $line, $captures)) {
  1054. $this->comment_terminated = null;
  1055. // ignore comment
  1056. foreach ($this->ignore_comments as $pattern) {
  1057. if (eregi($pattern, $captures[1])) return;
  1058. }
  1059. $this->comment_fragment_open = true;
  1060. $this->comment_fragment = "";
  1061. $this->comment_block_open = true;
  1062. $this->comment_block_closed = false;
  1063. // prevent against empty comments
  1064. if ((!$captures[1]) || ($captures[1] == "\n")) {
  1065. $this->comment_block_open = true;
  1066. return;
  1067. }
  1068. // append the line if valid
  1069. $comment = $this->TrimComment($captures[1], false);
  1070. if ($comment) $this->comment_fragment .= $comment;
  1071. return;
  1072. }
  1073. }
  1074. // Resets current comment references
  1075. function ResetComment () {
  1076. $this->comment_fragment = null;
  1077. $this->comment_eol = null;
  1078. $this->comment_terminated = null;
  1079. $this->comment_block_open = false;
  1080. $this->comment_block_closed = false;
  1081. }
  1082. // Checks/sets if a comment is a duplicate in the file
  1083. function CheckDuplicateComment ($comment) {
  1084. if ((@!in_array($comment, $this->dump[$this->comment_header]["comments"])) && ($comment)) {
  1085. $this->dump[$this->comment_header]["comments"][] = $comment;
  1086. return true;
  1087. } else {
  1088. return false;
  1089. }
  1090. }
  1091. // Appends the eol comment to the output and clears the reference
  1092. function AppendEOLComment () {
  1093. if ($this->comment_eol) {
  1094. $comment = " // ".$this->TrimComment($this->comment_eol, false);
  1095. $this->comment_eol = "";
  1096. return $comment;
  1097. } else {
  1098. return null;
  1099. }
  1100. }
  1101. // Inserts the recently terminated comment to the output on a new line and clears the reference
  1102. function InsertCurrentComment () {
  1103. if (($this->comment_terminated) && ($this->CheckDuplicateComment($this->comment_terminated))) {
  1104. if ($this->comment_terminated != $this->comment_previous) $comment = "$this->comment_break{".$this->comment_padding_left.$this->TrimComment($this->comment_terminated, false).$this->comment_padding_right."}";
  1105. $this->comment_previous = $this->comment_terminated;
  1106. $this->comment_terminated = "";
  1107. return $comment;
  1108. } else {
  1109. return null;
  1110. }
  1111. }
  1112. // Appends the recently terminated comment to a recipient and clears the reference
  1113. function AppendCurrentComment (&$recipient) {
  1114. if (($this->comment_terminated) && ($this->CheckDuplicateComment($this->comment_terminated)) && (gettype($recipient) == "array")) {
  1115. if ($this->comment_terminated != $this->comment_previous) $comment = "$this->comment_break{".$this->comment_padding_left.$this->TrimComment($this->comment_terminated, false).$this->comment_padding_right."}";
  1116. $recipient[] = $comment;
  1117. //if (is_array($recipient)) $recipient[] = $comment;
  1118. //if (is_string($recipient)) $recipient .= $comment;
  1119. $this->comment_previous = $this->comment_terminated;
  1120. $this->comment_terminated = "";
  1121. }
  1122. }
  1123. // Removes all comments from a line
  1124. function RemoveComments ($line) {
  1125. // remove single-line comments
  1126. $line = eregi_replace("[[:space:]]+//(.*)", "", $line);
  1127. // remove multi-line comments /* ... */
  1128. $line = eregi_replace("/\*.*\*/", "", $line);
  1129. return $line;
  1130. }
  1131. /**
  1132. * PRINTING METHODS
  1133. */
  1134. function PrintClass ($class) {
  1135. $this->PrintOutput(0, "");
  1136. $this->PrintOutput(0, "{ ".$class["name"]." }");
  1137. if ($class["comment"]) $this->PrintOutput(0, $class["comment"]);
  1138. //print_r($class["methods"]);
  1139. // print super class or protocol which the class conforms to
  1140. if ($class["adopts"]) {
  1141. if ($class["super"]) {
  1142. $this->PrintOutput(1, $class["name"]." = objcclass external (".$class["super"].", ".$class["adopts"].")");
  1143. } else {
  1144. $this->PrintOutput(1, $class["name"]." = objcclass external (".$class["adopts"].")");
  1145. }
  1146. } elseif ($class["super"]) {
  1147. $this->PrintOutput(1, $class["name"]." = objcclass external (".$class["super"].")");
  1148. } else {
  1149. $this->PrintOutput(1, $class["name"]." = objcclass external");
  1150. }
  1151. // print instance variables
  1152. if ($class["ivars"]) {
  1153. $this->PrintOutput(1, "private");
  1154. foreach ($class["ivars"] as $ivar) {
  1155. $this->PrintOutput(2, $ivar);
  1156. }
  1157. }
  1158. // print methods (public)
  1159. $this->PrintOutput(2, "");
  1160. $this->PrintOutput(1, "public");
  1161. // print class-level methods
  1162. if ($class["methods"]) {
  1163. foreach ($class["methods"] as $method) {
  1164. if ($method["comment"]) $this->PrintOutput(2, $method["comment"]);
  1165. if ($method["documentation"]) $this->PrintOutput(2, $method["documentation"]);
  1166. $this->PrintOutput(2, $method["blocks_disable_comment"].$method["def"]." message '".$method["objc_method"]."';".$method["deprecated"]);
  1167. }
  1168. }
  1169. // print adopted protocol methods
  1170. if (count($class["protocols"]) > 0) {
  1171. $this->PrintOutput(0, "");
  1172. $this->PrintOutput(2, "{ Adopted Protocols }");
  1173. //print_r($this->dump["protocols"]);
  1174. foreach ($class["protocols"] as $name) {
  1175. if ($this->dump["protocols"][$name]) {
  1176. foreach ($this->dump["protocols"][$name] as $method) {
  1177. if (!$this->ClassContainsMethod($class, $method)) $this->PrintOutput(2, $method["blocks_disable_comment"].$method["def"]);
  1178. }
  1179. }
  1180. }
  1181. }
  1182. $this->PrintOutput(1, "end;");
  1183. }
  1184. function PrintCategory ($class, $category) {
  1185. // declare real category if external
  1186. if ($category["external"]) {
  1187. $new_name = " name '".$category["external_name"]."'";
  1188. }
  1189. $category_name = $category["name"].$this->category_suffix;
  1190. $this->PrintOutput(0, "");
  1191. $this->PrintOutput(0, "{ $category_name }");
  1192. if ($category["comment"]) $this->PrintOutput(0, $category["comment"]);
  1193. // print super class or protocol which the class conforms to
  1194. $this->PrintOutput(1, "$category_name = objccategory external$new_name (".$category["super"].")");
  1195. // print methods
  1196. if ($category["methods"]) {
  1197. foreach ($category["methods"] as $method) {
  1198. if ($method["comment"]) $this->PrintOutput(2, $method["comment"]);
  1199. $this->PrintOutput(2, $method["blocks_disable_comment"].$method["def"]." message '".$method["objc_method"]."';".$method["deprecated"]);
  1200. }
  1201. }
  1202. $this->PrintOutput(1, "end;");
  1203. }
  1204. function PrintHeader ($header) {
  1205. global $version;
  1206. //print_r($header);
  1207. //print_r($this->dump["categories"]);
  1208. // open the output file if we not printing to terminal
  1209. if (!$this->show) {
  1210. if ($this->merge_headers) {
  1211. $this->output = fopen($header["path_merge"], "w+");
  1212. } else {
  1213. $this->output = fopen($header["path"], "w+");
  1214. }
  1215. }
  1216. $this->PrintOutput(0, "{ Parsed from ".ucfirst($header["framework"]).".framework ".$header["name"]." }");
  1217. //$date = @date("D M j G:i:s T Y");
  1218. //$this->PrintOutput(0, "{ Version: $version - $date }");
  1219. //$this->PrintOutput(0, "");
  1220. $macro = strtoupper(substr($header["name"], 0, (strripos($header["name"], "."))));
  1221. $this->PrintOutput(0, "");
  1222. $this->PrintOutput(0, "{\$ifdef TYPES}");
  1223. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_T}");
  1224. $this->PrintOutput(0, "{\$define $macro"."_PAS_T}");
  1225. $this->PrintTypes($header, false);
  1226. $this->PrintOutput(0, "");
  1227. $this->PrintOutput(0, "{\$endif}");
  1228. $this->PrintOutput(0, "{\$endif}");
  1229. $this->PrintOutput(0, "");
  1230. $this->PrintOutput(0, "{\$ifdef RECORDS}");
  1231. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_R}");
  1232. $this->PrintOutput(0, "{\$define $macro"."_PAS_R}");
  1233. // Records from types
  1234. $this->PrintRecords($header);
  1235. $this->PrintOutput(0, "");
  1236. $this->PrintOutput(0, "{\$endif}");
  1237. $this->PrintOutput(0, "{\$endif}");
  1238. $this->PrintOutput(0, "");
  1239. $this->PrintOutput(0, "{\$ifdef FUNCTIONS}");
  1240. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_F}");
  1241. $this->PrintOutput(0, "{\$define $macro"."_PAS_F}");
  1242. $this->PrintFunctions($header);
  1243. $this->PrintOutput(0, "");
  1244. $this->PrintOutput(0, "{\$endif}");
  1245. $this->PrintOutput(0, "{\$endif}");
  1246. $this->PrintOutput(0, "");
  1247. $this->PrintOutput(0, "{\$ifdef EXTERNAL_SYMBOLS}");
  1248. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_S}");
  1249. $this->PrintOutput(0, "{\$define $macro"."_PAS_S}");
  1250. $this->PrintExternalSymbols($header);
  1251. $this->PrintOutput(0, "");
  1252. $this->PrintOutput(0, "{\$endif}");
  1253. $this->PrintOutput(0, "{\$endif}");
  1254. // insert user patches
  1255. if ($this->HeaderContainsPatch($header)) {
  1256. $this->PrintOutput(0, "");
  1257. $this->PrintOutput(0, "{\$ifdef USER_PATCHES}");
  1258. //$this->PrintOutput(0, "{\$ifndef $macro"."_PAS_PATCH}");
  1259. //$this->PrintOutput(0, "{\$define $macro"."_PAS_PATCH}");
  1260. $this->InsertPatches($header);
  1261. $this->PrintOutput(0, "");
  1262. //$this->PrintOutput(0, "{\$endif}");
  1263. $this->PrintOutput(0, "{\$endif}");
  1264. }
  1265. // print class/protocol forward declarations
  1266. if (($header["classes"]) || ($header["protocols"])) {
  1267. $this->PrintOutput(0, "");
  1268. $this->PrintOutput(0, "{\$ifdef FORWARD}");
  1269. if ($header["protocols"]) {
  1270. foreach ($header["protocols"] as $protocol) $this->PrintOutput(1, $protocol["name"]."$this->protocol_suffix = objcprotocol;");
  1271. }
  1272. if ($header["classes"]) {
  1273. foreach ($header["classes"] as $class) {
  1274. if ($class["name"]) {
  1275. $this->PrintOutput(1, $class["name"]." = objcclass;");
  1276. $this->PrintOutput(1, $class["name"].$this->class_pointer_suffix." = ^".$class["name"].";");
  1277. // for consistency also offer Ptr-name variant
  1278. $this->PrintOutput(1, $class["name"]."Ptr = ".$class["name"].$this->class_pointer_suffix.";");
  1279. }
  1280. }
  1281. }
  1282. $this->PrintOutput(0, "");
  1283. $this->PrintOutput(0, "{\$endif}");
  1284. }
  1285. // print classes
  1286. if ($header["classes"]) {
  1287. $this->PrintOutput(0, "");
  1288. $this->PrintOutput(0, "{\$ifdef CLASSES}");
  1289. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_C}");
  1290. $this->PrintOutput(0, "{\$define $macro"."_PAS_C}");
  1291. foreach ($header["classes"] as $class) {
  1292. if ($class["name"]) $this->PrintClass($class);
  1293. }
  1294. if (count($header["categories"]) > 0) {
  1295. foreach ($header["categories"] as $category) {
  1296. $this->PrintCategory($class, $category);
  1297. }
  1298. }
  1299. $this->PrintOutput(0, "");
  1300. $this->PrintOutput(0, "{\$endif}");
  1301. $this->PrintOutput(0, "{\$endif}");
  1302. }
  1303. // print protocols
  1304. if ($header["protocols"]) {
  1305. $this->PrintOutput(0, "{\$ifdef PROTOCOLS}");
  1306. $this->PrintOutput(0, "{\$ifndef $macro"."_PAS_P}");
  1307. $this->PrintOutput(0, "{\$define $macro"."_PAS_P}");
  1308. foreach ($header["protocols"] as $protocol) {
  1309. $this->PrintOutput(1, "");
  1310. $this->PrintOutput(0, "{ ".$protocol["name"]." Protocol }");
  1311. if ($protocol["comment"]) $this->PrintOutput(0, $protocol["comment"]);
  1312. $this->PrintOutput(1, $protocol["name"]."$this->protocol_suffix = objcprotocol external name '".$protocol["name"]."'");
  1313. // print methods
  1314. if ($protocol["methods"]) {
  1315. $section="";
  1316. foreach ($protocol["methods"] as $name => $method) {
  1317. // print the required/optional section
  1318. if ($method["section"] != $section) {
  1319. $section = $method["section"];
  1320. $this->PrintOutput(1, $section);
  1321. }
  1322. if ($method["comment"]) $this->PrintOutput(2, $method["comment"]);
  1323. $this->PrintOutput(2, $method["blocks_disable_comment"].$method["def"]." message '".$method["objc_method"]."';".$method["deprecated"]);
  1324. }
  1325. }
  1326. $this->PrintOutput(1, "end;");
  1327. }
  1328. $this->PrintOutput(0, "{\$endif}");
  1329. $this->PrintOutput(0, "{\$endif}");
  1330. }
  1331. }
  1332. // Prints all externally defined symbols
  1333. function PrintExternalSymbols ($header) {
  1334. if (!$this->dump[$header["name"]]["types"]) return;
  1335. foreach ($this->dump[$header["name"]]["types"] as $key => $type_array) {
  1336. // External string constants
  1337. if ($key == "string_constant") {
  1338. $this->PrintOutput(0, "");
  1339. $this->PrintOutput(0, "{ External string constants }");
  1340. $this->PrintOutput(0, "var");
  1341. foreach ($type_array as $type) $this->PrintOutput(1, $type);
  1342. }
  1343. if ($key == "external_symbol") {
  1344. $this->PrintOutput(0, "");
  1345. $this->PrintOutput(0, "{ External symbols }");
  1346. $this->PrintOutput(0, "var");
  1347. foreach ($type_array as $type) $this->PrintOutput(1, $type);
  1348. }
  1349. }
  1350. }
  1351. // Prints all types in the header
  1352. function PrintTypes ($header) {
  1353. if (!$this->dump[$header["name"]]["types"]) return;
  1354. foreach ($this->dump[$header["name"]]["types"] as $key => $type_array) {
  1355. $section_printed = false;
  1356. // External defines
  1357. if ($key == "defines") {
  1358. foreach ($type_array as $type) {
  1359. if (!$section_printed) {
  1360. $this->PrintOutput(0, "");
  1361. $this->PrintOutput(0, "{ Defines }");
  1362. $this->PrintOutput(0, "const");
  1363. $section_printed = true;
  1364. }
  1365. $this->PrintOutput(1, $type);
  1366. }
  1367. }
  1368. // Enumerations
  1369. if ($key == "enums") {
  1370. $this->PrintOutput(0, "");
  1371. $this->PrintOutput(0, "{ Constants }");
  1372. foreach ($type_array as $block) {
  1373. $section_printed = false;
  1374. foreach ($block as $type) {
  1375. if (!$section_printed) {
  1376. $this->PrintOutput(0, "");
  1377. $this->PrintOutput(0, "const");
  1378. $section_printed = true;
  1379. }
  1380. $this->PrintOutput(1, $type);
  1381. }
  1382. }
  1383. }
  1384. // Typedefs
  1385. if (($key == "typedef") || ($key == "named_enums")) {
  1386. foreach ($type_array as $type) {
  1387. if (!$section_printed) {
  1388. $this->PrintOutput(0, "");
  1389. $this->PrintOutput(0, "{ Types }");
  1390. $this->PrintOutput(0, "type");
  1391. $section_printed = true;
  1392. }
  1393. $this->PrintOutput(1, $type);
  1394. }
  1395. }
  1396. // CallBacks
  1397. if ($key == "callbacks") {
  1398. foreach ($type_array as $name => $type) {
  1399. if (!$section_printed) {
  1400. $this->PrintOutput(0, "");
  1401. $this->PrintOutput(0, "{ Callbacks }");
  1402. $this->PrintOutput(0, "type");
  1403. $section_printed = true;
  1404. }
  1405. $this->PrintOutput(1, "$name = $type");
  1406. }
  1407. }
  1408. }
  1409. }
  1410. // Prints all records in the header
  1411. function PrintRecords ($header) {
  1412. if (!$this->dump[$header["name"]]["types"]) return;
  1413. foreach ($this->dump[$header["name"]]["types"] as $key => $type_array) {
  1414. // Structures
  1415. if ($key == "structs") {
  1416. $this->PrintOutput(0, "");
  1417. $this->PrintOutput(0, "{ Records }");
  1418. foreach ($type_array as $type) {
  1419. $this->PrintOutput(0, "type");
  1420. $this->PrintOutput(1, $type);
  1421. }
  1422. }
  1423. }
  1424. }
  1425. // Prints all external functions in the header
  1426. function PrintFunctions ($header) {
  1427. if (!$this->dump[$header["name"]]["types"]) return;
  1428. foreach ($this->dump[$header["name"]]["types"] as $key => $type_array) {
  1429. if ($key == "functions") {
  1430. $this->PrintOutput(0, "");
  1431. $this->PrintOutput(0, "{ Functions }");
  1432. foreach ($type_array as $type) $this->PrintOutput(0, $type);
  1433. }
  1434. }
  1435. }
  1436. // Prints all classes from the header in reference format (not for compiling)
  1437. function PrintHeaderReference ($header, $path) {
  1438. $this->output = fopen($path, "w+");
  1439. //$this->PrintOutput(0, "{ ".ucfirst($header["framework"]).".framework ".$header["name"]." }");
  1440. $this->PrintOutput(0, "unit ".$header["name_clean"].";");
  1441. $this->PrintOutput(0, "interface");
  1442. $this->PrintOutput(0, "uses");
  1443. $this->PrintOutput(1, "ctypes, objc, MacOSAll, AnonClassDefinitions;");
  1444. if ($header["classes"]) {
  1445. foreach ($header["classes"] as $class) {
  1446. $this->PrintOutput(0, "");
  1447. $this->PrintOutput(0, "type");
  1448. $this->PrintOutput(1, $class["name"]."Ref = ".$this->objc_id.";");
  1449. $this->PrintOutput(1, $class["name"]."Ptr = Pointer;");
  1450. }
  1451. }
  1452. // types
  1453. $this->PrintTypes($header);
  1454. $this->PrintRecords($header);
  1455. $this->PrintFunctions($header);
  1456. $this->PrintExternalSymbols($header);
  1457. if ($header["classes"]) {
  1458. foreach ($header["classes"] as $class) {
  1459. if (in_array($class["name"], $this->cocoa_classes)) {
  1460. $this->PrintOutput(0, "");
  1461. $this->PrintOutput(0, "type");
  1462. $this->PrintOutput(1, $class["name"]." = object(".$class["super"].")");
  1463. // print class-level methods
  1464. if (count($class["methods"]) > 0) {
  1465. $this->PrintOutput(0, "");
  1466. foreach ($class["methods"] as $method) {
  1467. $this->PrintOutput(2, $method["blocks_disable_comment"].$method["def"]);
  1468. }
  1469. }
  1470. // print category-level methods
  1471. if (count($class["categories"]) > 0) {
  1472. foreach ($class["categories"] as $name => $category) {
  1473. $this->PrintOutput(0, "");
  1474. $this->PrintOutput(2, "{ Category: $name }");
  1475. if ($category["methods"]) {
  1476. foreach ($category["methods"] as $method) {
  1477. $this->PrintOutput(2, $method["blocks_disable_comment"].$method["def"]);
  1478. }
  1479. }
  1480. }
  1481. }
  1482. $this->PrintOutput(1, "end;");
  1483. }
  1484. }
  1485. }
  1486. // print procedural protocols
  1487. if ($header["protocols"]) {
  1488. foreach ($header["protocols"] as $protocol) {
  1489. if ($protocol["methods"]) {
  1490. $this->PrintOutput(0, "");
  1491. $this->PrintOutput(0, "{ Protocol: ".$protocol["name"]." }");
  1492. foreach ($protocol["methods"] as $name => $method) {
  1493. if ($method["kind"] != "constructor") {
  1494. $this->PrintProtocolDeclaration($protocol, $method);
  1495. }
  1496. }
  1497. }
  1498. }
  1499. }
  1500. $this->PrintOutput(0, "");
  1501. $this->PrintOutput(0, "implementation");
  1502. $this->PrintOutput(0, "end.");
  1503. }
  1504. // Merges two headers by difference
  1505. function MergeHeader ($header) {
  1506. /*
  1507. diff QLPreviewPanel_ORIGINAL.inc QLPreviewPanel_UPDATED.inc > diff.patch
  1508. patch QLPreviewPanel_ORIGINAL.inc -i diff.patch -o output.txt
  1509. patch QLPreviewPanel.inc -i header.patch -o QLPreviewPanel.inc.merged
  1510. also add -section="types,classes" which only prints into the sections and copies text from
  1511. the previous version if not specified
  1512. */
  1513. // set the paths to use
  1514. $path = $header["path"];
  1515. $patch = "$this->root$this->out/$this->framework/header.patch";
  1516. $merged = $header["path_merge"];
  1517. // create a patch using diff
  1518. exec("/usr/bin/diff \"$path\" \"$merged\" > \"$patch\"");
  1519. // parse the diff file by mode
  1520. $lines = file($patch);
  1521. $content = "";
  1522. $section_lines = null;
  1523. $section_part = null;
  1524. $count = 0;
  1525. $section_id = 0;
  1526. //print_r($lines);
  1527. if ($lines) {
  1528. foreach ($lines as $line) {
  1529. $count++;
  1530. // remove line endings to aid regex
  1531. $line = trim($line, "\n");
  1532. // parse section
  1533. if (($section_lines) && (!eregi("^[0-9]+", $line))) {
  1534. // append line to section
  1535. $section_lines[] = "$line\n";
  1536. // the section id is adding
  1537. if (eregi("^>", $line)) $section_part[$section_id] = true;
  1538. if (eregi("^<", $line)) $section_part[$section_id] = 0;
  1539. // section is changing type
  1540. if ($line == "---") {
  1541. $section_id++;
  1542. continue;
  1543. }
  1544. }
  1545. // line is a new section or eof
  1546. if ((eregi("^[0-9]+", $line)) || ($count == count($lines))) {
  1547. // append the section to the content
  1548. // we only accept sections where the first part (before ---) contains additions ">"
  1549. //print_r($section_part);
  1550. if (($section_part[0]) && (!$section_part[1])) $content .= implode($section_lines);
  1551. // init the new section
  1552. $section_lines = array();
  1553. $section_lines[] = "$line\n";
  1554. $section_id = 0;
  1555. $section_part = array(null, null);
  1556. continue;
  1557. }
  1558. }
  1559. //print($content);
  1560. // write the parsed patch back to the file
  1561. if ($content) {
  1562. file_put_contents($patch, $content);
  1563. } else {
  1564. $content = null;
  1565. }
  1566. }
  1567. // patch the header to the merged file
  1568. if ($content) {
  1569. exec("/usr/bin/patch \"$path\" -i \"$patch\" -o \"$merged\" ");
  1570. // swap the contents of the merged file with the original
  1571. file_put_contents($path, file_get_contents($merged));
  1572. }
  1573. // delete the temporary files
  1574. unlink($patch);
  1575. unlink($merged);
  1576. }
  1577. function PrintGlobalClassInfo($all_classes, $defined_classes, $anon_classes) {
  1578. // add all classes as anonymous external classes to a separate unit.
  1579. // They will be overridden by the actual definitions in the translated
  1580. // headers part of the main unit, but this way they can appear as record
  1581. // field types and as callback parameters
  1582. // open the output file if we not printing to terminal
  1583. if (!$this->show) {
  1584. $this->output = fopen("$this->root$this->out/AnonClassDefinitions".ucfirst($this->framework).".pas", "w+");
  1585. }
  1586. $this->PrintOutput(0, "{ Parsed from ".ucfirst($this->framework)." }");
  1587. $date = @date("D M j G:i:s T Y");
  1588. $this->PrintOutput(0, "");
  1589. // allows parameter names conflicting with field names
  1590. $this->PrintOutput(0, "{\$mode delphi}");
  1591. $this->PrintOutput(0, "{\$modeswitch objectivec1}");
  1592. // enables "external" after the semi-colon
  1593. $this->PrintOutput(0, "{\$modeswitch cvar}");
  1594. $this->PrintOutput(0, "");
  1595. $this->PrintOutPut(0,"unit AnonClassDefinitions".ucfirst($this->framework).";");
  1596. $this->PrintOutput(0, "");
  1597. $this->PrintOutput(0, "interface");
  1598. $this->PrintOutput(0, "");
  1599. $this->PrintOutput(0, "type");
  1600. foreach ($all_classes as $class) {
  1601. $this->PrintOutput(1, $class." = objcclass external;");
  1602. }
  1603. $this->PrintOutput(0, "");
  1604. $this->PrintOutput(0, "implementation");
  1605. $this->PrintOutput(0, "");
  1606. $this->PrintOutput(0, "end.");
  1607. // Now all anonymous external classes that have no real definition to an
  1608. // include file that is added to the main unit. This way it is possible
  1609. // to declare variables of these types in user programs without having to
  1610. // include the unit above will all anonymous classes (should not be used)
  1611. // open the output file if we not printing to terminal
  1612. if (!$this->show) {
  1613. $this->output = fopen("$this->root$this->out/$this->framework/AnonIncludeClassDefinitions".ucfirst($this->framework).".inc", "w+");
  1614. }
  1615. $this->PrintOutput(0, "{ Parsed from ".ucfirst($this->framework)." }");
  1616. $date = @date("D M j G:i:s T Y");
  1617. // add all classes as anonymous external classes. They will be overridden
  1618. // by the actual definitions in the translated headers, but this way they
  1619. // can appear as record field types and as callback parameters
  1620. $first = true;
  1621. foreach ($anon_classes as $class) {
  1622. if (!in_array($class,$defined_classes)) {
  1623. if ($first) {
  1624. $this->PrintOutput(0, "type");
  1625. $first = false;
  1626. }
  1627. $this->PrintOutput(1, $class." = objcclass external;");
  1628. }
  1629. }
  1630. }
  1631. // Prints all headers parsed
  1632. function PrintAllHeaders ($output_path, $ignore_output, $only_files, $print_header_references) {
  1633. //print("• Printing ".count($this->dump)." headers...\n");
  1634. foreach ($this->dump as $file => $header) {
  1635. // the framework is set to not print, ignore the header
  1636. if (!$this->frameworks[$header["framework"]]["print"]) continue;
  1637. if (eregi("^[a-zA-Z0-9]+\.h", $file)) {
  1638. // ignore these files
  1639. if (@in_array($header["path_partial"], $ignore_output)) continue;
  1640. // only parse these files
  1641. if ((@count($only_files) > 0) && (@!in_array($header["name"], $only_files))) continue;
  1642. $name_clean = substr($file, 0, (strripos($file, ".")));
  1643. // assign output path
  1644. if ($output_path != "") {
  1645. $header["path"] = $output_path."/".$name_clean.".inc";
  1646. }
  1647. // print the header
  1648. $this->PrintHeader($header);
  1649. // merge the headers
  1650. if (($this->merge_headers) && (!$this->show)) {
  1651. $this->MergeHeader($header);
  1652. }
  1653. if ($print_header_references) $this->PrintHeaderReference($header, $this->root.$this->out."/reference/".$name_clean.".pas");
  1654. if (!$this->show) print("* Printed $name_clean.h to ".$header["path"]."\n");
  1655. }
  1656. }
  1657. // print global stuff (for Obj-P: a unit containing all classes as
  1658. // anonymous external classes, so they can be used before they are
  1659. // declared)
  1660. $this->PrintGlobalClassInfo($this->cocoa_classes, $this->defined_cocoa_classes, $this->anon_cocoa_classes);
  1661. }
  1662. /**
  1663. * PRE-PARSING METHODS
  1664. */
  1665. // Parse all "pre-defined" category methods in a header
  1666. function PreparseCategoryMethods ($file) {
  1667. $contents = file_get_contents($file);
  1668. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  1669. $lines = explode("\n", $contents);
  1670. foreach ($lines as $line) {
  1671. // skip blocks
  1672. if ($this->SkipBlock($line)) continue;
  1673. // parse category
  1674. if ($got_category) {
  1675. // build method fragment
  1676. if ($method_fragment) $method_fragment .= " ".trim($line, " ");
  1677. // found method fragment termination
  1678. if (($method_fragment) && (preg_match($this->pregex_objc_method_terminate, $line))) {
  1679. $line = $method_fragment;
  1680. $method_fragment = null;
  1681. }
  1682. // found method
  1683. $method = null;
  1684. if (preg_match($this->pregex_objc_method_params, $line, $captures)) {
  1685. $method = $this->ConvertObjcMethodToPascal($current_category, $line, $captures, array(), true, "");
  1686. } elseif (preg_match($this->pregex_objc_method_no_params, $line, $captures)) {
  1687. $method = $this->ConvertObjcMethodToPascal($current_category, $line, $captures, array(), false, "");
  1688. } elseif (preg_match($this->pregex_objc_method_partial, $line, $captures)) {
  1689. $method_fragment = $line;
  1690. }
  1691. // append to classes
  1692. if (($method) && ($current_class)) {
  1693. $this->dump[$category_owner]["category_methods"][] = $method["name"];
  1694. //print($method["name"]."\n");
  1695. }
  1696. // found the end
  1697. if (ereg("^@end", $line)) $got_category = false;
  1698. }
  1699. // got category
  1700. if (eregi($this->regex_objc_category, $line, $captures)) {
  1701. $category_owner = $this->FindCategoryHeader($captures[1]);
  1702. if ($category_owner) {
  1703. $got_category = true;
  1704. $current_category = $captures[2];
  1705. $current_class = $captures[1];
  1706. } else {
  1707. $current_class = null;
  1708. }
  1709. }
  1710. }
  1711. return $this->dump[$category_owner]["category_methods"];
  1712. }
  1713. // Preparses a class for protected keywords
  1714. function PreparseClass ($lines, $line_count) {
  1715. $protected_keywords = array();
  1716. for ($i=$line_count; $i < count($lines); $i++) {
  1717. $line = $lines[$i - 1];
  1718. // skip blocks
  1719. if ($this->SkipBlock($line)) continue;
  1720. // build method fragment
  1721. if ($method_fragment) $method_fragment .= " ".trim($line, " ");
  1722. // found method fragment termination
  1723. if (($method_fragment) && (preg_match($this->pregex_objc_method_terminate, $line))) {
  1724. $line = $method_fragment;
  1725. $method_fragment = null;
  1726. }
  1727. // found method
  1728. if (preg_match($this->pregex_objc_method_params, $line, $captures)) {
  1729. $method = $this->ConvertObjcMethodToPascal($current, $line, $captures, $protected_keywords, true, "");
  1730. $this->current_class["protected_keywords"][] = strtolower($method["name"]);
  1731. } elseif (preg_match($this->pregex_objc_method_no_params, $line, $captures)) {
  1732. $method = $this->ConvertObjcMethodToPascal($current, $line, $captures, $protected_keywords, false, "");
  1733. $this->current_class["protected_keywords"][] = strtolower($method["name"]);
  1734. } elseif (preg_match($this->pregex_objc_method_partial, $line, $captures)) {
  1735. $method_fragment = $line;
  1736. }
  1737. // class ended
  1738. if (ereg("^@end", $line)) return $protected_keywords;
  1739. }
  1740. }
  1741. /**
  1742. * PARSING METHODS
  1743. */
  1744. function ParseNormalField($line, $protected_keywords, &$field_bitpacked, &$bitpacked_real_type, &$this_class_field_names) {
  1745. if (preg_match("!^\s*([^-{}*@+,<;:]*struct[^-*@+,<;:]+|[^-{}*@+,<;:]+)\b\s*(?:<([^>]*)>)?\s*([*]*)\s*(\w+)((?:\s*,\s*[^:;[]+)*)?\s*(:[0-9]+)?\s*(\[.*\])?\s*(?:__attribute__\(\(([^)]*)\)\))?\s*;!", $line, $captures)) { // regular field
  1746. // captures[1]: type name (may be multiple words, and may not be complete
  1747. // in case of an anonymous bitfield without a name)
  1748. // Curly braces are only allowed in case of structs
  1749. // captures[2]: in case of id<protocol list>, the protocol list, empty
  1750. // otherwise
  1751. // captures[3]: all pointer modifiers (*, **, ...), but only "*" and "**"
  1752. // currently handled
  1753. // captures[4]: the field name (may be last part of type in case of
  1754. // anonymous bit field, e.g. "unsigned long : 32")
  1755. // captures[5]: in case of multiple fields, the other field names (note
  1756. // that things will go wrong if some of the fields in the
  1757. // list are arrays/bitfields, that is only supported for
  1758. // a single field)
  1759. // captures[6]: bitfield specification if any, empty otherwise
  1760. // captures[7]: array specification if any, empty otherwise
  1761. // captures[8]: attributes if any, empty otherwise
  1762. if ($captures[3] != "") { // pointer type -> ok (cannot be wrongly interpreted)
  1763. $type = trim($captures[1]);
  1764. $pointertype = $captures[3];
  1765. $name = trim($captures[4]);
  1766. // print("pointer field: $name: $pointertype $type\n");
  1767. } else {
  1768. // make sure we properly interpret stuff like "unsigned int :32"
  1769. // print("regular: type = \"$captures[1]\", name = $captures[3]\n");
  1770. $pair = $this->ExtractCFieldSimpleTypeAndSingleName($captures[1]." ".$captures[4]);
  1771. $type = $pair["type"];
  1772. $name = $pair["name"];
  1773. if ($name == "") $name = $this->GetAnonBitFieldName();
  1774. // print("regular field: \"$name\": $type\n");
  1775. }
  1776. // print("field \"$name\": \"$type\", attr: $captures[8]\n");
  1777. // if we have id <protocollist> and there's only one protocol in the
  1778. // in the list, we can replace id with the protocol
  1779. if (($type == "id") && ($captures[2] != "") && !strstr(",", $captures[2])) {
  1780. // print("id<protocol>: $type -> $captures[2]Protocol\n");
  1781. $type = $captures[2]."Protocol";
  1782. }
  1783. // in case of "type field1, field2, field3", ", field2, field3" gets
  1784. // stored in othernames
  1785. $othernames = $captures[5];
  1786. $field_prefix = "";
  1787. // Multiple Objective-C fields cannot have the same name, but they
  1788. // are case-insensitive and some only differ in case (only "reserved"
  1789. // fields until now, but that can change)
  1790. while (in_array(strtolower("$field_prefix$name"),$protected_keywords)) $field_prefix.="_";
  1791. if ($this_class_field_names != null) {
  1792. while (in_array(strtolower("$field_prefix$name"),$this_class_field_names)) $field_prefix.="_";
  1793. }
  1794. if ($this->IsKeywordReserved($field_prefix.$name)) $field_prefix .= "_";
  1795. // protect the name of these fields, in case there are methods with
  1796. // the same name
  1797. $this_class_field_names[] = strtolower($field_prefix.$name);
  1798. if ($othernames != "") {
  1799. $other_lower_case_names = preg_split("/\s*,\s*/",$othernames,-1,PREG_SPLIT_NO_EMPTY);
  1800. array_walk($other_lower_case_names,strtolowerref);
  1801. // should actually also check these for conflicts and add underscores if necessary
  1802. $this_class_field_names = array_merge ($this_class_field_names, $other_lower_case_names);
  1803. }
  1804. /*
  1805. // we should also add prefixes to the other names if required, but my
  1806. // php-fu is too weak to replace the elements in original string
  1807. // efficiently, and this problem does not occur in the supported headers
  1808. foreach (explode($othernames, ",") as $othername) {
  1809. while (in_array(strtolower("$field_prefix$othername"),$protected_keywords)) $field_prefix.="_";
  1810. if ($this->IsKeywordReserved($field_prefix.$othernamename)) $field_prefix .= "_";
  1811. }
  1812. */
  1813. // remove "struct" from the type
  1814. $type = preg_replace("!(\b)struct\b!","\1",$type);
  1815. // clean/convert type
  1816. $type = $this->ReplaceObjcType($type);
  1817. $bitpacked_real_type = $type;
  1818. $type = $this->MakeFieldBitPacked($type, $line, $field_bitpacked);
  1819. // add pointer modifiers
  1820. $type = $this->EncodePointerModifiers($type,$pointertype);
  1821. $field = "$field_prefix$name$othernames: $type";
  1822. if ($captures[8] && strstr("deprecated",$captures[8])) $field .= " deprecated";
  1823. $field .= ";";
  1824. $field = $this->MakeFieldInlineArray($field, $line, $name, $type);
  1825. $field = eregi_replace("<.*>", "", $field);
  1826. return $field;
  1827. }
  1828. return "";
  1829. }
  1830. function ParseInstanceVariables ($line, &$struct, $protected_keywords, &$this_class_field_names) {
  1831. $field = null;
  1832. $field_bitpacked = false;
  1833. // print("$line\n");
  1834. // insert macros
  1835. if ($macro = $this->InsertMacroBlocks($line, $this->inside_macro_block)) {
  1836. if ($struct["valid"]) {
  1837. $struct["fields"][] = $macro;
  1838. return null;
  1839. } else {
  1840. return $macro;
  1841. }
  1842. }
  1843. // got inline struct, probably a reference to a private struct
  1844. if (eregi("[[:space:]]*struct[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+\*([a-zA-Z0-9_]+)", $line, $captures)) {
  1845. if ($struct["valid"]) {
  1846. // ??? These don't compile as inline records which I thought they did...
  1847. //$struct["fields"][] = "$captures[1] = record end;";
  1848. //$struct["fields"][] = "$captures[1]Pointer = ^$captures[1];";
  1849. $struct["fields"][] = "$captures[2]: Pointer;";
  1850. $struct["ignore"][] = "$captures[2]";
  1851. return null;
  1852. } else {
  1853. //$fields = array();
  1854. //$fields[] = "$captures[1] = record end;";
  1855. //$fields[] = "$captures[1]Ptr = ^$captures[1];";
  1856. $field = "_$captures[2]: Pointer;";
  1857. return $field;
  1858. }
  1859. }
  1860. // got struct
  1861. if (eregi("^[[:space:]]*struct.*{", $line)) {
  1862. $struct["valid"] = true;
  1863. $struct["ispsuedostruct"] = false;
  1864. $struct["fieldnames"] = array();
  1865. return null;
  1866. }
  1867. // create an anonymous struct in case we have bitpacked fields without a
  1868. // surrounding struct
  1869. if (!$struct["valid"] && preg_match("!.*:[0-9]+\s*;!", $line)) {
  1870. $struct["valid"] = true;
  1871. $struct["ispsuedostruct"] = true;
  1872. $struct["fieldnames"] = array();
  1873. }
  1874. // end of bunch of bitfields -> go to normal mode again
  1875. if ($struct["ispsuedostruct"] && !preg_match("!.*:[0-9]+\s*;!", $line)) {
  1876. $struct["name"] = "_anoninternstruct_".$this->current_header["name_clean"].$this->current_header["anoninternstrucs"];
  1877. $this->current_header["anoninternstrucs"]++;
  1878. $struct["isfinished"] = true;
  1879. // make sure the current field isn't added anymore
  1880. $struct["valid"] = false;
  1881. }
  1882. // end of struct
  1883. if (eregi("^[[:space:]]*}[[:space:]]*([a-zA-Z_0-9]+);", $line, $captures)) {
  1884. $struct["name"] = "_".trim($captures[1], " ");
  1885. //print_r($struct);
  1886. $struct["isfinished"] = true;
  1887. return "struct";
  1888. }
  1889. // set field prefix to protect scope
  1890. if (!$struct["valid"]) $field_prefix = "_";
  1891. // remove null-defined macros:
  1892. $line = str_ireplace($this->null_macros, "", $line);
  1893. // replace garbage collector hints in the field
  1894. $line = $this->ReplaceGarbageCollectorHints($line, $garbage_collector_hint);
  1895. if (preg_match($this->pregex_function_pointer, $line, $captures)) { // function pointer
  1896. // print("function pointer: $line\n");
  1897. $field =$this->ParseFunctionDeclaration($captures[1], $captures[2], $captures[3], $captures[4], false, "");
  1898. } else {
  1899. if (!$struct["valid"])
  1900. $field = $this->ParseNormalField($line,$protected_keywords,$field_bitpacked,$bitpacked_real_type,$this_class_field_names);
  1901. else
  1902. // don't register the names of fields of embedded structs and field names of the current
  1903. // class, but do track them for the current struct as there may be conflicts due to
  1904. // Pascal's case-insensitivity
  1905. $field = $this->ParseNormalField($line,$protected_keywords,$field_bitpacked,$bitpacked_real_type,$struct["fieldnames"]);
  1906. }
  1907. // mark the field as having a garbage collector field
  1908. if ($garbage_collector_hint) $field = "$field {garbage collector: $garbage_collector_hint }";
  1909. // return field
  1910. if ($struct["valid"]) {
  1911. if (!$struct["bitpacked"]) $struct["bitpacked_first_type"] = $bitpacked_real_type;
  1912. if ($field_bitpacked) $struct["bitpacked"] = true;
  1913. $struct["fields"][] = $field;
  1914. } else {
  1915. return $field;
  1916. }
  1917. }
  1918. // Parses a struct field into a list
  1919. function ParseStructList ($line, $input, $name, $type) {
  1920. $field = "";
  1921. $list = explode(",", $input);
  1922. if (count($list) > 1) {
  1923. $field = " ";
  1924. foreach ($list as $key) {
  1925. $key = trim($key, " ");
  1926. $field .= "$key, ";
  1927. }
  1928. $field = rtrim($field, ", ");
  1929. $field .= ": $type;\n";
  1930. } else {
  1931. $field = " $name: $type;";
  1932. $field = $this->MakeFieldInlineArray($field, $line, $name, $type)."\n";
  1933. }
  1934. return $field;
  1935. }
  1936. // Parses $line into a function declaration string. Handles both
  1937. // function pointers (isexternfunc = false) and external functions
  1938. // (isexternfunc = true)
  1939. function ParseFunctionDeclaration($rettypestr, $retpointertypestr, $funcname, $parastr, $isexternfunc, $deprecatedmods) {
  1940. if ($deprecatedmods != "") $deprecatedmods .= ";";
  1941. if ($this->IsKeywordReserved($funcname)) $funcname .= "_";
  1942. $rettype = trim(str_replace_word("const","",$rettypestr));
  1943. $rettype = $this->ReplaceObjcType($rettype);
  1944. $rettype = $this->EncodePointerModifiers($rettype,$retpointertypestr);
  1945. $params = $this->ConvertCParamsPascal($parastr);
  1946. if ($rettype == "void")
  1947. $result = "procedure ";
  1948. else
  1949. $result = "function ";
  1950. // if no name specified, the caller will add it
  1951. if ($funcname != "") {
  1952. if (!$isexternfunc)
  1953. $result = "$funcname: " . $result;
  1954. else
  1955. $result .= $funcname;
  1956. }
  1957. if ($params != "") $params = "(" . $params . ")";
  1958. if ($rettype == "void")
  1959. $result .= $params . "; cdecl;";
  1960. else
  1961. $result .= $params . ": " . $rettype . "; cdecl;";
  1962. if ($isexternfunc)
  1963. $result .= " external;$deprecatedmods";
  1964. else
  1965. $result .= "\n";
  1966. return $result;
  1967. }
  1968. // Parses $line into the combined $struct_fields string
  1969. function ParseStructFields ($line, $protected_keywords, &$struct_fields, &$found_any_bitpacked, &$all_bitpacked, &$first_bitpacked_type) {
  1970. if (preg_match($this->pregex_function_pointer, $line, $captures)) {
  1971. $struct_fields .= " " . $this->ParseFunctionDeclaration($captures[1], $captures[2], $captures[3], $captures[4], false, "");
  1972. $all_bitpacked = false;
  1973. } else {
  1974. // better: keep for entire struct, so we can escape conflicting names due to
  1975. // case-insensitivity
  1976. $tempnewprotected = array();
  1977. $new_field = $this->ParseNormalField($line,$protected_keywords,$field_bitpacked,$bitpacked_real_type,$tempnewprotected);
  1978. // print("field: '$new_field', bitpacked: $field_bitpacked, any: $found_any_bitpacked, all: $all_bitpacked\n");
  1979. if ($new_field != "") {
  1980. $found_any_bitpacked |= $field_bitpacked;
  1981. if ($struct_fields == "") {
  1982. $all_bitpacked = $field_bitpacked;
  1983. $first_bitpacked_type = $bitpacked_real_type;
  1984. }
  1985. else $all_bitpacked &= $field_bitpacked;
  1986. $struct_fields .= " " . $new_field . $this->AppendEOLComment() . "\n";
  1987. }
  1988. // print(" after: any: $found_any_bitpacked, all: $all_bitpacked\n");
  1989. }
  1990. }
  1991. // Parse a single enum field
  1992. function ParseEnumFields ($line, $file_name, &$block_count, &$auto_increment) {
  1993. // insert macros
  1994. //if ($macro = $this->InsertMacroBlocks($line, $this->inside_macro_block)) $this->dump[$file_name]["types"]["enums"][$block_count][] = $macro;
  1995. if (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*[(]*([a-zA-Z_]+)[)]*[,]*[[:space:]]*$", $line, $captures)) { // string value
  1996. $captures[2] = trim($captures[2], ", ");
  1997. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  1998. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*[(]*([0-9-]+)[)]*[,]*[[:space:]]*$", $line, $captures)) { // integer value
  1999. $captures[2] = trim($captures[2], ", ");
  2000. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  2001. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*[(]*([0-9]+[xX]+[a-fA-F0-9]+)[)]*", $line, $captures)) { // hexadecimal value
  2002. $captures[2] = trim($captures[2], ", ");
  2003. $captures[2] = eregi_replace("^0x", "$", $captures[2]);
  2004. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  2005. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*([a-zA-Z0-9]+[[:space:]]*<<[[:space:]]*[a-zA-Z0-9]+)", $line, $captures)) { // << shl value, no ()
  2006. $captures[2] = ereg_replace("[[:space:]]?<<[[:space:]]?", " shl ", $captures[2]);
  2007. // remove integer type hints
  2008. $captures[2] = ereg_replace("([0-9]+)[UL]+([[:space:]]+)shl([[:space:]])", "\\1\\2shl\\3", $captures[2]);
  2009. $captures[2] = ereg_replace("([[:space:]])shl([[:space:]]+)([0-9]+)[UL]+", "\\1shl\\2\\3", $captures[2]);
  2010. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  2011. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*\(([a-zA-Z0-9]+[[:space:]]*<<[[:space:]]*[a-zA-Z0-9]+)\)", $line, $captures)) { // << shl value
  2012. $captures[2] = trim($captures[2], ", ");
  2013. $captures[2] = ereg_replace("[[:space:]]?<<[[:space:]]?", " shl ", $captures[2]);
  2014. // remove integer type hints
  2015. $captures[2] = ereg_replace("([0-9]+)[UL]+([[:space:]]+)shl([[:space:]])", "\\1\\2shl\\3", $captures[2]);
  2016. $captures[2] = ereg_replace("([[:space:]])shl([[:space:]]+)([0-9]+)[UL]+", "\\1shl\\2\\3", $captures[2]);
  2017. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  2018. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*[,}]*[[:space:]]*$", $line, $captures)) { // non-value
  2019. // omit lines which started nested structures.
  2020. // bad practice but the single-line regex parser can't handle them
  2021. if (!eregi("[=|]+", $line)) {
  2022. $captures[1] = trim($captures[1], ", ");
  2023. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$auto_increment.";";
  2024. $auto_increment ++;
  2025. }
  2026. }
  2027. }
  2028. // Parse external symbols, enums and typedef's from the header
  2029. function ParseHeaderTypes ($file) {
  2030. $contents = file_get_contents($file);
  2031. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2032. $any_field_bitpacked = false;
  2033. $all_fields_bitpacked = false;
  2034. // reset comments from previous parsing sections
  2035. $this->ResetComment();
  2036. $lines = explode("\n", $contents);
  2037. foreach ($lines as $line) {
  2038. // skip blocks
  2039. if ($this->SkipBlock($line)) continue;
  2040. // ignore lines
  2041. if (in_array($line, $this->ignore_lines)) continue;
  2042. // build comments
  2043. $this->BuildComment($line, $file_name);
  2044. // build macro blocks
  2045. $this->BuildMacroBlocks($line);
  2046. // garbage collector hints
  2047. $line = $this->ReplaceGarbageCollectorHints($line, $garbage_collector_hint);
  2048. // remove macros
  2049. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  2050. // remove comments
  2051. $line = $this->RemoveComments($line);
  2052. $line = trim($line, " ");
  2053. if ($got_struct) {
  2054. // insert macros
  2055. if ($macro = $this->InsertMacroBlocks($line, $this->inside_macro_block)) $struct_fields .= "$macro\n";
  2056. // collect fields
  2057. $this->ParseStructFields($line, array(), $struct_fields, $any_field_bitpacked, $all_fields_bitpacked, $first_bitpacked_type);
  2058. // got end of struct
  2059. if (ereg("^}[[:space:]]*([a-zA-Z_0-9]+);", $line, $captures)) {
  2060. if ($struct_name == "") {
  2061. $struct_name = $captures[1];
  2062. } else {
  2063. $struct_type = $captures[1];
  2064. }
  2065. // ignore this struct
  2066. if (in_array($struct_name, $this->ignore_types)) continue;
  2067. if ($all_fields_bitpacked) {
  2068. $struct = "$struct_comment$struct_name = $this->bitpacked_record_keyword\n";
  2069. $struct .= $this->BitPackedForceAlignment($first_bitpacked_type, " ", " ") . "\n";
  2070. $struct_fields = str_replace(" "," ",$struct_fields);
  2071. $struct_fields .= " );\n";
  2072. } else {
  2073. $struct = "$struct_comment$struct_name = $this->record_keyword\n";
  2074. }
  2075. $struct .= $struct_fields;
  2076. $struct .= " end$deprecatedmods;\n";
  2077. if (($struct_type) && ($struct_name != $struct_type)) {
  2078. $struct .= "$struct_type = $struct_name;\n";
  2079. }
  2080. // pointer type
  2081. $struct .= $struct_name."Ptr = ^".$struct_name.";\n";
  2082. $this->dump[$file_name]["types"]["structs"][] = $struct;
  2083. $this->dump["global_structs"][] = $struct_name;
  2084. $got_struct = false;
  2085. $any_field_bitpacked = false;
  2086. $all_fields_bitpacked = false;
  2087. }
  2088. }
  2089. // got single-line struct
  2090. if (ereg("^typedef[[:space:]]+struct[[:space:]]+{(.*)}[[:space:]]+([a-zA-Z0-9_]+)", $line, $captures)) {
  2091. $struct_name = trim($captures[2], " ");
  2092. if (!in_array($struct_name, $this->ignore_types)) {
  2093. // break the struct into lines
  2094. $single_struct_fields = "";
  2095. $fields = explode(";", $captures[1]);
  2096. $comment = $this->InsertCurrentComment();
  2097. $this->ResetComment();
  2098. // parse each line
  2099. foreach ($fields as $field) {
  2100. $field = trim($field);
  2101. $this->ParseStructFields($field.";", array(), $single_struct_fields, $any_field_bitpacked, $all_fields_bitpacked, $first_bitpacked_type);
  2102. }
  2103. // merge the fields into the definition
  2104. $struct = "$comment\n"."$struct_name = ";
  2105. if ($all_fields_bitpacked) {
  2106. $struct .= "$this->bitpacked_record_keyword\n";
  2107. $struct .= $this->BitPackedForceAlignment($first_bitpacked_type, " ", " ") . "\n";
  2108. $single_struct_fields = str_replace(" "," ",$single_struct_fields);
  2109. $single_struct_fields .= " );\n";
  2110. } else $struct .= "$this->record_keyword\n";
  2111. $struct .= $single_struct_fields;
  2112. $struct .= " end$deprecatedmods;\n";
  2113. // pointer type
  2114. $struct .= $struct_name."Ptr = ^".$struct_name.";\n";
  2115. $this->dump[$file_name]["types"]["structs"][] = $struct;
  2116. $this->dump["global_structs"][] = $struct_name;
  2117. $any_field_bitpacked = false;
  2118. $all_fields_bitpacked = false;
  2119. //print("$single_struct_fields\n");
  2120. }
  2121. // got begin of struct
  2122. } elseif (ereg("^typedef struct(.*){", $line, $captures)) {
  2123. $struct_name = trim($captures[1], " ");
  2124. if (!in_array($struct_name, $this->ignore_types)) {
  2125. $struct_type = null;
  2126. $struct_fields = "";
  2127. $struct_comment = $this->InsertCurrentComment();
  2128. $this->ResetComment();
  2129. if ($struct_comment != "") $struct_comment = "$struct_comment\n";
  2130. $got_struct = true;
  2131. }
  2132. }
  2133. // got function pointer type
  2134. if (preg_match($this->pregex_function_pointer_typedef, $line, $captures)) {
  2135. $typestr = $this->ParseFunctionDeclaration($captures[1], $captures[2], "", $captures[6], false, $deprecatedmods);
  2136. $functypename = $captures[7];
  2137. if ($functypename == "") $functypename = $captures[5];
  2138. if ($functypename == "") $functypename = $captures[4];
  2139. $this->dump[$file_name]["types"]["callbacks"][$functypename] = $typestr;
  2140. // record if it is a function type instead of a function pointer type
  2141. if ($captures[3] == "") $this->implicit_function_pointer_types[] = $functypename;
  2142. continue;
  2143. }
  2144. // #defines
  2145. $got_define = false;
  2146. if (ereg("#[[:space:]]*define[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+\(\(.*\)(.*)\)", $line, $captures)) { // named constant with type case
  2147. $got_define = true;
  2148. } elseif (ereg("#[[:space:]]*define[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+[(]*([0-9.-]+)[)]*", $line, $captures)) { //integer
  2149. $got_define = true;
  2150. }
  2151. if ($got_define) {
  2152. $define_name = $captures[1];
  2153. if (!in_array($define_name, $this->ignore_types)) {
  2154. $define_name = $this->ReplaceObjcType($define_name);
  2155. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["defines"][] = $this->InsertCurrentComment();
  2156. $this->AppendCurrentComment($this->dump[$file_name]["types"]["defines"]);
  2157. $this->dump[$file_name]["types"]["defines"][] = $define_name." = ".$captures[2].";".$this->AppendEOLComment();
  2158. } else {
  2159. $this->ResetComment();
  2160. }
  2161. }
  2162. // parse enum fields
  2163. if (($got_enum) || ($got_named_enum)) {
  2164. // print($line."\n");
  2165. $this->ParseEnumFields($line, $file_name, &$block_count, &$auto_increment);
  2166. // found the end
  2167. if (ereg("^};", $line)) $got_enum = false;
  2168. }
  2169. // ==== got inline named enum ===
  2170. if (ereg("^[[:space:]]*enum[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]*{(.*)};", $line, $captures)) {
  2171. //print("$line\n");
  2172. $enum_name = trim($captures[1], " ");
  2173. if (!in_array($enum_name, $this->ignore_types)) {
  2174. $block_count ++;
  2175. $auto_increment = 0;
  2176. // break the enum into lines
  2177. $fields = explode(",", $captures[2]);
  2178. //$this->AppendCurrentMacro($this->dump[$file_name]["types"]["enums"][$block_count]);
  2179. $this->AppendCurrentComment($this->dump[$file_name]["types"]["enums"][$block_count]);
  2180. if ($this->comment_terminated) $this->dump[$file_name]["types"]["enums"][$block_count][] = $this->InsertCurrentComment();
  2181. $this->ResetComment();
  2182. // parse each line
  2183. foreach ($fields as $field) {
  2184. $field = trim($field, " ");
  2185. $this->ParseEnumFields($field.",", $file_name, &$block_count, &$auto_increment);
  2186. }
  2187. }
  2188. continue;
  2189. }
  2190. // ==== got inline enum ===
  2191. if (ereg("^[[:space:]]*enum[[:space:]]*{(.*)};", $line, $captures)) {
  2192. //print("$line\n");
  2193. $block_count ++;
  2194. $auto_increment = 0;
  2195. // break the enum into lines
  2196. $fields = explode(",", $captures[1]);
  2197. $this->AppendCurrentComment($this->dump[$file_name]["types"]["enums"][$block_count]);
  2198. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["enums"][$block_count][] = $this->InsertCurrentComment();
  2199. $this->ResetComment();
  2200. // parse each line
  2201. foreach ($fields as $field) {
  2202. $field = trim($field, " ");
  2203. $this->ParseEnumFields($field.",", $file_name, &$block_count, &$auto_increment);
  2204. }
  2205. continue;
  2206. }
  2207. // ==== got enum ===
  2208. if (ereg("^enum", $line)) {
  2209. $got_enum = true;
  2210. $block_count ++;
  2211. $auto_increment = 0;
  2212. $this->AppendCurrentComment($this->dump[$file_name]["types"]["enums"][$block_count]);
  2213. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["enums"][$block_count][] = $this->InsertCurrentComment();
  2214. }
  2215. // terminate named enum
  2216. if ($got_named_enum) {
  2217. if (ereg("^}[[:space:]]*([a-zA-Z0-9_]+);", $line, $captures)) {
  2218. $got_named_enum = false;
  2219. $named_enum = trim($named_enum, ", \n");
  2220. $name = $captures[1];
  2221. if (!in_array($name, $this->ignore_types)) {
  2222. $this->dump[$file_name]["types"]["named_enums"][] = "$name = culong;";
  2223. $this->dump["global_types"][$name] = $name;
  2224. }
  2225. }
  2226. }
  2227. // ==== got named enum ===
  2228. if (ereg("^typedef enum {", $line)) {
  2229. $got_named_enum = true;
  2230. $named_enum = "";
  2231. $auto_increment = 0;
  2232. $block_count ++;
  2233. $this->AppendCurrentComment($this->dump[$file_name]["types"]["named_enums"][$block_count]);
  2234. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["named_enums"][] = $this->InsertCurrentComment();
  2235. }
  2236. // ==== external functions ===
  2237. // doesn't work when $this->external_string_macros is added to
  2238. // the string at initialisation time, because it can still change
  2239. // later (while loading frameworks.xml)
  2240. if (preg_match("!^(?:$this->external_string_macros)+".$this->pregex_external_function_end, $line, $captures)) {
  2241. // ignore symbols
  2242. if (in_array($captures[3], $this->ignore_types)) continue;
  2243. $typestr = $this->ParseFunctionDeclaration($captures[1], $captures[2], $captures[3], $captures[4], true, $deprecatedmods);
  2244. $this->dump[$file_name]["types"]["functions"][] = $typestr;
  2245. continue;
  2246. }
  2247. // ==== external string constant ===
  2248. if (eregi("^($this->external_string_macros)+[[:space:]]+NSString[[:space:]]*\*[[:space:]]*(const)*[[:space:]]*([a-zA-Z0-9_]+)", $line, $captures)) {
  2249. $name = $captures[3];
  2250. if (in_array($name, $this->ignore_types)) continue;
  2251. // insert comments
  2252. $this->AppendCurrentComment($this->dump[$file_name]["types"]["string_constant"]);
  2253. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["string_constant"][] = $this->InsertCurrentComment();
  2254. $this->dump[$file_name]["types"]["string_constant"][] = "$name: $this->string_macro$deprecatedmods; cvar; external;";
  2255. }
  2256. // ==== external symbol ===
  2257. if (eregi("^($this->external_string_macros)+[[:space:]]+([a-zA-Z0-9_ ]+)[[:space:]]+([a-zA-Z0-9_]+)", $line, $captures)) {
  2258. $name = $captures[3];
  2259. $type = $captures[2];
  2260. // ignore symbols
  2261. if (in_array($name, $this->ignore_types)) continue;
  2262. $type = istr_replace_word("const", "", $type);
  2263. $type = $this->ReplaceObjcType(trim($type, " "));
  2264. $this->AppendCurrentComment($this->dump[$file_name]["types"]["external_symbol"]);
  2265. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["external_symbol"][] = $this->InsertCurrentComment();
  2266. $this->dump[$file_name]["types"]["external_symbol"][] = "$name: $type$deprecatedmods; cvar; external;";
  2267. }
  2268. // ==== got typedef ===
  2269. if (ereg("^typedef[[:space:]]+struct[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_]+);", $line, $captures)) { // defined struct type
  2270. $real_type = $captures[1];
  2271. $struct_type = $captures[1];
  2272. $new_type = $captures[2];
  2273. // ignore types
  2274. if (in_array($struct_type, $this->ignore_types)) continue;
  2275. if (in_array($new_type, $this->ignore_types)) continue;
  2276. $this->AddTypeDef($this->dump[$file_name], "$struct_type = record end$deprecatedmods;");
  2277. $struct_type = $this->ReplaceObjcType($struct_type);
  2278. if ($new_type != $struct_type) {
  2279. $this->AddTypeDef($this->dump[$file_name], "$new_type = $struct_type$deprecatedmods;");
  2280. $this->dump["global_types"][$new_type] = $real_type;
  2281. }
  2282. $this->opaque_structs[] = $struct_type;
  2283. // also add pointer type to the opaque struct
  2284. $this->AddTypeDef($this->dump[$file_name], $new_type."Ptr = ^$new_type$deprecatedmods;");
  2285. $this->dump["global_types"][$struct_type] = "record end";
  2286. $this->dump["global_types"][$new_type."Ptr"] = "^".$new_type;
  2287. } elseif (ereg("^typedef[[:space:]]+struct[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_*]+);", $line, $captures)) { // pointer to struct
  2288. $real_type = $captures[1];
  2289. $clean_name = trim($captures[2], "*");
  2290. $pointer_type = $captures[1];
  2291. // ignore types
  2292. if (in_array($clean_name, $this->ignore_types)) continue;
  2293. $pointer_type = "Pointer";
  2294. $this->AddTypeDef($this->dump[$file_name], "$clean_name = $pointer_type$deprecatedmods;");
  2295. $this->dump["global_types"][$clean_name] = $real_type;
  2296. // also add pointer type
  2297. $this->AddTypeDef($this->dump[$file_name], $clean_name."Ptr = ^$clean_name$deprecatedmods;");
  2298. $this->dump["global_types"][$clean_name."Ptr"] = "^".$clean_name;
  2299. } elseif (ereg("^typedef[[:space:]]+(const)*[[:space:]]*struct[[:space:]]+([a-zA-Z0-9_*]+)[[:space:]]+([a-zA-Z0-9_]+);", $line, $captures)) { // struct type (complex)
  2300. $real_type = $captures[1];
  2301. $typedef_name = $captures[3];
  2302. // ignore types
  2303. if (in_array($typedef_name, $this->ignore_types)) continue;
  2304. $captures[2] = $this->FormatObjcType($captures[2], $modifiers);
  2305. $this->AddTypeDef($this->dump[$file_name], $typedef_name." = ".$captures[2].$deprecatedmods.";");
  2306. $this->dump["global_types"][$typedef_name] = $real_type;
  2307. // also add pointer type
  2308. $this->AddTypeDef($this->dump[$file_name], $typedef_name."Ptr = ^$typedef_name$deprecatedmods;");
  2309. $this->dump["global_types"][$typedef_name."Ptr"] = "^".$typedef_name;
  2310. } elseif (ereg("^typedef[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_*]+);", $line, $captures)) { // single-word type
  2311. $real_type = $captures[1];
  2312. // type is a pointer
  2313. if ($captures[2][0] == "*") {
  2314. $captures[2] = trim($captures[2], "*");
  2315. $captures[1] = $this->ReplaceObjcType($captures[1]);
  2316. // ignore types
  2317. if (in_array($captures[2], $this->ignore_types)) continue;
  2318. $this->AddTypeDef($this->dump[$file_name], $captures[2]." = ^".$captures[1]."$deprecatedmods;");
  2319. $this->dump["global_types"][$captures[2]] = $real_type;
  2320. } else {
  2321. $captures[2] = trim($captures[2], "*");
  2322. $captures[1] = $this->ReplaceObjcType($captures[1]);
  2323. // ignore types
  2324. if (in_array($captures[2], $this->ignore_types)) continue;
  2325. $this->AddTypeDef($this->dump[$file_name],$captures[2]." = ".$captures[1]."$deprecatedmods;");
  2326. }
  2327. // also add pointer type
  2328. $this->AddTypeDef($this->dump[$file_name], $captures[2]."Ptr = ^$captures[2]$deprecatedmods;");
  2329. $this->dump["global_types"][$captures[2]."Ptr"] = "^".$captures[2];
  2330. } elseif (ereg("^typedef[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_*]+);", $line, $captures)) { // double-word type
  2331. $real_type = $captures[1];
  2332. $typedef_name = trim($captures[3], "*");
  2333. $long_type = $captures[1]." ".$captures[2];
  2334. $long_type = $this->ReplaceObjcType($long_type);
  2335. // ignore types
  2336. if (in_array($captures[2], $this->ignore_types)) continue;
  2337. $this->AddTypeDef($this->dump[$file_name], $typedef_name." = $long_type$deprecatedmods;");
  2338. $this->dump["global_types"][$typedef_name] = $real_type;
  2339. // also add pointer type
  2340. $this->AddTypeDef($this->dump[$file_name], $typedef_name."Ptr = ^$typedef_name$deprecatedmods;");
  2341. $this->dump["global_types"][$typedef_name."Ptr"] = "^".$typedef_name;
  2342. }
  2343. }
  2344. //print_r($this->dump[$file_name]["types"]);
  2345. }
  2346. // Parse all protocols in a header
  2347. function ParseHeaderProtocols ($file) {
  2348. $contents = file_get_contents($file);
  2349. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2350. $section = null;
  2351. // reset comments from previous parsing sections
  2352. $this->ResetComment();
  2353. $lines = explode("\n", $contents);
  2354. foreach ($lines as $line) {
  2355. // skip blocks
  2356. if ($this->SkipBlock($line)) continue;
  2357. // ignore lines
  2358. if (in_array($line, $this->ignore_lines)) continue;
  2359. // remove macros
  2360. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  2361. // build comments
  2362. if (!$got_got_protocol) $this->BuildComment($line, $file_name);
  2363. // parse protocol
  2364. if ($got_protocol) {
  2365. // build comments
  2366. $this->BuildComment($line, $file_name);
  2367. // remove comments
  2368. $line = $this->RemoveComments($line);
  2369. // found @optional/@required section
  2370. if (eregi("^[[:space:]]*@(optional|required)+", $line, $captures)) {
  2371. $section = $captures[1];
  2372. }
  2373. // found property
  2374. if ($this->LineHasProperty($line, $captures)) {
  2375. $properties = $this->ParseClassProperty($current_protocol, $captures, $deprecatedmods);
  2376. foreach ($properties as $property) {
  2377. if ($property["setter"]) {
  2378. $property["setter"]["comment"] = $this->InsertCurrentComment();
  2379. $property["setter"]["section"] = $section;
  2380. $property["setter"]["documentation"] = $this->FindDocumentationForMethod($current_protocol, $property["setter"]["property"]);
  2381. $this->current_header["protocols"][$current_protocol]["methods"][$property["setter"]["objc_method"]] = $property["setter"];
  2382. // append to master list of protocols
  2383. $this->dump["protocols"][$current_protocol][] = $property["setter"];
  2384. }
  2385. if ($property["getter"]) {
  2386. $property["getter"]["comment"] = $this->InsertCurrentComment();
  2387. $property["getter"]["section"] = $section;
  2388. $property["getter"]["documentation"] = $this->FindDocumentationForMethod($current_protocol, $property["getter"]["property"]);
  2389. $this->current_header["protocols"][$current_protocol]["methods"][$property["getter"]["objc_method"]] = $property["getter"];
  2390. // append to master list of protocols
  2391. $this->dump["protocols"][$current_protocol][] = $property["getter"];
  2392. }
  2393. }
  2394. continue;
  2395. }
  2396. // found method
  2397. $method = null;
  2398. if (preg_match($this->pregex_objc_method_params, $line, $captures)) {
  2399. $method = $this->ConvertObjcMethodToPascal($current_protocol, $line, $captures, array(), true, $deprecatedmods);
  2400. } elseif (preg_match($this->pregex_objc_method_no_params, $line, $captures)) {
  2401. $method = $this->ConvertObjcMethodToPascal($current_protocol, $line, $captures, array(), false, $deprecatedmods);
  2402. } elseif (preg_match($this->pregex_objc_method_partial, $line, $captures)) {
  2403. $method_fragment = $line;
  2404. }
  2405. // append to classes
  2406. if (($method) && (!in_array($method["name"], $this->ignore_methods)) ) {
  2407. // add comment to the method
  2408. $method["comment"] = $this->InsertCurrentComment();
  2409. // add optional/required section to the method
  2410. $method["section"] = $section;
  2411. // add documentation for method
  2412. $method["documentation"] = $this->FindDocumentationForMethod($current_protocol, $method["objc_method"]);
  2413. $this->current_header["protocols"][$current_protocol]["methods"][$method["objc_method"]] = $method;
  2414. // append to master list of protocols
  2415. $this->dump["protocols"][$current_protocol][] = $method;
  2416. }
  2417. // found the end
  2418. if (ereg("^@end", $line)) {
  2419. $this->ResetComment();
  2420. $got_protocol = false;
  2421. }
  2422. }
  2423. // got protocol
  2424. if ((eregi($this->regex_objc_protocol, $line, $captures)) && (!eregi(".*;$", $line))) {
  2425. $got_protocol = true;
  2426. $current_protocol = $captures[1];
  2427. print("+ Protocol $current_protocol\n");
  2428. if ($this->comment_terminated) $this->current_header["protocols"][$current_protocol]["comment"] = $this->InsertCurrentComment();
  2429. $this->current_header["protocols"][$current_protocol]["name"] = $captures[1];
  2430. }
  2431. }
  2432. //print_r($this->current_class);
  2433. }
  2434. // Parse all categories in a header
  2435. function ParseHeaderCategories ($file) {
  2436. $contents = file_get_contents($file);
  2437. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2438. // reset comments from previous parsing sections
  2439. $this->ResetComment();
  2440. $lines = explode("\n", $contents);
  2441. foreach ($lines as $line) {
  2442. // skip blocks
  2443. if ($this->SkipBlock($line)) continue;
  2444. // ignore lines
  2445. if (in_array($line, $this->ignore_lines)) continue;
  2446. // remove macros
  2447. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  2448. // build comments
  2449. if (!$got_category) $this->BuildComment($line, $file_name);
  2450. // parse category
  2451. if ($got_category) {
  2452. // build comments
  2453. $this->BuildComment($line, $file_name);
  2454. // remove comments
  2455. $line = $this->RemoveComments($line);
  2456. // found property
  2457. if ($this->LineHasProperty($line, $captures)) {
  2458. $properties = $this->ParseClassProperty($current_category, $captures, $deprecatedmods);
  2459. if (!in_array($current_category, $this->ignore_categories)) {
  2460. foreach ($properties as $property) {
  2461. if ($property["setter"]) {
  2462. if ($this->AddMethodToClass($property["setter"], $this->current_class)) {
  2463. $this->dump[$category_owner]["classes"][$current_class]["categories"][$current_category]["methods"][] = $property["setter"];
  2464. $this->dump[$category_owner]["categories"][$current_category]["methods"][] = $property["setter"];
  2465. }
  2466. }
  2467. if ($property["getter"]) {
  2468. if ($this->AddMethodToClass($property["getter"], $this->current_class)) {
  2469. $this->dump[$category_owner]["classes"][$current_class]["categories"][$current_category]["methods"][] = $property["getter"];
  2470. $this->dump[$category_owner]["categories"][$current_category]["methods"][] = $property["getter"];
  2471. }
  2472. }
  2473. }
  2474. }
  2475. continue;
  2476. }
  2477. // build method fragment
  2478. if ($method_fragment) $method_fragment .= " ".trim($line);
  2479. // found method fragment termination
  2480. if (($method_fragment) && (preg_match($this->pregex_objc_method_terminate, $line))) {
  2481. $line = $method_fragment;
  2482. $method_fragment = null;
  2483. }
  2484. // found method
  2485. $method = null;
  2486. if (preg_match($this->pregex_objc_method_params, $line, $captures)) {
  2487. $method = $this->ConvertObjcMethodToPascal($current_category, $line, $captures, $this->GetProtectedKeywords($this->current_class), true, $deprecatedmods);
  2488. } elseif (preg_match($this->pregex_objc_method_no_params, $line, $captures)) {
  2489. $method = $this->ConvertObjcMethodToPascal($current_category, $line, $captures, $this->GetProtectedKeywords($this->current_class), false, $deprecatedmods);
  2490. } elseif (preg_match($this->pregex_objc_method_partial, $line, $captures)) {
  2491. $method_fragment = $line;
  2492. }
  2493. // append to classes
  2494. if (($method) && !in_array($current_category, $this->ignore_categories)) {
  2495. if ($current_class) {
  2496. if ($this->AddMethodToClass($method, $this->current_class)) {
  2497. $this->dump[$category_owner]["classes"][$current_class]["categories"][$current_category]["methods"][] = $method;
  2498. $this->dump[$category_owner]["categories"][$current_category]["methods"][] = $method;
  2499. }
  2500. }
  2501. }
  2502. // found the end
  2503. if (ereg("^@end", $line)) {
  2504. $got_category = false;
  2505. continue;
  2506. }
  2507. }
  2508. // got category
  2509. if (eregi($this->regex_objc_category, $line, $captures)) {
  2510. $got_category = true;
  2511. $category_owner = $file_name;
  2512. $category_name = $captures[2];
  2513. $current_class = $captures[1];
  2514. $current_category = $category_name;
  2515. if (!in_array($current_category, $this->ignore_categories)) {
  2516. // Protect category names against duplicate identifiers by appending the class it extends to the name
  2517. if ((count($this->dump["categories"][$current_category]) > 0) || (in_array($category_name, $this->cocoa_classes))) {
  2518. $current_category = $category_name."_".$current_class;
  2519. $this->dump[$file_name]["categories"][$current_category]["external"] = true;
  2520. } else {
  2521. $this->dump[$file_name]["categories"][$current_category]["external"] = false;
  2522. }
  2523. $this->current_class = &$this->dump[$category_owner]["classes"][$current_class];
  2524. // insert into headers category array
  2525. $this->dump[$file_name]["categories"][$current_category]["name"] = $current_category;
  2526. $this->dump[$file_name]["categories"][$current_category]["super"] = $current_class;
  2527. $this->dump[$file_name]["categories"][$current_category]["comment"] = $this->InsertCurrentComment();
  2528. $this->dump[$file_name]["categories"][$current_category]["external_name"] = $category_name;
  2529. // append to master list of categories
  2530. $this->dump["categories"][$category_name][] = $current_class;
  2531. }
  2532. }
  2533. }
  2534. //print_r($this->current_class);
  2535. }
  2536. // Parse a property into accessor methods
  2537. function ParseClassProperty ($class, $parts, $deprecatedmods) {
  2538. $method = array();
  2539. //print_r($parts);
  2540. // property has attributes
  2541. if (count($parts) == 5) {
  2542. //$property["parameters"] = explode(",", $parts[1]);
  2543. $property["parameters"] = preg_split("/\s*,\s*/", $parts[1]);
  2544. $attributes = $parts[1];
  2545. $type = $parts[2];
  2546. $pointertype = $parts[3];
  2547. $content = $parts[4];
  2548. } else {
  2549. $property["parameters"] = array();
  2550. $type = $parts[1];
  2551. $pointertype = $parts[2];
  2552. $content = $parts[3];
  2553. }
  2554. // unspecified type -> id
  2555. if ($type == "") $type = "id";
  2556. // get property list
  2557. $list = explode(",", $content);
  2558. if (count($list) > 1) {
  2559. $property_list = array();
  2560. foreach ($list as $key) {
  2561. // clean the name and remove the return type
  2562. $property_list[] = trim($key);
  2563. }
  2564. //print_r($property_list);
  2565. } else {
  2566. $property_list = array($content);
  2567. }
  2568. $methods = array();
  2569. foreach ($property_list as $property_name) {
  2570. // property name
  2571. if (eregi("([a-zA-Z0-9_]+)[[:space:]]*$", $property_name, $captures)) {
  2572. $property["name"] = ucwords($captures[1]);
  2573. $property["name_raw"] = $captures[1];
  2574. }
  2575. // property type
  2576. $type = $this->ConvertReturnType($type,$pointertype);
  2577. // prepare for appending
  2578. if ($deprecatedmods != "") $deprecatedmods .= ";";
  2579. // setter
  2580. if (!in_array("readonly", $property["parameters"])) {
  2581. $method["setter"] = array();
  2582. $name = $property["name"];
  2583. if (!$this->GetPropertyName("setter", $property["parameters"], $name)) {
  2584. $name = "set$name";
  2585. }
  2586. // protect method name from keywords
  2587. if ($this->IsKeywordReserved($name)) $name .= "_";
  2588. $method["setter"]["def"] = "procedure $name (newValue: $type);";
  2589. $method["setter"]["objc_method"] = "$name:";
  2590. $method["setter"]["class"] = $class;
  2591. $method["setter"]["name"] = $name;
  2592. $method["setter"]["kind"] = "procedure";
  2593. $method["setter"]["deprecated"] = $deprecatedmods;
  2594. $method["setter"]["property"] = $property["name_raw"];
  2595. //$method["setter"]["comment"] = $this->InsertCurrentComment();
  2596. }
  2597. // getter
  2598. $method["getter"] = array();
  2599. $name = $property["name"];
  2600. if (!$this->GetPropertyName("getter", $property["parameters"], $name)) {
  2601. $name = strtolower(substr($name, 0, 1)) . substr($name, 1);
  2602. }
  2603. // protect method name from keywords
  2604. if ($this->IsKeywordReserved($name)) $name .= "_";
  2605. $method["getter"]["def"] = "function $name: $type;";
  2606. $method["getter"]["objc_method"] = $name;
  2607. $method["getter"]["class"] = $class;
  2608. $method["getter"]["name"] = $name;
  2609. $method["getter"]["kind"] = "function";
  2610. $method["getter"]["deprecated"] = $deprecatedmods;
  2611. $method["getter"]["property"] = $property["name_raw"];
  2612. //$method["getter"]["comment"] = $this->InsertCurrentComment();
  2613. // append to array of methods
  2614. $methods[] = $method;
  2615. }
  2616. //print_r($methods);
  2617. return $methods;
  2618. }
  2619. // Parse header classes and methods
  2620. function ParseHeaderClasses ($file) {
  2621. $contents = file_get_contents($file);
  2622. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2623. $line_count = 0;
  2624. // reset comments from previous parsing sections
  2625. $this->ResetComment();
  2626. $lines = explode("\n", $contents);
  2627. foreach ($lines as $line) {
  2628. $line_count++;
  2629. // skip blocks
  2630. if ($this->SkipBlock($line)) continue;
  2631. // ignore lines
  2632. if (in_array($line, $this->ignore_lines)) continue;
  2633. // remove macros
  2634. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  2635. // remove external class macros
  2636. $line = eregi_replace("^[A-Z0-9]+_EXTERN_CLASS[[:space:]]+", "", $line);
  2637. // build comments
  2638. if (!$got_class) $this->BuildComment($line, $file_name);
  2639. // parse instance vars
  2640. if ($got_instance_vars) {
  2641. // scope compiler directive
  2642. if (eregi($this->regex_scope_compiler_directive, $line, $captures)) {
  2643. $this->instance_var_scope = $captures[1];
  2644. continue;
  2645. }
  2646. // remove comments
  2647. $line = $this->RemoveComments($line);
  2648. // parse instance variables
  2649. $result = $this->ParseInstanceVariables($line, $struct, $this->GetProtectedKeywords($this->current_class),$this->dump["master"][$this->current_class["name"]]["field_names"]);
  2650. // parse structures
  2651. if ($struct["isfinished"]) {
  2652. //print_r($struct);
  2653. //$this->dump[$file_name]["classes"][$current]["ivars"][] = $struct["name"].": $current"."_".$struct["name"].";";
  2654. $this->dump[$file_name]["classes"][$current]["ivars_structs"][] = $struct;
  2655. // print inline-record type
  2656. if ($struct["bitpacked"]) {
  2657. $this->dump[$file_name]["classes"][$current]["ivars"][] = $struct["name"].": ".$this->bitpacked_record_keyword;
  2658. $this->dump[$file_name]["classes"][$current]["ivars"][] = $this->BitPackedForceAlignment($struct["bitpacked_first_type"], " ", " ");
  2659. } else {
  2660. $this->dump[$file_name]["classes"][$current]["ivars"][] = $struct["name"].": ".$this->record_keyword;
  2661. }
  2662. // print fields
  2663. if ($struct["fields"]) {
  2664. foreach ($struct["fields"] as $field) $this->dump[$file_name]["classes"][$current]["ivars"][] = " ".$field;
  2665. }
  2666. if ($struct["bitpacked"]) $this->dump[$file_name]["classes"][$current]["ivars"][] = " );";
  2667. $this->dump[$file_name]["classes"][$current]["ivars"][] = " end;";
  2668. $struct = null;
  2669. }
  2670. if(($result != null) && ($result != "struct")) {
  2671. //print($result);
  2672. // add a single string or an array of fields to the ivars array
  2673. if (count($result) <= 1) {
  2674. $this->dump[$file_name]["classes"][$current]["ivars"][] = $result;
  2675. } else {
  2676. foreach ($result as $field) {
  2677. $this->dump[$file_name]["classes"][$current]["ivars"][] = $field;
  2678. }
  2679. }
  2680. }
  2681. // instance var section terminated.
  2682. if (eregi("^\s*}\s*[;]*$", $line)) {
  2683. $struct = null;
  2684. $got_instance_vars = false;
  2685. $this->instance_var_scope = null;
  2686. }
  2687. } elseif ($got_class) { // parse the class
  2688. // the instance variable section started after the class line and no other ivar's were parsed yet
  2689. if (!$this->dump[$file_name]["classes"][$current]["ivars"]) {
  2690. if (eregi("{\s*$", $line)) {
  2691. $got_instance_vars = true;
  2692. continue;
  2693. }
  2694. }
  2695. // build comments
  2696. $this->BuildComment($line, $file_name);
  2697. // remove comments
  2698. $line = $this->RemoveComments($line);
  2699. // found property
  2700. if ($this->LineHasProperty($line, $captures)) {
  2701. $properties = $this->ParseClassProperty($current, $captures, $deprecatedmods);
  2702. foreach ($properties as $property) {
  2703. if ($property["setter"]) {
  2704. if ($this->AddMethodToClass($property["setter"], $this->dump[$file_name]["classes"][$current])) {
  2705. $this->dump[$file_name]["classes"][$current]["methods"][] = $property["setter"];
  2706. }
  2707. }
  2708. if ($property["getter"]) {
  2709. if ($this->AddMethodToClass($property["getter"], $this->dump[$file_name]["classes"][$current])) {
  2710. $this->dump[$file_name]["classes"][$current]["methods"][] = $property["getter"];
  2711. }
  2712. }
  2713. }
  2714. continue;
  2715. }
  2716. // build method fragment
  2717. if ($method_fragment) $method_fragment .= " ".trim($line, " ");
  2718. // found method fragment termination
  2719. if (($method_fragment) && (preg_match($this->pregex_objc_method_terminate, $line))) {
  2720. $line = $method_fragment;
  2721. $method_fragment = null;
  2722. }
  2723. // found method
  2724. if (preg_match($this->pregex_objc_method_params, $line, $captures)) {
  2725. $method = $this->ConvertObjcMethodToPascal($current, $line, $captures, $this->GetProtectedKeywords($this->current_class), true, $deprecatedmods);
  2726. // add documentation for method
  2727. $method["documentation"] = $this->FindDocumentationForMethod($current, $method["objc_method"]);
  2728. if ($this->AddMethodToClass($method, $this->dump[$file_name]["classes"][$current])) {
  2729. //if ($this->comment_terminated) $method["comment"] = $this->InsertCurrentComment();
  2730. $this->dump[$file_name]["classes"][$current]["methods"][] = $method;
  2731. }
  2732. } elseif (preg_match($this->pregex_objc_method_no_params, $line, $captures)) {
  2733. $method = $this->ConvertObjcMethodToPascal($current, $line, $captures, $this->GetProtectedKeywords($this->current_class), false, $deprecatedmods);
  2734. // add documentation for method
  2735. $method["documentation"] = $this->FindDocumentationForMethod($current, $method["objc_method"]);
  2736. if ($this->AddMethodToClass($method, $this->dump[$file_name]["classes"][$current])) {
  2737. //if ($this->comment_terminated) $method["comment"] = $this->InsertCurrentComment();
  2738. $this->dump[$file_name]["classes"][$current]["methods"][] = $method;
  2739. }
  2740. } elseif (preg_match($this->pregex_objc_method_partial, $line, $captures)) {
  2741. $method_fragment = $line;
  2742. }
  2743. // found the end
  2744. if (ereg("^@end", $line)) {
  2745. $got_class = false;
  2746. $this->ResetComment();
  2747. }
  2748. }
  2749. // ==== got class ====
  2750. if ((eregi($this->regex_objc_class, $line, $captures)) || (eregi($this->regex_objc_class_no_super, $line, $captures))) {
  2751. $current = $captures[1];
  2752. $got_class = true;
  2753. $has_superclass = true;
  2754. // check for instance variable section
  2755. if (eregi("{\s*$", $line)) $got_instance_vars = true;
  2756. // get the protocols which the class adopts
  2757. if (eregi($this->regex_objc_class, $line, $captures)) {
  2758. if ($captures[3]) $this->dump[$file_name]["classes"][$current]["adopts"] = $captures[3];
  2759. } else {
  2760. if ($captures[2]) $this->dump[$file_name]["classes"][$current]["adopts"] = $captures[2];
  2761. $has_superclass=false;
  2762. }
  2763. // clean up the conforms string
  2764. if ($this->dump[$file_name]["classes"][$current]["adopts"]) {
  2765. $conform_protocols = explode(",", $this->dump[$file_name]["classes"][$current]["adopts"]);
  2766. $protocol_list = array();
  2767. foreach ($conform_protocols as $protocol) {
  2768. $protocol = trim($protocol, "<> ");
  2769. $protocol_clean .= $protocol."$this->protocol_suffix, ";
  2770. $protocol_list[] = $protocol;
  2771. }
  2772. $protocol_clean = trim($protocol_clean, ", ");
  2773. $this->dump[$file_name]["classes"][$current]["adopts"] = $protocol_clean;
  2774. $this->dump[$file_name]["classes"][$current]["protocols"] = $protocol_list;
  2775. $protocol_clean = "";
  2776. }
  2777. $this->dump[$file_name]["classes"][$current]["name"] = $captures[1];
  2778. if ($has_superclass) {
  2779. $this->dump[$file_name]["classes"][$current]["super"] = $captures[2];
  2780. $this->dump[$file_name]["classes"][$current]["super_class"] = &$this->dump["master"][$captures[2]];
  2781. }
  2782. $this->dump[$file_name]["classes"][$current]["file_name"] = $file_name;
  2783. $this->dump[$file_name]["classes"][$current]["file_clean"] = substr($file_name, 0, (strripos($file_name, ".")));
  2784. $this->dump[$file_name]["classes"][$current]["protected_keywords"] = array();
  2785. $this->dump[$file_name]["classes"][$current]["declared_methods"] = array();
  2786. $this->dump[$file_name]["classes"][$current]["comment"] = $this->InsertCurrentComment();
  2787. $this->dump[$file_name]["category_methods"] = array();
  2788. $this->current_class = &$this->dump[$file_name]["classes"][$current];
  2789. // append master class listing
  2790. $this->dump["master"][$current] = &$this->dump[$file_name]["classes"][$current];
  2791. // preparse for protected keywords
  2792. $this->PreparseClass($lines, $line_count);
  2793. // preparse for category methods that may present naming conflicts
  2794. $category_methods = $this->PreparseCategoryMethods($file);
  2795. // add category methods to protected keywords
  2796. if ($category_methods) $this->current_class["protected_keywords"] = array_merge($this->current_class["protected_keywords"], $category_methods);
  2797. // print class hierarchy
  2798. if ($this->show_class_hierarchy) {
  2799. $this->GetClassHierarchy($this->current_class, $hierarchy);
  2800. $hierarchy_string = "";
  2801. foreach ($hierarchy as $value) {
  2802. $hierarchy_string .= "$value->";
  2803. }
  2804. $hierarchy_string = trim($hierarchy_string, "->");
  2805. print(" - $current: $hierarchy_string\n");
  2806. }
  2807. $this->class_count ++;
  2808. //print_r($this->dump[$file_name]["classes"][$current]);
  2809. }
  2810. }
  2811. //print_r($this->dump[$file_name]["classes"][$current]);
  2812. }
  2813. // Parse categories which depend on another header
  2814. function ParseHeaderDependents ($file) {
  2815. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2816. $this->ParseHeaderCategories($file);
  2817. print("+ Parsed $file_name for dependents\n");
  2818. }
  2819. // Main entry to parse a header
  2820. function ParseHeader ($file) {
  2821. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2822. $name_clean = substr($file_name, 0, (strripos($file_name, ".")));
  2823. // get the framework we're parsing from if it was specified during a batch parse
  2824. if ((!$this->framework) && (eregi("/([a-zA-Z]+)\.framework", $file, $captures))) $this->framework = strtolower($captures[1]);
  2825. // set the dump for the header
  2826. $this->dump[$file_name]["path"] = "$this->root$this->out/$this->framework/$name_clean.inc";
  2827. $this->dump[$file_name]["path_partial"] = "$this->framework/$name_clean.inc";
  2828. $this->dump[$file_name]["path_merge"] = $this->dump[$file_name]["path"].".merge";
  2829. $this->dump[$file_name]["framework"] = $this->framework;
  2830. $this->dump[$file_name]["name"] = $file_name;
  2831. $this->dump[$file_name]["name_clean"] = $name_clean;
  2832. $this->dump[$file_name]["anoninternstrucs"] = 0;
  2833. $this->dump[$file_name]["anonbitfields"] = 0;
  2834. $this->current_header = &$this->dump[$file_name];
  2835. // parse each section of the header
  2836. $this->ParseHeaderTypes($file);
  2837. $this->ParseHeaderProtocols($file);
  2838. $this->ParseHeaderClasses($file);
  2839. print("+ Parsed $file_name\n");
  2840. }
  2841. // Parses the docset at $path for the current framework
  2842. function ParseFrameworkDocset ($path) {
  2843. $name = basename($path);
  2844. $parser = new DocSetParser($path);
  2845. if ($parser->parse_directory($this->docset_paths[$name])) {
  2846. $this->docset = $parser->methods;
  2847. print("+ Parsed documentation for $name.\n");
  2848. }
  2849. }
  2850. // Parse all headers assigned in $this->frameworks
  2851. function ParseAllFrameworks ($ignore_files, $parse_only) {
  2852. foreach ($this->frameworks as $framework_name => $framework_info) {
  2853. // framework is disabled
  2854. if ($framework_info["enabled"] != 1) continue;
  2855. // set the current framework being parsed
  2856. $this->framework = $framework_name;
  2857. // get the root file path
  2858. if ($this->out != "/") {
  2859. $path = $this->root.$this->out."/".$framework_info["root"];
  2860. } else {
  2861. $path = $this->root.$framework_info["root"];
  2862. }
  2863. // Parse the framework docset
  2864. if ($this->parse_docsets) $this->ParseFrameworkDocset($framework_info["docset"]);
  2865. // Load the header if found
  2866. if (file_exists($path)) {
  2867. $contents = file_get_contents($path);
  2868. $lines = explode("\n", $contents);
  2869. foreach ($lines as $line) {
  2870. $header = null;
  2871. $path = null;
  2872. // parse the header path from the {$include} macro
  2873. if (eregi($framework_info["include_pattern"], $line, $captures)) {
  2874. $header = $captures[1].".h";
  2875. $path = $framework_info["headers"]."/$header";
  2876. }
  2877. // parse the header path from {-parse} directive
  2878. if (eregi("^\{-parse[[:space:]]+(.*)[[:space:]]*\}", $line, $captures)) {
  2879. $header = $captures[1];
  2880. $path = $framework_info["headers"]."/$header";
  2881. }
  2882. // parse the header if valid
  2883. if (file_exists($path)) {
  2884. // main header
  2885. if ($parse_only) {
  2886. if (@in_array($header, $parse_only)) $this->ParseHeader($path);
  2887. } elseif (@!in_array($header, $ignore_files)) {
  2888. $this->ParseHeader($path);
  2889. }
  2890. // header dependents
  2891. if ($parse_only) {
  2892. if (@in_array($header, $parse_only)) $this->ParseHeaderDependents($path);
  2893. } elseif (@!in_array($header, $ignore_files)) {
  2894. $this->ParseHeaderDependents($path);
  2895. }
  2896. } elseif ($header) {
  2897. print("*** The header $path could not be found. ***\n");
  2898. $this->warning_count++;
  2899. }
  2900. }
  2901. } else {
  2902. die("FATAL: The master include \"$path\" is missing.\n");
  2903. }
  2904. }
  2905. // diagnostics
  2906. print("\n• Parsed $this->method_count methods in $this->class_count classes.\n\n");
  2907. if ($this->warning_count > 0) print("• $this->warning_count warnings were encountered.\n\n");
  2908. }
  2909. /**
  2910. * MAIN METHODS
  2911. */
  2912. // Parse all classes/categories (non-delegate) from the header
  2913. function CollectHeaderClasses ($file) {
  2914. // can't find the header, bail
  2915. if (!file_exists($file)) return;
  2916. $contents = file_get_contents($file);
  2917. $lines = explode("\n", $contents);
  2918. foreach ($lines as $line) {
  2919. // remove external class macros
  2920. $line = eregi_replace("^[A-Z0-9]+_EXTERN_CLASS[[:space:]]+", "", $line);
  2921. // remove version macro's (some can appear before a class)
  2922. $line = $this->RemoveVersionMacros($line, $dummy_deprecated_mods);
  2923. // classes
  2924. if (eregi($this->regex_objc_class, $line, $captures)) {
  2925. $this->defined_cocoa_classes[] = $captures[1];
  2926. // may already have been parsed as an anonymous class
  2927. if (!in_array($captures[1], $this->cocoa_classes))
  2928. $this->cocoa_classes[] = $captures[1];
  2929. }
  2930. if (eregi($this->regex_objc_class_no_super, $line, $captures)) {
  2931. $this->defined_cocoa_classes[] = $captures[1];
  2932. // may already have been parsed as an anonymous class
  2933. if (!in_array($captures[1], $this->cocoa_classes))
  2934. $this->cocoa_classes[] = $captures[1];
  2935. }
  2936. // anonymous classes ===
  2937. if (eregi($this->regex_objc_anon_class, $line, $captures)) {
  2938. $anon_classes = explode(",", $captures[1]);
  2939. foreach ($anon_classes as $anon_class) {
  2940. $anon_class=trim($anon_class);
  2941. // may already have been parsed as a regular class
  2942. if (!in_array($anon_class, $this->cocoa_classes)) {
  2943. $this->cocoa_classes[] = $anon_class;
  2944. if (!in_array($anon_class, $this->anon_cocoa_classes)) {
  2945. $this->anon_cocoa_classes[] = $anon_class;
  2946. }
  2947. }
  2948. }
  2949. }
  2950. // categories
  2951. if (eregi($this->regex_objc_category, $line, $captures)) {
  2952. $this->cocoa_categories[] = $captures[1];
  2953. }
  2954. }
  2955. }
  2956. // Build array of all known classes in frameworks
  2957. function BuildFrameworkClasses () {
  2958. foreach ($this->frameworks as $framework_name => $framework_info) {
  2959. // framework is disabled
  2960. if ($framework_info["enabled"] != 1) continue;
  2961. if ($handle = @opendir($framework_info["headers"])) {
  2962. while (($file = readdir($handle)) !== false) {
  2963. if (eregi($framework_info["header_pattern"], $file)) {
  2964. $this->CollectHeaderClasses($framework_info["headers"]."/$file");
  2965. }
  2966. }
  2967. closedir($handle);
  2968. } else {
  2969. die("FATAL: The framework \"$framework_name\" can not be located at ".$framework_info["headers"]);
  2970. }
  2971. }
  2972. }
  2973. // Process a single and print output
  2974. function ProcessFile ($file, $print) {
  2975. // set the current framework to null so it's parsed from the framework
  2976. $this->framework = null;
  2977. $this->ParseHeader($file);
  2978. $this->ParseHeaderDependents($file);
  2979. if ($print) $this->PrintAllHeaders("", null, null, false);
  2980. }
  2981. // Loads parser settings from the XML file
  2982. function LoadFrameworksXML ($framework_path) {
  2983. $xml = new SimpleXMLElement(file_get_contents("frameworks.xml"));
  2984. foreach ($xml as $framework) {
  2985. $this->frameworks[(string) $framework->name]["root"] = (string) $framework->root;
  2986. $this->frameworks[(string) $framework->name]["headers"] = (string) $framework->headers;
  2987. if ($framework_path != "")
  2988. $this->frameworks[(string) $framework->name]["headers"] = preg_replace("!^.*/System/Library/Frameworks!", $framework_path, $this->frameworks[(string) $framework->name]["headers"]);
  2989. $this->frameworks[(string) $framework->name]["include_pattern"] = (string) $framework->include_pattern;
  2990. $this->frameworks[(string) $framework->name]["header_pattern"] = (string) $framework->header_pattern;
  2991. $this->frameworks[(string) $framework->name]["external_macro"] = (string) $framework->external_macro;
  2992. $this->frameworks[(string) $framework->name]["ignore_types"] = (string) $framework->ignore_types;
  2993. $this->frameworks[(string) $framework->name]["ignore_methods"] = (string) $framework->ignore_methods;
  2994. $this->frameworks[(string) $framework->name]["replace_types"] = $framework->replace_types;
  2995. $this->frameworks[(string) $framework->name]["ignore_lines"] = $framework->ignore_lines;
  2996. $this->frameworks[(string) $framework->name]["ignore_comments"] = $framework->ignore_comments;
  2997. $this->frameworks[(string) $framework->name]["docset"] = (string)$framework->docset;
  2998. $this->frameworks[(string) $framework->name]["enabled"] = false;
  2999. $this->frameworks[(string) $framework->name]["print"] = true;
  3000. }
  3001. }
  3002. function __construct ($directory, $out_directory, $frameworks, $frameworks_path, $show) {
  3003. $this->root = $directory;
  3004. $this->out = $out_directory;
  3005. $this->show = $show;
  3006. // load defined frameworks from xml
  3007. $this->LoadFrameworksXML($frameworks_path);
  3008. // enable frameworks requested by the command line options
  3009. if ($frameworks) {
  3010. foreach ($frameworks as $name) {
  3011. $name_clean = trim($name, "^ ");
  3012. $this->frameworks[$name_clean]["enabled"] = true;
  3013. // apply options from framework definition
  3014. if ($this->frameworks[$name_clean]["external_macro"]) $this->external_string_macros .= "|".$this->frameworks[$name_clean]["external_macro"];
  3015. if ($this->frameworks[$name_clean]["ignore_types"]) $this->ignore_types = array_merge($this->ignore_types, explode(",", $this->frameworks[$name_clean]["ignore_types"]));
  3016. if ($this->frameworks[$name_clean]["ignore_methods"]) $this->ignore_methods = array_merge($this->ignore_methods, explode(",", $this->frameworks[$name_clean]["ignore_methods"]));
  3017. if ($this->frameworks[$name_clean]["ignore_lines"]) {
  3018. foreach ($this->frameworks[$name_clean]["ignore_lines"]->line as $line) {
  3019. if (!in_array($line, $this->ignore_lines)) $this->ignore_lines[] = (string)$line;
  3020. }
  3021. }
  3022. if ($this->frameworks[$name_clean]["ignore_comments"]) {
  3023. foreach ($this->frameworks[$name_clean]["ignore_comments"]->line as $line) {
  3024. if (!in_array($line, $this->ignore_comments)) $this->ignore_comments[] = (string)$line;
  3025. }
  3026. }
  3027. if ($this->frameworks[$name_clean]["replace_types"]) {
  3028. foreach ($this->frameworks[$name_clean]["replace_types"]->type as $type) {
  3029. $pair = explode("=", (string)$type);
  3030. $this->replace_types[$pair[0]] = $pair[1];
  3031. }
  3032. }
  3033. // print mode
  3034. if ($name[0] == "^") $this->frameworks[$name_clean]["print"] = false;
  3035. }
  3036. }
  3037. //print_r($this->ignore_comments);
  3038. //print_r($this->ignore_lines);
  3039. //print_r($this->frameworks);
  3040. //exit;
  3041. $this->BuildFrameworkClasses();
  3042. }
  3043. }
  3044. ?>