symdef.pas 176 KB

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