symdef.pas 161 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267
  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)>100) 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)>100 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. tmp: tobjectdef;
  2787. begin
  2788. {$ifdef EXTDEBUG}
  2789. showhidden:=true;
  2790. {$endif EXTDEBUG}
  2791. s:='';
  2792. if assigned(_class) then
  2793. begin
  2794. tmp:=_class;
  2795. while assigned(tmp) do
  2796. begin
  2797. s:=tmp.objrealname^+'.'+s;
  2798. if assigned(tmp.owner) and (tmp.owner.symtabletype=ObjectSymtable) then
  2799. tmp:=tobjectdef(tmp.owner.defowner)
  2800. else
  2801. tmp:=nil;
  2802. end;
  2803. if (po_classmethod in procoptions) then
  2804. s:='class ' + s;
  2805. end;
  2806. if owner.symtabletype=localsymtable then
  2807. s:='local ' + s;
  2808. if proctypeoption=potype_operator then
  2809. begin
  2810. for t:=NOTOKEN to last_overloaded do
  2811. if procsym.realname='$'+overloaded_names[t] then
  2812. begin
  2813. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  2814. break;
  2815. end;
  2816. end
  2817. else
  2818. s:=s+procsym.realname+typename_paras(showhidden);
  2819. case proctypeoption of
  2820. potype_constructor:
  2821. s:='constructor '+s;
  2822. potype_destructor:
  2823. s:='destructor '+s;
  2824. else
  2825. if assigned(returndef) and
  2826. not(is_void(returndef)) then
  2827. s:=s+':'+returndef.GetTypeName;
  2828. end;
  2829. s:=s+';';
  2830. { forced calling convention? }
  2831. if (po_hascallingconvention in procoptions) then
  2832. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  2833. if po_staticmethod in procoptions then
  2834. s:=s+' Static;';
  2835. fullprocname:=s;
  2836. end;
  2837. function tprocdef.is_methodpointer:boolean;
  2838. begin
  2839. result:=assigned(_class);
  2840. end;
  2841. function tprocdef.is_addressonly:boolean;
  2842. begin
  2843. result:=assigned(owner) and
  2844. (owner.symtabletype<>ObjectSymtable);
  2845. end;
  2846. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  2847. begin
  2848. case t of
  2849. gs_local :
  2850. GetSymtable:=localst;
  2851. gs_para :
  2852. GetSymtable:=parast;
  2853. else
  2854. GetSymtable:=nil;
  2855. end;
  2856. end;
  2857. procedure tprocdef.buildderef;
  2858. begin
  2859. inherited buildderef;
  2860. _classderef.build(_class);
  2861. { procsym that originaly defined this definition, should be in the
  2862. same symtable }
  2863. procsymderef.build(procsym);
  2864. {$ifdef powerpc}
  2865. { library symbol for AmigaOS/MorphOS }
  2866. libsymderef.build(libsym);
  2867. {$endif powerpc}
  2868. end;
  2869. procedure tprocdef.buildderefimpl;
  2870. begin
  2871. inherited buildderefimpl;
  2872. { Localst is not available for main/unit init }
  2873. if assigned(localst) then
  2874. begin
  2875. tlocalsymtable(localst).buildderef;
  2876. tlocalsymtable(localst).buildderefimpl;
  2877. end;
  2878. { inline tree }
  2879. if (po_has_inlininginfo in procoptions) then
  2880. begin
  2881. funcretsymderef.build(funcretsym);
  2882. inlininginfo^.code.buildderefimpl;
  2883. end;
  2884. end;
  2885. procedure tprocdef.deref;
  2886. begin
  2887. inherited deref;
  2888. _class:=tobjectdef(_classderef.resolve);
  2889. { procsym that originaly defined this definition, should be in the
  2890. same symtable }
  2891. procsym:=tprocsym(procsymderef.resolve);
  2892. {$ifdef powerpc}
  2893. { library symbol for AmigaOS/MorphOS }
  2894. libsym:=tsym(libsymderef.resolve);
  2895. {$endif powerpc}
  2896. end;
  2897. procedure tprocdef.derefimpl;
  2898. begin
  2899. { Enable has_inlininginfo when the inlininginfo
  2900. structure is available. The has_inlininginfo was disabled
  2901. after the load, since the data was invalid }
  2902. if assigned(inlininginfo) then
  2903. include(procoptions,po_has_inlininginfo);
  2904. { Locals }
  2905. if assigned(localst) then
  2906. begin
  2907. tlocalsymtable(localst).deref;
  2908. tlocalsymtable(localst).derefimpl;
  2909. end;
  2910. { Inline }
  2911. if (po_has_inlininginfo in procoptions) then
  2912. begin
  2913. inlininginfo^.code.derefimpl;
  2914. { funcretsym, this is always located in the localst }
  2915. funcretsym:=tsym(funcretsymderef.resolve);
  2916. end
  2917. else
  2918. begin
  2919. { safety }
  2920. { Not safe! A unit may be reresolved after its interface has been
  2921. parsed but before its implementation has been parsed, and in that
  2922. case the funcretsym is still required!
  2923. funcretsym:=nil; }
  2924. end;
  2925. end;
  2926. function tprocdef.GetTypeName : string;
  2927. begin
  2928. GetTypeName := FullProcName(false);
  2929. end;
  2930. function tprocdef.mangledname : string;
  2931. var
  2932. hp : TParavarsym;
  2933. hs : string;
  2934. crc : dword;
  2935. newlen,
  2936. oldlen,
  2937. i : integer;
  2938. begin
  2939. if assigned(_mangledname) then
  2940. begin
  2941. {$ifdef compress}
  2942. mangledname:=minilzw_decode(_mangledname^);
  2943. {$else}
  2944. mangledname:=_mangledname^;
  2945. {$endif}
  2946. exit;
  2947. end;
  2948. { we need to use the symtable where the procsym is inserted,
  2949. because that is visible to the world }
  2950. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  2951. oldlen:=length(mangledname);
  2952. { add parameter types }
  2953. for i:=0 to paras.count-1 do
  2954. begin
  2955. hp:=tparavarsym(paras[i]);
  2956. if not(vo_is_hidden_para in hp.varoptions) then
  2957. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  2958. end;
  2959. { add resultdef, add $$ as separator to make it unique from a
  2960. parameter separator }
  2961. if not is_void(returndef) then
  2962. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  2963. newlen:=length(mangledname);
  2964. { Replace with CRC if the parameter line is very long }
  2965. if (newlen-oldlen>12) and
  2966. ((newlen>100) or (newlen-oldlen>64)) then
  2967. begin
  2968. crc:=0;
  2969. for i:=0 to paras.count-1 do
  2970. begin
  2971. hp:=tparavarsym(paras[i]);
  2972. if not(vo_is_hidden_para in hp.varoptions) then
  2973. begin
  2974. hs:=hp.vardef.mangledparaname;
  2975. crc:=UpdateCrc32(crc,hs[1],length(hs));
  2976. end;
  2977. end;
  2978. hs:=hp.vardef.mangledparaname;
  2979. crc:=UpdateCrc32(crc,hs[1],length(hs));
  2980. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  2981. end;
  2982. {$ifdef compress}
  2983. _mangledname:=stringdup(minilzw_encode(mangledname));
  2984. {$else}
  2985. _mangledname:=stringdup(mangledname);
  2986. {$endif}
  2987. end;
  2988. function tprocdef.cplusplusmangledname : string;
  2989. function getcppparaname(p : tdef) : string;
  2990. const
  2991. {$ifdef NAMEMANGLING_GCC2}
  2992. ordtype2str : array[tordtype] of string[2] = (
  2993. '',
  2994. 'Uc','Us','Ui','Us',
  2995. 'Sc','s','i','x',
  2996. 'b','b','b','b','b',
  2997. 'c','w','x');
  2998. {$else NAMEMANGLING_GCC2}
  2999. ordtype2str : array[tordtype] of string[1] = (
  3000. 'v',
  3001. 'h','t','j','y',
  3002. 'a','s','i','x',
  3003. 'b','b','b','b','b',
  3004. 'c','w','x');
  3005. floattype2str : array[tfloattype] of string[1] = (
  3006. 'f','d','e',
  3007. 'd','d','g');
  3008. {$endif NAMEMANGLING_GCC2}
  3009. var
  3010. s : string;
  3011. begin
  3012. case p.typ of
  3013. orddef:
  3014. s:=ordtype2str[torddef(p).ordtype];
  3015. pointerdef:
  3016. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  3017. {$ifndef NAMEMANGLING_GCC2}
  3018. floatdef:
  3019. s:=floattype2str[tfloatdef(p).floattype];
  3020. {$endif NAMEMANGLING_GCC2}
  3021. else
  3022. internalerror(2103001);
  3023. end;
  3024. getcppparaname:=s;
  3025. end;
  3026. var
  3027. s,s2 : string;
  3028. hp : TParavarsym;
  3029. i : integer;
  3030. begin
  3031. {$ifdef NAMEMANGLING_GCC2}
  3032. { outdated gcc 2.x name mangling scheme }
  3033. s := procsym.realname;
  3034. if procsym.owner.symtabletype=ObjectSymtable then
  3035. begin
  3036. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  3037. case proctypeoption of
  3038. potype_destructor:
  3039. s:='_$_'+tostr(length(s2))+s2;
  3040. potype_constructor:
  3041. s:='___'+tostr(length(s2))+s2;
  3042. else
  3043. s:='_'+s+'__'+tostr(length(s2))+s2;
  3044. end;
  3045. end
  3046. else s:=s+'__';
  3047. s:=s+'F';
  3048. { concat modifiers }
  3049. { !!!!! }
  3050. { now we handle the parameters }
  3051. if maxparacount>0 then
  3052. begin
  3053. for i:=0 to paras.count-1 do
  3054. begin
  3055. hp:=tparavarsym(paras[i]);
  3056. { no hidden parameters form part of a C++ mangled name:
  3057. a) self is not included
  3058. b) there are no "high" or other hidden parameters
  3059. }
  3060. if vo_is_hidden_para in hp.varoptions then
  3061. continue;
  3062. s2:=getcppparaname(hp.vardef);
  3063. if hp.varspez in [vs_var,vs_out] then
  3064. s2:='R'+s2;
  3065. s:=s+s2;
  3066. end;
  3067. end
  3068. else
  3069. s:=s+'v';
  3070. cplusplusmangledname:=s;
  3071. {$else NAMEMANGLING_GCC2}
  3072. { gcc 3.x and 4.x name mangling scheme }
  3073. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  3074. if procsym.owner.symtabletype=ObjectSymtable then
  3075. begin
  3076. s:='_ZN';
  3077. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  3078. s:=s+tostr(length(s2))+s2;
  3079. case proctypeoption of
  3080. potype_constructor:
  3081. s:=s+'C1';
  3082. potype_destructor:
  3083. s:=s+'D1';
  3084. else
  3085. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3086. end;
  3087. s:=s+'E';
  3088. end
  3089. else
  3090. s:=procsym.realname;
  3091. { now we handle the parameters }
  3092. if maxparacount>0 then
  3093. begin
  3094. for i:=0 to paras.count-1 do
  3095. begin
  3096. hp:=tparavarsym(paras[i]);
  3097. { no hidden parameters form part of a C++ mangled name:
  3098. a) self is not included
  3099. b) there are no "high" or other hidden parameters
  3100. }
  3101. if vo_is_hidden_para in hp.varoptions then
  3102. continue;
  3103. s2:=getcppparaname(hp.vardef);
  3104. if hp.varspez in [vs_var,vs_out] then
  3105. s2:='R'+s2;
  3106. s:=s+s2;
  3107. end;
  3108. end
  3109. else
  3110. s:=s+'v';
  3111. cplusplusmangledname:=s;
  3112. {$endif NAMEMANGLING_GCC2}
  3113. end;
  3114. function tprocdef.objcmangledname : string;
  3115. var
  3116. manglednamelen: longint;
  3117. iscatmethod : boolean;
  3118. begin
  3119. if not (po_msgstr in procoptions) then
  3120. internalerror(2009030901);
  3121. { we may very well need longer strings to handle these... }
  3122. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  3123. length('+"[ ]"')+length(messageinf.str^);
  3124. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  3125. if (iscatmethod) then
  3126. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  3127. if manglednamelen>255 then
  3128. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  3129. if not(po_classmethod in procoptions) then
  3130. result:='"-['
  3131. else
  3132. result:='"+[';
  3133. { quotes are necessary because the +/- otherwise confuse the assembler
  3134. into expecting a number
  3135. }
  3136. if iscatmethod then
  3137. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  3138. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  3139. if iscatmethod then
  3140. result:=result+')';
  3141. result:=result+' '+messageinf.str^+']"';
  3142. end;
  3143. procedure tprocdef.setmangledname(const s : string);
  3144. begin
  3145. { This is not allowed anymore, the forward declaration
  3146. already needs to create the correct mangledname, no changes
  3147. afterwards are allowed (PFV) }
  3148. { Exception: interface definitions in mode macpas, since in that }
  3149. { case no reference to the old name can exist yet (JM) }
  3150. if assigned(_mangledname) then
  3151. if ((m_mac in current_settings.modeswitches) and
  3152. (interfacedef)) then
  3153. stringdispose(_mangledname)
  3154. else
  3155. internalerror(200411171);
  3156. {$ifdef compress}
  3157. _mangledname:=stringdup(minilzw_encode(s));
  3158. {$else}
  3159. _mangledname:=stringdup(s);
  3160. {$endif}
  3161. include(procoptions,po_has_mangledname);
  3162. end;
  3163. {***************************************************************************
  3164. TPROCVARDEF
  3165. ***************************************************************************}
  3166. constructor tprocvardef.create(level:byte);
  3167. begin
  3168. inherited create(procvardef,level);
  3169. end;
  3170. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3171. begin
  3172. inherited ppuload(procvardef,ppufile);
  3173. { load para symtable }
  3174. parast:=tparasymtable.create(self,unknown_level);
  3175. tparasymtable(parast).ppuload(ppufile);
  3176. end;
  3177. function tprocvardef.getcopy : tstoreddef;
  3178. var
  3179. i : tcallercallee;
  3180. j : longint;
  3181. begin
  3182. result:=tprocvardef.create(parast.symtablelevel);
  3183. tprocvardef(result).returndef:=returndef;
  3184. tprocvardef(result).returndefderef:=returndefderef;
  3185. tprocvardef(result).parast:=parast.getcopy;
  3186. tprocvardef(result).savesize:=savesize;
  3187. { create paralist copy }
  3188. tprocvardef(result).paras:=tparalist.create(false);
  3189. tprocvardef(result).paras.count:=paras.count;
  3190. for j:=0 to paras.count-1 do
  3191. tprocvardef(result).paras[j]:=paras[j];
  3192. tprocvardef(result).proctypeoption:=proctypeoption;
  3193. tprocvardef(result).proccalloption:=proccalloption;
  3194. tprocvardef(result).procoptions:=procoptions;
  3195. tprocvardef(result).requiredargarea:=requiredargarea;
  3196. tprocvardef(result).maxparacount:=maxparacount;
  3197. tprocvardef(result).minparacount:=minparacount;
  3198. for i:=low(tcallercallee) to high(tcallercallee) do
  3199. location_copy(tprocvardef(result).funcretloc[i],funcretloc[i]);
  3200. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3201. {$ifdef m68k}
  3202. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3203. {$endif}
  3204. end;
  3205. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3206. begin
  3207. inherited ppuwrite(ppufile);
  3208. { Write this entry }
  3209. ppufile.writeentry(ibprocvardef);
  3210. { Save the para symtable, this is taken from the interface }
  3211. tparasymtable(parast).ppuwrite(ppufile);
  3212. end;
  3213. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3214. begin
  3215. case t of
  3216. gs_para :
  3217. GetSymtable:=parast;
  3218. else
  3219. GetSymtable:=nil;
  3220. end;
  3221. end;
  3222. function tprocvardef.size : aint;
  3223. begin
  3224. if (po_methodpointer in procoptions) and
  3225. not(po_addressonly in procoptions) then
  3226. size:=2*sizeof(pint)
  3227. else
  3228. size:=sizeof(pint);
  3229. end;
  3230. function tprocvardef.is_methodpointer:boolean;
  3231. begin
  3232. result:=(po_methodpointer in procoptions);
  3233. end;
  3234. function tprocvardef.is_addressonly:boolean;
  3235. begin
  3236. result:=not(po_methodpointer in procoptions) or
  3237. (po_addressonly in procoptions);
  3238. end;
  3239. function tprocvardef.getmangledparaname:string;
  3240. begin
  3241. result:='procvar';
  3242. end;
  3243. function tprocvardef.is_publishable : boolean;
  3244. begin
  3245. is_publishable:=(po_methodpointer in procoptions);
  3246. end;
  3247. function tprocvardef.GetTypeName : string;
  3248. var
  3249. s: string;
  3250. showhidden : boolean;
  3251. begin
  3252. {$ifdef EXTDEBUG}
  3253. showhidden:=true;
  3254. {$else EXTDEBUG}
  3255. showhidden:=false;
  3256. {$endif EXTDEBUG}
  3257. s:='<';
  3258. if po_classmethod in procoptions then
  3259. s := s+'class method type of'
  3260. else
  3261. if po_addressonly in procoptions then
  3262. s := s+'address of'
  3263. else
  3264. s := s+'procedure variable type of';
  3265. if po_local in procoptions then
  3266. s := s+' local';
  3267. if assigned(returndef) and
  3268. (returndef<>voidtype) then
  3269. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3270. else
  3271. s:=s+' procedure'+typename_paras(showhidden);
  3272. if po_methodpointer in procoptions then
  3273. s := s+' of object';
  3274. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3275. end;
  3276. {***************************************************************************
  3277. TOBJECTDEF
  3278. ***************************************************************************}
  3279. constructor tobjectdef.create(ot : tobjecttyp;const n : string;c : tobjectdef);
  3280. begin
  3281. inherited create(objectdef);
  3282. objecttype:=ot;
  3283. objectoptions:=[];
  3284. childof:=nil;
  3285. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3286. { create space for vmt !! }
  3287. vmtentries:=TFPList.Create;
  3288. vmt_offset:=0;
  3289. set_parent(c);
  3290. objname:=stringdup(upper(n));
  3291. objrealname:=stringdup(n);
  3292. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3293. prepareguid;
  3294. { setup implemented interfaces }
  3295. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3296. ImplementedInterfaces:=TFPObjectList.Create(true)
  3297. else
  3298. ImplementedInterfaces:=nil;
  3299. writing_class_record_dbginfo:=false;
  3300. end;
  3301. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3302. var
  3303. i,
  3304. implintfcount : longint;
  3305. d : tderef;
  3306. ImplIntf : TImplementedInterface;
  3307. vmtentry : pvmtentry;
  3308. begin
  3309. inherited ppuload(objectdef,ppufile);
  3310. objecttype:=tobjecttyp(ppufile.getbyte);
  3311. objrealname:=stringdup(ppufile.getstring);
  3312. objname:=stringdup(upper(objrealname^));
  3313. objextname:=stringdup(ppufile.getstring);
  3314. { only used for external Objective-C classes/protocols }
  3315. if (objextname^='') then
  3316. stringdispose(objextname);
  3317. import_lib:=stringdup(ppufile.getstring);
  3318. { only used for external C++ classes }
  3319. if (import_lib^='') then
  3320. stringdispose(import_lib);
  3321. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3322. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3323. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3324. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3325. vmt_offset:=ppufile.getlongint;
  3326. ppufile.getderef(childofderef);
  3327. ppufile.getsmallset(objectoptions);
  3328. { load guid }
  3329. iidstr:=nil;
  3330. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3331. begin
  3332. new(iidguid);
  3333. ppufile.getguid(iidguid^);
  3334. iidstr:=stringdup(ppufile.getstring);
  3335. end;
  3336. vmtentries:=TFPList.Create;
  3337. vmtentries.count:=ppufile.getlongint;
  3338. for i:=0 to vmtentries.count-1 do
  3339. begin
  3340. ppufile.getderef(d);
  3341. new(vmtentry);
  3342. vmtentry^.procdef:=nil;
  3343. vmtentry^.procdefderef:=d;
  3344. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  3345. vmtentries[i]:=vmtentry;
  3346. end;
  3347. { load implemented interfaces }
  3348. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3349. begin
  3350. ImplementedInterfaces:=TFPObjectList.Create(true);
  3351. implintfcount:=ppufile.getlongint;
  3352. for i:=0 to implintfcount-1 do
  3353. begin
  3354. ppufile.getderef(d);
  3355. ImplIntf:=TImplementedInterface.Create_deref(d);
  3356. ImplIntf.IOffset:=ppufile.getlongint;
  3357. ImplementedInterfaces.Add(ImplIntf);
  3358. end;
  3359. end
  3360. else
  3361. ImplementedInterfaces:=nil;
  3362. if df_copied_def in defoptions then
  3363. ppufile.getderef(cloneddefderef)
  3364. else
  3365. tObjectSymtable(symtable).ppuload(ppufile);
  3366. { handles the predefined class tobject }
  3367. { the last TOBJECT which is loaded gets }
  3368. { it ! }
  3369. if (childof=nil) and
  3370. (objecttype=odt_class) and
  3371. (objname^='TOBJECT') then
  3372. class_tobject:=self;
  3373. if (childof=nil) and
  3374. (objecttype=odt_interfacecom) and
  3375. (objname^='IUNKNOWN') then
  3376. interface_iunknown:=self;
  3377. if (childof=nil) and
  3378. (objecttype=odt_objcclass) and
  3379. (objname^='PROTOCOL') then
  3380. objc_protocoltype:=self;
  3381. writing_class_record_dbginfo:=false;
  3382. end;
  3383. destructor tobjectdef.destroy;
  3384. begin
  3385. if assigned(symtable) then
  3386. begin
  3387. symtable.free;
  3388. symtable:=nil;
  3389. end;
  3390. stringdispose(objname);
  3391. stringdispose(objrealname);
  3392. stringdispose(objextname);
  3393. stringdispose(import_lib);
  3394. stringdispose(iidstr);
  3395. if assigned(ImplementedInterfaces) then
  3396. begin
  3397. ImplementedInterfaces.free;
  3398. ImplementedInterfaces:=nil;
  3399. end;
  3400. if assigned(iidguid) then
  3401. begin
  3402. dispose(iidguid);
  3403. iidguid:=nil;
  3404. end;
  3405. if assigned(vmtentries) then
  3406. begin
  3407. resetvmtentries;
  3408. vmtentries.free;
  3409. vmtentries:=nil;
  3410. end;
  3411. if assigned(vmcallstaticinfo) then
  3412. begin
  3413. freemem(vmcallstaticinfo);
  3414. vmcallstaticinfo:=nil;
  3415. end;
  3416. inherited destroy;
  3417. end;
  3418. function tobjectdef.getcopy : tstoreddef;
  3419. var
  3420. i : longint;
  3421. begin
  3422. result:=tobjectdef.create(objecttype,objname^,childof);
  3423. { the constructor allocates a symtable which we release to avoid memory leaks }
  3424. tobjectdef(result).symtable.free;
  3425. tobjectdef(result).symtable:=symtable.getcopy;
  3426. if assigned(objname) then
  3427. tobjectdef(result).objname:=stringdup(objname^);
  3428. if assigned(objrealname) then
  3429. tobjectdef(result).objrealname:=stringdup(objrealname^);
  3430. if assigned(objextname) then
  3431. tobjectdef(result).objextname:=stringdup(objextname^);
  3432. if assigned(import_lib) then
  3433. tobjectdef(result).import_lib:=stringdup(import_lib^);
  3434. tobjectdef(result).objectoptions:=objectoptions;
  3435. include(tobjectdef(result).defoptions,df_copied_def);
  3436. tobjectdef(result).vmt_offset:=vmt_offset;
  3437. if assigned(iidguid) then
  3438. begin
  3439. new(tobjectdef(result).iidguid);
  3440. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3441. end;
  3442. if assigned(iidstr) then
  3443. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3444. if assigned(ImplementedInterfaces) then
  3445. begin
  3446. for i:=0 to ImplementedInterfaces.count-1 do
  3447. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3448. end;
  3449. if assigned(vmtentries) then
  3450. begin
  3451. tobjectdef(result).vmtentries:=TFPList.Create;
  3452. tobjectdef(result).copyvmtentries(self);
  3453. end;
  3454. end;
  3455. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3456. var
  3457. i : longint;
  3458. vmtentry : pvmtentry;
  3459. ImplIntf : TImplementedInterface;
  3460. old_do_indirect_crc: boolean;
  3461. begin
  3462. { if class1 in unit A changes, and class2 in unit B inherits from it
  3463. (so unit B uses unit A), then unit B with class2 will be recompiled.
  3464. However, if there is also a class3 in unit C that only depends on
  3465. unit B, then unit C will not be recompiled because nothing changed
  3466. to the interface of unit B. Nevertheless, unit C can indirectly
  3467. depend on unit A via derefs, and these must be updated -> the
  3468. indirect crc keeps track of such changes. }
  3469. old_do_indirect_crc:=ppufile.do_indirect_crc;
  3470. ppufile.do_indirect_crc:=true;
  3471. inherited ppuwrite(ppufile);
  3472. ppufile.putbyte(byte(objecttype));
  3473. ppufile.putstring(objrealname^);
  3474. if assigned(objextname) then
  3475. ppufile.putstring(objextname^)
  3476. else
  3477. ppufile.putstring('');
  3478. if assigned(import_lib) then
  3479. ppufile.putstring(import_lib^)
  3480. else
  3481. ppufile.putstring('');
  3482. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3483. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3484. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3485. ppufile.putlongint(vmt_offset);
  3486. ppufile.putderef(childofderef);
  3487. ppufile.putsmallset(objectoptions);
  3488. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3489. begin
  3490. ppufile.putguid(iidguid^);
  3491. ppufile.putstring(iidstr^);
  3492. end;
  3493. ppufile.putlongint(vmtentries.count);
  3494. for i:=0 to vmtentries.count-1 do
  3495. begin
  3496. vmtentry:=pvmtentry(vmtentries[i]);
  3497. ppufile.putderef(vmtentry^.procdefderef);
  3498. ppufile.putbyte(byte(vmtentry^.visibility));
  3499. end;
  3500. if assigned(ImplementedInterfaces) then
  3501. begin
  3502. ppufile.putlongint(ImplementedInterfaces.Count);
  3503. for i:=0 to ImplementedInterfaces.Count-1 do
  3504. begin
  3505. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3506. ppufile.putderef(ImplIntf.intfdefderef);
  3507. ppufile.putlongint(ImplIntf.Ioffset);
  3508. end;
  3509. end;
  3510. if df_copied_def in defoptions then
  3511. ppufile.putderef(cloneddefderef);
  3512. ppufile.writeentry(ibobjectdef);
  3513. if not(df_copied_def in defoptions) then
  3514. tObjectSymtable(symtable).ppuwrite(ppufile);
  3515. ppufile.do_indirect_crc:=old_do_indirect_crc;
  3516. end;
  3517. function tobjectdef.GetTypeName:string;
  3518. begin
  3519. { in this case we will go in endless recursion, because then }
  3520. { there is no tsym associated yet with the def. It can occur }
  3521. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3522. { instead of the actual type name }
  3523. if not assigned(typesym) then
  3524. result:='<Currently Parsed Class>'
  3525. else
  3526. result:=typename;
  3527. end;
  3528. procedure tobjectdef.buildderef;
  3529. var
  3530. i : longint;
  3531. vmtentry : pvmtentry;
  3532. begin
  3533. inherited buildderef;
  3534. childofderef.build(childof);
  3535. if df_copied_def in defoptions then
  3536. cloneddefderef.build(symtable.defowner)
  3537. else
  3538. tstoredsymtable(symtable).buildderef;
  3539. for i:=0 to vmtentries.count-1 do
  3540. begin
  3541. vmtentry:=pvmtentry(vmtentries[i]);
  3542. vmtentry^.procdefderef.build(vmtentry^.procdef);
  3543. end;
  3544. if assigned(ImplementedInterfaces) then
  3545. begin
  3546. for i:=0 to ImplementedInterfaces.count-1 do
  3547. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  3548. end;
  3549. end;
  3550. procedure tobjectdef.deref;
  3551. var
  3552. i : longint;
  3553. vmtentry : pvmtentry;
  3554. begin
  3555. inherited deref;
  3556. childof:=tobjectdef(childofderef.resolve);
  3557. if df_copied_def in defoptions then
  3558. begin
  3559. cloneddef:=tobjectdef(cloneddefderef.resolve);
  3560. symtable:=cloneddef.symtable.getcopy;
  3561. end
  3562. else
  3563. tstoredsymtable(symtable).deref;
  3564. for i:=0 to vmtentries.count-1 do
  3565. begin
  3566. vmtentry:=pvmtentry(vmtentries[i]);
  3567. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  3568. end;
  3569. if assigned(ImplementedInterfaces) then
  3570. begin
  3571. for i:=0 to ImplementedInterfaces.count-1 do
  3572. TImplementedInterface(ImplementedInterfaces[i]).deref;
  3573. end;
  3574. end;
  3575. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  3576. var
  3577. pd: tprocdef absolute def;
  3578. st: tsymtable;
  3579. psym: tsym;
  3580. nname: TIDString;
  3581. begin
  3582. if (tdef(def).typ<>procdef) then
  3583. exit;
  3584. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  3585. owner = symtable in which objcclassdef is defined
  3586. }
  3587. st:=pd.owner.defowner.owner;
  3588. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  3589. { check for an existing procsym with our special name }
  3590. psym:=tsym(st.find(nname));
  3591. if not assigned(psym) then
  3592. begin
  3593. psym:=tprocsym.create(nname);
  3594. { avoid warning about this symbol being unused }
  3595. psym.IncRefCount;
  3596. st.insert(psym,true);
  3597. end
  3598. else if (psym.typ<>procsym) then
  3599. internalerror(2009111501);
  3600. { add ourselves to this special procsym }
  3601. tprocsym(psym).procdeflist.add(def);
  3602. end;
  3603. procedure tobjectdef.buildderefimpl;
  3604. begin
  3605. inherited buildderefimpl;
  3606. if not (df_copied_def in defoptions) then
  3607. tstoredsymtable(symtable).buildderefimpl;
  3608. end;
  3609. procedure tobjectdef.derefimpl;
  3610. begin
  3611. inherited derefimpl;
  3612. if not (df_copied_def in defoptions) then
  3613. tstoredsymtable(symtable).derefimpl;
  3614. { the procdefs are not owned by the class helper procsyms, so they
  3615. are not stored/restored either -> re-add them here }
  3616. if (objecttype=odt_objcclass) or
  3617. (oo_is_classhelper in objectoptions) then
  3618. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  3619. end;
  3620. procedure tobjectdef.resetvmtentries;
  3621. var
  3622. i : longint;
  3623. begin
  3624. for i:=0 to vmtentries.Count-1 do
  3625. Dispose(pvmtentry(vmtentries[i]));
  3626. vmtentries.clear;
  3627. end;
  3628. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  3629. var
  3630. i : longint;
  3631. vmtentry : pvmtentry;
  3632. begin
  3633. resetvmtentries;
  3634. vmtentries.count:=objdef.vmtentries.count;
  3635. for i:=0 to objdef.vmtentries.count-1 do
  3636. begin
  3637. new(vmtentry);
  3638. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  3639. vmtentries[i]:=vmtentry;
  3640. end;
  3641. end;
  3642. function tobjectdef.getparentdef:tdef;
  3643. begin
  3644. { TODO: Remove getparentdef hack}
  3645. { With 2 forward declared classes with the child class before the
  3646. parent class the child class is written earlier to the ppu. Leaving it
  3647. possible to have a reference to the parent class for property overriding,
  3648. but the parent class still has the childof not resolved yet (PFV) }
  3649. if childof=nil then
  3650. childof:=tobjectdef(childofderef.resolve);
  3651. result:=childof;
  3652. end;
  3653. procedure tobjectdef.prepareguid;
  3654. begin
  3655. { set up guid }
  3656. if not assigned(iidguid) then
  3657. begin
  3658. new(iidguid);
  3659. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  3660. end;
  3661. { setup iidstring }
  3662. if not assigned(iidstr) then
  3663. iidstr:=stringdup(''); { default is empty string }
  3664. end;
  3665. procedure tobjectdef.set_parent( c : tobjectdef);
  3666. begin
  3667. if assigned(childof) then
  3668. exit;
  3669. childof:=c;
  3670. if not assigned(c) then
  3671. exit;
  3672. { inherit options and status }
  3673. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  3674. { add the data of the anchestor class/object }
  3675. if (objecttype in [odt_class,odt_object,odt_objcclass]) then
  3676. begin
  3677. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  3678. { inherit recordalignment }
  3679. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  3680. if (oo_has_vmt in objectoptions) and
  3681. (oo_has_vmt in c.objectoptions) then
  3682. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  3683. { if parent has a vmt field then the offset is the same for the child PM }
  3684. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  3685. begin
  3686. vmt_offset:=c.vmt_offset;
  3687. include(objectoptions,oo_has_vmt);
  3688. end;
  3689. end;
  3690. end;
  3691. procedure tobjectdef.insertvmt;
  3692. var
  3693. vs: tfieldvarsym;
  3694. begin
  3695. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3696. exit;
  3697. if (oo_has_vmt in objectoptions) then
  3698. internalerror(12345)
  3699. else
  3700. begin
  3701. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  3702. tObjectSymtable(symtable).fieldalignment);
  3703. if (tf_requires_proper_alignment in target_info.flags) then
  3704. begin
  3705. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  3706. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  3707. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  3708. end;
  3709. vmt_offset:=tObjectSymtable(symtable).datasize;
  3710. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  3711. hidesym(vs);
  3712. tObjectSymtable(symtable).insert(vs);
  3713. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  3714. include(objectoptions,oo_has_vmt);
  3715. end;
  3716. end;
  3717. procedure tobjectdef.check_forwards;
  3718. begin
  3719. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol]) then
  3720. tstoredsymtable(symtable).check_forwards;
  3721. if (oo_is_forward in objectoptions) then
  3722. begin
  3723. { ok, in future, the forward can be resolved }
  3724. Message1(sym_e_class_forward_not_resolved,objrealname^);
  3725. exclude(objectoptions,oo_is_forward);
  3726. end;
  3727. end;
  3728. { true if prot implements d (or if they are equal) }
  3729. function is_related_protocol(prot: tobjectdef; d : tdef) : boolean;
  3730. var
  3731. i : longint;
  3732. begin
  3733. { objcprotocols have multiple inheritance, all protocols from which
  3734. the current protocol inherits are stored in implementedinterfaces }
  3735. result:=prot=d;
  3736. if result then
  3737. exit;
  3738. for i:=0 to prot.ImplementedInterfaces.count-1 do
  3739. begin
  3740. result:=is_related_protocol(tobjectdef(prot.ImplementedInterfaces[i]),d);
  3741. if result then
  3742. exit;
  3743. end;
  3744. end;
  3745. { true, if self inherits from d (or if they are equal) }
  3746. function tobjectdef.is_related(d : tdef) : boolean;
  3747. var
  3748. hp : tobjectdef;
  3749. begin
  3750. if (objecttype=odt_objcprotocol) then
  3751. begin
  3752. is_related:=is_related_protocol(self,d);
  3753. exit
  3754. end;
  3755. hp:=self;
  3756. while assigned(hp) do
  3757. begin
  3758. if hp=d then
  3759. begin
  3760. is_related:=true;
  3761. exit;
  3762. end;
  3763. hp:=hp.childof;
  3764. end;
  3765. is_related:=false;
  3766. end;
  3767. function tobjectdef.FindDestructor : tprocdef;
  3768. var
  3769. objdef : tobjectdef;
  3770. i : longint;
  3771. sym : tsym;
  3772. pd : tprocdef;
  3773. begin
  3774. result:=nil;
  3775. objdef:=self;
  3776. while assigned(objdef) do
  3777. begin
  3778. for i:=0 to objdef.symtable.SymList.Count-1 do
  3779. begin
  3780. sym:=TSym(objdef.symtable.SymList[i]);
  3781. if sym.typ=procsym then
  3782. begin
  3783. pd:=Tprocsym(sym).Find_procdef_bytype(potype_destructor);
  3784. if assigned(pd) then
  3785. begin
  3786. result:=pd;
  3787. exit;
  3788. end;
  3789. end;
  3790. end;
  3791. objdef:=objdef.childof;
  3792. end;
  3793. end;
  3794. function tobjectdef.implements_any_interfaces: boolean;
  3795. begin
  3796. result := (ImplementedInterfaces.Count > 0) or
  3797. (assigned(childof) and childof.implements_any_interfaces);
  3798. end;
  3799. function tobjectdef.size : aint;
  3800. begin
  3801. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3802. result:=sizeof(pint)
  3803. else
  3804. result:=tObjectSymtable(symtable).datasize;
  3805. end;
  3806. function tobjectdef.alignment:shortint;
  3807. begin
  3808. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3809. alignment:=sizeof(pint)
  3810. else
  3811. alignment:=tObjectSymtable(symtable).recordalignment;
  3812. end;
  3813. function tobjectdef.vmtmethodoffset(index:longint):longint;
  3814. begin
  3815. { for offset of methods for classes, see rtl/inc/objpash.inc }
  3816. case objecttype of
  3817. odt_class:
  3818. { the +2*sizeof(pint) is size and -size }
  3819. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  3820. odt_objcclass,
  3821. odt_objcprotocol:
  3822. vmtmethodoffset:=0;
  3823. odt_interfacecom,odt_interfacecorba:
  3824. vmtmethodoffset:=index*sizeof(pint);
  3825. else
  3826. {$ifdef WITHDMT}
  3827. vmtmethodoffset:=(index+4)*sizeof(pint);
  3828. {$else WITHDMT}
  3829. vmtmethodoffset:=(index+3)*sizeof(pint);
  3830. {$endif WITHDMT}
  3831. end;
  3832. end;
  3833. function tobjectdef.vmt_mangledname : string;
  3834. begin
  3835. if not(oo_has_vmt in objectoptions) then
  3836. Message1(parser_n_object_has_no_vmt,objrealname^);
  3837. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  3838. end;
  3839. function tobjectdef.needs_inittable : boolean;
  3840. begin
  3841. case objecttype of
  3842. odt_dispinterface,
  3843. odt_class :
  3844. needs_inittable:=false;
  3845. odt_interfacecom:
  3846. needs_inittable:=true;
  3847. odt_interfacecorba:
  3848. needs_inittable:=is_related(interface_iunknown);
  3849. odt_object:
  3850. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  3851. odt_cppclass,
  3852. odt_objcclass,
  3853. odt_objcprotocol:
  3854. needs_inittable:=false;
  3855. else
  3856. internalerror(200108267);
  3857. end;
  3858. end;
  3859. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  3860. begin
  3861. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  3862. result:=inherited rtti_mangledname(rt)
  3863. else
  3864. begin
  3865. if not(target_info.system in systems_objc_nfabi) then
  3866. begin
  3867. result:=target_asm.labelprefix;
  3868. case objecttype of
  3869. odt_objcclass:
  3870. begin
  3871. case rt of
  3872. objcclassrtti:
  3873. if not(oo_is_classhelper in objectoptions) then
  3874. result:=result+'_OBJC_CLASS_'
  3875. else
  3876. result:=result+'_OBJC_CATEGORY_';
  3877. objcmetartti:
  3878. if not(oo_is_classhelper in objectoptions) then
  3879. result:=result+'_OBJC_METACLASS_'
  3880. else
  3881. internalerror(2009111511);
  3882. else
  3883. internalerror(2009092302);
  3884. end;
  3885. end;
  3886. odt_objcprotocol:
  3887. result:=result+'_OBJC_PROTOCOL_';
  3888. end;
  3889. end
  3890. else
  3891. begin
  3892. case objecttype of
  3893. odt_objcclass:
  3894. begin
  3895. if (oo_is_classhelper in objectoptions) and
  3896. (rt<>objcclassrtti) then
  3897. internalerror(2009111512);
  3898. case rt of
  3899. objcclassrtti:
  3900. if not(oo_is_classhelper in objectoptions) then
  3901. result:='_OBJC_CLASS_$_'
  3902. else
  3903. result:='_OBJC_$_CATEGORY_';
  3904. objcmetartti:
  3905. result:='_OBJC_METACLASS_$_';
  3906. objcclassrortti:
  3907. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  3908. objcmetarortti:
  3909. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  3910. else
  3911. internalerror(2009092303);
  3912. end;
  3913. end;
  3914. odt_objcprotocol:
  3915. begin
  3916. result:=lower(target_asm.labelprefix);
  3917. case rt of
  3918. objcclassrtti:
  3919. result:=result+'_OBJC_PROTOCOL_$_';
  3920. objcmetartti:
  3921. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  3922. else
  3923. internalerror(2009092501);
  3924. end;
  3925. end;
  3926. end;
  3927. end;
  3928. result:=result+objextname^;
  3929. end;
  3930. end;
  3931. function tobjectdef.members_need_inittable : boolean;
  3932. begin
  3933. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  3934. end;
  3935. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  3936. var
  3937. ImplIntf : TImplementedInterface;
  3938. i : longint;
  3939. begin
  3940. result:=nil;
  3941. if not assigned(ImplementedInterfaces) then
  3942. exit;
  3943. for i:=0 to ImplementedInterfaces.Count-1 do
  3944. begin
  3945. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3946. if ImplIntf.intfdef=aintfdef then
  3947. begin
  3948. result:=ImplIntf;
  3949. exit;
  3950. end;
  3951. end;
  3952. end;
  3953. function tobjectdef.is_publishable : boolean;
  3954. begin
  3955. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  3956. end;
  3957. procedure tobjectdef.reset;
  3958. begin
  3959. inherited reset;
  3960. created_in_current_module:=false;
  3961. maybe_created_in_current_module:=false;
  3962. classref_created_in_current_module:=false;
  3963. end;
  3964. function tobjectdef.search_enumerator_get: tprocdef;
  3965. var
  3966. objdef : tobjectdef;
  3967. sym : tsym;
  3968. i : integer;
  3969. pd : tprocdef;
  3970. hashedid : THashedIDString;
  3971. begin
  3972. result:=nil;
  3973. objdef:=self;
  3974. hashedid.id:='GETENUMERATOR';
  3975. while assigned(objdef) do
  3976. begin
  3977. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  3978. if assigned(sym) and (sym.typ=procsym) then
  3979. begin
  3980. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3981. begin
  3982. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3983. if (pd.proctypeoption = potype_function) and
  3984. is_class_or_interface_or_object(pd.returndef) and
  3985. (pd.visibility >= vis_public) then
  3986. begin
  3987. result:=pd;
  3988. exit;
  3989. end;
  3990. end;
  3991. end;
  3992. objdef:=objdef.childof;
  3993. end;
  3994. end;
  3995. function tobjectdef.search_enumerator_move: tprocdef;
  3996. var
  3997. objdef : tobjectdef;
  3998. sym : tsym;
  3999. i : integer;
  4000. pd : tprocdef;
  4001. hashedid : THashedIDString;
  4002. begin
  4003. result:=nil;
  4004. objdef:=self;
  4005. // first search for po_enumerator_movenext method modifier
  4006. // then search for public function MoveNext: Boolean
  4007. hashedid.id:='MOVENEXT';
  4008. while assigned(objdef) do
  4009. begin
  4010. for i:=0 to objdef.symtable.SymList.Count-1 do
  4011. begin
  4012. sym:=TSym(objdef.symtable.SymList[i]);
  4013. if (sym.typ=procsym) then
  4014. begin
  4015. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  4016. if assigned(pd) then
  4017. begin
  4018. result:=pd;
  4019. exit;
  4020. end;
  4021. end;
  4022. end;
  4023. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4024. if assigned(sym) and (sym.typ=procsym) then
  4025. begin
  4026. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  4027. begin
  4028. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4029. if (pd.proctypeoption = potype_function) and
  4030. is_boolean(pd.returndef) and
  4031. (pd.minparacount = 0) and
  4032. (pd.visibility >= vis_public) then
  4033. begin
  4034. result:=pd;
  4035. exit;
  4036. end;
  4037. end;
  4038. end;
  4039. objdef:=objdef.childof;
  4040. end;
  4041. end;
  4042. function tobjectdef.search_enumerator_current: tsym;
  4043. var
  4044. objdef : tobjectdef;
  4045. sym: tsym;
  4046. i: integer;
  4047. hashedid : THashedIDString;
  4048. begin
  4049. result:=nil;
  4050. objdef:=self;
  4051. hashedid.id:='CURRENT';
  4052. // first search for ppo_enumerator_current property modifier
  4053. // then search for public property Current
  4054. while assigned(objdef) do
  4055. begin
  4056. for i:=0 to objdef.symtable.SymList.Count-1 do
  4057. begin
  4058. sym:=TSym(objdef.symtable.SymList[i]);
  4059. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  4060. begin
  4061. result:=sym;
  4062. exit;
  4063. end;
  4064. end;
  4065. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4066. if assigned(sym) and (sym.typ=propertysym) and
  4067. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  4068. begin
  4069. result:=sym;
  4070. exit;
  4071. end;
  4072. objdef:=objdef.childof;
  4073. end;
  4074. end;
  4075. procedure tobjectdef.register_created_classref_type;
  4076. begin
  4077. if not classref_created_in_current_module then
  4078. begin
  4079. classref_created_in_current_module:=true;
  4080. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  4081. end;
  4082. end;
  4083. procedure tobjectdef.register_created_object_type;
  4084. begin
  4085. if not created_in_current_module then
  4086. begin
  4087. created_in_current_module:=true;
  4088. current_module.wpoinfo.addcreatedobjtype(self);
  4089. end;
  4090. end;
  4091. procedure tobjectdef.register_maybe_created_object_type;
  4092. begin
  4093. { if we know it has been created for sure, no need
  4094. to also record that it maybe can be created in
  4095. this module
  4096. }
  4097. if not (created_in_current_module) and
  4098. not (maybe_created_in_current_module) then
  4099. begin
  4100. maybe_created_in_current_module:=true;
  4101. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  4102. end;
  4103. end;
  4104. procedure tobjectdef.register_vmt_call(index: longint);
  4105. begin
  4106. if (is_object(self) or is_class(self)) then
  4107. current_module.wpoinfo.addcalledvmtentry(self,index);
  4108. end;
  4109. procedure make_procdef_external(data: tobject; arg: pointer);
  4110. var
  4111. def: tdef absolute data;
  4112. begin
  4113. if (def.typ = procdef) then
  4114. begin
  4115. include(tprocdef(def).procoptions,po_external);
  4116. tprocdef(def).forwarddef:=false;
  4117. end;
  4118. end;
  4119. procedure tobjectdef.make_all_methods_external;
  4120. begin
  4121. self.symtable.deflist.foreachcall(@make_procdef_external,nil);
  4122. end;
  4123. procedure check_and_finish_msg(data: tobject; arg: pointer);
  4124. var
  4125. def: tdef absolute data;
  4126. pd: tprocdef absolute data;
  4127. i,
  4128. paracount: longint;
  4129. begin
  4130. if (def.typ=procdef) then
  4131. begin
  4132. { add all messages also under a dummy name to the symtable in
  4133. which the objcclass/protocol/category is declared, so they can
  4134. be called via id.<name>
  4135. }
  4136. create_class_helper_for_procdef(pd,nil);
  4137. { we have to wait until now to set the mangled name because it
  4138. depends on the (possibly external) class name, which is defined
  4139. at the very end. }
  4140. if not(po_msgstr in pd.procoptions) then
  4141. begin
  4142. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  4143. { recover to avoid internalerror later on }
  4144. include(pd.procoptions,po_msgstr);
  4145. pd.messageinf.str:=stringdup('MissingDeclaration');
  4146. end;
  4147. { Mangled name is already set in case this is a copy of
  4148. another type. }
  4149. if not(po_has_mangledname in pd.procoptions) then
  4150. begin
  4151. { check whether the number of formal parameters is correct }
  4152. paracount:=0;
  4153. for i:=1 to length(pd.messageinf.str^) do
  4154. if pd.messageinf.str^[i]=':' then
  4155. inc(paracount);
  4156. for i:=0 to pd.paras.count-1 do
  4157. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  4158. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  4159. dec(paracount);
  4160. if (paracount<>0) then
  4161. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  4162. pd.setmangledname(pd.objcmangledname);
  4163. end
  4164. else
  4165. { all checks already done }
  4166. exit;
  4167. if not(oo_is_external in pd._class.objectoptions) then
  4168. begin
  4169. if (po_varargs in pd.procoptions) then
  4170. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  4171. else
  4172. begin
  4173. { check for "array of const" parameters }
  4174. for i:=0 to pd.parast.symlist.count-1 do
  4175. begin
  4176. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  4177. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  4178. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  4179. end;
  4180. end;
  4181. end;
  4182. end;
  4183. end;
  4184. procedure mark_private_fields_used(data: tobject; arg: pointer);
  4185. var
  4186. sym: tsym absolute data;
  4187. begin
  4188. if (sym.typ=fieldvarsym) and
  4189. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  4190. sym.IncRefCount;
  4191. end;
  4192. procedure tobjectdef.finish_objc_data;
  4193. begin
  4194. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  4195. if (oo_is_external in objectoptions) then
  4196. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  4197. end;
  4198. procedure do_cpp_import_info(data: tobject; arg: pointer);
  4199. var
  4200. def: tdef absolute data;
  4201. pd: tprocdef absolute data;
  4202. begin
  4203. if (def.typ=procdef) then
  4204. begin
  4205. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  4206. if (oo_is_external in pd._class.objectoptions) then
  4207. begin
  4208. { copied from psub.read_proc }
  4209. if assigned(pd._class.import_lib) then
  4210. current_module.AddExternalImport(pd._class.import_lib^,pd.mangledname,0,false,false)
  4211. else
  4212. begin
  4213. { add import name to external list for DLL scanning }
  4214. if tf_has_dllscanner in target_info.flags then
  4215. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  4216. end;
  4217. end;
  4218. end;
  4219. end;
  4220. procedure tobjectdef.finish_cpp_data;
  4221. begin
  4222. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  4223. end;
  4224. {****************************************************************************
  4225. TImplementedInterface
  4226. ****************************************************************************}
  4227. constructor TImplementedInterface.create(aintf: tobjectdef);
  4228. begin
  4229. inherited create;
  4230. intfdef:=aintf;
  4231. IOffset:=-1;
  4232. IType:=etStandard;
  4233. NameMappings:=nil;
  4234. procdefs:=nil;
  4235. end;
  4236. constructor TImplementedInterface.create_deref(d:tderef);
  4237. begin
  4238. inherited create;
  4239. intfdef:=nil;
  4240. intfdefderef:=d;
  4241. IOffset:=-1;
  4242. IType:=etStandard;
  4243. NameMappings:=nil;
  4244. procdefs:=nil;
  4245. end;
  4246. destructor TImplementedInterface.destroy;
  4247. var
  4248. i : longint;
  4249. mappedname : pshortstring;
  4250. begin
  4251. if assigned(NameMappings) then
  4252. begin
  4253. for i:=0 to NameMappings.Count-1 do
  4254. begin
  4255. mappedname:=pshortstring(NameMappings[i]);
  4256. stringdispose(mappedname);
  4257. end;
  4258. NameMappings.free;
  4259. NameMappings:=nil;
  4260. end;
  4261. if assigned(procdefs) then
  4262. begin
  4263. procdefs.free;
  4264. procdefs:=nil;
  4265. end;
  4266. inherited destroy;
  4267. end;
  4268. procedure TImplementedInterface.buildderef;
  4269. begin
  4270. intfdefderef.build(intfdef);
  4271. end;
  4272. procedure TImplementedInterface.deref;
  4273. begin
  4274. intfdef:=tobjectdef(intfdefderef.resolve);
  4275. end;
  4276. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  4277. begin
  4278. if not assigned(NameMappings) then
  4279. NameMappings:=TFPHashList.Create;
  4280. NameMappings.Add(origname,stringdup(newname));
  4281. end;
  4282. function TImplementedInterface.GetMapping(const origname: string):string;
  4283. var
  4284. mappedname : pshortstring;
  4285. begin
  4286. result:='';
  4287. if not assigned(NameMappings) then
  4288. exit;
  4289. mappedname:=PShortstring(NameMappings.Find(origname));
  4290. if assigned(mappedname) then
  4291. result:=mappedname^;
  4292. end;
  4293. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  4294. begin
  4295. if not assigned(procdefs) then
  4296. procdefs:=TFPObjectList.Create(false);
  4297. { duplicate entries must be stored, because multiple }
  4298. { interfaces can declare methods with the same name }
  4299. { and all of these get their own VMT entry }
  4300. procdefs.Add(pd);
  4301. end;
  4302. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  4303. var
  4304. i : longint;
  4305. begin
  4306. result:=false;
  4307. { interfaces being implemented through delegation are not mergable (FK) }
  4308. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) then
  4309. exit;
  4310. weight:=0;
  4311. { empty interface is mergeable }
  4312. if ProcDefs.Count=0 then
  4313. begin
  4314. result:=true;
  4315. exit;
  4316. end;
  4317. { The interface to merge must at least the number of
  4318. procedures of this interface }
  4319. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  4320. exit;
  4321. for i:=0 to ProcDefs.Count-1 do
  4322. begin
  4323. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  4324. exit;
  4325. end;
  4326. weight:=ProcDefs.Count;
  4327. result:=true;
  4328. end;
  4329. function TImplementedInterface.getcopy:TImplementedInterface;
  4330. begin
  4331. Result:=TImplementedInterface.Create(nil);
  4332. {$warning: this is completely wrong on so many levels...}
  4333. { 1) the procdefs list will be freed once for each copy
  4334. 2) since the procdefs list owns its elements, those will also be freed for each copy
  4335. 3) idem for the name mappings
  4336. }
  4337. Move(pointer(self)^,pointer(result)^,InstanceSize);
  4338. end;
  4339. {****************************************************************************
  4340. TFORWARDDEF
  4341. ****************************************************************************}
  4342. constructor tforwarddef.create(const s:string;const pos : tfileposinfo);
  4343. begin
  4344. inherited create(forwarddef);
  4345. tosymname:=stringdup(s);
  4346. forwardpos:=pos;
  4347. end;
  4348. function tforwarddef.GetTypeName:string;
  4349. begin
  4350. GetTypeName:='unresolved forward to '+tosymname^;
  4351. end;
  4352. destructor tforwarddef.destroy;
  4353. begin
  4354. stringdispose(tosymname);
  4355. inherited destroy;
  4356. end;
  4357. {****************************************************************************
  4358. TUNDEFINEDDEF
  4359. ****************************************************************************}
  4360. constructor tundefineddef.create;
  4361. begin
  4362. inherited create(undefineddef);
  4363. end;
  4364. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  4365. begin
  4366. inherited ppuload(undefineddef,ppufile);
  4367. end;
  4368. function tundefineddef.GetTypeName:string;
  4369. begin
  4370. GetTypeName:='<undefined type>';
  4371. end;
  4372. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  4373. begin
  4374. inherited ppuwrite(ppufile);
  4375. ppufile.writeentry(ibundefineddef);
  4376. end;
  4377. {****************************************************************************
  4378. TERRORDEF
  4379. ****************************************************************************}
  4380. constructor terrordef.create;
  4381. begin
  4382. inherited create(errordef);
  4383. { prevent consecutive faults }
  4384. savesize:=1;
  4385. end;
  4386. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  4387. begin
  4388. { Can't write errordefs to ppu }
  4389. internalerror(200411063);
  4390. end;
  4391. function terrordef.GetTypeName:string;
  4392. begin
  4393. GetTypeName:='<erroneous type>';
  4394. end;
  4395. function terrordef.getmangledparaname:string;
  4396. begin
  4397. getmangledparaname:='error';
  4398. end;
  4399. {****************************************************************************
  4400. Definition Helpers
  4401. ****************************************************************************}
  4402. function is_interfacecom(def: tdef): boolean;
  4403. begin
  4404. is_interfacecom:=
  4405. assigned(def) and
  4406. (def.typ=objectdef) and
  4407. (tobjectdef(def).objecttype=odt_interfacecom);
  4408. end;
  4409. function is_interfacecorba(def: tdef): boolean;
  4410. begin
  4411. is_interfacecorba:=
  4412. assigned(def) and
  4413. (def.typ=objectdef) and
  4414. (tobjectdef(def).objecttype=odt_interfacecorba);
  4415. end;
  4416. function is_interface(def: tdef): boolean;
  4417. begin
  4418. is_interface:=
  4419. assigned(def) and
  4420. (def.typ=objectdef) and
  4421. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  4422. end;
  4423. function is_dispinterface(def: tdef): boolean;
  4424. begin
  4425. result:=
  4426. assigned(def) and
  4427. (def.typ=objectdef) and
  4428. (tobjectdef(def).objecttype=odt_dispinterface);
  4429. end;
  4430. function is_class(def: tdef): boolean;
  4431. begin
  4432. is_class:=
  4433. assigned(def) and
  4434. (def.typ=objectdef) and
  4435. (tobjectdef(def).objecttype=odt_class);
  4436. end;
  4437. function is_object(def: tdef): boolean;
  4438. begin
  4439. is_object:=
  4440. assigned(def) and
  4441. (def.typ=objectdef) and
  4442. (tobjectdef(def).objecttype=odt_object);
  4443. end;
  4444. function is_cppclass(def: tdef): boolean;
  4445. begin
  4446. is_cppclass:=
  4447. assigned(def) and
  4448. (def.typ=objectdef) and
  4449. (tobjectdef(def).objecttype=odt_cppclass);
  4450. end;
  4451. function is_objcclass(def: tdef): boolean;
  4452. begin
  4453. is_objcclass:=
  4454. assigned(def) and
  4455. (def.typ=objectdef) and
  4456. (tobjectdef(def).objecttype=odt_objcclass);
  4457. end;
  4458. function is_objcclassref(def: tdef): boolean;
  4459. begin
  4460. is_objcclassref:=
  4461. assigned(def) and
  4462. (def.typ=classrefdef) and
  4463. is_objcclass(tclassrefdef(def).pointeddef);
  4464. end;
  4465. function is_objcprotocol(def: tdef): boolean;
  4466. begin
  4467. result:=
  4468. assigned(def) and
  4469. (def.typ=objectdef) and
  4470. (tobjectdef(def).objecttype=odt_objcprotocol);
  4471. end;
  4472. function is_objccategory(def: tdef): boolean;
  4473. begin
  4474. result:=
  4475. assigned(def) and
  4476. (def.typ=objectdef) and
  4477. { if used as a forward type }
  4478. ((tobjectdef(def).objecttype=odt_objccategory) or
  4479. { if used as after it has been resolved }
  4480. ((tobjectdef(def).objecttype=odt_objcclass) and
  4481. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4482. end;
  4483. function is_objc_class_or_protocol(def: tdef): boolean;
  4484. begin
  4485. result:=
  4486. assigned(def) and
  4487. (def.typ=objectdef) and
  4488. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  4489. end;
  4490. function is_objc_protocol_or_category(def: tdef): boolean;
  4491. begin
  4492. result:=
  4493. assigned(def) and
  4494. (def.typ=objectdef) and
  4495. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  4496. ((tobjectdef(def).objecttype = odt_objcclass) and
  4497. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4498. end;
  4499. function is_class_or_interface(def: tdef): boolean;
  4500. begin
  4501. result:=
  4502. assigned(def) and
  4503. (def.typ=objectdef) and
  4504. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  4505. end;
  4506. function is_class_or_interface_or_objc(def: tdef): boolean;
  4507. begin
  4508. result:=
  4509. assigned(def) and
  4510. (def.typ=objectdef) and
  4511. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  4512. end;
  4513. function is_class_or_interface_or_object(def: tdef): boolean;
  4514. begin
  4515. result:=
  4516. assigned(def) and
  4517. (def.typ=objectdef) and
  4518. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  4519. end;
  4520. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  4521. begin
  4522. result:=
  4523. assigned(def) and
  4524. (def.typ=objectdef) and
  4525. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  4526. end;
  4527. function is_class_or_interface_or_dispinterface_or_objc(def: tdef): boolean;
  4528. begin
  4529. result:=
  4530. assigned(def) and
  4531. (def.typ=objectdef) and
  4532. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol]);
  4533. end;
  4534. procedure loadobjctypes;
  4535. begin
  4536. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS').typedef);
  4537. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER').typedef);
  4538. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID').typedef);
  4539. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL').typedef);
  4540. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT').typedef);
  4541. end;
  4542. function use_vectorfpu(def : tdef) : boolean;
  4543. begin
  4544. {$ifdef x86}
  4545. {$define use_vectorfpuimplemented}
  4546. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  4547. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  4548. {$endif x86}
  4549. {$ifdef arm}
  4550. {$define use_vectorfpuimplemented}
  4551. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  4552. {$endif arm}
  4553. {$ifndef use_vectorfpuimplemented}
  4554. use_vectorfpu:=false;
  4555. {$endif}
  4556. end;
  4557. end.