symdef.pas 168 KB

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