symdef.pas 172 KB

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