objp.php 124 KB

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