symdef.pas 192 KB

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