symdef.pas 190 KB

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