symdef.pas 161 KB

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