objp.php 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802
  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. $auto_increment = $captures[2] + 1;
  2002. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*[(]*([0-9]+[xX]+[a-fA-F0-9]+)[)]*", $line, $captures)) { // hexadecimal value
  2003. $captures[2] = trim($captures[2], ", ");
  2004. $auto_increment = $captures[2] + 1;
  2005. $captures[2] = eregi_replace("^0x", "$", $captures[2]);
  2006. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  2007. } 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 ()
  2008. $captures[2] = ereg_replace("[[:space:]]?<<[[:space:]]?", " shl ", $captures[2]);
  2009. // remove integer type hints
  2010. $captures[2] = ereg_replace("([0-9]+)[UL]+([[:space:]]+)shl([[:space:]])", "\\1\\2shl\\3", $captures[2]);
  2011. $captures[2] = ereg_replace("([[:space:]])shl([[:space:]]+)([0-9]+)[UL]+", "\\1shl\\2\\3", $captures[2]);
  2012. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  2013. $operands = preg_split("/\s*shl\s*/", $captures[2]);
  2014. $auto_increment = ($operands[0] << $operands[1]) + 1;
  2015. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*=[[:space:]]*\(([a-zA-Z0-9]+[[:space:]]*<<[[:space:]]*[a-zA-Z0-9]+)\)", $line, $captures)) { // << shl value
  2016. $captures[2] = trim($captures[2], ", ");
  2017. $captures[2] = ereg_replace("[[:space:]]?<<[[:space:]]?", " shl ", $captures[2]);
  2018. // remove integer type hints
  2019. $captures[2] = ereg_replace("([0-9]+)[UL]+([[:space:]]+)shl([[:space:]])", "\\1\\2shl\\3", $captures[2]);
  2020. $captures[2] = ereg_replace("([[:space:]])shl([[:space:]]+)([0-9]+)[UL]+", "\\1shl\\2\\3", $captures[2]);
  2021. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$captures[2].";".$this->AppendEOLComment();
  2022. $operands = preg_split("/\s*shl\s*/", $captures[2]);
  2023. $auto_increment = ($operands[0] << $operands[1]) + 1;
  2024. } elseif (ereg("^[[:space:]]*[,]*[[:space:]]*([a-zA-Z0-9_]+)[[:space:]]*[,}]*[[:space:]]*$", $line, $captures)) { // non-value
  2025. // omit lines which started nested structures.
  2026. // bad practice but the single-line regex parser can't handle them
  2027. if (!eregi("[=|]+", $line)) {
  2028. $captures[1] = trim($captures[1], ", ");
  2029. $this->dump[$file_name]["types"]["enums"][$block_count][] = $captures[1]." = ".$auto_increment.";";
  2030. $auto_increment ++;
  2031. }
  2032. }
  2033. }
  2034. // Parse external symbols, enums and typedef's from the header
  2035. function ParseHeaderTypes ($file) {
  2036. $contents = file_get_contents($file);
  2037. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2038. $any_field_bitpacked = false;
  2039. $all_fields_bitpacked = false;
  2040. // reset comments from previous parsing sections
  2041. $this->ResetComment();
  2042. $lines = explode("\n", $contents);
  2043. foreach ($lines as $line) {
  2044. // skip blocks
  2045. if ($this->SkipBlock($line)) continue;
  2046. // ignore lines
  2047. if (in_array($line, $this->ignore_lines)) continue;
  2048. // build comments
  2049. $this->BuildComment($line, $file_name);
  2050. // build macro blocks
  2051. $this->BuildMacroBlocks($line);
  2052. // garbage collector hints
  2053. $line = $this->ReplaceGarbageCollectorHints($line, $garbage_collector_hint);
  2054. // remove macros
  2055. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  2056. // remove comments
  2057. $line = $this->RemoveComments($line);
  2058. $line = trim($line, " ");
  2059. if ($got_struct) {
  2060. // insert macros
  2061. if ($macro = $this->InsertMacroBlocks($line, $this->inside_macro_block)) $struct_fields .= "$macro\n";
  2062. // collect fields
  2063. $this->ParseStructFields($line, array(), $struct_fields, $any_field_bitpacked, $all_fields_bitpacked, $first_bitpacked_type);
  2064. // got end of struct
  2065. if (ereg("^}[[:space:]]*([a-zA-Z_0-9]+);", $line, $captures)) {
  2066. if ($struct_name == "") {
  2067. $struct_name = $captures[1];
  2068. } else {
  2069. $struct_type = $captures[1];
  2070. }
  2071. // ignore this struct
  2072. if (in_array($struct_name, $this->ignore_types)) continue;
  2073. if ($all_fields_bitpacked) {
  2074. $struct = "$struct_comment$struct_name = $this->bitpacked_record_keyword\n";
  2075. $struct .= $this->BitPackedForceAlignment($first_bitpacked_type, " ", " ") . "\n";
  2076. $struct_fields = str_replace(" "," ",$struct_fields);
  2077. $struct_fields .= " );\n";
  2078. } else {
  2079. $struct = "$struct_comment$struct_name = $this->record_keyword\n";
  2080. }
  2081. $struct .= $struct_fields;
  2082. $struct .= " end$deprecatedmods;\n";
  2083. if (($struct_type) && ($struct_name != $struct_type)) {
  2084. $struct .= "$struct_type = $struct_name;\n";
  2085. }
  2086. // pointer type
  2087. $struct .= $struct_name."Ptr = ^".$struct_name.";\n";
  2088. $this->dump[$file_name]["types"]["structs"][] = $struct;
  2089. $this->dump["global_structs"][] = $struct_name;
  2090. $got_struct = false;
  2091. $any_field_bitpacked = false;
  2092. $all_fields_bitpacked = false;
  2093. }
  2094. }
  2095. // got single-line struct
  2096. if (ereg("^typedef[[:space:]]+struct[[:space:]]+{(.*)}[[:space:]]+([a-zA-Z0-9_]+)", $line, $captures)) {
  2097. $struct_name = trim($captures[2], " ");
  2098. if (!in_array($struct_name, $this->ignore_types)) {
  2099. // break the struct into lines
  2100. $single_struct_fields = "";
  2101. $fields = explode(";", $captures[1]);
  2102. $comment = $this->InsertCurrentComment();
  2103. $this->ResetComment();
  2104. // parse each line
  2105. foreach ($fields as $field) {
  2106. $field = trim($field);
  2107. $this->ParseStructFields($field.";", array(), $single_struct_fields, $any_field_bitpacked, $all_fields_bitpacked, $first_bitpacked_type);
  2108. }
  2109. // merge the fields into the definition
  2110. $struct = "$comment\n"."$struct_name = ";
  2111. if ($all_fields_bitpacked) {
  2112. $struct .= "$this->bitpacked_record_keyword\n";
  2113. $struct .= $this->BitPackedForceAlignment($first_bitpacked_type, " ", " ") . "\n";
  2114. $single_struct_fields = str_replace(" "," ",$single_struct_fields);
  2115. $single_struct_fields .= " );\n";
  2116. } else $struct .= "$this->record_keyword\n";
  2117. $struct .= $single_struct_fields;
  2118. $struct .= " end$deprecatedmods;\n";
  2119. // pointer type
  2120. $struct .= $struct_name."Ptr = ^".$struct_name.";\n";
  2121. $this->dump[$file_name]["types"]["structs"][] = $struct;
  2122. $this->dump["global_structs"][] = $struct_name;
  2123. $any_field_bitpacked = false;
  2124. $all_fields_bitpacked = false;
  2125. //print("$single_struct_fields\n");
  2126. }
  2127. // got begin of struct
  2128. } elseif (ereg("^typedef struct(.*){", $line, $captures)) {
  2129. $struct_name = trim($captures[1], " ");
  2130. if (!in_array($struct_name, $this->ignore_types)) {
  2131. $struct_type = null;
  2132. $struct_fields = "";
  2133. $struct_comment = $this->InsertCurrentComment();
  2134. $this->ResetComment();
  2135. if ($struct_comment != "") $struct_comment = "$struct_comment\n";
  2136. $got_struct = true;
  2137. }
  2138. }
  2139. // got function pointer type
  2140. if (preg_match($this->pregex_function_pointer_typedef, $line, $captures)) {
  2141. $typestr = $this->ParseFunctionDeclaration($captures[1], $captures[2], "", $captures[6], false, $deprecatedmods);
  2142. $functypename = $captures[7];
  2143. if ($functypename == "") $functypename = $captures[5];
  2144. if ($functypename == "") $functypename = $captures[4];
  2145. $this->dump[$file_name]["types"]["callbacks"][$functypename] = $typestr;
  2146. // record if it is a function type instead of a function pointer type
  2147. if ($captures[3] == "") $this->implicit_function_pointer_types[] = $functypename;
  2148. continue;
  2149. }
  2150. // #defines
  2151. $got_define = false;
  2152. if (ereg("#[[:space:]]*define[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+\(\(.*\)(.*)\)", $line, $captures)) { // named constant with type case
  2153. $got_define = true;
  2154. } elseif (ereg("#[[:space:]]*define[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+[(]*([0-9.-]+)[)]*", $line, $captures)) { //integer
  2155. $got_define = true;
  2156. }
  2157. if ($got_define) {
  2158. $define_name = $captures[1];
  2159. if (!in_array($define_name, $this->ignore_types)) {
  2160. $define_name = $this->ReplaceObjcType($define_name);
  2161. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["defines"][] = $this->InsertCurrentComment();
  2162. $this->AppendCurrentComment($this->dump[$file_name]["types"]["defines"]);
  2163. $this->dump[$file_name]["types"]["defines"][] = $define_name." = ".$captures[2].";".$this->AppendEOLComment();
  2164. } else {
  2165. $this->ResetComment();
  2166. }
  2167. }
  2168. // parse enum fields
  2169. if (($got_enum) || ($got_named_enum)) {
  2170. // print($line.", auto_inc = $auto_increment\n");
  2171. $this->ParseEnumFields($line, $file_name, &$block_count, &$auto_increment);
  2172. // found the end
  2173. if (ereg("^};", $line)) $got_enum = false;
  2174. }
  2175. // ==== got inline named enum ===
  2176. if (ereg("^[[:space:]]*enum[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]*{(.*)};", $line, $captures)) {
  2177. //print("$line\n");
  2178. $enum_name = trim($captures[1], " ");
  2179. if (!in_array($enum_name, $this->ignore_types)) {
  2180. $block_count ++;
  2181. $auto_increment = 0;
  2182. // break the enum into lines
  2183. $fields = explode(",", $captures[2]);
  2184. //$this->AppendCurrentMacro($this->dump[$file_name]["types"]["enums"][$block_count]);
  2185. $this->AppendCurrentComment($this->dump[$file_name]["types"]["enums"][$block_count]);
  2186. if ($this->comment_terminated) $this->dump[$file_name]["types"]["enums"][$block_count][] = $this->InsertCurrentComment();
  2187. $this->ResetComment();
  2188. // parse each line
  2189. foreach ($fields as $field) {
  2190. $field = trim($field, " ");
  2191. $this->ParseEnumFields($field.",", $file_name, &$block_count, &$auto_increment);
  2192. }
  2193. }
  2194. continue;
  2195. }
  2196. // ==== got inline enum ===
  2197. if (ereg("^[[:space:]]*enum[[:space:]]*{(.*)};", $line, $captures)) {
  2198. //print("$line\n");
  2199. $block_count ++;
  2200. $auto_increment = 0;
  2201. // break the enum into lines
  2202. $fields = explode(",", $captures[1]);
  2203. $this->AppendCurrentComment($this->dump[$file_name]["types"]["enums"][$block_count]);
  2204. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["enums"][$block_count][] = $this->InsertCurrentComment();
  2205. $this->ResetComment();
  2206. // parse each line
  2207. foreach ($fields as $field) {
  2208. $field = trim($field, " ");
  2209. $this->ParseEnumFields($field.",", $file_name, &$block_count, &$auto_increment);
  2210. }
  2211. continue;
  2212. }
  2213. // ==== got enum ===
  2214. if (ereg("^enum", $line)) {
  2215. $got_enum = true;
  2216. $block_count ++;
  2217. $auto_increment = 0;
  2218. $this->AppendCurrentComment($this->dump[$file_name]["types"]["enums"][$block_count]);
  2219. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["enums"][$block_count][] = $this->InsertCurrentComment();
  2220. }
  2221. // terminate named enum
  2222. if ($got_named_enum) {
  2223. if (ereg("^}[[:space:]]*([a-zA-Z0-9_]+);", $line, $captures)) {
  2224. $got_named_enum = false;
  2225. $named_enum = trim($named_enum, ", \n");
  2226. $name = $captures[1];
  2227. if (!in_array($name, $this->ignore_types)) {
  2228. $this->dump[$file_name]["types"]["named_enums"][] = "$name = culong;";
  2229. $this->dump["global_types"][$name] = $name;
  2230. }
  2231. }
  2232. }
  2233. // ==== got named enum ===
  2234. if (ereg("^typedef enum {", $line)) {
  2235. $got_named_enum = true;
  2236. $named_enum = "";
  2237. $auto_increment = 0;
  2238. $block_count ++;
  2239. $this->AppendCurrentComment($this->dump[$file_name]["types"]["named_enums"][$block_count]);
  2240. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["named_enums"][] = $this->InsertCurrentComment();
  2241. }
  2242. // ==== external functions ===
  2243. // doesn't work when $this->external_string_macros is added to
  2244. // the string at initialisation time, because it can still change
  2245. // later (while loading frameworks.xml)
  2246. if (preg_match("!^(?:$this->external_string_macros)+".$this->pregex_external_function_end, $line, $captures)) {
  2247. // ignore symbols
  2248. if (in_array($captures[3], $this->ignore_types)) continue;
  2249. $typestr = $this->ParseFunctionDeclaration($captures[1], $captures[2], $captures[3], $captures[4], true, $deprecatedmods);
  2250. $this->dump[$file_name]["types"]["functions"][] = $typestr;
  2251. continue;
  2252. }
  2253. // ==== external string constant ===
  2254. if (eregi("^($this->external_string_macros)+[[:space:]]+NSString[[:space:]]*\*[[:space:]]*(const)*[[:space:]]*([a-zA-Z0-9_]+)", $line, $captures)) {
  2255. $name = $captures[3];
  2256. if (in_array($name, $this->ignore_types)) continue;
  2257. // insert comments
  2258. $this->AppendCurrentComment($this->dump[$file_name]["types"]["string_constant"]);
  2259. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["string_constant"][] = $this->InsertCurrentComment();
  2260. $this->dump[$file_name]["types"]["string_constant"][] = "$name: $this->string_macro$deprecatedmods; cvar; external;";
  2261. }
  2262. // ==== external symbol ===
  2263. if (eregi("^($this->external_string_macros)+[[:space:]]+([a-zA-Z0-9_ ]+)[[:space:]]+([a-zA-Z0-9_]+)", $line, $captures)) {
  2264. $name = $captures[3];
  2265. $type = $captures[2];
  2266. // ignore symbols
  2267. if (in_array($name, $this->ignore_types)) continue;
  2268. $type = istr_replace_word("const", "", $type);
  2269. $type = $this->ReplaceObjcType(trim($type, " "));
  2270. $this->AppendCurrentComment($this->dump[$file_name]["types"]["external_symbol"]);
  2271. //if ($this->comment_terminated) $this->dump[$file_name]["types"]["external_symbol"][] = $this->InsertCurrentComment();
  2272. $this->dump[$file_name]["types"]["external_symbol"][] = "$name: $type$deprecatedmods; cvar; external;";
  2273. }
  2274. // ==== got typedef ===
  2275. if (ereg("^typedef[[:space:]]+struct[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_]+);", $line, $captures)) { // defined struct type
  2276. $real_type = $captures[1];
  2277. $struct_type = $captures[1];
  2278. $new_type = $captures[2];
  2279. // ignore types
  2280. if (in_array($struct_type, $this->ignore_types)) continue;
  2281. if (in_array($new_type, $this->ignore_types)) continue;
  2282. $this->AddTypeDef($this->dump[$file_name], "$struct_type = record end$deprecatedmods;");
  2283. $struct_type = $this->ReplaceObjcType($struct_type);
  2284. if ($new_type != $struct_type) {
  2285. $this->AddTypeDef($this->dump[$file_name], "$new_type = $struct_type$deprecatedmods;");
  2286. $this->dump["global_types"][$new_type] = $real_type;
  2287. }
  2288. $this->opaque_structs[] = $struct_type;
  2289. // also add pointer type to the opaque struct
  2290. $this->AddTypeDef($this->dump[$file_name], $new_type."Ptr = ^$new_type$deprecatedmods;");
  2291. $this->dump["global_types"][$struct_type] = "record end";
  2292. $this->dump["global_types"][$new_type."Ptr"] = "^".$new_type;
  2293. } elseif (ereg("^typedef[[:space:]]+struct[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_*]+);", $line, $captures)) { // pointer to struct
  2294. $real_type = $captures[1];
  2295. $clean_name = trim($captures[2], "*");
  2296. $pointer_type = $captures[1];
  2297. // ignore types
  2298. if (in_array($clean_name, $this->ignore_types)) continue;
  2299. $pointer_type = "Pointer";
  2300. $this->AddTypeDef($this->dump[$file_name], "$clean_name = $pointer_type$deprecatedmods;");
  2301. $this->dump["global_types"][$clean_name] = $real_type;
  2302. // also add pointer type
  2303. $this->AddTypeDef($this->dump[$file_name], $clean_name."Ptr = ^$clean_name$deprecatedmods;");
  2304. $this->dump["global_types"][$clean_name."Ptr"] = "^".$clean_name;
  2305. } elseif (ereg("^typedef[[:space:]]+(const)*[[:space:]]*struct[[:space:]]+([a-zA-Z0-9_*]+)[[:space:]]+([a-zA-Z0-9_]+);", $line, $captures)) { // struct type (complex)
  2306. $real_type = $captures[1];
  2307. $typedef_name = $captures[3];
  2308. // ignore types
  2309. if (in_array($typedef_name, $this->ignore_types)) continue;
  2310. $captures[2] = $this->FormatObjcType($captures[2], $modifiers);
  2311. $this->AddTypeDef($this->dump[$file_name], $typedef_name." = ".$captures[2].$deprecatedmods.";");
  2312. $this->dump["global_types"][$typedef_name] = $real_type;
  2313. // also add pointer type
  2314. $this->AddTypeDef($this->dump[$file_name], $typedef_name."Ptr = ^$typedef_name$deprecatedmods;");
  2315. $this->dump["global_types"][$typedef_name."Ptr"] = "^".$typedef_name;
  2316. } elseif (ereg("^typedef[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_*]+);", $line, $captures)) { // single-word type
  2317. $real_type = $captures[1];
  2318. // type is a pointer
  2319. if ($captures[2][0] == "*") {
  2320. $captures[2] = trim($captures[2], "*");
  2321. $captures[1] = $this->ReplaceObjcType($captures[1]);
  2322. // ignore types
  2323. if (in_array($captures[2], $this->ignore_types)) continue;
  2324. $this->AddTypeDef($this->dump[$file_name], $captures[2]." = ^".$captures[1]."$deprecatedmods;");
  2325. $this->dump["global_types"][$captures[2]] = $real_type;
  2326. } else {
  2327. $captures[2] = trim($captures[2], "*");
  2328. $captures[1] = $this->ReplaceObjcType($captures[1]);
  2329. // ignore types
  2330. if (in_array($captures[2], $this->ignore_types)) continue;
  2331. $this->AddTypeDef($this->dump[$file_name],$captures[2]." = ".$captures[1]."$deprecatedmods;");
  2332. }
  2333. // also add pointer type
  2334. $this->AddTypeDef($this->dump[$file_name], $captures[2]."Ptr = ^$captures[2]$deprecatedmods;");
  2335. $this->dump["global_types"][$captures[2]."Ptr"] = "^".$captures[2];
  2336. } elseif (ereg("^typedef[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_]+)[[:space:]]+([a-zA-Z0-9_*]+);", $line, $captures)) { // double-word type
  2337. $real_type = $captures[1];
  2338. $typedef_name = trim($captures[3], "*");
  2339. $long_type = $captures[1]." ".$captures[2];
  2340. $long_type = $this->ReplaceObjcType($long_type);
  2341. // ignore types
  2342. if (in_array($captures[2], $this->ignore_types)) continue;
  2343. $this->AddTypeDef($this->dump[$file_name], $typedef_name." = $long_type$deprecatedmods;");
  2344. $this->dump["global_types"][$typedef_name] = $real_type;
  2345. // also add pointer type
  2346. $this->AddTypeDef($this->dump[$file_name], $typedef_name."Ptr = ^$typedef_name$deprecatedmods;");
  2347. $this->dump["global_types"][$typedef_name."Ptr"] = "^".$typedef_name;
  2348. }
  2349. }
  2350. //print_r($this->dump[$file_name]["types"]);
  2351. }
  2352. // Parse all protocols in a header
  2353. function ParseHeaderProtocols ($file) {
  2354. $contents = file_get_contents($file);
  2355. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2356. $section = null;
  2357. // reset comments from previous parsing sections
  2358. $this->ResetComment();
  2359. $lines = explode("\n", $contents);
  2360. foreach ($lines as $line) {
  2361. // skip blocks
  2362. if ($this->SkipBlock($line)) continue;
  2363. // ignore lines
  2364. if (in_array($line, $this->ignore_lines)) continue;
  2365. // remove macros
  2366. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  2367. // build comments
  2368. if (!$got_got_protocol) $this->BuildComment($line, $file_name);
  2369. // parse protocol
  2370. if ($got_protocol) {
  2371. // build comments
  2372. $this->BuildComment($line, $file_name);
  2373. // remove comments
  2374. $line = $this->RemoveComments($line);
  2375. // found @optional/@required section
  2376. if (eregi("^[[:space:]]*@(optional|required)+", $line, $captures)) {
  2377. $section = $captures[1];
  2378. }
  2379. // found property
  2380. if ($this->LineHasProperty($line, $captures)) {
  2381. $properties = $this->ParseClassProperty($current_protocol, $captures, $deprecatedmods);
  2382. foreach ($properties as $property) {
  2383. if ($property["setter"]) {
  2384. $property["setter"]["comment"] = $this->InsertCurrentComment();
  2385. $property["setter"]["section"] = $section;
  2386. $property["setter"]["documentation"] = $this->FindDocumentationForMethod($current_protocol, $property["setter"]["property"]);
  2387. $this->current_header["protocols"][$current_protocol]["methods"][$property["setter"]["objc_method"]] = $property["setter"];
  2388. // append to master list of protocols
  2389. $this->dump["protocols"][$current_protocol][] = $property["setter"];
  2390. }
  2391. if ($property["getter"]) {
  2392. $property["getter"]["comment"] = $this->InsertCurrentComment();
  2393. $property["getter"]["section"] = $section;
  2394. $property["getter"]["documentation"] = $this->FindDocumentationForMethod($current_protocol, $property["getter"]["property"]);
  2395. $this->current_header["protocols"][$current_protocol]["methods"][$property["getter"]["objc_method"]] = $property["getter"];
  2396. // append to master list of protocols
  2397. $this->dump["protocols"][$current_protocol][] = $property["getter"];
  2398. }
  2399. }
  2400. continue;
  2401. }
  2402. // found method
  2403. $method = null;
  2404. if (preg_match($this->pregex_objc_method_params, $line, $captures)) {
  2405. $method = $this->ConvertObjcMethodToPascal($current_protocol, $line, $captures, array(), true, $deprecatedmods);
  2406. } elseif (preg_match($this->pregex_objc_method_no_params, $line, $captures)) {
  2407. $method = $this->ConvertObjcMethodToPascal($current_protocol, $line, $captures, array(), false, $deprecatedmods);
  2408. } elseif (preg_match($this->pregex_objc_method_partial, $line, $captures)) {
  2409. $method_fragment = $line;
  2410. }
  2411. // append to classes
  2412. if (($method) && (!in_array($method["name"], $this->ignore_methods)) ) {
  2413. // add comment to the method
  2414. $method["comment"] = $this->InsertCurrentComment();
  2415. // add optional/required section to the method
  2416. $method["section"] = $section;
  2417. // add documentation for method
  2418. $method["documentation"] = $this->FindDocumentationForMethod($current_protocol, $method["objc_method"]);
  2419. $this->current_header["protocols"][$current_protocol]["methods"][$method["objc_method"]] = $method;
  2420. // append to master list of protocols
  2421. $this->dump["protocols"][$current_protocol][] = $method;
  2422. }
  2423. // found the end
  2424. if (ereg("^@end", $line)) {
  2425. $this->ResetComment();
  2426. $got_protocol = false;
  2427. }
  2428. }
  2429. // got protocol
  2430. if ((eregi($this->regex_objc_protocol, $line, $captures)) && (!eregi(".*;$", $line))) {
  2431. $got_protocol = true;
  2432. $current_protocol = $captures[1];
  2433. print("+ Protocol $current_protocol\n");
  2434. if ($this->comment_terminated) $this->current_header["protocols"][$current_protocol]["comment"] = $this->InsertCurrentComment();
  2435. $this->current_header["protocols"][$current_protocol]["name"] = $captures[1];
  2436. }
  2437. }
  2438. //print_r($this->current_class);
  2439. }
  2440. // Parse all categories in a header
  2441. function ParseHeaderCategories ($file) {
  2442. $contents = file_get_contents($file);
  2443. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2444. // reset comments from previous parsing sections
  2445. $this->ResetComment();
  2446. $lines = explode("\n", $contents);
  2447. foreach ($lines as $line) {
  2448. // skip blocks
  2449. if ($this->SkipBlock($line)) continue;
  2450. // ignore lines
  2451. if (in_array($line, $this->ignore_lines)) continue;
  2452. // remove macros
  2453. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  2454. // build comments
  2455. if (!$got_category) $this->BuildComment($line, $file_name);
  2456. // parse category
  2457. if ($got_category) {
  2458. // build comments
  2459. $this->BuildComment($line, $file_name);
  2460. // remove comments
  2461. $line = $this->RemoveComments($line);
  2462. // found property
  2463. if ($this->LineHasProperty($line, $captures)) {
  2464. $properties = $this->ParseClassProperty($current_category, $captures, $deprecatedmods);
  2465. if (!in_array($current_category, $this->ignore_categories)) {
  2466. foreach ($properties as $property) {
  2467. if ($property["setter"]) {
  2468. if ($this->AddMethodToClass($property["setter"], $this->current_class)) {
  2469. $this->dump[$category_owner]["classes"][$current_class]["categories"][$current_category]["methods"][] = $property["setter"];
  2470. $this->dump[$category_owner]["categories"][$current_category]["methods"][] = $property["setter"];
  2471. }
  2472. }
  2473. if ($property["getter"]) {
  2474. if ($this->AddMethodToClass($property["getter"], $this->current_class)) {
  2475. $this->dump[$category_owner]["classes"][$current_class]["categories"][$current_category]["methods"][] = $property["getter"];
  2476. $this->dump[$category_owner]["categories"][$current_category]["methods"][] = $property["getter"];
  2477. }
  2478. }
  2479. }
  2480. }
  2481. continue;
  2482. }
  2483. // build method fragment
  2484. if ($method_fragment) $method_fragment .= " ".trim($line);
  2485. // found method fragment termination
  2486. if (($method_fragment) && (preg_match($this->pregex_objc_method_terminate, $line))) {
  2487. $line = $method_fragment;
  2488. $method_fragment = null;
  2489. }
  2490. // found method
  2491. $method = null;
  2492. if (preg_match($this->pregex_objc_method_params, $line, $captures)) {
  2493. $method = $this->ConvertObjcMethodToPascal($current_category, $line, $captures, $this->GetProtectedKeywords($this->current_class), true, $deprecatedmods);
  2494. } elseif (preg_match($this->pregex_objc_method_no_params, $line, $captures)) {
  2495. $method = $this->ConvertObjcMethodToPascal($current_category, $line, $captures, $this->GetProtectedKeywords($this->current_class), false, $deprecatedmods);
  2496. } elseif (preg_match($this->pregex_objc_method_partial, $line, $captures)) {
  2497. $method_fragment = $line;
  2498. }
  2499. // append to classes
  2500. if (($method) && !in_array($current_category, $this->ignore_categories)) {
  2501. if ($current_class) {
  2502. if ($this->AddMethodToClass($method, $this->current_class)) {
  2503. $this->dump[$category_owner]["classes"][$current_class]["categories"][$current_category]["methods"][] = $method;
  2504. $this->dump[$category_owner]["categories"][$current_category]["methods"][] = $method;
  2505. }
  2506. }
  2507. }
  2508. // found the end
  2509. if (ereg("^@end", $line)) {
  2510. $got_category = false;
  2511. continue;
  2512. }
  2513. }
  2514. // got category
  2515. if (eregi($this->regex_objc_category, $line, $captures)) {
  2516. $got_category = true;
  2517. $category_owner = $file_name;
  2518. $category_name = $captures[2];
  2519. $current_class = $captures[1];
  2520. $current_category = $category_name;
  2521. if (!in_array($current_category, $this->ignore_categories)) {
  2522. // Protect category names against duplicate identifiers by appending the class it extends to the name
  2523. if ((count($this->dump["categories"][$current_category]) > 0) || (in_array($category_name, $this->cocoa_classes))) {
  2524. $current_category = $category_name."_".$current_class;
  2525. $this->dump[$file_name]["categories"][$current_category]["external"] = true;
  2526. } else {
  2527. $this->dump[$file_name]["categories"][$current_category]["external"] = false;
  2528. }
  2529. $this->current_class = &$this->dump[$category_owner]["classes"][$current_class];
  2530. // insert into headers category array
  2531. $this->dump[$file_name]["categories"][$current_category]["name"] = $current_category;
  2532. $this->dump[$file_name]["categories"][$current_category]["super"] = $current_class;
  2533. $this->dump[$file_name]["categories"][$current_category]["comment"] = $this->InsertCurrentComment();
  2534. $this->dump[$file_name]["categories"][$current_category]["external_name"] = $category_name;
  2535. // append to master list of categories
  2536. $this->dump["categories"][$category_name][] = $current_class;
  2537. }
  2538. }
  2539. }
  2540. //print_r($this->current_class);
  2541. }
  2542. // Parse a property into accessor methods
  2543. function ParseClassProperty ($class, $parts, $deprecatedmods) {
  2544. $method = array();
  2545. //print_r($parts);
  2546. // property has attributes
  2547. if (count($parts) == 5) {
  2548. //$property["parameters"] = explode(",", $parts[1]);
  2549. $property["parameters"] = preg_split("/\s*,\s*/", $parts[1]);
  2550. $attributes = $parts[1];
  2551. $type = $parts[2];
  2552. $pointertype = $parts[3];
  2553. $content = $parts[4];
  2554. } else {
  2555. $property["parameters"] = array();
  2556. $type = $parts[1];
  2557. $pointertype = $parts[2];
  2558. $content = $parts[3];
  2559. }
  2560. // unspecified type -> id
  2561. if ($type == "") $type = "id";
  2562. // get property list
  2563. $list = explode(",", $content);
  2564. if (count($list) > 1) {
  2565. $property_list = array();
  2566. foreach ($list as $key) {
  2567. // clean the name and remove the return type
  2568. $property_list[] = trim($key);
  2569. }
  2570. //print_r($property_list);
  2571. } else {
  2572. $property_list = array($content);
  2573. }
  2574. $methods = array();
  2575. foreach ($property_list as $property_name) {
  2576. // property name
  2577. if (eregi("([a-zA-Z0-9_]+)[[:space:]]*$", $property_name, $captures)) {
  2578. $property["name"] = ucwords($captures[1]);
  2579. $property["name_raw"] = $captures[1];
  2580. }
  2581. // property type
  2582. $type = $this->ConvertReturnType($type,$pointertype);
  2583. // prepare for appending
  2584. if ($deprecatedmods != "") $deprecatedmods .= ";";
  2585. // setter
  2586. if (!in_array("readonly", $property["parameters"])) {
  2587. $method["setter"] = array();
  2588. $name = $property["name"];
  2589. if (!$this->GetPropertyName("setter", $property["parameters"], $name)) {
  2590. $name = "set$name";
  2591. }
  2592. // protect method name from keywords
  2593. if ($this->IsKeywordReserved($name)) $name .= "_";
  2594. $method["setter"]["def"] = "procedure $name (newValue: $type);";
  2595. $method["setter"]["objc_method"] = "$name:";
  2596. $method["setter"]["class"] = $class;
  2597. $method["setter"]["name"] = $name;
  2598. $method["setter"]["kind"] = "procedure";
  2599. $method["setter"]["deprecated"] = $deprecatedmods;
  2600. $method["setter"]["property"] = $property["name_raw"];
  2601. //$method["setter"]["comment"] = $this->InsertCurrentComment();
  2602. }
  2603. // getter
  2604. $method["getter"] = array();
  2605. $name = $property["name"];
  2606. if (!$this->GetPropertyName("getter", $property["parameters"], $name)) {
  2607. $name = strtolower(substr($name, 0, 1)) . substr($name, 1);
  2608. }
  2609. // protect method name from keywords
  2610. if ($this->IsKeywordReserved($name)) $name .= "_";
  2611. $method["getter"]["def"] = "function $name: $type;";
  2612. $method["getter"]["objc_method"] = $name;
  2613. $method["getter"]["class"] = $class;
  2614. $method["getter"]["name"] = $name;
  2615. $method["getter"]["kind"] = "function";
  2616. $method["getter"]["deprecated"] = $deprecatedmods;
  2617. $method["getter"]["property"] = $property["name_raw"];
  2618. //$method["getter"]["comment"] = $this->InsertCurrentComment();
  2619. // append to array of methods
  2620. $methods[] = $method;
  2621. }
  2622. //print_r($methods);
  2623. return $methods;
  2624. }
  2625. // Parse header classes and methods
  2626. function ParseHeaderClasses ($file) {
  2627. $contents = file_get_contents($file);
  2628. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2629. $line_count = 0;
  2630. // reset comments from previous parsing sections
  2631. $this->ResetComment();
  2632. $lines = explode("\n", $contents);
  2633. foreach ($lines as $line) {
  2634. $line_count++;
  2635. // skip blocks
  2636. if ($this->SkipBlock($line)) continue;
  2637. // ignore lines
  2638. if (in_array($line, $this->ignore_lines)) continue;
  2639. // remove macros
  2640. $line = $this->RemoveVersionMacros($line, $deprecatedmods);
  2641. // remove external class macros
  2642. $line = eregi_replace("^[A-Z0-9]+_EXTERN_CLASS[[:space:]]+", "", $line);
  2643. // build comments
  2644. if (!$got_class) $this->BuildComment($line, $file_name);
  2645. // parse instance vars
  2646. if ($got_instance_vars) {
  2647. // scope compiler directive
  2648. if (eregi($this->regex_scope_compiler_directive, $line, $captures)) {
  2649. $this->instance_var_scope = $captures[1];
  2650. continue;
  2651. }
  2652. // remove comments
  2653. $line = $this->RemoveComments($line);
  2654. // parse instance variables
  2655. $result = $this->ParseInstanceVariables($line, $struct, $this->GetProtectedKeywords($this->current_class),$this->dump["master"][$this->current_class["name"]]["field_names"]);
  2656. // parse structures
  2657. if ($struct["isfinished"]) {
  2658. //print_r($struct);
  2659. //$this->dump[$file_name]["classes"][$current]["ivars"][] = $struct["name"].": $current"."_".$struct["name"].";";
  2660. $this->dump[$file_name]["classes"][$current]["ivars_structs"][] = $struct;
  2661. // print inline-record type
  2662. if ($struct["bitpacked"]) {
  2663. $this->dump[$file_name]["classes"][$current]["ivars"][] = $struct["name"].": ".$this->bitpacked_record_keyword;
  2664. $this->dump[$file_name]["classes"][$current]["ivars"][] = $this->BitPackedForceAlignment($struct["bitpacked_first_type"], " ", " ");
  2665. } else {
  2666. $this->dump[$file_name]["classes"][$current]["ivars"][] = $struct["name"].": ".$this->record_keyword;
  2667. }
  2668. // print fields
  2669. if ($struct["fields"]) {
  2670. foreach ($struct["fields"] as $field) $this->dump[$file_name]["classes"][$current]["ivars"][] = " ".$field;
  2671. }
  2672. if ($struct["bitpacked"]) $this->dump[$file_name]["classes"][$current]["ivars"][] = " );";
  2673. $this->dump[$file_name]["classes"][$current]["ivars"][] = " end;";
  2674. $struct = null;
  2675. }
  2676. if(($result != null) && ($result != "struct")) {
  2677. //print($result);
  2678. // add a single string or an array of fields to the ivars array
  2679. if (count($result) <= 1) {
  2680. $this->dump[$file_name]["classes"][$current]["ivars"][] = $result;
  2681. } else {
  2682. foreach ($result as $field) {
  2683. $this->dump[$file_name]["classes"][$current]["ivars"][] = $field;
  2684. }
  2685. }
  2686. }
  2687. // instance var section terminated.
  2688. if (eregi("^\s*}\s*[;]*$", $line)) {
  2689. $struct = null;
  2690. $got_instance_vars = false;
  2691. $this->instance_var_scope = null;
  2692. }
  2693. } elseif ($got_class) { // parse the class
  2694. // the instance variable section started after the class line and no other ivar's were parsed yet
  2695. if (!$this->dump[$file_name]["classes"][$current]["ivars"]) {
  2696. if (eregi("{\s*$", $line)) {
  2697. $got_instance_vars = true;
  2698. continue;
  2699. }
  2700. }
  2701. // build comments
  2702. $this->BuildComment($line, $file_name);
  2703. // remove comments
  2704. $line = $this->RemoveComments($line);
  2705. // found property
  2706. if ($this->LineHasProperty($line, $captures)) {
  2707. $properties = $this->ParseClassProperty($current, $captures, $deprecatedmods);
  2708. foreach ($properties as $property) {
  2709. if ($property["setter"]) {
  2710. if ($this->AddMethodToClass($property["setter"], $this->dump[$file_name]["classes"][$current])) {
  2711. $this->dump[$file_name]["classes"][$current]["methods"][] = $property["setter"];
  2712. }
  2713. }
  2714. if ($property["getter"]) {
  2715. if ($this->AddMethodToClass($property["getter"], $this->dump[$file_name]["classes"][$current])) {
  2716. $this->dump[$file_name]["classes"][$current]["methods"][] = $property["getter"];
  2717. }
  2718. }
  2719. }
  2720. continue;
  2721. }
  2722. // build method fragment
  2723. if ($method_fragment) $method_fragment .= " ".trim($line, " ");
  2724. // found method fragment termination
  2725. if (($method_fragment) && (preg_match($this->pregex_objc_method_terminate, $line))) {
  2726. $line = $method_fragment;
  2727. $method_fragment = null;
  2728. }
  2729. // found method
  2730. if (preg_match($this->pregex_objc_method_params, $line, $captures)) {
  2731. $method = $this->ConvertObjcMethodToPascal($current, $line, $captures, $this->GetProtectedKeywords($this->current_class), true, $deprecatedmods);
  2732. // add documentation for method
  2733. $method["documentation"] = $this->FindDocumentationForMethod($current, $method["objc_method"]);
  2734. if ($this->AddMethodToClass($method, $this->dump[$file_name]["classes"][$current])) {
  2735. //if ($this->comment_terminated) $method["comment"] = $this->InsertCurrentComment();
  2736. $this->dump[$file_name]["classes"][$current]["methods"][] = $method;
  2737. }
  2738. } elseif (preg_match($this->pregex_objc_method_no_params, $line, $captures)) {
  2739. $method = $this->ConvertObjcMethodToPascal($current, $line, $captures, $this->GetProtectedKeywords($this->current_class), false, $deprecatedmods);
  2740. // add documentation for method
  2741. $method["documentation"] = $this->FindDocumentationForMethod($current, $method["objc_method"]);
  2742. if ($this->AddMethodToClass($method, $this->dump[$file_name]["classes"][$current])) {
  2743. //if ($this->comment_terminated) $method["comment"] = $this->InsertCurrentComment();
  2744. $this->dump[$file_name]["classes"][$current]["methods"][] = $method;
  2745. }
  2746. } elseif (preg_match($this->pregex_objc_method_partial, $line, $captures)) {
  2747. $method_fragment = $line;
  2748. }
  2749. // found the end
  2750. if (ereg("^@end", $line)) {
  2751. $got_class = false;
  2752. $this->ResetComment();
  2753. }
  2754. }
  2755. // ==== got class ====
  2756. if ((eregi($this->regex_objc_class, $line, $captures)) || (eregi($this->regex_objc_class_no_super, $line, $captures))) {
  2757. $current = $captures[1];
  2758. $got_class = true;
  2759. $has_superclass = true;
  2760. // check for instance variable section
  2761. if (eregi("{\s*$", $line)) $got_instance_vars = true;
  2762. // get the protocols which the class adopts
  2763. if (eregi($this->regex_objc_class, $line, $captures)) {
  2764. if ($captures[3]) $this->dump[$file_name]["classes"][$current]["adopts"] = $captures[3];
  2765. } else {
  2766. if ($captures[2]) $this->dump[$file_name]["classes"][$current]["adopts"] = $captures[2];
  2767. $has_superclass=false;
  2768. }
  2769. // clean up the conforms string
  2770. if ($this->dump[$file_name]["classes"][$current]["adopts"]) {
  2771. $conform_protocols = explode(",", $this->dump[$file_name]["classes"][$current]["adopts"]);
  2772. $protocol_list = array();
  2773. foreach ($conform_protocols as $protocol) {
  2774. $protocol = trim($protocol, "<> ");
  2775. $protocol_clean .= $protocol."$this->protocol_suffix, ";
  2776. $protocol_list[] = $protocol;
  2777. }
  2778. $protocol_clean = trim($protocol_clean, ", ");
  2779. $this->dump[$file_name]["classes"][$current]["adopts"] = $protocol_clean;
  2780. $this->dump[$file_name]["classes"][$current]["protocols"] = $protocol_list;
  2781. $protocol_clean = "";
  2782. }
  2783. $this->dump[$file_name]["classes"][$current]["name"] = $captures[1];
  2784. if ($has_superclass) {
  2785. $this->dump[$file_name]["classes"][$current]["super"] = $captures[2];
  2786. $this->dump[$file_name]["classes"][$current]["super_class"] = &$this->dump["master"][$captures[2]];
  2787. }
  2788. $this->dump[$file_name]["classes"][$current]["file_name"] = $file_name;
  2789. $this->dump[$file_name]["classes"][$current]["file_clean"] = substr($file_name, 0, (strripos($file_name, ".")));
  2790. $this->dump[$file_name]["classes"][$current]["protected_keywords"] = array();
  2791. $this->dump[$file_name]["classes"][$current]["declared_methods"] = array();
  2792. $this->dump[$file_name]["classes"][$current]["comment"] = $this->InsertCurrentComment();
  2793. $this->dump[$file_name]["category_methods"] = array();
  2794. $this->current_class = &$this->dump[$file_name]["classes"][$current];
  2795. // append master class listing
  2796. $this->dump["master"][$current] = &$this->dump[$file_name]["classes"][$current];
  2797. // preparse for protected keywords
  2798. $this->PreparseClass($lines, $line_count);
  2799. // preparse for category methods that may present naming conflicts
  2800. $category_methods = $this->PreparseCategoryMethods($file);
  2801. // add category methods to protected keywords
  2802. if ($category_methods) $this->current_class["protected_keywords"] = array_merge($this->current_class["protected_keywords"], $category_methods);
  2803. // print class hierarchy
  2804. if ($this->show_class_hierarchy) {
  2805. $this->GetClassHierarchy($this->current_class, $hierarchy);
  2806. $hierarchy_string = "";
  2807. foreach ($hierarchy as $value) {
  2808. $hierarchy_string .= "$value->";
  2809. }
  2810. $hierarchy_string = trim($hierarchy_string, "->");
  2811. print(" - $current: $hierarchy_string\n");
  2812. }
  2813. $this->class_count ++;
  2814. //print_r($this->dump[$file_name]["classes"][$current]);
  2815. }
  2816. }
  2817. //print_r($this->dump[$file_name]["classes"][$current]);
  2818. }
  2819. // Parse categories which depend on another header
  2820. function ParseHeaderDependents ($file) {
  2821. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2822. $this->ParseHeaderCategories($file);
  2823. print("+ Parsed $file_name for dependents\n");
  2824. }
  2825. // Main entry to parse a header
  2826. function ParseHeader ($file) {
  2827. $file_name = substr($file, (strripos($file, "/")) + 1, strlen($file));
  2828. $name_clean = substr($file_name, 0, (strripos($file_name, ".")));
  2829. // get the framework we're parsing from if it was specified during a batch parse
  2830. if ((!$this->framework) && (eregi("/([a-zA-Z]+)\.framework", $file, $captures))) $this->framework = strtolower($captures[1]);
  2831. // set the dump for the header
  2832. $this->dump[$file_name]["path"] = "$this->root$this->out/$this->framework/$name_clean.inc";
  2833. $this->dump[$file_name]["path_partial"] = "$this->framework/$name_clean.inc";
  2834. $this->dump[$file_name]["path_merge"] = $this->dump[$file_name]["path"].".merge";
  2835. $this->dump[$file_name]["framework"] = $this->framework;
  2836. $this->dump[$file_name]["name"] = $file_name;
  2837. $this->dump[$file_name]["name_clean"] = $name_clean;
  2838. $this->dump[$file_name]["anoninternstrucs"] = 0;
  2839. $this->dump[$file_name]["anonbitfields"] = 0;
  2840. $this->current_header = &$this->dump[$file_name];
  2841. // parse each section of the header
  2842. $this->ParseHeaderTypes($file);
  2843. $this->ParseHeaderProtocols($file);
  2844. $this->ParseHeaderClasses($file);
  2845. print("+ Parsed $file_name\n");
  2846. }
  2847. // Parses the docset at $path for the current framework
  2848. function ParseFrameworkDocset ($path) {
  2849. $name = basename($path);
  2850. $parser = new DocSetParser($path);
  2851. if ($parser->parse_directory($this->docset_paths[$name])) {
  2852. $this->docset = $parser->methods;
  2853. print("+ Parsed documentation for $name.\n");
  2854. }
  2855. }
  2856. // Parse all headers assigned in $this->frameworks
  2857. function ParseAllFrameworks ($ignore_files, $parse_only) {
  2858. foreach ($this->frameworks as $framework_name => $framework_info) {
  2859. // framework is disabled
  2860. if ($framework_info["enabled"] != 1) continue;
  2861. // set the current framework being parsed
  2862. $this->framework = $framework_name;
  2863. // get the root file path
  2864. if ($this->out != "/") {
  2865. $path = $this->root.$this->out."/".$framework_info["root"];
  2866. } else {
  2867. $path = $this->root.$framework_info["root"];
  2868. }
  2869. // Parse the framework docset
  2870. if ($this->parse_docsets) $this->ParseFrameworkDocset($framework_info["docset"]);
  2871. // Load the header if found
  2872. if (file_exists($path)) {
  2873. $contents = file_get_contents($path);
  2874. $lines = explode("\n", $contents);
  2875. foreach ($lines as $line) {
  2876. $header = null;
  2877. $path = null;
  2878. // parse the header path from the {$include} macro
  2879. if (eregi($framework_info["include_pattern"], $line, $captures)) {
  2880. $header = $captures[1].".h";
  2881. $path = $framework_info["headers"]."/$header";
  2882. }
  2883. // parse the header path from {-parse} directive
  2884. if (eregi("^\{-parse[[:space:]]+(.*)[[:space:]]*\}", $line, $captures)) {
  2885. $header = $captures[1];
  2886. $path = $framework_info["headers"]."/$header";
  2887. }
  2888. // parse the header if valid
  2889. if (file_exists($path)) {
  2890. // main header
  2891. if ($parse_only) {
  2892. if (@in_array($header, $parse_only)) $this->ParseHeader($path);
  2893. } elseif (@!in_array($header, $ignore_files)) {
  2894. $this->ParseHeader($path);
  2895. }
  2896. // header dependents
  2897. if ($parse_only) {
  2898. if (@in_array($header, $parse_only)) $this->ParseHeaderDependents($path);
  2899. } elseif (@!in_array($header, $ignore_files)) {
  2900. $this->ParseHeaderDependents($path);
  2901. }
  2902. } elseif ($header) {
  2903. print("*** The header $path could not be found. ***\n");
  2904. $this->warning_count++;
  2905. }
  2906. }
  2907. } else {
  2908. die("FATAL: The master include \"$path\" is missing.\n");
  2909. }
  2910. }
  2911. // diagnostics
  2912. print("\n• Parsed $this->method_count methods in $this->class_count classes.\n\n");
  2913. if ($this->warning_count > 0) print("• $this->warning_count warnings were encountered.\n\n");
  2914. }
  2915. /**
  2916. * MAIN METHODS
  2917. */
  2918. // Parse all classes/categories (non-delegate) from the header
  2919. function CollectHeaderClasses ($file) {
  2920. // can't find the header, bail
  2921. if (!file_exists($file)) return;
  2922. $contents = file_get_contents($file);
  2923. $lines = explode("\n", $contents);
  2924. foreach ($lines as $line) {
  2925. // remove external class macros
  2926. $line = eregi_replace("^[A-Z0-9]+_EXTERN_CLASS[[:space:]]+", "", $line);
  2927. // remove version macro's (some can appear before a class)
  2928. $line = $this->RemoveVersionMacros($line, $dummy_deprecated_mods);
  2929. // classes
  2930. if (eregi($this->regex_objc_class, $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. if (eregi($this->regex_objc_class_no_super, $line, $captures)) {
  2937. $this->defined_cocoa_classes[] = $captures[1];
  2938. // may already have been parsed as an anonymous class
  2939. if (!in_array($captures[1], $this->cocoa_classes))
  2940. $this->cocoa_classes[] = $captures[1];
  2941. }
  2942. // anonymous classes ===
  2943. if (eregi($this->regex_objc_anon_class, $line, $captures)) {
  2944. $anon_classes = explode(",", $captures[1]);
  2945. foreach ($anon_classes as $anon_class) {
  2946. $anon_class=trim($anon_class);
  2947. // may already have been parsed as a regular class
  2948. if (!in_array($anon_class, $this->cocoa_classes)) {
  2949. $this->cocoa_classes[] = $anon_class;
  2950. if (!in_array($anon_class, $this->anon_cocoa_classes)) {
  2951. $this->anon_cocoa_classes[] = $anon_class;
  2952. }
  2953. }
  2954. }
  2955. }
  2956. // categories
  2957. if (eregi($this->regex_objc_category, $line, $captures)) {
  2958. $this->cocoa_categories[] = $captures[1];
  2959. }
  2960. }
  2961. }
  2962. // Build array of all known classes in frameworks
  2963. function BuildFrameworkClasses () {
  2964. foreach ($this->frameworks as $framework_name => $framework_info) {
  2965. // framework is disabled
  2966. if ($framework_info["enabled"] != 1) continue;
  2967. if ($handle = @opendir($framework_info["headers"])) {
  2968. while (($file = readdir($handle)) !== false) {
  2969. if (eregi($framework_info["header_pattern"], $file)) {
  2970. $this->CollectHeaderClasses($framework_info["headers"]."/$file");
  2971. }
  2972. }
  2973. closedir($handle);
  2974. } else {
  2975. die("FATAL: The framework \"$framework_name\" can not be located at ".$framework_info["headers"]);
  2976. }
  2977. }
  2978. }
  2979. // Process a single and print output
  2980. function ProcessFile ($file, $print) {
  2981. // set the current framework to null so it's parsed from the framework
  2982. $this->framework = null;
  2983. $this->ParseHeader($file);
  2984. $this->ParseHeaderDependents($file);
  2985. if ($print) $this->PrintAllHeaders("", null, null, false);
  2986. }
  2987. // Loads parser settings from the XML file
  2988. function LoadFrameworksXML ($framework_path) {
  2989. $xml = new SimpleXMLElement(file_get_contents("frameworks.xml"));
  2990. foreach ($xml as $framework) {
  2991. $this->frameworks[(string) $framework->name]["root"] = (string) $framework->root;
  2992. $this->frameworks[(string) $framework->name]["headers"] = (string) $framework->headers;
  2993. if ($framework_path != "")
  2994. $this->frameworks[(string) $framework->name]["headers"] = preg_replace("!^.*/System/Library/Frameworks!", $framework_path, $this->frameworks[(string) $framework->name]["headers"]);
  2995. $this->frameworks[(string) $framework->name]["include_pattern"] = (string) $framework->include_pattern;
  2996. $this->frameworks[(string) $framework->name]["header_pattern"] = (string) $framework->header_pattern;
  2997. $this->frameworks[(string) $framework->name]["external_macro"] = (string) $framework->external_macro;
  2998. $this->frameworks[(string) $framework->name]["ignore_types"] = (string) $framework->ignore_types;
  2999. $this->frameworks[(string) $framework->name]["ignore_methods"] = (string) $framework->ignore_methods;
  3000. $this->frameworks[(string) $framework->name]["replace_types"] = $framework->replace_types;
  3001. $this->frameworks[(string) $framework->name]["ignore_lines"] = $framework->ignore_lines;
  3002. $this->frameworks[(string) $framework->name]["ignore_comments"] = $framework->ignore_comments;
  3003. $this->frameworks[(string) $framework->name]["docset"] = (string)$framework->docset;
  3004. $this->frameworks[(string) $framework->name]["enabled"] = false;
  3005. $this->frameworks[(string) $framework->name]["print"] = true;
  3006. }
  3007. }
  3008. function __construct ($directory, $out_directory, $frameworks, $frameworks_path, $show) {
  3009. $this->root = $directory;
  3010. $this->out = $out_directory;
  3011. $this->show = $show;
  3012. // load defined frameworks from xml
  3013. $this->LoadFrameworksXML($frameworks_path);
  3014. // enable frameworks requested by the command line options
  3015. if ($frameworks) {
  3016. foreach ($frameworks as $name) {
  3017. $name_clean = trim($name, "^ ");
  3018. $this->frameworks[$name_clean]["enabled"] = true;
  3019. // apply options from framework definition
  3020. if ($this->frameworks[$name_clean]["external_macro"]) $this->external_string_macros .= "|".$this->frameworks[$name_clean]["external_macro"];
  3021. if ($this->frameworks[$name_clean]["ignore_types"]) $this->ignore_types = array_merge($this->ignore_types, explode(",", $this->frameworks[$name_clean]["ignore_types"]));
  3022. if ($this->frameworks[$name_clean]["ignore_methods"]) $this->ignore_methods = array_merge($this->ignore_methods, explode(",", $this->frameworks[$name_clean]["ignore_methods"]));
  3023. if ($this->frameworks[$name_clean]["ignore_lines"]) {
  3024. foreach ($this->frameworks[$name_clean]["ignore_lines"]->line as $line) {
  3025. if (!in_array($line, $this->ignore_lines)) $this->ignore_lines[] = (string)$line;
  3026. }
  3027. }
  3028. if ($this->frameworks[$name_clean]["ignore_comments"]) {
  3029. foreach ($this->frameworks[$name_clean]["ignore_comments"]->line as $line) {
  3030. if (!in_array($line, $this->ignore_comments)) $this->ignore_comments[] = (string)$line;
  3031. }
  3032. }
  3033. if ($this->frameworks[$name_clean]["replace_types"]) {
  3034. foreach ($this->frameworks[$name_clean]["replace_types"]->type as $type) {
  3035. $pair = explode("=", (string)$type);
  3036. $this->replace_types[$pair[0]] = $pair[1];
  3037. }
  3038. }
  3039. // print mode
  3040. if ($name[0] == "^") $this->frameworks[$name_clean]["print"] = false;
  3041. }
  3042. }
  3043. //print_r($this->ignore_comments);
  3044. //print_r($this->ignore_lines);
  3045. //print_r($this->frameworks);
  3046. //exit;
  3047. $this->BuildFrameworkClasses();
  3048. }
  3049. }
  3050. ?>