symdef.pas 195 KB

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