symdef.pas 168 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459
  1. {
  2. Symbol table implementation for the definitions
  3. Copyright (c) 1998-2005 by Florian Klaempfl, Pierre Muller
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symdef;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { global }
  24. globtype,globals,tokens,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,
  27. { ppu }
  28. ppu,
  29. { node }
  30. node,
  31. { aasm }
  32. aasmbase,aasmtai,aasmdata,
  33. cpubase,cpuinfo,
  34. cgbase,cgutils,
  35. parabase
  36. ;
  37. type
  38. {************************************************
  39. TDef
  40. ************************************************}
  41. { tstoreddef }
  42. tstoreddef = class(tdef)
  43. protected
  44. typesymderef : tderef;
  45. public
  46. {$ifdef EXTDEBUG}
  47. fileinfo : tfileposinfo;
  48. {$endif}
  49. { generic support }
  50. genericdef : tstoreddef;
  51. genericdefderef : tderef;
  52. generictokenbuf : tdynamicarray;
  53. constructor create(dt:tdeftyp);
  54. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  55. destructor destroy;override;
  56. function getcopy : tstoreddef;virtual;
  57. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  58. procedure buildderef;override;
  59. procedure buildderefimpl;override;
  60. procedure deref;override;
  61. procedure derefimpl;override;
  62. function size:aint;override;
  63. function getvardef:longint;override;
  64. function alignment:shortint;override;
  65. function is_publishable : boolean;override;
  66. function needs_inittable : boolean;override;
  67. function rtti_mangledname(rt:trttitype):string;override;
  68. function in_currentunit: boolean;
  69. { regvars }
  70. function is_intregable : boolean;
  71. function is_fpuregable : boolean;
  72. { generics }
  73. procedure initgeneric;
  74. private
  75. savesize : aint;
  76. end;
  77. tfiletyp = (ft_text,ft_typed,ft_untyped);
  78. tfiledef = class(tstoreddef)
  79. filetyp : tfiletyp;
  80. typedfiledef : tdef;
  81. typedfiledefderef : tderef;
  82. constructor createtext;
  83. constructor createuntyped;
  84. constructor createtyped(def : tdef);
  85. constructor ppuload(ppufile:tcompilerppufile);
  86. function getcopy : tstoreddef;override;
  87. procedure ppuwrite(ppufile:tcompilerppufile);override;
  88. procedure buildderef;override;
  89. procedure deref;override;
  90. function GetTypeName:string;override;
  91. function getmangledparaname:string;override;
  92. procedure setsize;
  93. end;
  94. tvariantdef = class(tstoreddef)
  95. varianttype : tvarianttype;
  96. constructor create(v : tvarianttype);
  97. constructor ppuload(ppufile:tcompilerppufile);
  98. function getcopy : tstoreddef;override;
  99. function GetTypeName:string;override;
  100. procedure ppuwrite(ppufile:tcompilerppufile);override;
  101. function getvardef:longint;override;
  102. procedure setsize;
  103. function is_publishable : boolean;override;
  104. function needs_inittable : boolean;override;
  105. end;
  106. tformaldef = class(tstoreddef)
  107. typed:boolean;
  108. constructor create(Atyped:boolean);
  109. constructor ppuload(ppufile:tcompilerppufile);
  110. procedure ppuwrite(ppufile:tcompilerppufile);override;
  111. function GetTypeName:string;override;
  112. end;
  113. tforwarddef = class(tstoreddef)
  114. tosymname : pshortstring;
  115. forwardpos : tfileposinfo;
  116. constructor create(const s:string;const pos : tfileposinfo);
  117. destructor destroy;override;
  118. function GetTypeName:string;override;
  119. end;
  120. tundefineddef = class(tstoreddef)
  121. constructor create;
  122. constructor ppuload(ppufile:tcompilerppufile);
  123. procedure ppuwrite(ppufile:tcompilerppufile);override;
  124. function GetTypeName:string;override;
  125. end;
  126. terrordef = class(tstoreddef)
  127. constructor create;
  128. procedure ppuwrite(ppufile:tcompilerppufile);override;
  129. function GetTypeName:string;override;
  130. function getmangledparaname : string;override;
  131. end;
  132. tabstractpointerdef = class(tstoreddef)
  133. pointeddef : tdef;
  134. pointeddefderef : tderef;
  135. constructor create(dt:tdeftyp;def:tdef);
  136. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  137. procedure ppuwrite(ppufile:tcompilerppufile);override;
  138. procedure buildderef;override;
  139. procedure deref;override;
  140. end;
  141. tpointerdef = class(tabstractpointerdef)
  142. is_far : boolean;
  143. constructor create(def:tdef);
  144. constructor createfar(def:tdef);
  145. function getcopy : tstoreddef;override;
  146. constructor ppuload(ppufile:tcompilerppufile);
  147. procedure ppuwrite(ppufile:tcompilerppufile);override;
  148. function GetTypeName:string;override;
  149. end;
  150. tabstractrecorddef= class(tstoreddef)
  151. symtable : TSymtable;
  152. cloneddef : tabstractrecorddef;
  153. cloneddefderef : tderef;
  154. function GetSymtable(t:tGetSymtable):TSymtable;override;
  155. function is_packed:boolean;
  156. end;
  157. trecorddef = class(tabstractrecorddef)
  158. public
  159. isunion : boolean;
  160. constructor create(p : TSymtable);
  161. constructor ppuload(ppufile:tcompilerppufile);
  162. destructor destroy;override;
  163. function getcopy : tstoreddef;override;
  164. procedure ppuwrite(ppufile:tcompilerppufile);override;
  165. procedure buildderef;override;
  166. procedure deref;override;
  167. function size:aint;override;
  168. function alignment : shortint;override;
  169. function padalignment: shortint;
  170. function GetTypeName:string;override;
  171. { debug }
  172. function needs_inittable : boolean;override;
  173. end;
  174. tprocdef = class;
  175. tobjectdef = class;
  176. { TImplementedInterface }
  177. TImplementedInterface = class
  178. IntfDef : tobjectdef;
  179. IntfDefDeref : tderef;
  180. IType : tinterfaceentrytype;
  181. IOffset : longint;
  182. VtblImplIntf : TImplementedInterface;
  183. NameMappings : TFPHashList;
  184. ProcDefs : TFPObjectList;
  185. ImplementsGetter : tsym;
  186. constructor create(aintf: tobjectdef);
  187. constructor create_deref(d:tderef);
  188. destructor destroy; override;
  189. function getcopy:TImplementedInterface;
  190. procedure buildderef;
  191. procedure deref;
  192. procedure AddMapping(const origname, newname: string);
  193. function GetMapping(const origname: string):string;
  194. procedure AddImplProc(pd:tprocdef);
  195. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  196. end;
  197. { tvmtentry }
  198. tvmtentry = record
  199. procdef : tprocdef;
  200. procdefderef : tderef;
  201. visibility : tvisibility;
  202. end;
  203. pvmtentry = ^tvmtentry;
  204. { tobjectdef }
  205. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  206. pmvcallstaticinfo = ^tmvcallstaticinfo;
  207. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  208. tobjectdef = class(tabstractrecorddef)
  209. private
  210. fcurrent_dispid: longint;
  211. public
  212. dwarf_struct_lab : tasmsymbol;
  213. childof : tobjectdef;
  214. childofderef : tderef;
  215. { for C++ classes: name of the library this class is imported from }
  216. import_lib,
  217. objname,
  218. objrealname,
  219. { for Objective-C: protocols and classes can have the same name there }
  220. objextname : pshortstring;
  221. objectoptions : tobjectoptions;
  222. { to be able to have a variable vmt position }
  223. { and no vmt field for objects without virtuals }
  224. vmtentries : TFPList;
  225. vmcallstaticinfo : pmvcallstaticinfo;
  226. vmt_offset : longint;
  227. objecttype : tobjecttyp;
  228. iidguid : pguid;
  229. iidstr : pshortstring;
  230. writing_class_record_dbginfo,
  231. { a class of this type has been created in this module }
  232. created_in_current_module,
  233. { a loadvmtnode for this class has been created in this
  234. module, so if a classrefdef variable of this or a parent
  235. class is used somewhere to instantiate a class, then this
  236. class may be instantiated
  237. }
  238. maybe_created_in_current_module,
  239. { a "class of" this particular class has been created in
  240. this module
  241. }
  242. classref_created_in_current_module : boolean;
  243. { store implemented interfaces defs and name mappings }
  244. ImplementedInterfaces : TFPObjectList;
  245. constructor create(ot : tobjecttyp;const n : string;c : tobjectdef);
  246. constructor ppuload(ppufile:tcompilerppufile);
  247. destructor destroy;override;
  248. function getcopy : tstoreddef;override;
  249. procedure ppuwrite(ppufile:tcompilerppufile);override;
  250. function GetTypeName:string;override;
  251. procedure buildderef;override;
  252. procedure deref;override;
  253. procedure buildderefimpl;override;
  254. procedure derefimpl;override;
  255. procedure resetvmtentries;
  256. procedure copyvmtentries(objdef:tobjectdef);
  257. function getparentdef:tdef;override;
  258. function size : aint;override;
  259. function alignment:shortint;override;
  260. function vmtmethodoffset(index:longint):longint;
  261. function members_need_inittable : boolean;
  262. function find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  263. { this should be called when this class implements an interface }
  264. procedure prepareguid;
  265. function is_publishable : boolean;override;
  266. function is_related(d : tdef) : boolean;override;
  267. function needs_inittable : boolean;override;
  268. function rtti_mangledname(rt:trttitype):string;override;
  269. function vmt_mangledname : string;
  270. procedure check_forwards;
  271. procedure insertvmt;
  272. procedure set_parent(c : tobjectdef);
  273. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  274. function find_destructor: tprocdef;
  275. function implements_any_interfaces: boolean;
  276. { dispinterface support }
  277. function get_next_dispid: longint;
  278. { enumerator support }
  279. function search_enumerator_get: tprocdef;
  280. function search_enumerator_move: tprocdef;
  281. function search_enumerator_current: tsym;
  282. { WPO }
  283. procedure register_created_object_type;override;
  284. procedure register_maybe_created_object_type;
  285. procedure register_created_classref_type;
  286. procedure register_vmt_call(index:longint);
  287. { ObjC & C++ }
  288. procedure make_all_methods_external;
  289. { ObjC }
  290. procedure finish_objc_data;
  291. function check_objc_types: boolean;
  292. { C++ }
  293. procedure finish_cpp_data;
  294. function RttiName: string;
  295. end;
  296. tclassrefdef = class(tabstractpointerdef)
  297. constructor create(def:tdef);
  298. constructor ppuload(ppufile:tcompilerppufile);
  299. procedure ppuwrite(ppufile:tcompilerppufile);override;
  300. function GetTypeName:string;override;
  301. function is_publishable : boolean;override;
  302. function rtti_mangledname(rt:trttitype):string;override;
  303. procedure register_created_object_type;override;
  304. end;
  305. tarraydef = class(tstoreddef)
  306. lowrange,
  307. highrange : aint;
  308. rangedef : tdef;
  309. rangedefderef : tderef;
  310. arrayoptions : tarraydefoptions;
  311. protected
  312. _elementdef : tdef;
  313. _elementdefderef : tderef;
  314. procedure setelementdef(def:tdef);
  315. public
  316. function elesize : aint;
  317. function elepackedbitsize : aint;
  318. function elecount : aword;
  319. constructor create_from_pointer(def:tdef);
  320. constructor create(l,h : aint;def:tdef);
  321. constructor ppuload(ppufile:tcompilerppufile);
  322. function getcopy : tstoreddef;override;
  323. procedure ppuwrite(ppufile:tcompilerppufile);override;
  324. function GetTypeName:string;override;
  325. function getmangledparaname : string;override;
  326. procedure buildderef;override;
  327. procedure deref;override;
  328. function size : aint;override;
  329. function alignment : shortint;override;
  330. { returns the label of the range check string }
  331. function needs_inittable : boolean;override;
  332. property elementdef : tdef read _elementdef write setelementdef;
  333. function is_publishable : boolean;override;
  334. end;
  335. torddef = class(tstoreddef)
  336. low,high : TConstExprInt;
  337. ordtype : tordtype;
  338. constructor create(t : tordtype;v,b : TConstExprInt);
  339. constructor ppuload(ppufile:tcompilerppufile);
  340. function getcopy : tstoreddef;override;
  341. procedure ppuwrite(ppufile:tcompilerppufile);override;
  342. function is_publishable : boolean;override;
  343. function GetTypeName:string;override;
  344. function alignment:shortint;override;
  345. procedure setsize;
  346. function packedbitsize: aint; override;
  347. function getvardef : longint;override;
  348. end;
  349. tfloatdef = class(tstoreddef)
  350. floattype : tfloattype;
  351. constructor create(t : tfloattype);
  352. constructor ppuload(ppufile:tcompilerppufile);
  353. function getcopy : tstoreddef;override;
  354. procedure ppuwrite(ppufile:tcompilerppufile);override;
  355. function GetTypeName:string;override;
  356. function is_publishable : boolean;override;
  357. function alignment:shortint;override;
  358. procedure setsize;
  359. function getvardef:longint;override;
  360. end;
  361. { tabstractprocdef }
  362. tabstractprocdef = class(tstoreddef)
  363. { saves a definition to the return type }
  364. returndef : tdef;
  365. returndefderef : tderef;
  366. parast : TSymtable;
  367. paras : tparalist;
  368. proctypeoption : tproctypeoption;
  369. proccalloption : tproccalloption;
  370. procoptions : tprocoptions;
  371. requiredargarea : aint;
  372. { number of user visibile parameters }
  373. maxparacount,
  374. minparacount : byte;
  375. {$ifdef m68k}
  376. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  377. {$endif}
  378. funcretloc : array[tcallercallee] of TCGPara;
  379. has_paraloc_info : boolean; { paraloc info is available }
  380. constructor create(dt:tdeftyp;level:byte);
  381. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  382. destructor destroy;override;
  383. procedure ppuwrite(ppufile:tcompilerppufile);override;
  384. procedure buildderef;override;
  385. procedure deref;override;
  386. procedure calcparas;
  387. function typename_paras(showhidden:boolean): string;
  388. function is_methodpointer:boolean;virtual;
  389. function is_addressonly:boolean;virtual;
  390. function no_self_node:boolean;
  391. procedure check_mark_as_nested;
  392. private
  393. procedure count_para(p:TObject;arg:pointer);
  394. procedure insert_para(p:TObject;arg:pointer);
  395. end;
  396. tprocvardef = class(tabstractprocdef)
  397. constructor create(level:byte);
  398. constructor ppuload(ppufile:tcompilerppufile);
  399. function getcopy : tstoreddef;override;
  400. procedure ppuwrite(ppufile:tcompilerppufile);override;
  401. function GetSymtable(t:tGetSymtable):TSymtable;override;
  402. function size : aint;override;
  403. function GetTypeName:string;override;
  404. function is_publishable : boolean;override;
  405. function is_methodpointer:boolean;override;
  406. function is_addressonly:boolean;override;
  407. function getmangledparaname:string;override;
  408. end;
  409. tmessageinf = record
  410. case integer of
  411. 0 : (str : pshortstring);
  412. 1 : (i : longint);
  413. end;
  414. tinlininginfo = record
  415. { node tree }
  416. code : tnode;
  417. flags : tprocinfoflags;
  418. end;
  419. pinlininginfo = ^tinlininginfo;
  420. {$ifdef oldregvars}
  421. { register variables }
  422. pregvarinfo = ^tregvarinfo;
  423. tregvarinfo = record
  424. regvars : array[1..maxvarregs] of tsym;
  425. regvars_para : array[1..maxvarregs] of boolean;
  426. regvars_refs : array[1..maxvarregs] of longint;
  427. fpuregvars : array[1..maxfpuvarregs] of tsym;
  428. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  429. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  430. end;
  431. {$endif oldregvars}
  432. { tprocdef }
  433. tprocdef = class(tabstractprocdef)
  434. private
  435. _mangledname : pshortstring;
  436. public
  437. messageinf : tmessageinf;
  438. dispid : longint;
  439. {$ifndef EXTDEBUG}
  440. { where is this function defined and what were the symbol
  441. flags, needed here because there
  442. is only one symbol for all overloaded functions
  443. EXTDEBUG has fileinfo in tdef (PFV) }
  444. fileinfo : tfileposinfo;
  445. {$endif}
  446. symoptions : tsymoptions;
  447. deprecatedmsg : pshortstring;
  448. { symbol owning this definition }
  449. procsym : tsym;
  450. procsymderef : tderef;
  451. { alias names }
  452. aliasnames : TCmdStrList;
  453. { symtables }
  454. localst : TSymtable;
  455. funcretsym : tsym;
  456. funcretsymderef : tderef;
  457. _class : tobjectdef;
  458. _classderef : tderef;
  459. {$if defined(powerpc) or defined(m68k)}
  460. { library symbol for AmigaOS/MorphOS }
  461. libsym : tsym;
  462. libsymderef : tderef;
  463. {$endif powerpc or m68k}
  464. { name of the result variable to insert in the localsymtable }
  465. resultname : pshortstring;
  466. { import info }
  467. import_dll,
  468. import_name : pshortstring;
  469. { info for inlining the subroutine, if this pointer is nil,
  470. the procedure can't be inlined }
  471. inlininginfo : pinlininginfo;
  472. {$ifdef oldregvars}
  473. regvarinfo: pregvarinfo;
  474. {$endif oldregvars}
  475. { First/last assembler symbol/instruction in aasmoutput list.
  476. Note: initialised after compiling the code for the procdef, but
  477. not saved to/restored from ppu. Used when inserting debug info }
  478. procstarttai,
  479. procendtai : tai;
  480. import_nr : word;
  481. extnumber : word;
  482. {$ifdef i386}
  483. fpu_used : byte;
  484. {$endif i386}
  485. visibility : tvisibility;
  486. { true, if the procedure is only declared
  487. (forward procedure) }
  488. forwarddef,
  489. { true if the procedure is declared in the interface }
  490. interfacedef : boolean;
  491. { true if the procedure has a forward declaration }
  492. hasforward : boolean;
  493. constructor create(level:byte);
  494. constructor ppuload(ppufile:tcompilerppufile);
  495. destructor destroy;override;
  496. procedure ppuwrite(ppufile:tcompilerppufile);override;
  497. procedure buildderef;override;
  498. procedure buildderefimpl;override;
  499. procedure deref;override;
  500. procedure derefimpl;override;
  501. function GetSymtable(t:tGetSymtable):TSymtable;override;
  502. function GetTypeName : string;override;
  503. function mangledname : string;
  504. procedure setmangledname(const s : string);
  505. function fullprocname(showhidden:boolean):string;
  506. function cplusplusmangledname : string;
  507. function objcmangledname : string;
  508. function is_methodpointer:boolean;override;
  509. function is_addressonly:boolean;override;
  510. end;
  511. { single linked list of overloaded procs }
  512. pprocdeflist = ^tprocdeflist;
  513. tprocdeflist = record
  514. def : tprocdef;
  515. defderef : tderef;
  516. next : pprocdeflist;
  517. end;
  518. tstringdef = class(tstoreddef)
  519. stringtype : tstringtype;
  520. len : aint;
  521. constructor createshort(l : byte);
  522. constructor loadshort(ppufile:tcompilerppufile);
  523. constructor createlong(l : aint);
  524. constructor loadlong(ppufile:tcompilerppufile);
  525. constructor createansi;
  526. constructor loadansi(ppufile:tcompilerppufile);
  527. constructor createwide;
  528. constructor loadwide(ppufile:tcompilerppufile);
  529. constructor createunicode;
  530. constructor loadunicode(ppufile:tcompilerppufile);
  531. function getcopy : tstoreddef;override;
  532. function stringtypname:string;
  533. procedure ppuwrite(ppufile:tcompilerppufile);override;
  534. function GetTypeName:string;override;
  535. function getmangledparaname:string;override;
  536. function is_publishable : boolean;override;
  537. function alignment : shortint;override;
  538. function needs_inittable : boolean;override;
  539. function getvardef:longint;override;
  540. end;
  541. { tenumdef }
  542. tenumdef = class(tstoreddef)
  543. minval,
  544. maxval : aint;
  545. has_jumps : boolean;
  546. basedef : tenumdef;
  547. basedefderef : tderef;
  548. symtable : TSymtable;
  549. constructor create;
  550. constructor create_subrange(_basedef:tenumdef;_min,_max:aint);
  551. constructor ppuload(ppufile:tcompilerppufile);
  552. destructor destroy;override;
  553. function getcopy : tstoreddef;override;
  554. procedure ppuwrite(ppufile:tcompilerppufile);override;
  555. procedure buildderef;override;
  556. procedure deref;override;
  557. function GetTypeName:string;override;
  558. function is_publishable : boolean;override;
  559. procedure calcsavesize;
  560. function packedbitsize: aint; override;
  561. procedure setmax(_max:aint);
  562. procedure setmin(_min:aint);
  563. function min:aint;
  564. function max:aint;
  565. function getfirstsym:tsym;
  566. end;
  567. tsetdef = class(tstoreddef)
  568. elementdef : tdef;
  569. elementdefderef : tderef;
  570. setbase,
  571. setmax : aword;
  572. constructor create(def:tdef;low, high : aint);
  573. constructor ppuload(ppufile:tcompilerppufile);
  574. function getcopy : tstoreddef;override;
  575. procedure ppuwrite(ppufile:tcompilerppufile);override;
  576. procedure buildderef;override;
  577. procedure deref;override;
  578. function GetTypeName:string;override;
  579. function is_publishable : boolean;override;
  580. end;
  581. var
  582. current_objectdef : tobjectdef; { used for private functions check !! }
  583. { default types }
  584. generrordef, { error in definition }
  585. voidpointertype, { pointer for Void-pointeddef }
  586. charpointertype, { pointer for Char-pointeddef }
  587. widecharpointertype, { pointer for WideChar-pointeddef }
  588. voidfarpointertype,
  589. cundefinedtype,
  590. cformaltype, { unique formal definition }
  591. ctypedformaltype, { unique typed formal definition }
  592. voidtype, { Void (procedure) }
  593. cchartype, { Char }
  594. cwidechartype, { WideChar }
  595. booltype, { boolean type }
  596. bool8type,
  597. bool16type,
  598. bool32type,
  599. bool64type, { implement me }
  600. u8inttype, { 8-Bit unsigned integer }
  601. s8inttype, { 8-Bit signed integer }
  602. u16inttype, { 16-Bit unsigned integer }
  603. s16inttype, { 16-Bit signed integer }
  604. u32inttype, { 32-Bit unsigned integer }
  605. s32inttype, { 32-Bit signed integer }
  606. u64inttype, { 64-bit unsigned integer }
  607. s64inttype, { 64-bit signed integer }
  608. s32floattype, { 32 bit floating point number }
  609. s64floattype, { 64 bit floating point number }
  610. s80floattype, { 80 bit floating point number }
  611. sc80floattype, { 80 bit floating point number but stored like in C }
  612. s64currencytype, { pointer to a currency type }
  613. cshortstringtype, { pointer to type of short string const }
  614. clongstringtype, { pointer to type of long string const }
  615. cansistringtype, { pointer to type of ansi string const }
  616. cwidestringtype, { pointer to type of wide string const }
  617. cunicodestringtype,
  618. openshortstringtype, { pointer to type of an open shortstring,
  619. needed for readln() }
  620. openchararraytype, { pointer to type of an open array of char,
  621. needed for readln() }
  622. cfiletype, { get the same definition for all file }
  623. { used for stabs }
  624. methodpointertype, { typecasting of methodpointers to extract self }
  625. hresultdef,
  626. { we use only one variant def for every variant class }
  627. cvarianttype,
  628. colevarianttype,
  629. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  630. sinttype,
  631. uinttype,
  632. { unsigned and signed ord type with the same size as a pointer }
  633. ptruinttype,
  634. ptrsinttype,
  635. { several types to simulate more or less C++ objects for GDB }
  636. vmttype,
  637. vmtarraytype,
  638. pvmttype : tdef; { type of classrefs, used for stabs }
  639. { pointer to the anchestor of all classes }
  640. class_tobject : tobjectdef;
  641. { pointer to the ancestor of all COM interfaces }
  642. interface_iunknown : tobjectdef;
  643. { pointer to the TGUID type
  644. of all interfaces }
  645. rec_tguid : trecorddef;
  646. { Objective-C base types }
  647. objc_metaclasstype,
  648. objc_superclasstype,
  649. objc_idtype,
  650. objc_seltype : tpointerdef;
  651. objc_objecttype : trecorddef;
  652. { base type of @protocol(protocolname) Objective-C statements }
  653. objc_protocoltype : tobjectdef;
  654. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  655. objc_fastenumeration : tobjectdef;
  656. objc_fastenumerationstate : trecorddef;
  657. const
  658. {$ifdef i386}
  659. pbestrealtype : ^tdef = @s80floattype;
  660. {$endif}
  661. {$ifdef x86_64}
  662. pbestrealtype : ^tdef = @s80floattype;
  663. {$endif}
  664. {$ifdef m68k}
  665. pbestrealtype : ^tdef = @s64floattype;
  666. {$endif}
  667. {$ifdef alpha}
  668. pbestrealtype : ^tdef = @s64floattype;
  669. {$endif}
  670. {$ifdef powerpc}
  671. pbestrealtype : ^tdef = @s64floattype;
  672. {$endif}
  673. {$ifdef POWERPC64}
  674. pbestrealtype : ^tdef = @s64floattype;
  675. {$endif}
  676. {$ifdef ia64}
  677. pbestrealtype : ^tdef = @s64floattype;
  678. {$endif}
  679. {$ifdef SPARC}
  680. pbestrealtype : ^tdef = @s64floattype;
  681. {$endif SPARC}
  682. {$ifdef vis}
  683. pbestrealtype : ^tdef = @s64floattype;
  684. {$endif vis}
  685. {$ifdef ARM}
  686. pbestrealtype : ^tdef = @s64floattype;
  687. {$endif ARM}
  688. {$ifdef MIPS}
  689. pbestrealtype : ^tdef = @s64floattype;
  690. {$endif MIPS}
  691. {$ifdef AVR}
  692. pbestrealtype : ^tdef = @s64floattype;
  693. {$endif AVR}
  694. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  695. { should be in the types unit, but the types unit uses the node stuff :( }
  696. function is_interfacecom(def: tdef): boolean;
  697. function is_interfacecorba(def: tdef): boolean;
  698. function is_interface(def: tdef): boolean;
  699. function is_dispinterface(def: tdef): boolean;
  700. function is_object(def: tdef): boolean;
  701. function is_class(def: tdef): boolean;
  702. function is_cppclass(def: tdef): boolean;
  703. function is_objcclass(def: tdef): boolean;
  704. function is_objcclassref(def: tdef): boolean;
  705. function is_objcprotocol(def: tdef): boolean;
  706. function is_objccategory(def: tdef): boolean;
  707. function is_objc_class_or_protocol(def: tdef): boolean;
  708. function is_objc_protocol_or_category(def: tdef): boolean;
  709. function is_class_or_interface(def: tdef): boolean;
  710. function is_class_or_interface_or_objc(def: tdef): boolean;
  711. function is_class_or_interface_or_object(def: tdef): boolean;
  712. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  713. function is_class_or_interface_or_dispinterface_or_objc(def: tdef): boolean;
  714. function is_class_or_object(def: tdef): boolean;
  715. procedure loadobjctypes;
  716. procedure maybeloadcocoatypes;
  717. function use_vectorfpu(def : tdef) : boolean;
  718. implementation
  719. uses
  720. SysUtils,
  721. cutils,
  722. { global }
  723. verbose,
  724. { target }
  725. systems,aasmcpu,paramgr,
  726. { symtable }
  727. symsym,symtable,symutil,defutil,objcdef,
  728. { module }
  729. fmodule,
  730. { other }
  731. gendef,
  732. fpccrc
  733. ;
  734. {****************************************************************************
  735. Constants
  736. ****************************************************************************}
  737. const
  738. varempty = 0;
  739. varnull = 1;
  740. varsmallint = 2;
  741. varinteger = 3;
  742. varsingle = 4;
  743. vardouble = 5;
  744. varcurrency = 6;
  745. vardate = 7;
  746. varolestr = 8;
  747. vardispatch = 9;
  748. varerror = 10;
  749. varboolean = 11;
  750. varvariant = 12;
  751. varunknown = 13;
  752. vardecimal = 14;
  753. varshortint = 16;
  754. varbyte = 17;
  755. varword = 18;
  756. varlongword = 19;
  757. varint64 = 20;
  758. varqword = 21;
  759. varunicodestr = 22;
  760. varUndefined = -1;
  761. varstrarg = $48;
  762. varstring = $100;
  763. varany = $101;
  764. vardefmask = $fff;
  765. vararray = $2000;
  766. varbyref = $4000;
  767. {****************************************************************************
  768. Helpers
  769. ****************************************************************************}
  770. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  771. var
  772. s,hs,
  773. prefix : string;
  774. oldlen,
  775. newlen,
  776. i : longint;
  777. crc : dword;
  778. hp : tparavarsym;
  779. begin
  780. prefix:='';
  781. if not assigned(st) then
  782. internalerror(200204212);
  783. { sub procedures }
  784. while (st.symtabletype=localsymtable) do
  785. begin
  786. if st.defowner.typ<>procdef then
  787. internalerror(200204173);
  788. { Add the full mangledname of procedure to prevent
  789. conflicts with 2 overloads having both a nested procedure
  790. with the same name, see tb0314 (PFV) }
  791. s:=tprocdef(st.defowner).procsym.name;
  792. oldlen:=length(s);
  793. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  794. begin
  795. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  796. if not(vo_is_hidden_para in hp.varoptions) then
  797. s:=s+'$'+hp.vardef.mangledparaname;
  798. end;
  799. if not is_void(tprocdef(st.defowner).returndef) then
  800. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  801. newlen:=length(s);
  802. { Replace with CRC if the parameter line is very long }
  803. if (newlen-oldlen>12) and
  804. ((newlen+length(prefix)>100) or (newlen-oldlen>32)) then
  805. begin
  806. crc:=0;
  807. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  808. begin
  809. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  810. if not(vo_is_hidden_para in hp.varoptions) then
  811. begin
  812. hs:=hp.vardef.mangledparaname;
  813. crc:=UpdateCrc32(crc,hs[1],length(hs));
  814. end;
  815. end;
  816. hs:=hp.vardef.mangledparaname;
  817. crc:=UpdateCrc32(crc,hs[1],length(hs));
  818. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  819. end;
  820. if prefix<>'' then
  821. prefix:=s+'_'+prefix
  822. else
  823. prefix:=s;
  824. if length(prefix)>100 then
  825. begin
  826. crc:=0;
  827. crc:=UpdateCrc32(crc,prefix[1],length(prefix));
  828. prefix:='$CRC'+hexstr(crc,8);
  829. end;
  830. st:=st.defowner.owner;
  831. end;
  832. { object/classes symtable, nested type definitions in classes require the while loop }
  833. while st.symtabletype=ObjectSymtable do
  834. begin
  835. if st.defowner.typ<>objectdef then
  836. internalerror(200204174);
  837. prefix:=tobjectdef(st.defowner).objname^+'_$_'+prefix;
  838. st:=st.defowner.owner;
  839. end;
  840. { symtable must now be static or global }
  841. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  842. internalerror(200204175);
  843. result:='';
  844. if typeprefix<>'' then
  845. result:=result+typeprefix+'_';
  846. { Add P$ for program, which can have the same name as
  847. a unit }
  848. if (TSymtable(main_module.localsymtable)=st) and
  849. (not main_module.is_unit) then
  850. result:=result+'P$'+st.name^
  851. else
  852. result:=result+st.name^;
  853. if prefix<>'' then
  854. result:=result+'_'+prefix;
  855. if suffix<>'' then
  856. result:=result+'_'+suffix;
  857. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  858. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  859. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  860. { those in the debug map, leading to troubles with dsymutil). So always }
  861. { add an underscore on darwin. }
  862. if (target_info.system in systems_darwin) then
  863. result := '_' + result;
  864. end;
  865. {****************************************************************************
  866. TDEF (base class for definitions)
  867. ****************************************************************************}
  868. constructor tstoreddef.create(dt:tdeftyp);
  869. var
  870. insertstack : psymtablestackitem;
  871. begin
  872. inherited create(dt);
  873. savesize := 0;
  874. {$ifdef EXTDEBUG}
  875. fileinfo := current_filepos;
  876. {$endif}
  877. generictokenbuf:=nil;
  878. genericdef:=nil;
  879. { Don't register forwarddefs, they are disposed at the
  880. end of an type block }
  881. if (dt=forwarddef) then
  882. exit;
  883. { Register in current_module }
  884. if assigned(current_module) then
  885. begin
  886. current_module.deflist.Add(self);
  887. DefId:=current_module.deflist.Count-1;
  888. end;
  889. { Register in symtable stack }
  890. if assigned(symtablestack) then
  891. begin
  892. insertstack:=symtablestack.stack;
  893. while assigned(insertstack) and
  894. (insertstack^.symtable.symtabletype=withsymtable) do
  895. insertstack:=insertstack^.next;
  896. if not assigned(insertstack) then
  897. internalerror(200602044);
  898. insertstack^.symtable.insertdef(self);
  899. end;
  900. end;
  901. destructor tstoreddef.destroy;
  902. begin
  903. { Direct calls are not allowed, use symtable.deletedef() }
  904. if assigned(owner) then
  905. internalerror(200612311);
  906. if assigned(generictokenbuf) then
  907. begin
  908. generictokenbuf.free;
  909. generictokenbuf:=nil;
  910. end;
  911. inherited destroy;
  912. end;
  913. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  914. var
  915. sizeleft,i : longint;
  916. buf : array[0..255] of byte;
  917. begin
  918. inherited create(dt);
  919. DefId:=ppufile.getlongint;
  920. current_module.deflist[DefId]:=self;
  921. {$ifdef EXTDEBUG}
  922. fillchar(fileinfo,sizeof(fileinfo),0);
  923. {$endif}
  924. { load }
  925. ppufile.getderef(typesymderef);
  926. ppufile.getsmallset(defoptions);
  927. ppufile.getsmallset(defstates);
  928. if df_generic in defoptions then
  929. begin
  930. sizeleft:=ppufile.getlongint;
  931. initgeneric;
  932. while sizeleft>0 do
  933. begin
  934. if sizeleft>sizeof(buf) then
  935. i:=sizeof(buf)
  936. else
  937. i:=sizeleft;
  938. ppufile.getdata(buf,i);
  939. generictokenbuf.write(buf,i);
  940. dec(sizeleft,i);
  941. end;
  942. end;
  943. if df_specialization in defoptions then
  944. ppufile.getderef(genericdefderef);
  945. end;
  946. function Tstoreddef.rtti_mangledname(rt:trttitype):string;
  947. var
  948. prefix : string[4];
  949. begin
  950. if rt=fullrtti then
  951. begin
  952. prefix:='RTTI';
  953. include(defstates,ds_rtti_table_used);
  954. end
  955. else
  956. begin
  957. prefix:='INIT';
  958. include(defstates,ds_init_table_used);
  959. end;
  960. if assigned(typesym) and
  961. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  962. result:=make_mangledname(prefix,owner,typesym.name)
  963. else
  964. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  965. end;
  966. function tstoreddef.in_currentunit: boolean;
  967. var
  968. st: tsymtable;
  969. begin
  970. st:=owner;
  971. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  972. st:=st.defowner.owner;
  973. result:=st.iscurrentunit;
  974. end;
  975. function tstoreddef.getcopy : tstoreddef;
  976. begin
  977. Message(sym_e_cant_create_unique_type);
  978. getcopy:=terrordef.create;
  979. end;
  980. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  981. var
  982. sizeleft,i : longint;
  983. buf : array[0..255] of byte;
  984. oldintfcrc : boolean;
  985. begin
  986. ppufile.putlongint(DefId);
  987. ppufile.putderef(typesymderef);
  988. ppufile.putsmallset(defoptions);
  989. oldintfcrc:=ppufile.do_crc;
  990. ppufile.do_crc:=false;
  991. ppufile.putsmallset(defstates);
  992. if df_generic in defoptions then
  993. begin
  994. if assigned(generictokenbuf) then
  995. begin
  996. sizeleft:=generictokenbuf.size;
  997. generictokenbuf.seek(0);
  998. end
  999. else
  1000. sizeleft:=0;
  1001. ppufile.putlongint(sizeleft);
  1002. while sizeleft>0 do
  1003. begin
  1004. if sizeleft>sizeof(buf) then
  1005. i:=sizeof(buf)
  1006. else
  1007. i:=sizeleft;
  1008. generictokenbuf.read(buf,i);
  1009. ppufile.putdata(buf,i);
  1010. dec(sizeleft,i);
  1011. end;
  1012. end;
  1013. ppufile.do_crc:=oldintfcrc;
  1014. if df_specialization in defoptions then
  1015. ppufile.putderef(genericdefderef);
  1016. end;
  1017. procedure tstoreddef.buildderef;
  1018. begin
  1019. typesymderef.build(typesym);
  1020. genericdefderef.build(genericdef);
  1021. end;
  1022. procedure tstoreddef.buildderefimpl;
  1023. begin
  1024. end;
  1025. procedure tstoreddef.deref;
  1026. begin
  1027. typesym:=ttypesym(typesymderef.resolve);
  1028. if df_specialization in defoptions then
  1029. genericdef:=tstoreddef(genericdefderef.resolve);
  1030. end;
  1031. procedure tstoreddef.derefimpl;
  1032. begin
  1033. end;
  1034. function tstoreddef.size : aint;
  1035. begin
  1036. size:=savesize;
  1037. end;
  1038. function tstoreddef.getvardef:longint;
  1039. begin
  1040. result:=varUndefined;
  1041. end;
  1042. function tstoreddef.alignment : shortint;
  1043. begin
  1044. { natural alignment by default }
  1045. alignment:=size_2_align(savesize);
  1046. { can happen if savesize = 0, e.g. for voiddef or
  1047. an empty record
  1048. }
  1049. if (alignment=0) then
  1050. alignment:=1;
  1051. end;
  1052. { returns true, if the definition can be published }
  1053. function tstoreddef.is_publishable : boolean;
  1054. begin
  1055. is_publishable:=false;
  1056. end;
  1057. { needs an init table }
  1058. function tstoreddef.needs_inittable : boolean;
  1059. begin
  1060. needs_inittable:=false;
  1061. end;
  1062. function tstoreddef.is_intregable : boolean;
  1063. var
  1064. recsize,temp: longint;
  1065. begin
  1066. is_intregable:=false;
  1067. case typ of
  1068. orddef,
  1069. pointerdef,
  1070. enumdef,
  1071. classrefdef:
  1072. is_intregable:=true;
  1073. procvardef :
  1074. is_intregable:=tprocvardef(self).is_addressonly;
  1075. objectdef:
  1076. is_intregable:=(is_class_or_interface_or_dispinterface_or_objc(self)) and not needs_inittable;
  1077. setdef:
  1078. is_intregable:=is_smallset(self);
  1079. recorddef:
  1080. begin
  1081. recsize:=size;
  1082. is_intregable:=
  1083. ispowerof2(recsize,temp) and
  1084. (recsize <= sizeof(aint));
  1085. end;
  1086. end;
  1087. end;
  1088. function tstoreddef.is_fpuregable : boolean;
  1089. begin
  1090. {$ifdef x86}
  1091. result:=use_vectorfpu(self);
  1092. {$else x86}
  1093. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1094. {$endif x86}
  1095. end;
  1096. procedure tstoreddef.initgeneric;
  1097. begin
  1098. if assigned(generictokenbuf) then
  1099. internalerror(200512131);
  1100. generictokenbuf:=tdynamicarray.create(256);
  1101. end;
  1102. {****************************************************************************
  1103. Tstringdef
  1104. ****************************************************************************}
  1105. constructor tstringdef.createshort(l : byte);
  1106. begin
  1107. inherited create(stringdef);
  1108. stringtype:=st_shortstring;
  1109. len:=l;
  1110. savesize:=len+1;
  1111. end;
  1112. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1113. begin
  1114. inherited ppuload(stringdef,ppufile);
  1115. stringtype:=st_shortstring;
  1116. len:=ppufile.getbyte;
  1117. savesize:=len+1;
  1118. end;
  1119. constructor tstringdef.createlong(l : aint);
  1120. begin
  1121. inherited create(stringdef);
  1122. stringtype:=st_longstring;
  1123. len:=l;
  1124. savesize:=sizeof(pint);
  1125. end;
  1126. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1127. begin
  1128. inherited ppuload(stringdef,ppufile);
  1129. stringtype:=st_longstring;
  1130. len:=ppufile.getaint;
  1131. savesize:=sizeof(pint);
  1132. end;
  1133. constructor tstringdef.createansi;
  1134. begin
  1135. inherited create(stringdef);
  1136. stringtype:=st_ansistring;
  1137. len:=-1;
  1138. savesize:=sizeof(pint);
  1139. end;
  1140. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1141. begin
  1142. inherited ppuload(stringdef,ppufile);
  1143. stringtype:=st_ansistring;
  1144. len:=ppufile.getaint;
  1145. savesize:=sizeof(pint);
  1146. end;
  1147. constructor tstringdef.createwide;
  1148. begin
  1149. inherited create(stringdef);
  1150. stringtype:=st_widestring;
  1151. len:=-1;
  1152. savesize:=sizeof(pint);
  1153. end;
  1154. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1155. begin
  1156. inherited ppuload(stringdef,ppufile);
  1157. stringtype:=st_widestring;
  1158. len:=ppufile.getaint;
  1159. savesize:=sizeof(pint);
  1160. end;
  1161. constructor tstringdef.createunicode;
  1162. begin
  1163. inherited create(stringdef);
  1164. stringtype:=st_unicodestring;
  1165. len:=-1;
  1166. savesize:=sizeof(pint);
  1167. end;
  1168. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  1169. begin
  1170. inherited ppuload(stringdef,ppufile);
  1171. stringtype:=st_unicodestring;
  1172. len:=ppufile.getaint;
  1173. savesize:=sizeof(pint);
  1174. end;
  1175. function tstringdef.getcopy : tstoreddef;
  1176. begin
  1177. result:=tstringdef.create(typ);
  1178. result.typ:=stringdef;
  1179. tstringdef(result).stringtype:=stringtype;
  1180. tstringdef(result).len:=len;
  1181. tstringdef(result).savesize:=savesize;
  1182. end;
  1183. function tstringdef.stringtypname:string;
  1184. const
  1185. typname:array[tstringtype] of string[10]=(
  1186. 'shortstr','longstr','ansistr','widestr','unicodestr'
  1187. );
  1188. begin
  1189. stringtypname:=typname[stringtype];
  1190. end;
  1191. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1192. begin
  1193. inherited ppuwrite(ppufile);
  1194. if stringtype=st_shortstring then
  1195. begin
  1196. {$ifdef extdebug}
  1197. if len > 255 then internalerror(12122002);
  1198. {$endif}
  1199. ppufile.putbyte(byte(len))
  1200. end
  1201. else
  1202. ppufile.putaint(len);
  1203. case stringtype of
  1204. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1205. st_longstring : ppufile.writeentry(iblongstringdef);
  1206. st_ansistring : ppufile.writeentry(ibansistringdef);
  1207. st_widestring : ppufile.writeentry(ibwidestringdef);
  1208. st_unicodestring : ppufile.writeentry(ibunicodestringdef);
  1209. end;
  1210. end;
  1211. function tstringdef.needs_inittable : boolean;
  1212. begin
  1213. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1214. end;
  1215. function tstringdef.GetTypeName : string;
  1216. const
  1217. names : array[tstringtype] of string[15] = (
  1218. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  1219. begin
  1220. GetTypeName:=names[stringtype];
  1221. end;
  1222. function tstringdef.getvardef : longint;
  1223. const
  1224. vardef : array[tstringtype] of longint = (
  1225. varUndefined,varUndefined,varString,varOleStr,varUnicodeStr);
  1226. begin
  1227. result:=vardef[stringtype];
  1228. end;
  1229. function tstringdef.alignment : shortint;
  1230. begin
  1231. case stringtype of
  1232. st_unicodestring,
  1233. st_widestring,
  1234. st_ansistring:
  1235. alignment:=size_2_align(savesize);
  1236. st_longstring,
  1237. st_shortstring:
  1238. { char to string accesses byte 0 and 1 with one word access }
  1239. if (tf_requires_proper_alignment in target_info.flags) or
  1240. { macpas needs an alignment of 2 (MetroWerks compatible) }
  1241. (m_mac in current_settings.modeswitches) then
  1242. alignment:=size_2_align(2)
  1243. else
  1244. alignment:=size_2_align(1);
  1245. else
  1246. internalerror(200412301);
  1247. end;
  1248. end;
  1249. function tstringdef.getmangledparaname : string;
  1250. begin
  1251. getmangledparaname:='STRING';
  1252. end;
  1253. function tstringdef.is_publishable : boolean;
  1254. begin
  1255. is_publishable:=true;
  1256. end;
  1257. {****************************************************************************
  1258. TENUMDEF
  1259. ****************************************************************************}
  1260. constructor tenumdef.create;
  1261. begin
  1262. inherited create(enumdef);
  1263. minval:=0;
  1264. maxval:=0;
  1265. calcsavesize;
  1266. has_jumps:=false;
  1267. basedef:=nil;
  1268. symtable:=tenumsymtable.create(self);
  1269. end;
  1270. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:aint);
  1271. begin
  1272. inherited create(enumdef);
  1273. minval:=_min;
  1274. maxval:=_max;
  1275. basedef:=_basedef;
  1276. calcsavesize;
  1277. has_jumps:=false;
  1278. symtable:=basedef.symtable.getcopy;
  1279. include(defoptions, df_copied_def);
  1280. end;
  1281. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1282. begin
  1283. inherited ppuload(enumdef,ppufile);
  1284. minval:=ppufile.getaint;
  1285. maxval:=ppufile.getaint;
  1286. savesize:=ppufile.getaint;
  1287. has_jumps:=false;
  1288. if df_copied_def in defoptions then
  1289. begin
  1290. symtable:=nil;
  1291. ppufile.getderef(basedefderef);
  1292. end
  1293. else
  1294. begin
  1295. // create with nil defowner first to prevent values changes on insert
  1296. symtable:=tenumsymtable.create(nil);
  1297. tenumsymtable(symtable).ppuload(ppufile);
  1298. symtable.defowner:=self;
  1299. end;
  1300. end;
  1301. destructor tenumdef.destroy;
  1302. begin
  1303. symtable.free;
  1304. symtable:=nil;
  1305. inherited destroy;
  1306. end;
  1307. function tenumdef.getcopy : tstoreddef;
  1308. begin
  1309. if assigned(basedef) then
  1310. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1311. else
  1312. begin
  1313. result:=tenumdef.create;
  1314. tenumdef(result).minval:=minval;
  1315. tenumdef(result).maxval:=maxval;
  1316. tenumdef(result).symtable.free;
  1317. tenumdef(result).symtable:=symtable.getcopy;
  1318. tenumdef(result).basedef:=self;
  1319. end;
  1320. tenumdef(result).has_jumps:=has_jumps;
  1321. tenumdef(result).basedefderef:=basedefderef;
  1322. include(tenumdef(result).defoptions,df_copied_def);
  1323. end;
  1324. procedure tenumdef.calcsavesize;
  1325. begin
  1326. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1327. savesize:=8
  1328. else
  1329. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  1330. savesize:=4
  1331. else
  1332. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1333. savesize:=2
  1334. else
  1335. savesize:=1;
  1336. end;
  1337. function tenumdef.packedbitsize: aint;
  1338. var
  1339. sizeval: tconstexprint;
  1340. power: longint;
  1341. begin
  1342. result := 0;
  1343. if (minval >= 0) and
  1344. (maxval <= 1) then
  1345. result := 1
  1346. else
  1347. begin
  1348. if (minval>=0) then
  1349. sizeval:=maxval
  1350. else
  1351. { don't count 0 twice }
  1352. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  1353. { 256 must become 512 etc. }
  1354. nextpowerof2(sizeval+1,power);
  1355. result := power;
  1356. end;
  1357. end;
  1358. procedure tenumdef.setmax(_max:aint);
  1359. begin
  1360. maxval:=_max;
  1361. calcsavesize;
  1362. end;
  1363. procedure tenumdef.setmin(_min:aint);
  1364. begin
  1365. minval:=_min;
  1366. calcsavesize;
  1367. end;
  1368. function tenumdef.min:aint;
  1369. begin
  1370. min:=minval;
  1371. end;
  1372. function tenumdef.max:aint;
  1373. begin
  1374. max:=maxval;
  1375. end;
  1376. function tenumdef.getfirstsym: tsym;
  1377. var
  1378. i:integer;
  1379. begin
  1380. for i := 0 to symtable.SymList.Count - 1 do
  1381. begin
  1382. result:=tsym(symtable.SymList[i]);
  1383. if tenumsym(result).value=minval then
  1384. exit;
  1385. end;
  1386. result:=nil;
  1387. end;
  1388. procedure tenumdef.buildderef;
  1389. begin
  1390. inherited buildderef;
  1391. if df_copied_def in defoptions then
  1392. basedefderef.build(basedef)
  1393. else
  1394. tenumsymtable(symtable).buildderef;
  1395. end;
  1396. procedure tenumdef.deref;
  1397. begin
  1398. inherited deref;
  1399. if df_copied_def in defoptions then
  1400. begin
  1401. basedef:=tenumdef(basedefderef.resolve);
  1402. symtable:=basedef.symtable.getcopy;
  1403. end
  1404. else
  1405. tenumsymtable(symtable).deref;
  1406. end;
  1407. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1408. begin
  1409. inherited ppuwrite(ppufile);
  1410. ppufile.putaint(min);
  1411. ppufile.putaint(max);
  1412. ppufile.putaint(savesize);
  1413. if df_copied_def in defoptions then
  1414. ppufile.putderef(basedefderef);
  1415. ppufile.writeentry(ibenumdef);
  1416. if not (df_copied_def in defoptions) then
  1417. tenumsymtable(symtable).ppuwrite(ppufile);
  1418. end;
  1419. function tenumdef.is_publishable : boolean;
  1420. begin
  1421. is_publishable:=true;
  1422. end;
  1423. function tenumdef.GetTypeName : string;
  1424. begin
  1425. GetTypeName:='<enumeration type>';
  1426. end;
  1427. {****************************************************************************
  1428. TORDDEF
  1429. ****************************************************************************}
  1430. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  1431. begin
  1432. inherited create(orddef);
  1433. low:=v;
  1434. high:=b;
  1435. ordtype:=t;
  1436. setsize;
  1437. end;
  1438. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1439. begin
  1440. inherited ppuload(orddef,ppufile);
  1441. ordtype:=tordtype(ppufile.getbyte);
  1442. low:=ppufile.getexprint;
  1443. high:=ppufile.getexprint;
  1444. setsize;
  1445. end;
  1446. function torddef.getcopy : tstoreddef;
  1447. begin
  1448. result:=torddef.create(ordtype,low,high);
  1449. result.typ:=orddef;
  1450. torddef(result).low:=low;
  1451. torddef(result).high:=high;
  1452. torddef(result).ordtype:=ordtype;
  1453. torddef(result).savesize:=savesize;
  1454. end;
  1455. function torddef.alignment:shortint;
  1456. begin
  1457. if (target_info.system in [system_i386_darwin,system_arm_darwin]) and
  1458. (ordtype in [s64bit,u64bit]) then
  1459. result := 4
  1460. else
  1461. result := inherited alignment;
  1462. end;
  1463. procedure torddef.setsize;
  1464. const
  1465. sizetbl : array[tordtype] of longint = (
  1466. 0,
  1467. 1,2,4,8,
  1468. 1,2,4,8,
  1469. 1,1,2,4,8,
  1470. 1,2,8
  1471. );
  1472. begin
  1473. savesize:=sizetbl[ordtype];
  1474. end;
  1475. function torddef.packedbitsize: aint;
  1476. var
  1477. sizeval: tconstexprint;
  1478. power: longint;
  1479. begin
  1480. result := 0;
  1481. if ordtype = uvoid then
  1482. exit;
  1483. {$ifndef cpu64bitalu}
  1484. if (ordtype in [s64bit,u64bit]) then
  1485. {$else not cpu64bitalu}
  1486. if (ordtype = u64bit) or
  1487. ((ordtype = s64bit) and
  1488. ((low <= (system.low(int64) div 2)) or
  1489. (high > (system.high(int64) div 2)))) then
  1490. {$endif cpu64bitalu}
  1491. result := 64
  1492. else if (low >= 0) and
  1493. (high <= 1) then
  1494. result := 1
  1495. else
  1496. begin
  1497. if (low>=0) then
  1498. sizeval:=high
  1499. else
  1500. { don't count 0 twice }
  1501. sizeval:=(cutils.max(-low,high)*2)-1;
  1502. { 256 must become 512 etc. }
  1503. nextpowerof2(sizeval+1,power);
  1504. result := power;
  1505. end;
  1506. end;
  1507. function torddef.getvardef : longint;
  1508. const
  1509. basetype2vardef : array[tordtype] of longint = (
  1510. varUndefined,
  1511. varbyte,varqword,varlongword,varqword,
  1512. varshortint,varsmallint,varinteger,varint64,
  1513. varboolean,varboolean,varboolean,varUndefined,varUndefined,
  1514. varUndefined,varUndefined,varCurrency);
  1515. begin
  1516. result:=basetype2vardef[ordtype];
  1517. end;
  1518. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  1519. begin
  1520. inherited ppuwrite(ppufile);
  1521. ppufile.putbyte(byte(ordtype));
  1522. ppufile.putexprint(low);
  1523. ppufile.putexprint(high);
  1524. ppufile.writeentry(iborddef);
  1525. end;
  1526. function torddef.is_publishable : boolean;
  1527. begin
  1528. is_publishable:=(ordtype<>uvoid);
  1529. end;
  1530. function torddef.GetTypeName : string;
  1531. const
  1532. names : array[tordtype] of string[20] = (
  1533. 'untyped',
  1534. 'Byte','Word','DWord','QWord',
  1535. 'ShortInt','SmallInt','LongInt','Int64',
  1536. 'Boolean','ByteBool','WordBool','LongBool','QWordBool',
  1537. 'Char','WideChar','Currency');
  1538. begin
  1539. GetTypeName:=names[ordtype];
  1540. end;
  1541. {****************************************************************************
  1542. TFLOATDEF
  1543. ****************************************************************************}
  1544. constructor tfloatdef.create(t : tfloattype);
  1545. begin
  1546. inherited create(floatdef);
  1547. floattype:=t;
  1548. setsize;
  1549. end;
  1550. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  1551. begin
  1552. inherited ppuload(floatdef,ppufile);
  1553. floattype:=tfloattype(ppufile.getbyte);
  1554. setsize;
  1555. end;
  1556. function tfloatdef.getcopy : tstoreddef;
  1557. begin
  1558. result:=tfloatdef.create(floattype);
  1559. result.typ:=floatdef;
  1560. tfloatdef(result).savesize:=savesize;
  1561. end;
  1562. function tfloatdef.alignment:shortint;
  1563. begin
  1564. if (target_info.system in [system_i386_darwin,system_arm_darwin]) then
  1565. case floattype of
  1566. s80real: result:=16;
  1567. s64real,
  1568. s64currency,
  1569. s64comp : result:=4;
  1570. else
  1571. result := inherited alignment;
  1572. end
  1573. else
  1574. result := inherited alignment;
  1575. end;
  1576. procedure tfloatdef.setsize;
  1577. begin
  1578. case floattype of
  1579. s32real : savesize:=4;
  1580. s80real : savesize:=10;
  1581. sc80real:
  1582. if target_info.system in [system_i386_darwin,system_x86_64_darwin,
  1583. system_x86_64_linux,system_x86_64_freebsd,
  1584. system_x86_64_solaris,system_x86_64_embedded] then
  1585. savesize:=16
  1586. else
  1587. savesize:=12;
  1588. s64real,
  1589. s64currency,
  1590. s64comp : savesize:=8;
  1591. else
  1592. savesize:=0;
  1593. end;
  1594. end;
  1595. function tfloatdef.getvardef : longint;
  1596. const
  1597. floattype2vardef : array[tfloattype] of longint = (
  1598. varSingle,varDouble,varUndefined,varUndefined,
  1599. varUndefined,varCurrency,varUndefined);
  1600. begin
  1601. if (upper(typename)='TDATETIME') and
  1602. assigned(owner) and
  1603. assigned(owner.name) and
  1604. (owner.name^='SYSTEM') then
  1605. result:=varDate
  1606. else
  1607. result:=floattype2vardef[floattype];
  1608. end;
  1609. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  1610. begin
  1611. inherited ppuwrite(ppufile);
  1612. ppufile.putbyte(byte(floattype));
  1613. ppufile.writeentry(ibfloatdef);
  1614. end;
  1615. function tfloatdef.is_publishable : boolean;
  1616. begin
  1617. is_publishable:=true;
  1618. end;
  1619. function tfloatdef.GetTypeName : string;
  1620. const
  1621. names : array[tfloattype] of string[20] = (
  1622. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  1623. begin
  1624. GetTypeName:=names[floattype];
  1625. end;
  1626. {****************************************************************************
  1627. TFILEDEF
  1628. ****************************************************************************}
  1629. constructor tfiledef.createtext;
  1630. begin
  1631. inherited create(filedef);
  1632. filetyp:=ft_text;
  1633. typedfiledef:=nil;
  1634. setsize;
  1635. end;
  1636. constructor tfiledef.createuntyped;
  1637. begin
  1638. inherited create(filedef);
  1639. filetyp:=ft_untyped;
  1640. typedfiledef:=nil;
  1641. setsize;
  1642. end;
  1643. constructor tfiledef.createtyped(def:tdef);
  1644. begin
  1645. inherited create(filedef);
  1646. filetyp:=ft_typed;
  1647. typedfiledef:=def;
  1648. setsize;
  1649. end;
  1650. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  1651. begin
  1652. inherited ppuload(filedef,ppufile);
  1653. filetyp:=tfiletyp(ppufile.getbyte);
  1654. if filetyp=ft_typed then
  1655. ppufile.getderef(typedfiledefderef)
  1656. else
  1657. typedfiledef:=nil;
  1658. setsize;
  1659. end;
  1660. function tfiledef.getcopy : tstoreddef;
  1661. begin
  1662. case filetyp of
  1663. ft_typed:
  1664. result:=tfiledef.createtyped(typedfiledef);
  1665. ft_untyped:
  1666. result:=tfiledef.createuntyped;
  1667. ft_text:
  1668. result:=tfiledef.createtext;
  1669. else
  1670. internalerror(2004121201);
  1671. end;
  1672. end;
  1673. procedure tfiledef.buildderef;
  1674. begin
  1675. inherited buildderef;
  1676. if filetyp=ft_typed then
  1677. typedfiledefderef.build(typedfiledef);
  1678. end;
  1679. procedure tfiledef.deref;
  1680. begin
  1681. inherited deref;
  1682. if filetyp=ft_typed then
  1683. typedfiledef:=tdef(typedfiledefderef.resolve);
  1684. end;
  1685. procedure tfiledef.setsize;
  1686. begin
  1687. {$ifdef cpu64bitaddr}
  1688. case filetyp of
  1689. ft_text :
  1690. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1691. savesize:=632{+8}
  1692. else
  1693. savesize:=628{+8};
  1694. ft_typed,
  1695. ft_untyped :
  1696. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1697. savesize:=372
  1698. else
  1699. savesize:=368;
  1700. end;
  1701. {$else cpu64bitaddr}
  1702. case filetyp of
  1703. ft_text :
  1704. savesize:=592{+4};
  1705. ft_typed,
  1706. ft_untyped :
  1707. savesize:=332;
  1708. end;
  1709. {$endif cpu64bitaddr}
  1710. end;
  1711. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  1712. begin
  1713. inherited ppuwrite(ppufile);
  1714. ppufile.putbyte(byte(filetyp));
  1715. if filetyp=ft_typed then
  1716. ppufile.putderef(typedfiledefderef);
  1717. ppufile.writeentry(ibfiledef);
  1718. end;
  1719. function tfiledef.GetTypeName : string;
  1720. begin
  1721. case filetyp of
  1722. ft_untyped:
  1723. GetTypeName:='File';
  1724. ft_typed:
  1725. GetTypeName:='File Of '+typedfiledef.typename;
  1726. ft_text:
  1727. GetTypeName:='Text'
  1728. end;
  1729. end;
  1730. function tfiledef.getmangledparaname : string;
  1731. begin
  1732. case filetyp of
  1733. ft_untyped:
  1734. getmangledparaname:='FILE';
  1735. ft_typed:
  1736. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  1737. ft_text:
  1738. getmangledparaname:='TEXT'
  1739. end;
  1740. end;
  1741. {****************************************************************************
  1742. TVARIANTDEF
  1743. ****************************************************************************}
  1744. constructor tvariantdef.create(v : tvarianttype);
  1745. begin
  1746. inherited create(variantdef);
  1747. varianttype:=v;
  1748. setsize;
  1749. end;
  1750. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  1751. begin
  1752. inherited ppuload(variantdef,ppufile);
  1753. varianttype:=tvarianttype(ppufile.getbyte);
  1754. setsize;
  1755. end;
  1756. function tvariantdef.getcopy : tstoreddef;
  1757. begin
  1758. result:=tvariantdef.create(varianttype);
  1759. end;
  1760. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  1761. begin
  1762. inherited ppuwrite(ppufile);
  1763. ppufile.putbyte(byte(varianttype));
  1764. ppufile.writeentry(ibvariantdef);
  1765. end;
  1766. function tvariantdef.getvardef : longint;
  1767. begin
  1768. Result:=varVariant;
  1769. end;
  1770. procedure tvariantdef.setsize;
  1771. begin
  1772. {$ifdef cpu64bitaddr}
  1773. savesize:=24;
  1774. {$else cpu64bitaddr}
  1775. savesize:=16;
  1776. {$endif cpu64bitaddr}
  1777. end;
  1778. function tvariantdef.GetTypeName : string;
  1779. begin
  1780. case varianttype of
  1781. vt_normalvariant:
  1782. GetTypeName:='Variant';
  1783. vt_olevariant:
  1784. GetTypeName:='OleVariant';
  1785. end;
  1786. end;
  1787. function tvariantdef.needs_inittable : boolean;
  1788. begin
  1789. needs_inittable:=true;
  1790. end;
  1791. function tvariantdef.is_publishable : boolean;
  1792. begin
  1793. is_publishable:=true;
  1794. end;
  1795. {****************************************************************************
  1796. TABSTRACtpointerdef
  1797. ****************************************************************************}
  1798. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  1799. begin
  1800. inherited create(dt);
  1801. pointeddef:=def;
  1802. savesize:=sizeof(pint);
  1803. end;
  1804. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1805. begin
  1806. inherited ppuload(dt,ppufile);
  1807. ppufile.getderef(pointeddefderef);
  1808. savesize:=sizeof(pint);
  1809. end;
  1810. procedure tabstractpointerdef.buildderef;
  1811. begin
  1812. inherited buildderef;
  1813. pointeddefderef.build(pointeddef);
  1814. end;
  1815. procedure tabstractpointerdef.deref;
  1816. begin
  1817. inherited deref;
  1818. pointeddef:=tdef(pointeddefderef.resolve);
  1819. end;
  1820. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1821. begin
  1822. inherited ppuwrite(ppufile);
  1823. ppufile.putderef(pointeddefderef);
  1824. end;
  1825. {****************************************************************************
  1826. tpointerdef
  1827. ****************************************************************************}
  1828. constructor tpointerdef.create(def:tdef);
  1829. begin
  1830. inherited create(pointerdef,def);
  1831. is_far:=false;
  1832. end;
  1833. constructor tpointerdef.createfar(def:tdef);
  1834. begin
  1835. inherited create(pointerdef,def);
  1836. is_far:=true;
  1837. end;
  1838. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  1839. begin
  1840. inherited ppuload(pointerdef,ppufile);
  1841. is_far:=(ppufile.getbyte<>0);
  1842. end;
  1843. function tpointerdef.getcopy : tstoreddef;
  1844. begin
  1845. result:=tpointerdef.create(pointeddef);
  1846. tpointerdef(result).is_far:=is_far;
  1847. tpointerdef(result).savesize:=savesize;
  1848. end;
  1849. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1850. begin
  1851. inherited ppuwrite(ppufile);
  1852. ppufile.putbyte(byte(is_far));
  1853. ppufile.writeentry(ibpointerdef);
  1854. end;
  1855. function tpointerdef.GetTypeName : string;
  1856. begin
  1857. if is_far then
  1858. GetTypeName:='^'+pointeddef.typename+';far'
  1859. else
  1860. GetTypeName:='^'+pointeddef.typename;
  1861. end;
  1862. {****************************************************************************
  1863. TCLASSREFDEF
  1864. ****************************************************************************}
  1865. constructor tclassrefdef.create(def:tdef);
  1866. begin
  1867. inherited create(classrefdef,def);
  1868. end;
  1869. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  1870. begin
  1871. inherited ppuload(classrefdef,ppufile);
  1872. end;
  1873. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  1874. begin
  1875. inherited ppuwrite(ppufile);
  1876. ppufile.writeentry(ibclassrefdef);
  1877. end;
  1878. function tclassrefdef.GetTypeName : string;
  1879. begin
  1880. GetTypeName:='Class Of '+pointeddef.typename;
  1881. end;
  1882. function tclassrefdef.is_publishable : boolean;
  1883. begin
  1884. result:=true;
  1885. end;
  1886. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  1887. begin
  1888. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  1889. result:=inherited rtti_mangledname(rt)
  1890. else
  1891. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  1892. end;
  1893. procedure tclassrefdef.register_created_object_type;
  1894. begin
  1895. tobjectdef(pointeddef).register_created_classref_type;
  1896. end;
  1897. {***************************************************************************
  1898. TSETDEF
  1899. ***************************************************************************}
  1900. constructor tsetdef.create(def:tdef;low, high : aint);
  1901. var
  1902. setallocbits: aint;
  1903. packedsavesize: aint;
  1904. begin
  1905. inherited create(setdef);
  1906. elementdef:=def;
  1907. setmax:=high;
  1908. if (current_settings.setalloc=0) then
  1909. begin
  1910. setbase:=0;
  1911. if (high<32) then
  1912. savesize:=Sizeof(longint)
  1913. else if (high<256) then
  1914. savesize:=32
  1915. else
  1916. savesize:=(high+7) div 8
  1917. end
  1918. else
  1919. begin
  1920. setallocbits:=current_settings.setalloc*8;
  1921. setbase:=low and not(setallocbits-1);
  1922. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  1923. savesize:=packedsavesize;
  1924. if savesize=3 then
  1925. savesize:=4;
  1926. end;
  1927. end;
  1928. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  1929. begin
  1930. inherited ppuload(setdef,ppufile);
  1931. ppufile.getderef(elementdefderef);
  1932. savesize:=ppufile.getaint;
  1933. setbase:=ppufile.getaint;
  1934. setmax:=ppufile.getaint;
  1935. end;
  1936. function tsetdef.getcopy : tstoreddef;
  1937. begin
  1938. result:=tsetdef.create(elementdef,setbase,setmax);
  1939. { the copy might have been created with a different setalloc setting }
  1940. tsetdef(result).savesize:=savesize;
  1941. end;
  1942. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  1943. begin
  1944. inherited ppuwrite(ppufile);
  1945. ppufile.putderef(elementdefderef);
  1946. ppufile.putaint(savesize);
  1947. ppufile.putaint(setbase);
  1948. ppufile.putaint(setmax);
  1949. ppufile.writeentry(ibsetdef);
  1950. end;
  1951. procedure tsetdef.buildderef;
  1952. begin
  1953. inherited buildderef;
  1954. elementdefderef.build(elementdef);
  1955. end;
  1956. procedure tsetdef.deref;
  1957. begin
  1958. inherited deref;
  1959. elementdef:=tdef(elementdefderef.resolve);
  1960. end;
  1961. function tsetdef.is_publishable : boolean;
  1962. begin
  1963. is_publishable:=savesize in [1,2,4];
  1964. end;
  1965. function tsetdef.GetTypeName : string;
  1966. begin
  1967. if assigned(elementdef) then
  1968. GetTypeName:='Set Of '+elementdef.typename
  1969. else
  1970. GetTypeName:='Empty Set';
  1971. end;
  1972. {***************************************************************************
  1973. TFORMALDEF
  1974. ***************************************************************************}
  1975. constructor tformaldef.create(Atyped:boolean);
  1976. begin
  1977. inherited create(formaldef);
  1978. typed:=Atyped;
  1979. savesize:=0;
  1980. end;
  1981. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  1982. begin
  1983. inherited ppuload(formaldef,ppufile);
  1984. typed:=boolean(ppufile.getbyte);
  1985. savesize:=0;
  1986. end;
  1987. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  1988. begin
  1989. inherited ppuwrite(ppufile);
  1990. ppufile.putbyte(byte(typed));
  1991. ppufile.writeentry(ibformaldef);
  1992. end;
  1993. function tformaldef.GetTypeName : string;
  1994. begin
  1995. if typed then
  1996. GetTypeName:='<Typed formal type>'
  1997. else
  1998. GetTypeName:='<Formal type>';
  1999. end;
  2000. {***************************************************************************
  2001. TARRAYDEF
  2002. ***************************************************************************}
  2003. constructor tarraydef.create(l,h : aint;def:tdef);
  2004. begin
  2005. inherited create(arraydef);
  2006. lowrange:=l;
  2007. highrange:=h;
  2008. rangedef:=def;
  2009. _elementdef:=nil;
  2010. arrayoptions:=[];
  2011. end;
  2012. constructor tarraydef.create_from_pointer(def:tdef);
  2013. begin
  2014. { use -1 so that the elecount will not overflow }
  2015. self.create(0,high(aint)-1,s32inttype);
  2016. arrayoptions:=[ado_IsConvertedPointer];
  2017. setelementdef(def);
  2018. end;
  2019. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  2020. begin
  2021. inherited ppuload(arraydef,ppufile);
  2022. { the addresses are calculated later }
  2023. ppufile.getderef(_elementdefderef);
  2024. ppufile.getderef(rangedefderef);
  2025. lowrange:=ppufile.getaint;
  2026. highrange:=ppufile.getaint;
  2027. ppufile.getsmallset(arrayoptions);
  2028. end;
  2029. function tarraydef.getcopy : tstoreddef;
  2030. begin
  2031. result:=tarraydef.create(lowrange,highrange,rangedef);
  2032. tarraydef(result).arrayoptions:=arrayoptions;
  2033. tarraydef(result)._elementdef:=_elementdef;
  2034. end;
  2035. procedure tarraydef.buildderef;
  2036. begin
  2037. inherited buildderef;
  2038. _elementdefderef.build(_elementdef);
  2039. rangedefderef.build(rangedef);
  2040. end;
  2041. procedure tarraydef.deref;
  2042. begin
  2043. inherited deref;
  2044. _elementdef:=tdef(_elementdefderef.resolve);
  2045. rangedef:=tdef(rangedefderef.resolve);
  2046. end;
  2047. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  2048. begin
  2049. inherited ppuwrite(ppufile);
  2050. ppufile.putderef(_elementdefderef);
  2051. ppufile.putderef(rangedefderef);
  2052. ppufile.putaint(lowrange);
  2053. ppufile.putaint(highrange);
  2054. ppufile.putsmallset(arrayoptions);
  2055. ppufile.writeentry(ibarraydef);
  2056. end;
  2057. function tarraydef.elesize : aint;
  2058. begin
  2059. if (ado_IsBitPacked in arrayoptions) then
  2060. internalerror(2006080101);
  2061. if assigned(_elementdef) then
  2062. result:=_elementdef.size
  2063. else
  2064. result:=0;
  2065. end;
  2066. function tarraydef.elepackedbitsize : aint;
  2067. begin
  2068. if not(ado_IsBitPacked in arrayoptions) then
  2069. internalerror(2006080102);
  2070. if assigned(_elementdef) then
  2071. result:=_elementdef.packedbitsize
  2072. else
  2073. result:=0;
  2074. end;
  2075. function tarraydef.elecount : aword;
  2076. var
  2077. qhigh,qlow : qword;
  2078. begin
  2079. if ado_IsDynamicArray in arrayoptions then
  2080. begin
  2081. result:=0;
  2082. exit;
  2083. end;
  2084. if (highrange>0) and (lowrange<0) then
  2085. begin
  2086. qhigh:=highrange;
  2087. qlow:=qword(-lowrange);
  2088. { prevent overflow, return 0 to indicate overflow }
  2089. if qhigh+qlow>qword(high(aint)-1) then
  2090. result:=0
  2091. else
  2092. result:=qhigh+qlow+1;
  2093. end
  2094. else
  2095. result:=int64(highrange)-lowrange+1;
  2096. end;
  2097. function tarraydef.size : aint;
  2098. var
  2099. cachedelecount : aword;
  2100. cachedelesize : aint;
  2101. begin
  2102. if ado_IsDynamicArray in arrayoptions then
  2103. begin
  2104. size:=sizeof(pint);
  2105. exit;
  2106. end;
  2107. { Tarraydef.size may never be called for an open array! }
  2108. if highrange<lowrange then
  2109. internalerror(99080501);
  2110. if not (ado_IsBitPacked in arrayoptions) then
  2111. cachedelesize:=elesize
  2112. else
  2113. cachedelesize := elepackedbitsize;
  2114. cachedelecount:=elecount;
  2115. if (cachedelesize = 0) then
  2116. begin
  2117. size := 0;
  2118. exit;
  2119. end;
  2120. if (cachedelecount = 0) then
  2121. begin
  2122. size := -1;
  2123. exit;
  2124. end;
  2125. { prevent overflow, return -1 to indicate overflow }
  2126. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2127. if (cachedelecount > aword(high(aint))) or
  2128. ((high(aint) div cachedelesize) < aint(cachedelecount)) or
  2129. { also lowrange*elesize must be < high(aint) to prevent overflow when
  2130. accessing the array, see ncgmem (PFV) }
  2131. ((high(aint) div cachedelesize) < abs(lowrange)) then
  2132. begin
  2133. result:=-1;
  2134. exit;
  2135. end;
  2136. result:=cachedelesize*aint(cachedelecount);
  2137. if (ado_IsBitPacked in arrayoptions) then
  2138. { can't just add 7 and divide by 8, because that may overflow }
  2139. result:=result div 8 + ord((result mod 8)<>0);
  2140. end;
  2141. procedure tarraydef.setelementdef(def:tdef);
  2142. begin
  2143. _elementdef:=def;
  2144. if not(
  2145. (ado_IsDynamicArray in arrayoptions) or
  2146. (ado_IsConvertedPointer in arrayoptions) or
  2147. (highrange<lowrange)
  2148. ) and
  2149. (size=-1) then
  2150. Message(sym_e_segment_too_large);
  2151. end;
  2152. function tarraydef.alignment : shortint;
  2153. begin
  2154. { alignment of dyn. arrays doesn't depend on the element size }
  2155. if (ado_IsDynamicArray in arrayoptions) then
  2156. alignment:=size_2_align(sizeof(pint))
  2157. { alignment is the alignment of the elements }
  2158. else if (elementdef.typ in [arraydef,recorddef,orddef,enumdef,floatdef]) or
  2159. ((elementdef.typ=objectdef) and
  2160. is_object(elementdef)) then
  2161. alignment:=elementdef.alignment
  2162. { alignment is the size of the elements }
  2163. else if not (ado_IsBitPacked in arrayoptions) then
  2164. alignment:=size_2_align(elesize)
  2165. else
  2166. alignment:=packedbitsloadsize(elepackedbitsize);
  2167. end;
  2168. function tarraydef.needs_inittable : boolean;
  2169. begin
  2170. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2171. end;
  2172. function tarraydef.GetTypeName : string;
  2173. begin
  2174. if (ado_IsConstString in arrayoptions) then
  2175. result:='Constant String'
  2176. else if (ado_isarrayofconst in arrayoptions) or
  2177. (ado_isConstructor in arrayoptions) then
  2178. begin
  2179. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2180. GetTypeName:='Array Of Const'
  2181. else
  2182. GetTypeName:='Array Of Const/Constant Open Array of '+elementdef.typename;
  2183. end
  2184. else if (ado_IsDynamicArray in arrayoptions) then
  2185. GetTypeName:='Dynamic Array Of '+elementdef.typename
  2186. else if ((highrange=-1) and (lowrange=0)) then
  2187. GetTypeName:='Open Array Of '+elementdef.typename
  2188. else
  2189. begin
  2190. result := '';
  2191. if (ado_IsBitPacked in arrayoptions) then
  2192. result:='Packed ';
  2193. if rangedef.typ=enumdef then
  2194. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2195. else
  2196. result:=result+'Array['+tostr(lowrange)+'..'+
  2197. tostr(highrange)+'] Of '+elementdef.typename
  2198. end;
  2199. end;
  2200. function tarraydef.getmangledparaname : string;
  2201. begin
  2202. if ado_isarrayofconst in arrayoptions then
  2203. getmangledparaname:='array_of_const'
  2204. else
  2205. if ((highrange=-1) and (lowrange=0)) then
  2206. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2207. else
  2208. internalerror(200204176);
  2209. end;
  2210. function tarraydef.is_publishable : boolean;
  2211. begin
  2212. Result:=ado_IsDynamicArray in arrayoptions;
  2213. end;
  2214. {***************************************************************************
  2215. tabstractrecorddef
  2216. ***************************************************************************}
  2217. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2218. begin
  2219. if t=gs_record then
  2220. GetSymtable:=symtable
  2221. else
  2222. GetSymtable:=nil;
  2223. end;
  2224. function tabstractrecorddef.is_packed:boolean;
  2225. begin
  2226. result:=tabstractrecordsymtable(symtable).is_packed;
  2227. end;
  2228. {***************************************************************************
  2229. trecorddef
  2230. ***************************************************************************}
  2231. constructor trecorddef.create(p : TSymtable);
  2232. begin
  2233. inherited create(recorddef);
  2234. symtable:=p;
  2235. { we can own the symtable only if nobody else owns a copy so far }
  2236. if symtable.refcount=1 then
  2237. symtable.defowner:=self;
  2238. isunion:=false;
  2239. end;
  2240. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2241. begin
  2242. inherited ppuload(recorddef,ppufile);
  2243. if df_copied_def in defoptions then
  2244. ppufile.getderef(cloneddefderef)
  2245. else
  2246. begin
  2247. symtable:=trecordsymtable.create(0);
  2248. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2249. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2250. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2251. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2252. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2253. trecordsymtable(symtable).ppuload(ppufile);
  2254. { requires usefieldalignment to be set }
  2255. symtable.defowner:=self;
  2256. end;
  2257. isunion:=false;
  2258. end;
  2259. destructor trecorddef.destroy;
  2260. begin
  2261. if assigned(symtable) then
  2262. begin
  2263. symtable.free;
  2264. symtable:=nil;
  2265. end;
  2266. inherited destroy;
  2267. end;
  2268. function trecorddef.getcopy : tstoreddef;
  2269. begin
  2270. result:=trecorddef.create(symtable.getcopy);
  2271. trecorddef(result).isunion:=isunion;
  2272. include(trecorddef(result).defoptions,df_copied_def);
  2273. end;
  2274. function trecorddef.needs_inittable : boolean;
  2275. begin
  2276. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2277. end;
  2278. procedure trecorddef.buildderef;
  2279. begin
  2280. inherited buildderef;
  2281. if df_copied_def in defoptions then
  2282. cloneddefderef.build(symtable.defowner)
  2283. else
  2284. tstoredsymtable(symtable).buildderef;
  2285. end;
  2286. procedure trecorddef.deref;
  2287. begin
  2288. inherited deref;
  2289. { now dereference the definitions }
  2290. if df_copied_def in defoptions then
  2291. begin
  2292. cloneddef:=trecorddef(cloneddefderef.resolve);
  2293. symtable:=cloneddef.symtable.getcopy;
  2294. end
  2295. else
  2296. tstoredsymtable(symtable).deref;
  2297. { assign TGUID? load only from system unit }
  2298. if not(assigned(rec_tguid)) and
  2299. (upper(typename)='TGUID') and
  2300. assigned(owner) and
  2301. assigned(owner.name) and
  2302. (owner.name^='SYSTEM') then
  2303. rec_tguid:=self;
  2304. end;
  2305. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2306. begin
  2307. inherited ppuwrite(ppufile);
  2308. if df_copied_def in defoptions then
  2309. ppufile.putderef(cloneddefderef)
  2310. else
  2311. begin
  2312. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2313. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2314. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2315. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2316. ppufile.putaint(trecordsymtable(symtable).datasize);
  2317. end;
  2318. ppufile.writeentry(ibrecorddef);
  2319. if not(df_copied_def in defoptions) then
  2320. trecordsymtable(symtable).ppuwrite(ppufile);
  2321. end;
  2322. function trecorddef.size:aint;
  2323. begin
  2324. result:=trecordsymtable(symtable).datasize;
  2325. end;
  2326. function trecorddef.alignment:shortint;
  2327. begin
  2328. alignment:=trecordsymtable(symtable).recordalignment;
  2329. end;
  2330. function trecorddef.padalignment:shortint;
  2331. begin
  2332. padalignment := trecordsymtable(symtable).padalignment;
  2333. end;
  2334. function trecorddef.GetTypeName : string;
  2335. begin
  2336. GetTypeName:='<record type>'
  2337. end;
  2338. {***************************************************************************
  2339. TABSTRACTPROCDEF
  2340. ***************************************************************************}
  2341. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2342. begin
  2343. inherited create(dt);
  2344. parast:=tparasymtable.create(self,level);
  2345. paras:=nil;
  2346. minparacount:=0;
  2347. maxparacount:=0;
  2348. proctypeoption:=potype_none;
  2349. proccalloption:=pocall_none;
  2350. procoptions:=[];
  2351. returndef:=voidtype;
  2352. savesize:=sizeof(pint);
  2353. requiredargarea:=0;
  2354. has_paraloc_info:=false;
  2355. funcretloc[callerside].init;
  2356. funcretloc[calleeside].init;
  2357. check_mark_as_nested;
  2358. end;
  2359. destructor tabstractprocdef.destroy;
  2360. begin
  2361. if assigned(paras) then
  2362. begin
  2363. {$ifdef MEMDEBUG}
  2364. memprocpara.start;
  2365. {$endif MEMDEBUG}
  2366. paras.free;
  2367. paras:=nil;
  2368. {$ifdef MEMDEBUG}
  2369. memprocpara.stop;
  2370. {$endif MEMDEBUG}
  2371. end;
  2372. if assigned(parast) then
  2373. begin
  2374. {$ifdef MEMDEBUG}
  2375. memprocparast.start;
  2376. {$endif MEMDEBUG}
  2377. parast.free;
  2378. parast:=nil;
  2379. {$ifdef MEMDEBUG}
  2380. memprocparast.stop;
  2381. {$endif MEMDEBUG}
  2382. end;
  2383. funcretloc[callerside].done;
  2384. funcretloc[calleeside].done;
  2385. inherited destroy;
  2386. end;
  2387. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2388. begin
  2389. if (tsym(p).typ<>paravarsym) then
  2390. exit;
  2391. inc(plongint(arg)^);
  2392. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2393. begin
  2394. if not assigned(tparavarsym(p).defaultconstsym) then
  2395. inc(minparacount);
  2396. inc(maxparacount);
  2397. end;
  2398. end;
  2399. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  2400. begin
  2401. if (tsym(p).typ<>paravarsym) then
  2402. exit;
  2403. paras.add(p);
  2404. end;
  2405. procedure tabstractprocdef.calcparas;
  2406. var
  2407. paracount : longint;
  2408. begin
  2409. { This can already be assigned when
  2410. we need to reresolve this unit (PFV) }
  2411. if assigned(paras) then
  2412. paras.free;
  2413. paras:=tparalist.create(false);
  2414. paracount:=0;
  2415. minparacount:=0;
  2416. maxparacount:=0;
  2417. parast.SymList.ForEachCall(@count_para,@paracount);
  2418. paras.capacity:=paracount;
  2419. { Insert parameters in table }
  2420. parast.SymList.ForEachCall(@insert_para,nil);
  2421. { Order parameters }
  2422. paras.sortparas;
  2423. end;
  2424. procedure tabstractprocdef.buildderef;
  2425. begin
  2426. { released procdef? }
  2427. if not assigned(parast) then
  2428. exit;
  2429. inherited buildderef;
  2430. returndefderef.build(returndef);
  2431. { parast }
  2432. tparasymtable(parast).buildderef;
  2433. end;
  2434. procedure tabstractprocdef.deref;
  2435. begin
  2436. inherited deref;
  2437. returndef:=tdef(returndefderef.resolve);
  2438. { parast }
  2439. tparasymtable(parast).deref;
  2440. { recalculated parameters }
  2441. calcparas;
  2442. end;
  2443. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2444. var
  2445. b : byte;
  2446. begin
  2447. inherited ppuload(dt,ppufile);
  2448. parast:=nil;
  2449. Paras:=nil;
  2450. minparacount:=0;
  2451. maxparacount:=0;
  2452. ppufile.getderef(returndefderef);
  2453. { TODO: remove fpu_used loading}
  2454. ppufile.getbyte;
  2455. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2456. proccalloption:=tproccalloption(ppufile.getbyte);
  2457. ppufile.getnormalset(procoptions);
  2458. funcretloc[callerside].init;
  2459. if po_explicitparaloc in procoptions then
  2460. funcretloc[callerside].ppuload(ppufile);
  2461. savesize:=sizeof(pint);
  2462. has_paraloc_info:=(po_explicitparaloc in procoptions);
  2463. end;
  2464. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2465. var
  2466. oldintfcrc : boolean;
  2467. begin
  2468. { released procdef? }
  2469. if not assigned(parast) then
  2470. exit;
  2471. inherited ppuwrite(ppufile);
  2472. ppufile.putderef(returndefderef);
  2473. oldintfcrc:=ppufile.do_interface_crc;
  2474. ppufile.do_interface_crc:=false;
  2475. ppufile.putbyte(0);
  2476. ppufile.putbyte(ord(proctypeoption));
  2477. ppufile.putbyte(ord(proccalloption));
  2478. ppufile.putnormalset(procoptions);
  2479. ppufile.do_interface_crc:=oldintfcrc;
  2480. if (po_explicitparaloc in procoptions) then
  2481. funcretloc[callerside].ppuwrite(ppufile);
  2482. end;
  2483. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2484. var
  2485. hs,s : string;
  2486. hp : TParavarsym;
  2487. hpc : tconstsym;
  2488. first : boolean;
  2489. i : integer;
  2490. begin
  2491. s:='';
  2492. first:=true;
  2493. for i:=0 to paras.count-1 do
  2494. begin
  2495. hp:=tparavarsym(paras[i]);
  2496. if not(vo_is_hidden_para in hp.varoptions) or
  2497. (showhidden) then
  2498. begin
  2499. if first then
  2500. begin
  2501. s:=s+'(';
  2502. first:=false;
  2503. end
  2504. else
  2505. s:=s+',';
  2506. if vo_is_hidden_para in hp.varoptions then
  2507. s:=s+'<';
  2508. case hp.varspez of
  2509. vs_var :
  2510. s:=s+'var ';
  2511. vs_const :
  2512. s:=s+'const ';
  2513. vs_out :
  2514. s:=s+'out ';
  2515. end;
  2516. if hp.univpara then
  2517. s:=s+'univ ';
  2518. if assigned(hp.vardef.typesym) then
  2519. begin
  2520. hs:=hp.vardef.typesym.realname;
  2521. if hs[1]<>'$' then
  2522. s:=s+hs
  2523. else
  2524. s:=s+hp.vardef.GetTypeName;
  2525. end
  2526. else
  2527. s:=s+hp.vardef.GetTypeName;
  2528. { default value }
  2529. if assigned(hp.defaultconstsym) then
  2530. begin
  2531. hpc:=tconstsym(hp.defaultconstsym);
  2532. hs:='';
  2533. case hpc.consttyp of
  2534. conststring,
  2535. constresourcestring :
  2536. begin
  2537. If hpc.value.len>0 then
  2538. begin
  2539. setLength(hs,hpc.value.len);
  2540. move(hpc.value.valueptr^,hs[1],hpc.value.len);
  2541. end;
  2542. end;
  2543. constreal :
  2544. str(pbestreal(hpc.value.valueptr)^,hs);
  2545. constpointer :
  2546. hs:=tostr(hpc.value.valueordptr);
  2547. constord :
  2548. begin
  2549. if is_boolean(hpc.constdef) then
  2550. begin
  2551. if hpc.value.valueord<>0 then
  2552. hs:='TRUE'
  2553. else
  2554. hs:='FALSE';
  2555. end
  2556. else
  2557. hs:=tostr(hpc.value.valueord);
  2558. end;
  2559. constnil :
  2560. hs:='nil';
  2561. constset :
  2562. hs:='<set>';
  2563. end;
  2564. if hs<>'' then
  2565. s:=s+'="'+hs+'"';
  2566. end;
  2567. if vo_is_hidden_para in hp.varoptions then
  2568. s:=s+'>';
  2569. end;
  2570. end;
  2571. if not first then
  2572. s:=s+')';
  2573. if (po_varargs in procoptions) then
  2574. s:=s+';VarArgs';
  2575. typename_paras:=s;
  2576. end;
  2577. function tabstractprocdef.is_methodpointer:boolean;
  2578. begin
  2579. result:=false;
  2580. end;
  2581. function tabstractprocdef.is_addressonly:boolean;
  2582. begin
  2583. result:=true;
  2584. end;
  2585. function tabstractprocdef.no_self_node: boolean;
  2586. begin
  2587. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  2588. (proctypeoption in [potype_class_constructor,potype_class_destructor]);
  2589. end;
  2590. procedure tabstractprocdef.check_mark_as_nested;
  2591. begin
  2592. { nested procvars require that nested functions use the Delphi-style
  2593. nested procedure calling convention }
  2594. if (parast.symtablelevel>normal_function_level) and
  2595. (m_nested_procvars in current_settings.modeswitches) then
  2596. include(procoptions,po_delphi_nested_cc);
  2597. end;
  2598. {***************************************************************************
  2599. TPROCDEF
  2600. ***************************************************************************}
  2601. constructor tprocdef.create(level:byte);
  2602. begin
  2603. inherited create(procdef,level);
  2604. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  2605. _mangledname:=nil;
  2606. fileinfo:=current_filepos;
  2607. extnumber:=$ffff;
  2608. aliasnames:=TCmdStrList.create;
  2609. funcretsym:=nil;
  2610. forwarddef:=true;
  2611. interfacedef:=false;
  2612. hasforward:=false;
  2613. _class := nil;
  2614. import_dll:=nil;
  2615. import_name:=nil;
  2616. import_nr:=0;
  2617. inlininginfo:=nil;
  2618. deprecatedmsg:=nil;
  2619. {$ifdef i386}
  2620. fpu_used:=maxfpuregs;
  2621. {$endif i386}
  2622. end;
  2623. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2624. var
  2625. i,aliasnamescount : longint;
  2626. level : byte;
  2627. begin
  2628. inherited ppuload(procdef,ppufile);
  2629. if po_has_mangledname in procoptions then
  2630. _mangledname:=stringdup(ppufile.getstring)
  2631. else
  2632. _mangledname:=nil;
  2633. extnumber:=ppufile.getword;
  2634. level:=ppufile.getbyte;
  2635. ppufile.getderef(_classderef);
  2636. ppufile.getderef(procsymderef);
  2637. ppufile.getposinfo(fileinfo);
  2638. visibility:=tvisibility(ppufile.getbyte);
  2639. ppufile.getsmallset(symoptions);
  2640. if sp_has_deprecated_msg in symoptions then
  2641. deprecatedmsg:=stringdup(ppufile.getstring)
  2642. else
  2643. deprecatedmsg:=nil;
  2644. {$ifdef powerpc}
  2645. { library symbol for AmigaOS/MorphOS }
  2646. ppufile.getderef(libsymderef);
  2647. {$endif powerpc}
  2648. { import stuff }
  2649. if po_has_importdll in procoptions then
  2650. import_dll:=stringdup(ppufile.getstring)
  2651. else
  2652. import_dll:=nil;
  2653. if po_has_importname in procoptions then
  2654. import_name:=stringdup(ppufile.getstring)
  2655. else
  2656. import_name:=nil;
  2657. import_nr:=ppufile.getword;
  2658. if (po_msgint in procoptions) then
  2659. messageinf.i:=ppufile.getlongint;
  2660. if (po_msgstr in procoptions) then
  2661. messageinf.str:=stringdup(ppufile.getstring);
  2662. if (po_dispid in procoptions) then
  2663. dispid:=ppufile.getlongint;
  2664. { inline stuff }
  2665. if (po_has_inlininginfo in procoptions) then
  2666. begin
  2667. ppufile.getderef(funcretsymderef);
  2668. new(inlininginfo);
  2669. ppufile.getsmallset(inlininginfo^.flags);
  2670. end
  2671. else
  2672. begin
  2673. inlininginfo:=nil;
  2674. funcretsym:=nil;
  2675. end;
  2676. aliasnames:=TCmdStrList.create;
  2677. { count alias names }
  2678. aliasnamescount:=ppufile.getbyte;
  2679. for i:=1 to aliasnamescount do
  2680. aliasnames.insert(ppufile.getstring);
  2681. { load para symtable }
  2682. parast:=tparasymtable.create(self,level);
  2683. tparasymtable(parast).ppuload(ppufile);
  2684. { load local symtable }
  2685. if (po_has_inlininginfo in procoptions) then
  2686. begin
  2687. localst:=tlocalsymtable.create(self,level);
  2688. tlocalsymtable(localst).ppuload(ppufile);
  2689. end
  2690. else
  2691. localst:=nil;
  2692. { inline stuff }
  2693. if (po_has_inlininginfo in procoptions) then
  2694. inlininginfo^.code:=ppuloadnodetree(ppufile);
  2695. { default values for no persistent data }
  2696. if (cs_link_deffile in current_settings.globalswitches) and
  2697. (tf_need_export in target_info.flags) and
  2698. (po_exports in procoptions) then
  2699. deffile.AddExport(mangledname);
  2700. forwarddef:=false;
  2701. interfacedef:=false;
  2702. hasforward:=false;
  2703. { Disable po_has_inlining until the derefimpl is done }
  2704. exclude(procoptions,po_has_inlininginfo);
  2705. {$ifdef i386}
  2706. fpu_used:=maxfpuregs;
  2707. {$endif i386}
  2708. end;
  2709. destructor tprocdef.destroy;
  2710. begin
  2711. aliasnames.free;
  2712. aliasnames:=nil;
  2713. if assigned(localst) and
  2714. (localst.symtabletype<>staticsymtable) then
  2715. begin
  2716. {$ifdef MEMDEBUG}
  2717. memproclocalst.start;
  2718. {$endif MEMDEBUG}
  2719. localst.free;
  2720. localst:=nil;
  2721. {$ifdef MEMDEBUG}
  2722. memproclocalst.start;
  2723. {$endif MEMDEBUG}
  2724. end;
  2725. if assigned(inlininginfo) then
  2726. begin
  2727. {$ifdef MEMDEBUG}
  2728. memprocnodetree.start;
  2729. {$endif MEMDEBUG}
  2730. tnode(inlininginfo^.code).free;
  2731. {$ifdef MEMDEBUG}
  2732. memprocnodetree.start;
  2733. {$endif MEMDEBUG}
  2734. dispose(inlininginfo);
  2735. inlininginfo:=nil;
  2736. end;
  2737. stringdispose(resultname);
  2738. stringdispose(import_dll);
  2739. stringdispose(import_name);
  2740. stringdispose(deprecatedmsg);
  2741. if (po_msgstr in procoptions) then
  2742. stringdispose(messageinf.str);
  2743. if assigned(_mangledname) then
  2744. begin
  2745. {$ifdef MEMDEBUG}
  2746. memmanglednames.start;
  2747. {$endif MEMDEBUG}
  2748. stringdispose(_mangledname);
  2749. {$ifdef MEMDEBUG}
  2750. memmanglednames.stop;
  2751. {$endif MEMDEBUG}
  2752. end;
  2753. inherited destroy;
  2754. end;
  2755. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  2756. var
  2757. oldintfcrc : boolean;
  2758. aliasnamescount : longint;
  2759. item : TCmdStrListItem;
  2760. begin
  2761. { released procdef? }
  2762. if not assigned(parast) then
  2763. exit;
  2764. inherited ppuwrite(ppufile);
  2765. if po_has_mangledname in procoptions then
  2766. ppufile.putstring(_mangledname^);
  2767. ppufile.putword(extnumber);
  2768. ppufile.putbyte(parast.symtablelevel);
  2769. ppufile.putderef(_classderef);
  2770. ppufile.putderef(procsymderef);
  2771. ppufile.putposinfo(fileinfo);
  2772. ppufile.putbyte(byte(visibility));
  2773. ppufile.putsmallset(symoptions);
  2774. if sp_has_deprecated_msg in symoptions then
  2775. ppufile.putstring(deprecatedmsg^);
  2776. {$ifdef powerpc}
  2777. { library symbol for AmigaOS/MorphOS }
  2778. ppufile.putderef(libsymderef);
  2779. {$endif powerpc}
  2780. { import }
  2781. if po_has_importdll in procoptions then
  2782. ppufile.putstring(import_dll^);
  2783. if po_has_importname in procoptions then
  2784. ppufile.putstring(import_name^);
  2785. ppufile.putword(import_nr);
  2786. if (po_msgint in procoptions) then
  2787. ppufile.putlongint(messageinf.i);
  2788. if (po_msgstr in procoptions) then
  2789. ppufile.putstring(messageinf.str^);
  2790. if (po_dispid in procoptions) then
  2791. ppufile.putlongint(dispid);
  2792. { inline stuff }
  2793. oldintfcrc:=ppufile.do_crc;
  2794. ppufile.do_crc:=false;
  2795. if (po_has_inlininginfo in procoptions) then
  2796. begin
  2797. ppufile.putderef(funcretsymderef);
  2798. ppufile.putsmallset(inlininginfo^.flags);
  2799. end;
  2800. { count alias names }
  2801. aliasnamescount:=0;
  2802. item:=TCmdStrListItem(aliasnames.first);
  2803. while assigned(item) do
  2804. begin
  2805. inc(aliasnamescount);
  2806. item:=TCmdStrListItem(item.next);
  2807. end;
  2808. if aliasnamescount>255 then
  2809. internalerror(200711021);
  2810. ppufile.putbyte(aliasnamescount);
  2811. item:=TCmdStrListItem(aliasnames.first);
  2812. while assigned(item) do
  2813. begin
  2814. ppufile.putstring(item.str);
  2815. item:=TCmdStrListItem(item.next);
  2816. end;
  2817. ppufile.do_crc:=oldintfcrc;
  2818. { write this entry }
  2819. ppufile.writeentry(ibprocdef);
  2820. { Save the para symtable, this is taken from the interface }
  2821. tparasymtable(parast).ppuwrite(ppufile);
  2822. { save localsymtable for inline procedures or when local
  2823. browser info is requested, this has no influence on the crc }
  2824. if (po_has_inlininginfo in procoptions) then
  2825. begin
  2826. oldintfcrc:=ppufile.do_crc;
  2827. ppufile.do_crc:=false;
  2828. tlocalsymtable(localst).ppuwrite(ppufile);
  2829. ppufile.do_crc:=oldintfcrc;
  2830. end;
  2831. { node tree for inlining }
  2832. oldintfcrc:=ppufile.do_crc;
  2833. ppufile.do_crc:=false;
  2834. if (po_has_inlininginfo in procoptions) then
  2835. ppuwritenodetree(ppufile,inlininginfo^.code);
  2836. ppufile.do_crc:=oldintfcrc;
  2837. end;
  2838. function tprocdef.fullprocname(showhidden:boolean):string;
  2839. var
  2840. s : string;
  2841. t : ttoken;
  2842. begin
  2843. {$ifdef EXTDEBUG}
  2844. showhidden:=true;
  2845. {$endif EXTDEBUG}
  2846. s:='';
  2847. if assigned(_class) then
  2848. begin
  2849. s:=_class.RttiName+'.';
  2850. if (po_classmethod in procoptions) and
  2851. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  2852. s:='class ' + s;
  2853. end;
  2854. if proctypeoption=potype_operator then
  2855. begin
  2856. for t:=NOTOKEN to last_overloaded do
  2857. if procsym.realname='$'+overloaded_names[t] then
  2858. begin
  2859. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  2860. break;
  2861. end;
  2862. end
  2863. else
  2864. s:=s+procsym.realname+typename_paras(showhidden);
  2865. case proctypeoption of
  2866. potype_constructor:
  2867. s:='constructor '+s;
  2868. potype_destructor:
  2869. s:='destructor '+s;
  2870. potype_class_constructor:
  2871. s:='class constructor '+s;
  2872. potype_class_destructor:
  2873. s:='class destructor '+s;
  2874. else
  2875. if assigned(returndef) and
  2876. not(is_void(returndef)) then
  2877. s:=s+':'+returndef.GetTypeName;
  2878. end;
  2879. if owner.symtabletype=localsymtable then
  2880. s:=s+' is nested';
  2881. s:=s+';';
  2882. { forced calling convention? }
  2883. if (po_hascallingconvention in procoptions) then
  2884. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  2885. if (po_staticmethod in procoptions) and
  2886. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  2887. s:=s+' Static;';
  2888. fullprocname:=s;
  2889. end;
  2890. function tprocdef.is_methodpointer:boolean;
  2891. begin
  2892. result:=assigned(_class);
  2893. end;
  2894. function tprocdef.is_addressonly:boolean;
  2895. begin
  2896. result:=assigned(owner) and
  2897. (owner.symtabletype<>ObjectSymtable) and
  2898. (not(m_nested_procvars in current_settings.modeswitches) or
  2899. not is_nested_pd(self));
  2900. end;
  2901. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  2902. begin
  2903. case t of
  2904. gs_local :
  2905. GetSymtable:=localst;
  2906. gs_para :
  2907. GetSymtable:=parast;
  2908. else
  2909. GetSymtable:=nil;
  2910. end;
  2911. end;
  2912. procedure tprocdef.buildderef;
  2913. begin
  2914. inherited buildderef;
  2915. _classderef.build(_class);
  2916. { procsym that originaly defined this definition, should be in the
  2917. same symtable }
  2918. procsymderef.build(procsym);
  2919. {$ifdef powerpc}
  2920. { library symbol for AmigaOS/MorphOS }
  2921. libsymderef.build(libsym);
  2922. {$endif powerpc}
  2923. end;
  2924. procedure tprocdef.buildderefimpl;
  2925. begin
  2926. inherited buildderefimpl;
  2927. { Localst is not available for main/unit init }
  2928. if assigned(localst) then
  2929. begin
  2930. tlocalsymtable(localst).buildderef;
  2931. tlocalsymtable(localst).buildderefimpl;
  2932. end;
  2933. { inline tree }
  2934. if (po_has_inlininginfo in procoptions) then
  2935. begin
  2936. funcretsymderef.build(funcretsym);
  2937. inlininginfo^.code.buildderefimpl;
  2938. end;
  2939. end;
  2940. procedure tprocdef.deref;
  2941. begin
  2942. inherited deref;
  2943. _class:=tobjectdef(_classderef.resolve);
  2944. { procsym that originaly defined this definition, should be in the
  2945. same symtable }
  2946. procsym:=tprocsym(procsymderef.resolve);
  2947. {$ifdef powerpc}
  2948. { library symbol for AmigaOS/MorphOS }
  2949. libsym:=tsym(libsymderef.resolve);
  2950. {$endif powerpc}
  2951. end;
  2952. procedure tprocdef.derefimpl;
  2953. begin
  2954. { Enable has_inlininginfo when the inlininginfo
  2955. structure is available. The has_inlininginfo was disabled
  2956. after the load, since the data was invalid }
  2957. if assigned(inlininginfo) then
  2958. include(procoptions,po_has_inlininginfo);
  2959. { Locals }
  2960. if assigned(localst) then
  2961. begin
  2962. tlocalsymtable(localst).deref;
  2963. tlocalsymtable(localst).derefimpl;
  2964. end;
  2965. { Inline }
  2966. if (po_has_inlininginfo in procoptions) then
  2967. begin
  2968. inlininginfo^.code.derefimpl;
  2969. { funcretsym, this is always located in the localst }
  2970. funcretsym:=tsym(funcretsymderef.resolve);
  2971. end
  2972. else
  2973. begin
  2974. { safety }
  2975. { Not safe! A unit may be reresolved after its interface has been
  2976. parsed but before its implementation has been parsed, and in that
  2977. case the funcretsym is still required!
  2978. funcretsym:=nil; }
  2979. end;
  2980. end;
  2981. function tprocdef.GetTypeName : string;
  2982. begin
  2983. GetTypeName := FullProcName(false);
  2984. end;
  2985. function tprocdef.mangledname : string;
  2986. var
  2987. hp : TParavarsym;
  2988. hs : string;
  2989. crc : dword;
  2990. newlen,
  2991. oldlen,
  2992. i : integer;
  2993. begin
  2994. if assigned(_mangledname) then
  2995. begin
  2996. {$ifdef compress}
  2997. mangledname:=minilzw_decode(_mangledname^);
  2998. {$else}
  2999. mangledname:=_mangledname^;
  3000. {$endif}
  3001. exit;
  3002. end;
  3003. { we need to use the symtable where the procsym is inserted,
  3004. because that is visible to the world }
  3005. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  3006. oldlen:=length(mangledname);
  3007. { add parameter types }
  3008. for i:=0 to paras.count-1 do
  3009. begin
  3010. hp:=tparavarsym(paras[i]);
  3011. if not(vo_is_hidden_para in hp.varoptions) then
  3012. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  3013. end;
  3014. { add resultdef, add $$ as separator to make it unique from a
  3015. parameter separator }
  3016. if not is_void(returndef) then
  3017. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  3018. newlen:=length(mangledname);
  3019. { Replace with CRC if the parameter line is very long }
  3020. if (newlen-oldlen>12) and
  3021. ((newlen>100) or (newlen-oldlen>64)) then
  3022. begin
  3023. crc:=0;
  3024. for i:=0 to paras.count-1 do
  3025. begin
  3026. hp:=tparavarsym(paras[i]);
  3027. if not(vo_is_hidden_para in hp.varoptions) then
  3028. begin
  3029. hs:=hp.vardef.mangledparaname;
  3030. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3031. end;
  3032. end;
  3033. hs:=hp.vardef.mangledparaname;
  3034. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3035. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  3036. end;
  3037. {$ifdef compress}
  3038. _mangledname:=stringdup(minilzw_encode(mangledname));
  3039. {$else}
  3040. _mangledname:=stringdup(mangledname);
  3041. {$endif}
  3042. end;
  3043. function tprocdef.cplusplusmangledname : string;
  3044. function getcppparaname(p : tdef) : string;
  3045. const
  3046. {$ifdef NAMEMANGLING_GCC2}
  3047. ordtype2str : array[tordtype] of string[2] = (
  3048. '',
  3049. 'Uc','Us','Ui','Us',
  3050. 'Sc','s','i','x',
  3051. 'b','b','b','b','b',
  3052. 'c','w','x');
  3053. {$else NAMEMANGLING_GCC2}
  3054. ordtype2str : array[tordtype] of string[1] = (
  3055. 'v',
  3056. 'h','t','j','y',
  3057. 'a','s','i','x',
  3058. 'b','b','b','b','b',
  3059. 'c','w','x');
  3060. floattype2str : array[tfloattype] of string[1] = (
  3061. 'f','d','e','e',
  3062. 'd','d','g');
  3063. {$endif NAMEMANGLING_GCC2}
  3064. var
  3065. s : string;
  3066. begin
  3067. case p.typ of
  3068. orddef:
  3069. s:=ordtype2str[torddef(p).ordtype];
  3070. pointerdef:
  3071. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  3072. {$ifndef NAMEMANGLING_GCC2}
  3073. floatdef:
  3074. s:=floattype2str[tfloatdef(p).floattype];
  3075. {$endif NAMEMANGLING_GCC2}
  3076. else
  3077. internalerror(2103001);
  3078. end;
  3079. getcppparaname:=s;
  3080. end;
  3081. var
  3082. s,s2 : string;
  3083. hp : TParavarsym;
  3084. i : integer;
  3085. begin
  3086. {$ifdef NAMEMANGLING_GCC2}
  3087. { outdated gcc 2.x name mangling scheme }
  3088. s := procsym.realname;
  3089. if procsym.owner.symtabletype=ObjectSymtable then
  3090. begin
  3091. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  3092. case proctypeoption of
  3093. potype_destructor:
  3094. s:='_$_'+tostr(length(s2))+s2;
  3095. potype_constructor:
  3096. s:='___'+tostr(length(s2))+s2;
  3097. else
  3098. s:='_'+s+'__'+tostr(length(s2))+s2;
  3099. end;
  3100. end
  3101. else s:=s+'__';
  3102. s:=s+'F';
  3103. { concat modifiers }
  3104. { !!!!! }
  3105. { now we handle the parameters }
  3106. if maxparacount>0 then
  3107. begin
  3108. for i:=0 to paras.count-1 do
  3109. begin
  3110. hp:=tparavarsym(paras[i]);
  3111. { no hidden parameters form part of a C++ mangled name:
  3112. a) self is not included
  3113. b) there are no "high" or other hidden parameters
  3114. }
  3115. if vo_is_hidden_para in hp.varoptions then
  3116. continue;
  3117. s2:=getcppparaname(hp.vardef);
  3118. if hp.varspez in [vs_var,vs_out] then
  3119. s2:='R'+s2;
  3120. s:=s+s2;
  3121. end;
  3122. end
  3123. else
  3124. s:=s+'v';
  3125. cplusplusmangledname:=s;
  3126. {$else NAMEMANGLING_GCC2}
  3127. { gcc 3.x and 4.x name mangling scheme }
  3128. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  3129. if procsym.owner.symtabletype=ObjectSymtable then
  3130. begin
  3131. s:='_ZN';
  3132. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  3133. s:=s+tostr(length(s2))+s2;
  3134. case proctypeoption of
  3135. potype_constructor:
  3136. s:=s+'C1';
  3137. potype_destructor:
  3138. s:=s+'D1';
  3139. else
  3140. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3141. end;
  3142. s:=s+'E';
  3143. end
  3144. else
  3145. s:=procsym.realname;
  3146. { now we handle the parameters }
  3147. if maxparacount>0 then
  3148. begin
  3149. for i:=0 to paras.count-1 do
  3150. begin
  3151. hp:=tparavarsym(paras[i]);
  3152. { no hidden parameters form part of a C++ mangled name:
  3153. a) self is not included
  3154. b) there are no "high" or other hidden parameters
  3155. }
  3156. if vo_is_hidden_para in hp.varoptions then
  3157. continue;
  3158. s2:=getcppparaname(hp.vardef);
  3159. if hp.varspez in [vs_var,vs_out] then
  3160. s2:='R'+s2;
  3161. s:=s+s2;
  3162. end;
  3163. end
  3164. else
  3165. s:=s+'v';
  3166. cplusplusmangledname:=s;
  3167. {$endif NAMEMANGLING_GCC2}
  3168. end;
  3169. function tprocdef.objcmangledname : string;
  3170. var
  3171. manglednamelen: longint;
  3172. iscatmethod : boolean;
  3173. begin
  3174. if not (po_msgstr in procoptions) then
  3175. internalerror(2009030901);
  3176. { we may very well need longer strings to handle these... }
  3177. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  3178. length('+"[ ]"')+length(messageinf.str^);
  3179. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  3180. if (iscatmethod) then
  3181. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  3182. if manglednamelen>255 then
  3183. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  3184. if not(po_classmethod in procoptions) then
  3185. result:='"-['
  3186. else
  3187. result:='"+[';
  3188. { quotes are necessary because the +/- otherwise confuse the assembler
  3189. into expecting a number
  3190. }
  3191. if iscatmethod then
  3192. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  3193. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  3194. if iscatmethod then
  3195. result:=result+')';
  3196. result:=result+' '+messageinf.str^+']"';
  3197. end;
  3198. procedure tprocdef.setmangledname(const s : string);
  3199. begin
  3200. { This is not allowed anymore, the forward declaration
  3201. already needs to create the correct mangledname, no changes
  3202. afterwards are allowed (PFV) }
  3203. { Exception: interface definitions in mode macpas, since in that }
  3204. { case no reference to the old name can exist yet (JM) }
  3205. if assigned(_mangledname) then
  3206. if ((m_mac in current_settings.modeswitches) and
  3207. (interfacedef)) then
  3208. stringdispose(_mangledname)
  3209. else
  3210. internalerror(200411171);
  3211. {$ifdef compress}
  3212. _mangledname:=stringdup(minilzw_encode(s));
  3213. {$else}
  3214. _mangledname:=stringdup(s);
  3215. {$endif}
  3216. include(procoptions,po_has_mangledname);
  3217. end;
  3218. {***************************************************************************
  3219. TPROCVARDEF
  3220. ***************************************************************************}
  3221. constructor tprocvardef.create(level:byte);
  3222. begin
  3223. inherited create(procvardef,level);
  3224. end;
  3225. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3226. begin
  3227. inherited ppuload(procvardef,ppufile);
  3228. { load para symtable }
  3229. parast:=tparasymtable.create(self,unknown_level);
  3230. tparasymtable(parast).ppuload(ppufile);
  3231. end;
  3232. function tprocvardef.getcopy : tstoreddef;
  3233. var
  3234. i : tcallercallee;
  3235. j : longint;
  3236. begin
  3237. result:=tprocvardef.create(parast.symtablelevel);
  3238. tprocvardef(result).returndef:=returndef;
  3239. tprocvardef(result).returndefderef:=returndefderef;
  3240. tprocvardef(result).parast:=parast.getcopy;
  3241. tprocvardef(result).savesize:=savesize;
  3242. { create paralist copy }
  3243. tprocvardef(result).paras:=tparalist.create(false);
  3244. tprocvardef(result).paras.count:=paras.count;
  3245. for j:=0 to paras.count-1 do
  3246. tprocvardef(result).paras[j]:=paras[j];
  3247. tprocvardef(result).proctypeoption:=proctypeoption;
  3248. tprocvardef(result).proccalloption:=proccalloption;
  3249. tprocvardef(result).procoptions:=procoptions;
  3250. tprocvardef(result).requiredargarea:=requiredargarea;
  3251. tprocvardef(result).maxparacount:=maxparacount;
  3252. tprocvardef(result).minparacount:=minparacount;
  3253. for i:=low(tcallercallee) to high(tcallercallee) do
  3254. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  3255. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3256. {$ifdef m68k}
  3257. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3258. {$endif}
  3259. end;
  3260. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3261. begin
  3262. inherited ppuwrite(ppufile);
  3263. { Write this entry }
  3264. ppufile.writeentry(ibprocvardef);
  3265. { Save the para symtable, this is taken from the interface }
  3266. tparasymtable(parast).ppuwrite(ppufile);
  3267. end;
  3268. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3269. begin
  3270. case t of
  3271. gs_para :
  3272. GetSymtable:=parast;
  3273. else
  3274. GetSymtable:=nil;
  3275. end;
  3276. end;
  3277. function tprocvardef.size : aint;
  3278. begin
  3279. if ((po_methodpointer in procoptions) or
  3280. is_nested_pd(self)) and
  3281. not(po_addressonly in procoptions) then
  3282. size:=2*sizeof(pint)
  3283. else
  3284. size:=sizeof(pint);
  3285. end;
  3286. function tprocvardef.is_methodpointer:boolean;
  3287. begin
  3288. result:=(po_methodpointer in procoptions);
  3289. end;
  3290. function tprocvardef.is_addressonly:boolean;
  3291. begin
  3292. result:=(not(po_methodpointer in procoptions) and
  3293. not is_nested_pd(self)) or
  3294. (po_addressonly in procoptions);
  3295. end;
  3296. function tprocvardef.getmangledparaname:string;
  3297. begin
  3298. if not(po_methodpointer in procoptions) then
  3299. if not is_nested_pd(self) then
  3300. result:='procvar'
  3301. else
  3302. result:='nestedprovar'
  3303. else
  3304. result:='procvarofobj'
  3305. end;
  3306. function tprocvardef.is_publishable : boolean;
  3307. begin
  3308. is_publishable:=(po_methodpointer in procoptions);
  3309. end;
  3310. function tprocvardef.GetTypeName : string;
  3311. var
  3312. s: string;
  3313. showhidden : boolean;
  3314. begin
  3315. {$ifdef EXTDEBUG}
  3316. showhidden:=true;
  3317. {$else EXTDEBUG}
  3318. showhidden:=false;
  3319. {$endif EXTDEBUG}
  3320. s:='<';
  3321. if po_classmethod in procoptions then
  3322. s := s+'class method type of'
  3323. else
  3324. if po_addressonly in procoptions then
  3325. s := s+'address of'
  3326. else
  3327. s := s+'procedure variable type of';
  3328. if assigned(returndef) and
  3329. (returndef<>voidtype) then
  3330. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3331. else
  3332. s:=s+' procedure'+typename_paras(showhidden);
  3333. if po_methodpointer in procoptions then
  3334. s := s+' of object';
  3335. if is_nested_pd(self) then
  3336. s := s+' is nested';
  3337. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3338. end;
  3339. {***************************************************************************
  3340. TOBJECTDEF
  3341. ***************************************************************************}
  3342. constructor tobjectdef.create(ot : tobjecttyp;const n : string;c : tobjectdef);
  3343. begin
  3344. inherited create(objectdef);
  3345. fcurrent_dispid:=0;
  3346. objecttype:=ot;
  3347. objectoptions:=[];
  3348. childof:=nil;
  3349. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3350. { create space for vmt !! }
  3351. vmtentries:=TFPList.Create;
  3352. vmt_offset:=0;
  3353. set_parent(c);
  3354. objname:=stringdup(upper(n));
  3355. objrealname:=stringdup(n);
  3356. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3357. prepareguid;
  3358. { setup implemented interfaces }
  3359. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3360. ImplementedInterfaces:=TFPObjectList.Create(true)
  3361. else
  3362. ImplementedInterfaces:=nil;
  3363. writing_class_record_dbginfo:=false;
  3364. end;
  3365. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3366. var
  3367. i,
  3368. implintfcount : longint;
  3369. d : tderef;
  3370. ImplIntf : TImplementedInterface;
  3371. vmtentry : pvmtentry;
  3372. begin
  3373. inherited ppuload(objectdef,ppufile);
  3374. objecttype:=tobjecttyp(ppufile.getbyte);
  3375. objrealname:=stringdup(ppufile.getstring);
  3376. objname:=stringdup(upper(objrealname^));
  3377. objextname:=stringdup(ppufile.getstring);
  3378. { only used for external Objective-C classes/protocols }
  3379. if (objextname^='') then
  3380. stringdispose(objextname);
  3381. import_lib:=stringdup(ppufile.getstring);
  3382. { only used for external C++ classes }
  3383. if (import_lib^='') then
  3384. stringdispose(import_lib);
  3385. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3386. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3387. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3388. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3389. vmt_offset:=ppufile.getlongint;
  3390. ppufile.getderef(childofderef);
  3391. ppufile.getsmallset(objectoptions);
  3392. { load guid }
  3393. iidstr:=nil;
  3394. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3395. begin
  3396. new(iidguid);
  3397. ppufile.getguid(iidguid^);
  3398. iidstr:=stringdup(ppufile.getstring);
  3399. end;
  3400. vmtentries:=TFPList.Create;
  3401. vmtentries.count:=ppufile.getlongint;
  3402. for i:=0 to vmtentries.count-1 do
  3403. begin
  3404. ppufile.getderef(d);
  3405. new(vmtentry);
  3406. vmtentry^.procdef:=nil;
  3407. vmtentry^.procdefderef:=d;
  3408. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  3409. vmtentries[i]:=vmtentry;
  3410. end;
  3411. { load implemented interfaces }
  3412. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3413. begin
  3414. ImplementedInterfaces:=TFPObjectList.Create(true);
  3415. implintfcount:=ppufile.getlongint;
  3416. for i:=0 to implintfcount-1 do
  3417. begin
  3418. ppufile.getderef(d);
  3419. ImplIntf:=TImplementedInterface.Create_deref(d);
  3420. ImplIntf.IOffset:=ppufile.getlongint;
  3421. ImplementedInterfaces.Add(ImplIntf);
  3422. end;
  3423. end
  3424. else
  3425. ImplementedInterfaces:=nil;
  3426. if df_copied_def in defoptions then
  3427. ppufile.getderef(cloneddefderef)
  3428. else
  3429. tObjectSymtable(symtable).ppuload(ppufile);
  3430. { handles the predefined class tobject }
  3431. { the last TOBJECT which is loaded gets }
  3432. { it ! }
  3433. if (childof=nil) and
  3434. (objecttype=odt_class) and
  3435. (objname^='TOBJECT') then
  3436. class_tobject:=self;
  3437. if (childof=nil) and
  3438. (objecttype=odt_interfacecom) and
  3439. (objname^='IUNKNOWN') then
  3440. interface_iunknown:=self;
  3441. if (childof=nil) and
  3442. (objecttype=odt_objcclass) and
  3443. (objname^='PROTOCOL') then
  3444. objc_protocoltype:=self;
  3445. writing_class_record_dbginfo:=false;
  3446. end;
  3447. destructor tobjectdef.destroy;
  3448. begin
  3449. if assigned(symtable) then
  3450. begin
  3451. symtable.free;
  3452. symtable:=nil;
  3453. end;
  3454. stringdispose(objname);
  3455. stringdispose(objrealname);
  3456. stringdispose(objextname);
  3457. stringdispose(import_lib);
  3458. stringdispose(iidstr);
  3459. if assigned(ImplementedInterfaces) then
  3460. begin
  3461. ImplementedInterfaces.free;
  3462. ImplementedInterfaces:=nil;
  3463. end;
  3464. if assigned(iidguid) then
  3465. begin
  3466. dispose(iidguid);
  3467. iidguid:=nil;
  3468. end;
  3469. if assigned(vmtentries) then
  3470. begin
  3471. resetvmtentries;
  3472. vmtentries.free;
  3473. vmtentries:=nil;
  3474. end;
  3475. if assigned(vmcallstaticinfo) then
  3476. begin
  3477. freemem(vmcallstaticinfo);
  3478. vmcallstaticinfo:=nil;
  3479. end;
  3480. inherited destroy;
  3481. end;
  3482. function tobjectdef.getcopy : tstoreddef;
  3483. var
  3484. i : longint;
  3485. begin
  3486. result:=tobjectdef.create(objecttype,objname^,childof);
  3487. { the constructor allocates a symtable which we release to avoid memory leaks }
  3488. tobjectdef(result).symtable.free;
  3489. tobjectdef(result).symtable:=symtable.getcopy;
  3490. if assigned(objname) then
  3491. tobjectdef(result).objname:=stringdup(objname^);
  3492. if assigned(objrealname) then
  3493. tobjectdef(result).objrealname:=stringdup(objrealname^);
  3494. if assigned(objextname) then
  3495. tobjectdef(result).objextname:=stringdup(objextname^);
  3496. if assigned(import_lib) then
  3497. tobjectdef(result).import_lib:=stringdup(import_lib^);
  3498. tobjectdef(result).objectoptions:=objectoptions;
  3499. include(tobjectdef(result).defoptions,df_copied_def);
  3500. tobjectdef(result).vmt_offset:=vmt_offset;
  3501. if assigned(iidguid) then
  3502. begin
  3503. new(tobjectdef(result).iidguid);
  3504. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3505. end;
  3506. if assigned(iidstr) then
  3507. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3508. if assigned(ImplementedInterfaces) then
  3509. begin
  3510. for i:=0 to ImplementedInterfaces.count-1 do
  3511. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3512. end;
  3513. if assigned(vmtentries) then
  3514. begin
  3515. tobjectdef(result).vmtentries:=TFPList.Create;
  3516. tobjectdef(result).copyvmtentries(self);
  3517. end;
  3518. end;
  3519. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3520. var
  3521. i : longint;
  3522. vmtentry : pvmtentry;
  3523. ImplIntf : TImplementedInterface;
  3524. old_do_indirect_crc: boolean;
  3525. begin
  3526. { if class1 in unit A changes, and class2 in unit B inherits from it
  3527. (so unit B uses unit A), then unit B with class2 will be recompiled.
  3528. However, if there is also a class3 in unit C that only depends on
  3529. unit B, then unit C will not be recompiled because nothing changed
  3530. to the interface of unit B. Nevertheless, unit C can indirectly
  3531. depend on unit A via derefs, and these must be updated -> the
  3532. indirect crc keeps track of such changes. }
  3533. old_do_indirect_crc:=ppufile.do_indirect_crc;
  3534. ppufile.do_indirect_crc:=true;
  3535. inherited ppuwrite(ppufile);
  3536. ppufile.putbyte(byte(objecttype));
  3537. ppufile.putstring(objrealname^);
  3538. if assigned(objextname) then
  3539. ppufile.putstring(objextname^)
  3540. else
  3541. ppufile.putstring('');
  3542. if assigned(import_lib) then
  3543. ppufile.putstring(import_lib^)
  3544. else
  3545. ppufile.putstring('');
  3546. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3547. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3548. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3549. ppufile.putlongint(vmt_offset);
  3550. ppufile.putderef(childofderef);
  3551. ppufile.putsmallset(objectoptions);
  3552. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3553. begin
  3554. ppufile.putguid(iidguid^);
  3555. ppufile.putstring(iidstr^);
  3556. end;
  3557. ppufile.putlongint(vmtentries.count);
  3558. for i:=0 to vmtentries.count-1 do
  3559. begin
  3560. vmtentry:=pvmtentry(vmtentries[i]);
  3561. ppufile.putderef(vmtentry^.procdefderef);
  3562. ppufile.putbyte(byte(vmtentry^.visibility));
  3563. end;
  3564. if assigned(ImplementedInterfaces) then
  3565. begin
  3566. ppufile.putlongint(ImplementedInterfaces.Count);
  3567. for i:=0 to ImplementedInterfaces.Count-1 do
  3568. begin
  3569. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3570. ppufile.putderef(ImplIntf.intfdefderef);
  3571. ppufile.putlongint(ImplIntf.Ioffset);
  3572. end;
  3573. end;
  3574. if df_copied_def in defoptions then
  3575. ppufile.putderef(cloneddefderef);
  3576. ppufile.writeentry(ibobjectdef);
  3577. if not(df_copied_def in defoptions) then
  3578. tObjectSymtable(symtable).ppuwrite(ppufile);
  3579. ppufile.do_indirect_crc:=old_do_indirect_crc;
  3580. end;
  3581. function tobjectdef.GetTypeName:string;
  3582. begin
  3583. { in this case we will go in endless recursion, because then }
  3584. { there is no tsym associated yet with the def. It can occur }
  3585. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3586. { instead of the actual type name }
  3587. if not assigned(typesym) then
  3588. result:='<Currently Parsed Class>'
  3589. else
  3590. result:=typename;
  3591. end;
  3592. procedure tobjectdef.buildderef;
  3593. var
  3594. i : longint;
  3595. vmtentry : pvmtentry;
  3596. begin
  3597. inherited buildderef;
  3598. childofderef.build(childof);
  3599. if df_copied_def in defoptions then
  3600. cloneddefderef.build(symtable.defowner)
  3601. else
  3602. tstoredsymtable(symtable).buildderef;
  3603. for i:=0 to vmtentries.count-1 do
  3604. begin
  3605. vmtentry:=pvmtentry(vmtentries[i]);
  3606. vmtentry^.procdefderef.build(vmtentry^.procdef);
  3607. end;
  3608. if assigned(ImplementedInterfaces) then
  3609. begin
  3610. for i:=0 to ImplementedInterfaces.count-1 do
  3611. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  3612. end;
  3613. end;
  3614. procedure tobjectdef.deref;
  3615. var
  3616. i : longint;
  3617. vmtentry : pvmtentry;
  3618. begin
  3619. inherited deref;
  3620. childof:=tobjectdef(childofderef.resolve);
  3621. if df_copied_def in defoptions then
  3622. begin
  3623. cloneddef:=tobjectdef(cloneddefderef.resolve);
  3624. symtable:=cloneddef.symtable.getcopy;
  3625. end
  3626. else
  3627. tstoredsymtable(symtable).deref;
  3628. for i:=0 to vmtentries.count-1 do
  3629. begin
  3630. vmtentry:=pvmtentry(vmtentries[i]);
  3631. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  3632. end;
  3633. if assigned(ImplementedInterfaces) then
  3634. begin
  3635. for i:=0 to ImplementedInterfaces.count-1 do
  3636. TImplementedInterface(ImplementedInterfaces[i]).deref;
  3637. end;
  3638. end;
  3639. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  3640. var
  3641. pd: tprocdef absolute def;
  3642. st: tsymtable;
  3643. psym: tsym;
  3644. nname: TIDString;
  3645. begin
  3646. if (tdef(def).typ<>procdef) then
  3647. exit;
  3648. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  3649. owner = symtable in which objcclassdef is defined
  3650. }
  3651. st:=pd.owner.defowner.owner;
  3652. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  3653. { check for an existing procsym with our special name }
  3654. psym:=tsym(st.find(nname));
  3655. if not assigned(psym) then
  3656. begin
  3657. psym:=tprocsym.create(nname);
  3658. { avoid warning about this symbol being unused }
  3659. psym.IncRefCount;
  3660. st.insert(psym,true);
  3661. end
  3662. else if (psym.typ<>procsym) then
  3663. internalerror(2009111501);
  3664. { add ourselves to this special procsym }
  3665. tprocsym(psym).procdeflist.add(def);
  3666. end;
  3667. procedure tobjectdef.buildderefimpl;
  3668. begin
  3669. inherited buildderefimpl;
  3670. if not (df_copied_def in defoptions) then
  3671. tstoredsymtable(symtable).buildderefimpl;
  3672. end;
  3673. procedure tobjectdef.derefimpl;
  3674. begin
  3675. inherited derefimpl;
  3676. if not (df_copied_def in defoptions) then
  3677. tstoredsymtable(symtable).derefimpl;
  3678. { the procdefs are not owned by the class helper procsyms, so they
  3679. are not stored/restored either -> re-add them here }
  3680. if (objecttype=odt_objcclass) or
  3681. (oo_is_classhelper in objectoptions) then
  3682. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  3683. end;
  3684. procedure tobjectdef.resetvmtentries;
  3685. var
  3686. i : longint;
  3687. begin
  3688. for i:=0 to vmtentries.Count-1 do
  3689. Dispose(pvmtentry(vmtentries[i]));
  3690. vmtentries.clear;
  3691. end;
  3692. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  3693. var
  3694. i : longint;
  3695. vmtentry : pvmtentry;
  3696. begin
  3697. resetvmtentries;
  3698. vmtentries.count:=objdef.vmtentries.count;
  3699. for i:=0 to objdef.vmtentries.count-1 do
  3700. begin
  3701. new(vmtentry);
  3702. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  3703. vmtentries[i]:=vmtentry;
  3704. end;
  3705. end;
  3706. function tobjectdef.getparentdef:tdef;
  3707. begin
  3708. { TODO: Remove getparentdef hack}
  3709. { With 2 forward declared classes with the child class before the
  3710. parent class the child class is written earlier to the ppu. Leaving it
  3711. possible to have a reference to the parent class for property overriding,
  3712. but the parent class still has the childof not resolved yet (PFV) }
  3713. if childof=nil then
  3714. childof:=tobjectdef(childofderef.resolve);
  3715. result:=childof;
  3716. end;
  3717. procedure tobjectdef.prepareguid;
  3718. begin
  3719. { set up guid }
  3720. if not assigned(iidguid) then
  3721. begin
  3722. new(iidguid);
  3723. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  3724. end;
  3725. { setup iidstring }
  3726. if not assigned(iidstr) then
  3727. iidstr:=stringdup(''); { default is empty string }
  3728. end;
  3729. procedure tobjectdef.set_parent( c : tobjectdef);
  3730. begin
  3731. if assigned(childof) then
  3732. exit;
  3733. childof:=c;
  3734. if not assigned(c) then
  3735. exit;
  3736. { inherit options and status }
  3737. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  3738. { add the data of the anchestor class/object }
  3739. if (objecttype in [odt_class,odt_object,odt_objcclass]) then
  3740. begin
  3741. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  3742. { inherit recordalignment }
  3743. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  3744. if (oo_has_vmt in objectoptions) and
  3745. (oo_has_vmt in c.objectoptions) then
  3746. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  3747. { if parent has a vmt field then the offset is the same for the child PM }
  3748. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  3749. begin
  3750. vmt_offset:=c.vmt_offset;
  3751. include(objectoptions,oo_has_vmt);
  3752. end;
  3753. end;
  3754. end;
  3755. procedure tobjectdef.insertvmt;
  3756. var
  3757. vs: tfieldvarsym;
  3758. begin
  3759. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3760. exit;
  3761. if (oo_has_vmt in objectoptions) then
  3762. internalerror(12345)
  3763. else
  3764. begin
  3765. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  3766. tObjectSymtable(symtable).fieldalignment);
  3767. if (tf_requires_proper_alignment in target_info.flags) then
  3768. begin
  3769. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  3770. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  3771. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  3772. end;
  3773. vmt_offset:=tObjectSymtable(symtable).datasize;
  3774. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  3775. hidesym(vs);
  3776. tObjectSymtable(symtable).insert(vs);
  3777. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  3778. include(objectoptions,oo_has_vmt);
  3779. end;
  3780. end;
  3781. procedure tobjectdef.check_forwards;
  3782. begin
  3783. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol]) then
  3784. tstoredsymtable(symtable).check_forwards;
  3785. if (oo_is_forward in objectoptions) then
  3786. begin
  3787. { ok, in future, the forward can be resolved }
  3788. Message1(sym_e_class_forward_not_resolved,objrealname^);
  3789. exclude(objectoptions,oo_is_forward);
  3790. end;
  3791. end;
  3792. { true if prot implements d (or if they are equal) }
  3793. function is_related_protocol(prot: tobjectdef; d : tdef) : boolean;
  3794. var
  3795. i : longint;
  3796. begin
  3797. { objcprotocols have multiple inheritance, all protocols from which
  3798. the current protocol inherits are stored in implementedinterfaces }
  3799. result:=prot=d;
  3800. if result then
  3801. exit;
  3802. for i:=0 to prot.ImplementedInterfaces.count-1 do
  3803. begin
  3804. result:=is_related_protocol(TImplementedInterface(prot.ImplementedInterfaces[i]).intfdef,d);
  3805. if result then
  3806. exit;
  3807. end;
  3808. end;
  3809. { true, if self inherits from d (or if they are equal) }
  3810. function tobjectdef.is_related(d : tdef) : boolean;
  3811. var
  3812. hp : tobjectdef;
  3813. begin
  3814. if self=d then
  3815. begin
  3816. is_related:=true;
  3817. exit;
  3818. end;
  3819. if (d.typ<>objectdef) then
  3820. begin
  3821. is_related:=false;
  3822. exit;
  3823. end;
  3824. { Objective-C protocols can use multiple inheritance }
  3825. if (objecttype=odt_objcprotocol) then
  3826. begin
  3827. is_related:=is_related_protocol(self,d);
  3828. exit
  3829. end;
  3830. { formally declared Objective-C classes match Objective-C classes with
  3831. the same name }
  3832. if (objecttype=odt_objcclass) and
  3833. (tobjectdef(d).objecttype=odt_objcclass) and
  3834. ((oo_is_formal in objectoptions) or
  3835. (oo_is_formal in tobjectdef(d).objectoptions)) and
  3836. (objrealname^=tobjectdef(d).objrealname^) then
  3837. begin
  3838. is_related:=true;
  3839. exit;
  3840. end;
  3841. hp:=childof;
  3842. while assigned(hp) do
  3843. begin
  3844. if hp=d then
  3845. begin
  3846. is_related:=true;
  3847. exit;
  3848. end;
  3849. hp:=hp.childof;
  3850. end;
  3851. is_related:=false;
  3852. end;
  3853. function tobjectdef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  3854. var
  3855. i: longint;
  3856. sym: tsym;
  3857. begin
  3858. for i:=0 to symtable.SymList.Count-1 do
  3859. begin
  3860. sym:=tsym(symtable.SymList[i]);
  3861. if sym.typ=procsym then
  3862. begin
  3863. result:=tprocsym(sym).find_procdef_bytype(pt);
  3864. if assigned(result) then
  3865. exit;
  3866. end;
  3867. end;
  3868. result:=nil;
  3869. end;
  3870. function tobjectdef.find_destructor: tprocdef;
  3871. var
  3872. objdef: tobjectdef;
  3873. begin
  3874. objdef:=self;
  3875. while assigned(objdef) do
  3876. begin
  3877. result:=objdef.find_procdef_bytype(potype_destructor);
  3878. if assigned(result) then
  3879. exit;
  3880. objdef:=objdef.childof;
  3881. end;
  3882. result:=nil;
  3883. end;
  3884. function tobjectdef.implements_any_interfaces: boolean;
  3885. begin
  3886. result := (ImplementedInterfaces.Count > 0) or
  3887. (assigned(childof) and childof.implements_any_interfaces);
  3888. end;
  3889. function tobjectdef.size : aint;
  3890. begin
  3891. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3892. result:=sizeof(pint)
  3893. else
  3894. result:=tObjectSymtable(symtable).datasize;
  3895. end;
  3896. function tobjectdef.alignment:shortint;
  3897. begin
  3898. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3899. alignment:=sizeof(pint)
  3900. else
  3901. alignment:=tObjectSymtable(symtable).recordalignment;
  3902. end;
  3903. function tobjectdef.vmtmethodoffset(index:longint):longint;
  3904. begin
  3905. { for offset of methods for classes, see rtl/inc/objpash.inc }
  3906. case objecttype of
  3907. odt_class:
  3908. { the +2*sizeof(pint) is size and -size }
  3909. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  3910. odt_objcclass,
  3911. odt_objcprotocol:
  3912. vmtmethodoffset:=0;
  3913. odt_interfacecom,odt_interfacecorba:
  3914. vmtmethodoffset:=index*sizeof(pint);
  3915. else
  3916. {$ifdef WITHDMT}
  3917. vmtmethodoffset:=(index+4)*sizeof(pint);
  3918. {$else WITHDMT}
  3919. vmtmethodoffset:=(index+3)*sizeof(pint);
  3920. {$endif WITHDMT}
  3921. end;
  3922. end;
  3923. function tobjectdef.vmt_mangledname : string;
  3924. begin
  3925. if not(oo_has_vmt in objectoptions) then
  3926. Message1(parser_n_object_has_no_vmt,objrealname^);
  3927. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  3928. end;
  3929. function tobjectdef.needs_inittable : boolean;
  3930. begin
  3931. case objecttype of
  3932. odt_dispinterface,
  3933. odt_class :
  3934. needs_inittable:=false;
  3935. odt_interfacecom:
  3936. needs_inittable:=true;
  3937. odt_interfacecorba:
  3938. needs_inittable:=is_related(interface_iunknown);
  3939. odt_object:
  3940. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  3941. odt_cppclass,
  3942. odt_objcclass,
  3943. odt_objcprotocol:
  3944. needs_inittable:=false;
  3945. else
  3946. internalerror(200108267);
  3947. end;
  3948. end;
  3949. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  3950. begin
  3951. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  3952. result:=inherited rtti_mangledname(rt)
  3953. else
  3954. begin
  3955. { necessary in case of a dynamic array of nsobject, or
  3956. if an nsobject field appears in a record that needs
  3957. init/finalisation }
  3958. if rt=initrtti then
  3959. begin
  3960. result:=voidpointertype.rtti_mangledname(rt);
  3961. exit;
  3962. end;
  3963. if not(target_info.system in systems_objc_nfabi) then
  3964. begin
  3965. result:=target_asm.labelprefix;
  3966. case objecttype of
  3967. odt_objcclass:
  3968. begin
  3969. case rt of
  3970. objcclassrtti:
  3971. if not(oo_is_classhelper in objectoptions) then
  3972. result:=result+'_OBJC_CLASS_'
  3973. else
  3974. result:=result+'_OBJC_CATEGORY_';
  3975. objcmetartti:
  3976. if not(oo_is_classhelper in objectoptions) then
  3977. result:=result+'_OBJC_METACLASS_'
  3978. else
  3979. internalerror(2009111511);
  3980. else
  3981. internalerror(2009092302);
  3982. end;
  3983. end;
  3984. odt_objcprotocol:
  3985. result:=result+'_OBJC_PROTOCOL_';
  3986. end;
  3987. end
  3988. else
  3989. begin
  3990. case objecttype of
  3991. odt_objcclass:
  3992. begin
  3993. if (oo_is_classhelper in objectoptions) and
  3994. (rt<>objcclassrtti) then
  3995. internalerror(2009111512);
  3996. case rt of
  3997. objcclassrtti:
  3998. if not(oo_is_classhelper in objectoptions) then
  3999. result:='_OBJC_CLASS_$_'
  4000. else
  4001. result:='_OBJC_$_CATEGORY_';
  4002. objcmetartti:
  4003. result:='_OBJC_METACLASS_$_';
  4004. objcclassrortti:
  4005. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  4006. objcmetarortti:
  4007. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  4008. else
  4009. internalerror(2009092303);
  4010. end;
  4011. end;
  4012. odt_objcprotocol:
  4013. begin
  4014. result:=lower(target_asm.labelprefix);
  4015. case rt of
  4016. objcclassrtti:
  4017. result:=result+'_OBJC_PROTOCOL_$_';
  4018. objcmetartti:
  4019. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  4020. else
  4021. internalerror(2009092501);
  4022. end;
  4023. end;
  4024. end;
  4025. end;
  4026. result:=result+objextname^;
  4027. end;
  4028. end;
  4029. function tobjectdef.members_need_inittable : boolean;
  4030. begin
  4031. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  4032. end;
  4033. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  4034. var
  4035. ImplIntf : TImplementedInterface;
  4036. i : longint;
  4037. begin
  4038. result:=nil;
  4039. if not assigned(ImplementedInterfaces) then
  4040. exit;
  4041. for i:=0 to ImplementedInterfaces.Count-1 do
  4042. begin
  4043. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  4044. if ImplIntf.intfdef=aintfdef then
  4045. begin
  4046. result:=ImplIntf;
  4047. exit;
  4048. end;
  4049. end;
  4050. end;
  4051. function tobjectdef.is_publishable : boolean;
  4052. begin
  4053. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  4054. end;
  4055. function tobjectdef.get_next_dispid: longint;
  4056. begin
  4057. inc(fcurrent_dispid);
  4058. result:=fcurrent_dispid;
  4059. end;
  4060. function tobjectdef.search_enumerator_get: tprocdef;
  4061. var
  4062. objdef : tobjectdef;
  4063. sym : tsym;
  4064. i : integer;
  4065. pd : tprocdef;
  4066. hashedid : THashedIDString;
  4067. begin
  4068. result:=nil;
  4069. objdef:=self;
  4070. hashedid.id:='GETENUMERATOR';
  4071. while assigned(objdef) do
  4072. begin
  4073. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4074. if assigned(sym) and (sym.typ=procsym) then
  4075. begin
  4076. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  4077. begin
  4078. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4079. if (pd.proctypeoption = potype_function) and
  4080. is_class_or_interface_or_object(pd.returndef) and
  4081. (pd.visibility >= vis_public) then
  4082. begin
  4083. result:=pd;
  4084. exit;
  4085. end;
  4086. end;
  4087. end;
  4088. objdef:=objdef.childof;
  4089. end;
  4090. end;
  4091. function tobjectdef.search_enumerator_move: tprocdef;
  4092. var
  4093. objdef : tobjectdef;
  4094. sym : tsym;
  4095. i : integer;
  4096. pd : tprocdef;
  4097. hashedid : THashedIDString;
  4098. begin
  4099. result:=nil;
  4100. objdef:=self;
  4101. // first search for po_enumerator_movenext method modifier
  4102. // then search for public function MoveNext: Boolean
  4103. hashedid.id:='MOVENEXT';
  4104. while assigned(objdef) do
  4105. begin
  4106. for i:=0 to objdef.symtable.SymList.Count-1 do
  4107. begin
  4108. sym:=TSym(objdef.symtable.SymList[i]);
  4109. if (sym.typ=procsym) then
  4110. begin
  4111. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  4112. if assigned(pd) then
  4113. begin
  4114. result:=pd;
  4115. exit;
  4116. end;
  4117. end;
  4118. end;
  4119. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4120. if assigned(sym) and (sym.typ=procsym) then
  4121. begin
  4122. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  4123. begin
  4124. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4125. if (pd.proctypeoption = potype_function) and
  4126. is_boolean(pd.returndef) and
  4127. (pd.minparacount = 0) and
  4128. (pd.visibility >= vis_public) then
  4129. begin
  4130. result:=pd;
  4131. exit;
  4132. end;
  4133. end;
  4134. end;
  4135. objdef:=objdef.childof;
  4136. end;
  4137. end;
  4138. function tobjectdef.search_enumerator_current: tsym;
  4139. var
  4140. objdef : tobjectdef;
  4141. sym: tsym;
  4142. i: integer;
  4143. hashedid : THashedIDString;
  4144. begin
  4145. result:=nil;
  4146. objdef:=self;
  4147. hashedid.id:='CURRENT';
  4148. // first search for ppo_enumerator_current property modifier
  4149. // then search for public property Current
  4150. while assigned(objdef) do
  4151. begin
  4152. for i:=0 to objdef.symtable.SymList.Count-1 do
  4153. begin
  4154. sym:=TSym(objdef.symtable.SymList[i]);
  4155. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  4156. begin
  4157. result:=sym;
  4158. exit;
  4159. end;
  4160. end;
  4161. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4162. if assigned(sym) and (sym.typ=propertysym) and
  4163. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  4164. begin
  4165. result:=sym;
  4166. exit;
  4167. end;
  4168. objdef:=objdef.childof;
  4169. end;
  4170. end;
  4171. procedure tobjectdef.register_created_classref_type;
  4172. begin
  4173. if not classref_created_in_current_module then
  4174. begin
  4175. classref_created_in_current_module:=true;
  4176. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  4177. end;
  4178. end;
  4179. procedure tobjectdef.register_created_object_type;
  4180. begin
  4181. if not created_in_current_module then
  4182. begin
  4183. created_in_current_module:=true;
  4184. current_module.wpoinfo.addcreatedobjtype(self);
  4185. end;
  4186. end;
  4187. procedure tobjectdef.register_maybe_created_object_type;
  4188. begin
  4189. { if we know it has been created for sure, no need
  4190. to also record that it maybe can be created in
  4191. this module
  4192. }
  4193. if not (created_in_current_module) and
  4194. not (maybe_created_in_current_module) then
  4195. begin
  4196. maybe_created_in_current_module:=true;
  4197. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  4198. end;
  4199. end;
  4200. procedure tobjectdef.register_vmt_call(index: longint);
  4201. begin
  4202. if (is_object(self) or is_class(self)) then
  4203. current_module.wpoinfo.addcalledvmtentry(self,index);
  4204. end;
  4205. procedure make_procdef_external(data: tobject; arg: pointer);
  4206. var
  4207. def: tdef absolute data;
  4208. begin
  4209. if (def.typ = procdef) then
  4210. begin
  4211. include(tprocdef(def).procoptions,po_external);
  4212. tprocdef(def).forwarddef:=false;
  4213. end;
  4214. end;
  4215. procedure tobjectdef.make_all_methods_external;
  4216. begin
  4217. self.symtable.deflist.foreachcall(@make_procdef_external,nil);
  4218. end;
  4219. procedure check_and_finish_msg(data: tobject; arg: pointer);
  4220. var
  4221. def: tdef absolute data;
  4222. pd: tprocdef absolute data;
  4223. i,
  4224. paracount: longint;
  4225. begin
  4226. if (def.typ=procdef) then
  4227. begin
  4228. { add all messages also under a dummy name to the symtable in
  4229. which the objcclass/protocol/category is declared, so they can
  4230. be called via id.<name>
  4231. }
  4232. create_class_helper_for_procdef(pd,nil);
  4233. { we have to wait until now to set the mangled name because it
  4234. depends on the (possibly external) class name, which is defined
  4235. at the very end. }
  4236. if not(po_msgstr in pd.procoptions) then
  4237. begin
  4238. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  4239. { recover to avoid internalerror later on }
  4240. include(pd.procoptions,po_msgstr);
  4241. pd.messageinf.str:=stringdup('MissingDeclaration');
  4242. end;
  4243. { Mangled name is already set in case this is a copy of
  4244. another type. }
  4245. if not(po_has_mangledname in pd.procoptions) then
  4246. begin
  4247. { check whether the number of formal parameters is correct,
  4248. and whether they have valid Objective-C types }
  4249. paracount:=0;
  4250. for i:=1 to length(pd.messageinf.str^) do
  4251. if pd.messageinf.str^[i]=':' then
  4252. inc(paracount);
  4253. for i:=0 to pd.paras.count-1 do
  4254. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  4255. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  4256. dec(paracount);
  4257. if (paracount<>0) then
  4258. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  4259. pd.setmangledname(pd.objcmangledname);
  4260. end
  4261. else
  4262. { all checks already done }
  4263. exit;
  4264. if not(oo_is_external in pd._class.objectoptions) then
  4265. begin
  4266. if (po_varargs in pd.procoptions) then
  4267. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  4268. else
  4269. begin
  4270. { check for "array of const" parameters }
  4271. for i:=0 to pd.parast.symlist.count-1 do
  4272. begin
  4273. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  4274. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  4275. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  4276. end;
  4277. end;
  4278. end;
  4279. end;
  4280. end;
  4281. procedure mark_private_fields_used(data: tobject; arg: pointer);
  4282. var
  4283. sym: tsym absolute data;
  4284. begin
  4285. if (sym.typ=fieldvarsym) and
  4286. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  4287. sym.IncRefCount;
  4288. end;
  4289. procedure tobjectdef.finish_objc_data;
  4290. begin
  4291. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  4292. if (oo_is_external in objectoptions) then
  4293. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  4294. end;
  4295. procedure verify_objc_vardef(data: tobject; arg: pointer);
  4296. var
  4297. sym: tabstractvarsym absolute data;
  4298. res: pboolean absolute arg;
  4299. founderrordef: tdef;
  4300. begin
  4301. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  4302. exit;
  4303. if (sym.typ=paravarsym) and
  4304. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  4305. is_array_of_const(tparavarsym(sym).vardef)) then
  4306. exit;
  4307. if not objcchecktype(sym.vardef,founderrordef) then
  4308. begin
  4309. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4310. res^:=false;
  4311. end;
  4312. end;
  4313. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  4314. var
  4315. def: tdef absolute data;
  4316. res: pboolean absolute arg;
  4317. founderrordef: tdef;
  4318. begin
  4319. if (def.typ<>procdef) then
  4320. exit;
  4321. { check parameter types for validity }
  4322. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  4323. { check the result type for validity }
  4324. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  4325. begin
  4326. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4327. res^:=false;
  4328. end;
  4329. end;
  4330. function tobjectdef.check_objc_types: boolean;
  4331. begin
  4332. { done in separate step from finish_objc_data, because when
  4333. finish_objc_data is called, not all forwarddefs have been resolved
  4334. yet and we need to know all types here }
  4335. result:=true;
  4336. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  4337. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  4338. end;
  4339. procedure do_cpp_import_info(data: tobject; arg: pointer);
  4340. var
  4341. def: tdef absolute data;
  4342. pd: tprocdef absolute data;
  4343. begin
  4344. if (def.typ=procdef) then
  4345. begin
  4346. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  4347. if (oo_is_external in pd._class.objectoptions) then
  4348. begin
  4349. { copied from psub.read_proc }
  4350. if assigned(pd._class.import_lib) then
  4351. current_module.AddExternalImport(pd._class.import_lib^,pd.mangledname,0,false,false)
  4352. else
  4353. begin
  4354. { add import name to external list for DLL scanning }
  4355. if tf_has_dllscanner in target_info.flags then
  4356. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  4357. end;
  4358. end;
  4359. end;
  4360. end;
  4361. procedure tobjectdef.finish_cpp_data;
  4362. begin
  4363. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  4364. end;
  4365. function tobjectdef.RttiName: string;
  4366. var
  4367. tmp: tobjectdef;
  4368. begin
  4369. Result:=objrealname^;
  4370. tmp:=self;
  4371. repeat
  4372. if tmp.owner.symtabletype=ObjectSymtable then
  4373. tmp:=tobjectdef(tmp.owner.defowner)
  4374. else
  4375. break;
  4376. Result:=tmp.objrealname^+'.'+Result;
  4377. until tmp=nil;
  4378. end;
  4379. {****************************************************************************
  4380. TImplementedInterface
  4381. ****************************************************************************}
  4382. constructor TImplementedInterface.create(aintf: tobjectdef);
  4383. begin
  4384. inherited create;
  4385. intfdef:=aintf;
  4386. IOffset:=-1;
  4387. IType:=etStandard;
  4388. NameMappings:=nil;
  4389. procdefs:=nil;
  4390. end;
  4391. constructor TImplementedInterface.create_deref(d:tderef);
  4392. begin
  4393. inherited create;
  4394. intfdef:=nil;
  4395. intfdefderef:=d;
  4396. IOffset:=-1;
  4397. IType:=etStandard;
  4398. NameMappings:=nil;
  4399. procdefs:=nil;
  4400. end;
  4401. destructor TImplementedInterface.destroy;
  4402. var
  4403. i : longint;
  4404. mappedname : pshortstring;
  4405. begin
  4406. if assigned(NameMappings) then
  4407. begin
  4408. for i:=0 to NameMappings.Count-1 do
  4409. begin
  4410. mappedname:=pshortstring(NameMappings[i]);
  4411. stringdispose(mappedname);
  4412. end;
  4413. NameMappings.free;
  4414. NameMappings:=nil;
  4415. end;
  4416. if assigned(procdefs) then
  4417. begin
  4418. procdefs.free;
  4419. procdefs:=nil;
  4420. end;
  4421. inherited destroy;
  4422. end;
  4423. procedure TImplementedInterface.buildderef;
  4424. begin
  4425. intfdefderef.build(intfdef);
  4426. end;
  4427. procedure TImplementedInterface.deref;
  4428. begin
  4429. intfdef:=tobjectdef(intfdefderef.resolve);
  4430. end;
  4431. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  4432. begin
  4433. if not assigned(NameMappings) then
  4434. NameMappings:=TFPHashList.Create;
  4435. NameMappings.Add(origname,stringdup(newname));
  4436. end;
  4437. function TImplementedInterface.GetMapping(const origname: string):string;
  4438. var
  4439. mappedname : pshortstring;
  4440. begin
  4441. result:='';
  4442. if not assigned(NameMappings) then
  4443. exit;
  4444. mappedname:=PShortstring(NameMappings.Find(origname));
  4445. if assigned(mappedname) then
  4446. result:=mappedname^;
  4447. end;
  4448. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  4449. begin
  4450. if not assigned(procdefs) then
  4451. procdefs:=TFPObjectList.Create(false);
  4452. { duplicate entries must be stored, because multiple }
  4453. { interfaces can declare methods with the same name }
  4454. { and all of these get their own VMT entry }
  4455. procdefs.Add(pd);
  4456. end;
  4457. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  4458. var
  4459. i : longint;
  4460. begin
  4461. result:=false;
  4462. { interfaces being implemented through delegation are not mergable (FK) }
  4463. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) then
  4464. exit;
  4465. weight:=0;
  4466. { empty interface is mergeable }
  4467. if ProcDefs.Count=0 then
  4468. begin
  4469. result:=true;
  4470. exit;
  4471. end;
  4472. { The interface to merge must at least the number of
  4473. procedures of this interface }
  4474. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  4475. exit;
  4476. for i:=0 to ProcDefs.Count-1 do
  4477. begin
  4478. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  4479. exit;
  4480. end;
  4481. weight:=ProcDefs.Count;
  4482. result:=true;
  4483. end;
  4484. function TImplementedInterface.getcopy:TImplementedInterface;
  4485. begin
  4486. Result:=TImplementedInterface.Create(nil);
  4487. {$warning: this is completely wrong on so many levels...}
  4488. { 1) the procdefs list will be freed once for each copy
  4489. 2) since the procdefs list owns its elements, those will also be freed for each copy
  4490. 3) idem for the name mappings
  4491. }
  4492. Move(pointer(self)^,pointer(result)^,InstanceSize);
  4493. end;
  4494. {****************************************************************************
  4495. TFORWARDDEF
  4496. ****************************************************************************}
  4497. constructor tforwarddef.create(const s:string;const pos : tfileposinfo);
  4498. begin
  4499. inherited create(forwarddef);
  4500. tosymname:=stringdup(s);
  4501. forwardpos:=pos;
  4502. end;
  4503. function tforwarddef.GetTypeName:string;
  4504. begin
  4505. GetTypeName:='unresolved forward to '+tosymname^;
  4506. end;
  4507. destructor tforwarddef.destroy;
  4508. begin
  4509. stringdispose(tosymname);
  4510. inherited destroy;
  4511. end;
  4512. {****************************************************************************
  4513. TUNDEFINEDDEF
  4514. ****************************************************************************}
  4515. constructor tundefineddef.create;
  4516. begin
  4517. inherited create(undefineddef);
  4518. end;
  4519. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  4520. begin
  4521. inherited ppuload(undefineddef,ppufile);
  4522. end;
  4523. function tundefineddef.GetTypeName:string;
  4524. begin
  4525. GetTypeName:='<undefined type>';
  4526. end;
  4527. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  4528. begin
  4529. inherited ppuwrite(ppufile);
  4530. ppufile.writeentry(ibundefineddef);
  4531. end;
  4532. {****************************************************************************
  4533. TERRORDEF
  4534. ****************************************************************************}
  4535. constructor terrordef.create;
  4536. begin
  4537. inherited create(errordef);
  4538. { prevent consecutive faults }
  4539. savesize:=1;
  4540. end;
  4541. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  4542. begin
  4543. { Can't write errordefs to ppu }
  4544. internalerror(200411063);
  4545. end;
  4546. function terrordef.GetTypeName:string;
  4547. begin
  4548. GetTypeName:='<erroneous type>';
  4549. end;
  4550. function terrordef.getmangledparaname:string;
  4551. begin
  4552. getmangledparaname:='error';
  4553. end;
  4554. {****************************************************************************
  4555. Definition Helpers
  4556. ****************************************************************************}
  4557. function is_interfacecom(def: tdef): boolean;
  4558. begin
  4559. is_interfacecom:=
  4560. assigned(def) and
  4561. (def.typ=objectdef) and
  4562. (tobjectdef(def).objecttype=odt_interfacecom);
  4563. end;
  4564. function is_interfacecorba(def: tdef): boolean;
  4565. begin
  4566. is_interfacecorba:=
  4567. assigned(def) and
  4568. (def.typ=objectdef) and
  4569. (tobjectdef(def).objecttype=odt_interfacecorba);
  4570. end;
  4571. function is_interface(def: tdef): boolean;
  4572. begin
  4573. is_interface:=
  4574. assigned(def) and
  4575. (def.typ=objectdef) and
  4576. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  4577. end;
  4578. function is_dispinterface(def: tdef): boolean;
  4579. begin
  4580. result:=
  4581. assigned(def) and
  4582. (def.typ=objectdef) and
  4583. (tobjectdef(def).objecttype=odt_dispinterface);
  4584. end;
  4585. function is_class(def: tdef): boolean;
  4586. begin
  4587. is_class:=
  4588. assigned(def) and
  4589. (def.typ=objectdef) and
  4590. (tobjectdef(def).objecttype=odt_class);
  4591. end;
  4592. function is_object(def: tdef): boolean;
  4593. begin
  4594. is_object:=
  4595. assigned(def) and
  4596. (def.typ=objectdef) and
  4597. (tobjectdef(def).objecttype=odt_object);
  4598. end;
  4599. function is_cppclass(def: tdef): boolean;
  4600. begin
  4601. is_cppclass:=
  4602. assigned(def) and
  4603. (def.typ=objectdef) and
  4604. (tobjectdef(def).objecttype=odt_cppclass);
  4605. end;
  4606. function is_objcclass(def: tdef): boolean;
  4607. begin
  4608. is_objcclass:=
  4609. assigned(def) and
  4610. (def.typ=objectdef) and
  4611. (tobjectdef(def).objecttype=odt_objcclass);
  4612. end;
  4613. function is_objcclassref(def: tdef): boolean;
  4614. begin
  4615. is_objcclassref:=
  4616. assigned(def) and
  4617. (def.typ=classrefdef) and
  4618. is_objcclass(tclassrefdef(def).pointeddef);
  4619. end;
  4620. function is_objcprotocol(def: tdef): boolean;
  4621. begin
  4622. result:=
  4623. assigned(def) and
  4624. (def.typ=objectdef) and
  4625. (tobjectdef(def).objecttype=odt_objcprotocol);
  4626. end;
  4627. function is_objccategory(def: tdef): boolean;
  4628. begin
  4629. result:=
  4630. assigned(def) and
  4631. (def.typ=objectdef) and
  4632. { if used as a forward type }
  4633. ((tobjectdef(def).objecttype=odt_objccategory) or
  4634. { if used as after it has been resolved }
  4635. ((tobjectdef(def).objecttype=odt_objcclass) and
  4636. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4637. end;
  4638. function is_objc_class_or_protocol(def: tdef): boolean;
  4639. begin
  4640. result:=
  4641. assigned(def) and
  4642. (def.typ=objectdef) and
  4643. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  4644. end;
  4645. function is_objc_protocol_or_category(def: tdef): boolean;
  4646. begin
  4647. result:=
  4648. assigned(def) and
  4649. (def.typ=objectdef) and
  4650. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  4651. ((tobjectdef(def).objecttype = odt_objcclass) and
  4652. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4653. end;
  4654. function is_class_or_interface(def: tdef): boolean;
  4655. begin
  4656. result:=
  4657. assigned(def) and
  4658. (def.typ=objectdef) and
  4659. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  4660. end;
  4661. function is_class_or_interface_or_objc(def: tdef): boolean;
  4662. begin
  4663. result:=
  4664. assigned(def) and
  4665. (def.typ=objectdef) and
  4666. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  4667. end;
  4668. function is_class_or_interface_or_object(def: tdef): boolean;
  4669. begin
  4670. result:=
  4671. assigned(def) and
  4672. (def.typ=objectdef) and
  4673. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  4674. end;
  4675. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  4676. begin
  4677. result:=
  4678. assigned(def) and
  4679. (def.typ=objectdef) and
  4680. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  4681. end;
  4682. function is_class_or_interface_or_dispinterface_or_objc(def: tdef): boolean;
  4683. begin
  4684. result:=
  4685. assigned(def) and
  4686. (def.typ=objectdef) and
  4687. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol]);
  4688. end;
  4689. function is_class_or_object(def: tdef): boolean;
  4690. begin
  4691. result:=
  4692. assigned(def) and
  4693. (def.typ=objectdef) and
  4694. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  4695. end;
  4696. procedure loadobjctypes;
  4697. begin
  4698. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  4699. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  4700. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  4701. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  4702. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  4703. end;
  4704. procedure maybeloadcocoatypes;
  4705. var
  4706. tsym: ttypesym;
  4707. begin
  4708. if assigned(objc_fastenumeration) then
  4709. exit;
  4710. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONPROTOCOL',false);
  4711. if assigned(tsym) then
  4712. objc_fastenumeration:=tobjectdef(tsym.typedef)
  4713. else
  4714. objc_fastenumeration:=nil;
  4715. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONSTATE',false);
  4716. if assigned(tsym) then
  4717. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  4718. else
  4719. objc_fastenumerationstate:=nil;
  4720. end;
  4721. function use_vectorfpu(def : tdef) : boolean;
  4722. begin
  4723. {$ifdef x86}
  4724. {$define use_vectorfpuimplemented}
  4725. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  4726. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  4727. {$endif x86}
  4728. {$ifdef arm}
  4729. {$define use_vectorfpuimplemented}
  4730. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  4731. {$endif arm}
  4732. {$ifndef use_vectorfpuimplemented}
  4733. use_vectorfpu:=false;
  4734. {$endif}
  4735. end;
  4736. end.