symdef.pas 160 KB

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