objp.php 123 KB

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