symdef.pas 305 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128
  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,widestr,
  23. { global }
  24. globtype,globals,tokens,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,
  27. { node }
  28. node,
  29. { aasm }
  30. aasmtai,
  31. cpuinfo,
  32. cgbase,
  33. parabase
  34. ;
  35. type
  36. {************************************************
  37. TDef
  38. ************************************************}
  39. tgenericconstraintdata=class
  40. interfaces : tfpobjectlist;
  41. interfacesderef : tfplist;
  42. flags : tgenericconstraintflags;
  43. { only required while parsing }
  44. fileinfo : tfileposinfo;
  45. constructor create;
  46. destructor destroy;override;
  47. procedure ppuload(ppufile:tcompilerppufile);
  48. procedure ppuwrite(ppufile:tcompilerppufile);
  49. procedure buildderef;
  50. procedure deref;
  51. end;
  52. { trtti_attribute_list }
  53. trtti_attribute = class
  54. typesym : tsym;
  55. typesymderef : tderef;
  56. typeconstr : tdef;
  57. typeconstrderef : tderef;
  58. { these two are not stored in PPU }
  59. constructorcall : tnode;
  60. constructorpd : tdef;
  61. paras : array of tnode;
  62. constructor ppuload(ppufile:tcompilerppufile);
  63. procedure ppuwrite(ppufile:tcompilerppufile);
  64. procedure ppuload_subentries(ppufile:tcompilerppufile);
  65. procedure ppuwrite_subentries(ppufile:tcompilerppufile);
  66. destructor destroy;override;
  67. procedure buildderef;
  68. procedure deref;
  69. end;
  70. trtti_attribute_list = class
  71. rtti_attributes : TFPObjectList;
  72. { if the attribute list is bound to a def or symbol }
  73. is_bound : Boolean;
  74. class procedure bind(var dangling,owned:trtti_attribute_list);
  75. procedure addattribute(atypesym:tsym;typeconstr:tdef;constructorcall:tnode;constref paras:array of tnode);
  76. procedure addattribute(attr:trtti_attribute);
  77. destructor destroy; override;
  78. function get_attribute_count:longint;
  79. procedure buildderef;
  80. procedure deref;
  81. class function ppuload(ppufile:tcompilerppufile):trtti_attribute_list;
  82. class procedure ppuwrite(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  83. class procedure ppuload_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  84. class procedure ppuwrite_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  85. end;
  86. { tstoreddef }
  87. tstoreddef = class(tdef)
  88. private
  89. {$ifdef symansistr}
  90. _fullownerhierarchyname : ansistring;
  91. {$else symansistr}
  92. _fullownerhierarchyname : pshortstring;
  93. {$endif symansistr}
  94. procedure writeentry(ppufile: tcompilerppufile; ibnr: byte);
  95. protected
  96. typesymderef : tderef;
  97. procedure ppuwrite_platform(ppufile:tcompilerppufile);virtual;
  98. procedure ppuload_platform(ppufile:tcompilerppufile);virtual;
  99. { a (possibly) reusable def is always created on the basis of another
  100. def, and contains a reference to this other def. If this other
  101. def is in a non-persistent symboltable, the new def cannot actually
  102. be safely reused everywhere in the current module. This routine
  103. abtracts that checking, and also restores the symtable stack
  104. (which had to be reset before creating the new def, so that the new
  105. def did not automatically get added to its top) }
  106. class procedure setup_reusable_def(origdef, newdef: tdef; res: PHashSetItem; oldsymtablestack: tsymtablestack);
  107. public
  108. {$ifdef EXTDEBUG}
  109. fileinfo : tfileposinfo;
  110. {$endif}
  111. { generic support }
  112. genericdef : tstoreddef;
  113. genericdefderef : tderef;
  114. generictokenbuf : tdynamicarray;
  115. { this list contains references to the symbols that make up the
  116. generic parameters; the symbols are not owned by this list
  117. Note: this list is allocated on demand! }
  118. genericparas : tfphashobjectlist;
  119. genericparaderefs : tfplist;
  120. { contains additional data if this def is a generic constraint
  121. Note: this class is allocated on demand! }
  122. genconstraintdata : tgenericconstraintdata;
  123. { this is Nil if the def has no RTTI attributes }
  124. rtti_attribute_list : trtti_attribute_list;
  125. constructor create(dt:tdeftyp;doregister:boolean);
  126. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  127. destructor destroy;override;
  128. function getcopy : tstoreddef;virtual;
  129. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  130. { this is called directly after ppuload }
  131. procedure ppuload_subentries(ppufile:tcompilerppufile);virtual;
  132. { this is called directly after ppuwrite }
  133. procedure ppuwrite_subentries(ppufile:tcompilerppufile);virtual;
  134. procedure buildderef;override;
  135. procedure buildderefimpl;override;
  136. procedure deref;override;
  137. procedure derefimpl;override;
  138. function size:asizeint;override;
  139. function getvardef:longint;override;
  140. function alignment:shortint;override;
  141. function is_publishable : boolean;override;
  142. function needs_inittable : boolean;override;
  143. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  144. function rtti_mangledname(rt:trttitype):TSymStr;override;
  145. function OwnerHierarchyName: string; override;
  146. function fullownerhierarchyname(skipprocparams:boolean):TSymStr;override;
  147. function needs_separate_initrtti:boolean;override;
  148. function in_currentunit: boolean;
  149. { regvars }
  150. function is_intregable : boolean;
  151. function is_fpuregable : boolean;
  152. { def can be put into a register if it is const/immutable }
  153. function is_const_intregable : boolean;
  154. { generics }
  155. procedure initgeneric;
  156. { this function can be used to determine whether a def is really a
  157. generic declaration or just a normal type declared inside another
  158. generic }
  159. function is_generic:boolean;
  160. { same as above for specializations }
  161. function is_specialization:boolean;
  162. { generic utilities }
  163. function is_generic_param_const(index:integer):boolean;inline;
  164. function get_generic_param_def(index:integer):tdef;inline;
  165. { registers this def in the unit's deflist; no-op if already registered }
  166. procedure register_def; override;
  167. { add the def to the top of the symtable stack if it's not yet owned
  168. by another symtable }
  169. procedure maybe_put_in_symtable_stack;
  170. private
  171. savesize : asizeuint;
  172. end;
  173. tfiletyp = (ft_text,ft_typed,ft_untyped);
  174. tfiledef = class(tstoreddef)
  175. filetyp : tfiletyp;
  176. typedfiledef : tdef;
  177. typedfiledefderef : tderef;
  178. constructor createtext;virtual;
  179. constructor createuntyped;virtual;
  180. constructor createtyped(def : tdef);virtual;
  181. constructor ppuload(ppufile:tcompilerppufile);
  182. function getcopy : tstoreddef;override;
  183. { do not override this routine in platform-specific subclasses,
  184. override ppuwrite_platform instead }
  185. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  186. procedure buildderef;override;
  187. procedure deref;override;
  188. function GetTypeName:string;override;
  189. function getmangledparaname:TSymStr;override;
  190. function size:asizeint;override;
  191. procedure setsize;
  192. function alignment: shortint; override;
  193. end;
  194. tfiledefclass = class of tfiledef;
  195. tvariantdef = class(tstoreddef)
  196. varianttype : tvarianttype;
  197. constructor create(v : tvarianttype);virtual;
  198. constructor ppuload(ppufile:tcompilerppufile);
  199. function getcopy : tstoreddef;override;
  200. function GetTypeName:string;override;
  201. function alignment : shortint;override;
  202. { do not override this routine in platform-specific subclasses,
  203. override ppuwrite_platform instead }
  204. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  205. function getvardef:longint;override;
  206. procedure setsize;
  207. function is_publishable : boolean;override;
  208. function needs_inittable : boolean;override;
  209. end;
  210. tvariantdefclass = class of tvariantdef;
  211. tformaldef = class(tstoreddef)
  212. typed:boolean;
  213. constructor create(Atyped:boolean);virtual;
  214. constructor ppuload(ppufile:tcompilerppufile);
  215. { do not override this routine in platform-specific subclasses,
  216. override ppuwrite_platform instead }
  217. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  218. function GetTypeName:string;override;
  219. end;
  220. tformaldefclass = class of tformaldef;
  221. tforwarddef = class(tstoreddef)
  222. tosymname : pshortstring;
  223. forwardpos : tfileposinfo;
  224. constructor create(const s:string;const pos:tfileposinfo);virtual;
  225. destructor destroy;override;
  226. function getcopy:tstoreddef;override;
  227. function GetTypeName:string;override;
  228. end;
  229. tforwarddefclass = class of tforwarddef;
  230. tundefineddef = class(tstoreddef)
  231. constructor create(doregister:boolean);virtual;
  232. constructor ppuload(ppufile:tcompilerppufile);
  233. { do not override this routine in platform-specific subclasses,
  234. override ppuwrite_platform instead }
  235. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  236. function GetTypeName:string;override;
  237. end;
  238. tundefineddefclass = class of tundefineddef;
  239. terrordef = class(tstoreddef)
  240. constructor create;virtual;
  241. { do not override this routine in platform-specific subclasses,
  242. override ppuwrite_platform instead }
  243. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  244. function GetTypeName:string;override;
  245. function getmangledparaname : TSymStr;override;
  246. end;
  247. terrordefclass = class of terrordef;
  248. tabstractpointerdef = class(tstoreddef)
  249. pointeddef : tdef;
  250. pointeddefderef : tderef;
  251. constructor create(dt:tdeftyp;def:tdef);
  252. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  253. procedure ppuwrite(ppufile:tcompilerppufile);override;
  254. procedure buildderef;override;
  255. procedure deref;override;
  256. function size:asizeint;override;
  257. function alignment:shortint;override;
  258. end;
  259. tpointerdef = class(tabstractpointerdef)
  260. has_pointer_math : boolean;
  261. constructor create(def:tdef);virtual;
  262. { returns a pointerdef for def, reusing an existing one in case it
  263. exists in the current module }
  264. class function getreusable(def: tdef): tpointerdef; virtual;
  265. { same as above, but in case the def must never be freed after the
  266. current module has been compiled -- even if the def was not written
  267. to the ppu file (for defs in para locations, as we don't reset them
  268. so we don't have to recalculate them all the time) }
  269. class function getreusable_no_free(def: tdef): tpointerdef;
  270. function size:asizeint;override;
  271. function getcopy:tstoreddef;override;
  272. constructor ppuload(ppufile:tcompilerppufile);
  273. { do not override this routine in platform-specific subclasses,
  274. override ppuwrite_platform instead }
  275. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  276. function GetTypeName:string;override;
  277. {# returns the appropriate int type for pointer arithmetic with the given pointer type.
  278. When adding or subtracting a number to/from a pointer, this function returns the
  279. int type to which that number has to be converted, before the operation can be performed.
  280. Normally, this is sinttype, except on i8086, where it takes into account the
  281. special i8086 pointer types (near, far, huge). }
  282. function pointer_arithmetic_int_type:tdef;virtual;
  283. {# the unsigned version of pointer_arithmetic_int_type. Used with inc/dec. }
  284. function pointer_arithmetic_uint_type:tdef;virtual;
  285. {# returns the int type produced when subtracting two pointers of the given type.
  286. Normally, this is sinttype, except on i8086, where it takes into account the
  287. special i8086 pointer types (near, far, huge). }
  288. function pointer_subtraction_result_type:tdef;virtual;
  289. function compatible_with_pointerdef_size(ptr: tpointerdef): boolean; virtual;
  290. {# the integer index type used to convert the pointer to array (i.e. denotes int_type in: ptr[int_type]) }
  291. function converted_pointer_to_array_range_type:tdef;virtual;
  292. end;
  293. tpointerdefclass = class of tpointerdef;
  294. tprocdef = class;
  295. tabstractrecorddef= class(tstoreddef)
  296. private
  297. rttistring : string;
  298. {$ifdef DEBUG_NODE_XML}
  299. protected
  300. procedure XMLPrintDefData(var T: Text; Sym: TSym); override;
  301. {$endif DEBUG_NODE_XML}
  302. public
  303. objname,
  304. objrealname : PShortString;
  305. { for C++ classes: name of the library this class is imported from }
  306. { for Java classes/records: package name }
  307. import_lib : PShortString;
  308. symtable : TSymtable;
  309. cloneddef : tabstractrecorddef;
  310. cloneddefderef : tderef;
  311. objectoptions : tobjectoptions;
  312. { for targets that initialise typed constants via explicit assignments
  313. instead of by generating an initialised data sectino }
  314. tcinitcode : tnode;
  315. constructor create(const n:string; dt:tdeftyp;doregister:boolean);
  316. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  317. procedure ppuwrite(ppufile:tcompilerppufile);override;
  318. destructor destroy; override;
  319. procedure buildderefimpl;override;
  320. procedure derefimpl;override;
  321. procedure check_forwards; virtual;
  322. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  323. function GetSymtable(t:tGetSymtable):TSymtable;override;
  324. function is_packed:boolean;
  325. function RttiName: string;
  326. { enumerator support }
  327. function search_enumerator_get: tprocdef; virtual;
  328. function search_enumerator_move: tprocdef; virtual;
  329. function search_enumerator_current: tsym; virtual;
  330. { JVM }
  331. function jvm_full_typename(with_package_name: boolean): string;
  332. { check if the symtable contains a float field }
  333. function contains_float_field : boolean;
  334. { check if the symtable contains a field that spans an aword boundary }
  335. function contains_cross_aword_field: boolean;
  336. end;
  337. pvariantrecdesc = ^tvariantrecdesc;
  338. tvariantrecbranch = record
  339. { we store only single values here and no ranges because tvariantrecdesc is only needed in iso mode
  340. which does not support range expressions in variant record definitions }
  341. values : array of Tconstexprint;
  342. nestedvariant : pvariantrecdesc;
  343. end;
  344. ppvariantrecdesc = ^pvariantrecdesc;
  345. tvariantrecdesc = record
  346. variantselector : tsym;
  347. variantselectorderef : tderef;
  348. branches : array of tvariantrecbranch;
  349. end;
  350. trecorddef = class(tabstractrecorddef)
  351. {$ifdef DEBUG_NODE_XML}
  352. protected
  353. function XMLPrintType: ansistring; override;
  354. {$endif DEBUG_NODE_XML}
  355. public
  356. variantrecdesc : pvariantrecdesc;
  357. isunion : boolean;
  358. constructor create(const n:string; p:TSymtable);virtual;
  359. constructor create_global_internal(const n: string; packrecords, recordalignmin: shortint); virtual;
  360. function add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
  361. procedure add_fields_from_deflist(fieldtypes: tfplist);
  362. constructor ppuload(ppufile:tcompilerppufile);
  363. destructor destroy;override;
  364. function getcopy : tstoreddef;override;
  365. { do not override this routine in platform-specific subclasses,
  366. override ppuwrite_platform instead }
  367. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  368. procedure buildderef;override;
  369. procedure deref;override;
  370. function size:asizeint;override;
  371. function alignment : shortint;override;
  372. function padalignment: shortint;
  373. function GetTypeName:string;override;
  374. { debug }
  375. function needs_inittable : boolean;override;
  376. function needs_separate_initrtti:boolean;override;
  377. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  378. end;
  379. trecorddefclass = class of trecorddef;
  380. tobjectdef = class;
  381. { TImplementedInterface }
  382. TImplementedInterface = class
  383. private
  384. fIOffset : longint;
  385. function GetIOffset: longint;
  386. public
  387. IntfDef : tobjectdef;
  388. IntfDefDeref : tderef;
  389. IType : tinterfaceentrytype;
  390. VtblImplIntf : TImplementedInterface;
  391. NameMappings : TFPHashList;
  392. ProcDefs : TFPObjectList;
  393. ImplementsGetter : tsym;
  394. ImplementsGetterDeref : tderef;
  395. ImplementsField : tsym;
  396. constructor create(aintf: tobjectdef);virtual;
  397. constructor create_deref(intfd,getterd:tderef);virtual;
  398. destructor destroy; override;
  399. function getcopy:TImplementedInterface;
  400. procedure buildderef;
  401. procedure deref;
  402. procedure AddMapping(const origname, newname: string);
  403. function GetMapping(const origname: string):string;
  404. procedure AddImplProc(pd:tprocdef);
  405. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  406. property IOffset: longint read GetIOffset write fIOffset;
  407. end;
  408. timplementedinterfaceclass = class of timplementedinterface;
  409. { tvmtentry }
  410. tvmtentry = record
  411. procdef : tprocdef;
  412. procdefderef : tderef;
  413. visibility : tvisibility;
  414. end;
  415. pvmtentry = ^tvmtentry;
  416. { tobjectdef }
  417. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  418. pmvcallstaticinfo = ^tmvcallstaticinfo;
  419. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  420. tobjectdef = class(tabstractrecorddef)
  421. private
  422. fcurrent_dispid: longint;
  423. {$ifdef DEBUG_NODE_XML}
  424. protected
  425. function XMLPrintType: ansistring; override;
  426. procedure XMLPrintDefInfo(var T: Text; Sym: TSym); override;
  427. procedure XMLPrintDefData(var T: Text; Sym: TSym); override;
  428. {$endif DEBUG_NODE_XML}
  429. public
  430. childof : tobjectdef;
  431. childofderef : tderef;
  432. { for Object Pascal helpers }
  433. extendeddef : tdef;
  434. extendeddefderef: tderef;
  435. helpertype : thelpertype;
  436. { for Objective-C: protocols and classes can have the same name there }
  437. objextname : pshortstring;
  438. { to be able to have a variable vmt position }
  439. { and no vmt field for objects without virtuals }
  440. vmtentries : TFPList;
  441. vmcallstaticinfo : pmvcallstaticinfo;
  442. vmt_field : tsym;
  443. vmt_fieldderef : tderef;
  444. iidguid : pguid;
  445. iidstr : pshortstring;
  446. { store implemented interfaces defs and name mappings }
  447. ImplementedInterfaces : TFPObjectList;
  448. { number of abstract methods (used by JVM target to determine whether
  449. or not the class should be marked as abstract: must be done if 1 or
  450. more abstract methods) }
  451. abstractcnt : longint;
  452. writing_class_record_dbginfo,
  453. { a class of this type has been created in this module }
  454. created_in_current_module,
  455. { a loadvmtnode for this class has been created in this
  456. module, so if a classrefdef variable of this or a parent
  457. class is used somewhere to instantiate a class, then this
  458. class may be instantiated
  459. }
  460. maybe_created_in_current_module,
  461. { a "class of" this particular class has been created in
  462. this module
  463. }
  464. classref_created_in_current_module : boolean;
  465. objecttype : tobjecttyp;
  466. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef;doregister:boolean);virtual;
  467. constructor ppuload(ppufile:tcompilerppufile);
  468. destructor destroy;override;
  469. function getcopy : tstoreddef;override;
  470. { do not override this routine in platform-specific subclasses,
  471. override ppuwrite_platform instead }
  472. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  473. function GetTypeName:string;override;
  474. procedure buildderef;override;
  475. procedure deref;override;
  476. procedure derefimpl;override;
  477. procedure copyvmtentries(objdef:tobjectdef);
  478. function getparentdef:tdef;override;
  479. function size : asizeint;override;
  480. function alignment:shortint;override;
  481. function vmtmethodoffset(index:longint):longint;
  482. function members_need_inittable : boolean;
  483. { this should be called when this class implements an interface }
  484. procedure prepareguid;
  485. function is_publishable : boolean;override;
  486. function needs_inittable : boolean;override;
  487. function needs_separate_initrtti : boolean;override;
  488. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  489. function rtti_mangledname(rt:trttitype):TSymStr;override;
  490. function is_unique_objpasdef: boolean;
  491. function vmt_mangledname : TSymStr;
  492. function vmt_def: trecorddef;
  493. procedure check_forwards; override;
  494. procedure insertvmt;
  495. function vmt_offset: asizeint;
  496. procedure set_parent(c : tobjectdef);
  497. function find_destructor: tprocdef;
  498. function implements_any_interfaces: boolean;
  499. { dispinterface support }
  500. function get_next_dispid: longint;
  501. { enumerator support }
  502. function search_enumerator_get: tprocdef; override;
  503. function search_enumerator_move: tprocdef; override;
  504. function search_enumerator_current: tsym; override;
  505. { WPO }
  506. procedure register_created_object_type;override;
  507. procedure register_maybe_created_object_type;
  508. procedure register_created_classref_type;
  509. procedure register_vmt_call(index:longint);
  510. { ObjC }
  511. procedure finish_objc_data;
  512. function check_objc_types: boolean;
  513. { C++ }
  514. procedure finish_cpp_data;
  515. end;
  516. tobjectdefclass = class of tobjectdef;
  517. tclassrefdef = class(tabstractpointerdef)
  518. constructor create(def:tdef);virtual;
  519. constructor ppuload(ppufile:tcompilerppufile);
  520. { do not override this routine in platform-specific subclasses,
  521. override ppuwrite_platform instead }
  522. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  523. function getcopy:tstoreddef;override;
  524. function GetTypeName:string;override;
  525. function is_publishable : boolean;override;
  526. function rtti_mangledname(rt:trttitype):TSymStr;override;
  527. procedure register_created_object_type;override;
  528. end;
  529. tclassrefdefclass = class of tclassrefdef;
  530. tarraydef = class(tstoreddef)
  531. lowrange,
  532. highrange : asizeint;
  533. rangedef : tdef;
  534. rangedefderef : tderef;
  535. arrayoptions : tarraydefoptions;
  536. symtable : TSymtable;
  537. protected
  538. _elementdef : tdef;
  539. _elementdefderef : tderef;
  540. procedure setelementdef(def:tdef);
  541. class function getreusable_intern(def: tdef; elems: asizeint; const options: tarraydefoptions): tarraydef;
  542. public
  543. function elesize : asizeint;
  544. function elepackedbitsize : asizeint;
  545. function elecount : asizeuint;
  546. constructor create_from_pointer(def: tpointerdef);virtual;
  547. constructor create(l, h: asizeint; def: tdef);virtual;
  548. constructor create_vector(l,h:asizeint; def:tdef);
  549. constructor create_openarray;virtual;
  550. class function getreusable(def: tdef; elems: asizeint): tarraydef;
  551. class function getreusable_vector(def: tdef; elems: asizeint): tarraydef;
  552. { same as above, but in case the def must never be freed after the
  553. current module has been compiled -- even if the def was not written
  554. to the ppu file (for defs in para locations, as we don't reset them
  555. so we don't have to recalculate them all the time) }
  556. class function getreusable_no_free(def: tdef; elems: asizeint): tarraydef;
  557. class function getreusable_no_free_vector(def: tdef; elems: asizeint): tarraydef;
  558. constructor ppuload(ppufile:tcompilerppufile);
  559. destructor destroy; override;
  560. function getcopy : tstoreddef;override;
  561. { do not override this routine in platform-specific subclasses,
  562. override ppuwrite_platform instead }
  563. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  564. function GetTypeName:string;override;
  565. function getmangledparaname : TSymStr;override;
  566. procedure buildderef;override;
  567. procedure deref;override;
  568. function size : asizeint;override;
  569. function alignment : shortint;override;
  570. { returns the label of the range check string }
  571. function needs_inittable : boolean;override;
  572. function needs_separate_initrtti : boolean;override;
  573. property elementdef : tdef read _elementdef write setelementdef;
  574. function is_publishable : boolean;override;
  575. function is_hwvector: boolean;
  576. end;
  577. tarraydefclass = class of tarraydef;
  578. torddef = class(tstoreddef)
  579. low,high : TConstExprInt;
  580. ordtype : tordtype;
  581. constructor create(t : tordtype;v,b : TConstExprInt; doregister: boolean);virtual;
  582. constructor ppuload(ppufile:tcompilerppufile);
  583. function getcopy : tstoreddef;override;
  584. { do not override this routine in platform-specific subclasses,
  585. override ppuwrite_platform instead }
  586. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  587. function is_publishable : boolean;override;
  588. function GetTypeName:string;override;
  589. function alignment:shortint;override;
  590. procedure setsize;
  591. function packedbitsize: asizeint; override;
  592. function getvardef : longint;override;
  593. end;
  594. torddefclass = class of torddef;
  595. tfloatdef = class(tstoreddef)
  596. floattype : tfloattype;
  597. constructor create(t: tfloattype; doregister: boolean);virtual;
  598. constructor ppuload(ppufile:tcompilerppufile);
  599. function getcopy : tstoreddef;override;
  600. { do not override this routine in platform-specific subclasses,
  601. override ppuwrite_platform instead }
  602. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  603. function GetTypeName:string;override;
  604. function is_publishable : boolean;override;
  605. function alignment:shortint;override;
  606. function structalignment: shortint;override;
  607. procedure setsize;
  608. function getvardef:longint;override;
  609. end;
  610. tfloatdefclass = class of tfloatdef;
  611. { tabstractprocdef }
  612. tprocnameoption = (pno_showhidden, pno_proctypeoption, pno_paranames,
  613. pno_ownername, pno_noclassmarker, pno_noleadingdollar,
  614. pno_mangledname, pno_noparams);
  615. tprocnameoptions = set of tprocnameoption;
  616. tproccopytyp = (pc_normal,
  617. {$ifdef i8086}
  618. { the address in a far format }
  619. pc_far_address,
  620. { the offset part of the far address }
  621. pc_offset,
  622. {$endif i8086}
  623. { creates a procvardef describing only the code pointer
  624. of a method/netsted function/... }
  625. pc_address_only,
  626. { everything except for hidden parameters }
  627. pc_normal_no_hidden,
  628. { always creates a top-level function, removes all
  629. special parameters (self, vmt, parentfp, ...) }
  630. pc_bareproc
  631. );
  632. tcacheableproccopytyp = pc_normal..pc_address_only;
  633. tabstractprocdef = class(tstoreddef)
  634. { saves a definition to the return type }
  635. returndef : tdef;
  636. returndefderef : tderef;
  637. parast : TSymtable;
  638. paras : tparalist;
  639. proctypeoption : tproctypeoption;
  640. proccalloption : tproccalloption;
  641. procoptions : tprocoptions;
  642. callerargareasize,
  643. calleeargareasize: pint;
  644. {$ifdef m68k}
  645. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  646. {$endif}
  647. funcretloc : array[callerside..calleeside] of TCGPara;
  648. has_paraloc_info : tcallercallee; { paraloc info is available }
  649. { number of user visible parameters }
  650. maxparacount,
  651. minparacount : byte;
  652. constructor create(dt:tdeftyp;level:byte;doregister:boolean);
  653. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  654. destructor destroy;override;
  655. procedure ppuwrite(ppufile:tcompilerppufile);override;
  656. procedure buildderef;override;
  657. procedure deref;override;
  658. procedure calcparas;
  659. function mangledprocparanames(oldlen : longint) : string;
  660. function typename_paras(pno: tprocnameoptions): ansistring;
  661. function is_methodpointer:boolean;virtual;
  662. function is_addressonly:boolean;virtual;
  663. function no_self_node:boolean;
  664. { get either a copy as a procdef or procvardef }
  665. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp; const paraprefix: string): tstoreddef; virtual;
  666. function compatible_with_pointerdef_size(ptr: tpointerdef): boolean; virtual;
  667. procedure check_mark_as_nested;
  668. procedure init_paraloc_info(side: tcallercallee);
  669. procedure done_paraloc_info(side: tcallercallee);
  670. function stack_tainting_parameter(side: tcallercallee): boolean;
  671. function is_pushleftright: boolean;virtual;
  672. function address_type:tdef;virtual;
  673. { address type, generated for ofs() }
  674. function ofs_address_type:tdef;virtual;
  675. procedure declared_far;virtual;
  676. procedure declared_near;virtual;
  677. function generate_safecall_wrapper: boolean; virtual;
  678. { returns true if the def is a generic param of the procdef }
  679. function is_generic_param(def:tdef): boolean;
  680. private
  681. procedure count_para(p:TObject;arg:pointer);
  682. procedure insert_para(p:TObject;arg:pointer);
  683. end;
  684. tprocvardef = class(tabstractprocdef)
  685. constructor create(level:byte);virtual;
  686. { returns a procvardef that represents the address of a proc(var)def }
  687. class function getreusableprocaddr(def: tabstractprocdef; copytyp: tcacheableproccopytyp): tprocvardef; virtual;
  688. { same as above, but in case the def must never be freed after the
  689. current module has been compiled -- even if the def was not written
  690. to the ppu file (for defs in para locations, as we don't reset them
  691. so we don't have to recalculate them all the time) }
  692. class function getreusableprocaddr_no_free(def: tabstractprocdef): tprocvardef;
  693. constructor ppuload(ppufile:tcompilerppufile);
  694. function getcopy : tstoreddef;override;
  695. { do not override this routine in platform-specific subclasses,
  696. override ppuwrite_platform instead }
  697. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  698. function GetSymtable(t:tGetSymtable):TSymtable;override;
  699. function size : asizeint;override;
  700. function GetTypeName:string;override;
  701. function is_publishable : boolean;override;
  702. function is_methodpointer:boolean;override;
  703. function is_addressonly:boolean;override;
  704. function getmangledparaname:TSymStr;override;
  705. function getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string): tstoreddef; override;
  706. end;
  707. tprocvardefclass = class of tprocvardef;
  708. tmessageinf = record
  709. case integer of
  710. 0 : (str : pshortstring);
  711. 1 : (i : longint);
  712. end;
  713. tinlininginfo = record
  714. { node tree }
  715. code : tnode;
  716. flags : tprocinfoflags;
  717. end;
  718. pinlininginfo = ^tinlininginfo;
  719. timplprocdefinfo = record
  720. resultname : pshortstring;
  721. parentfpstruct: tsym;
  722. parentfpstructptrtype: tdef;
  723. parentfpinitblock: tnode;
  724. procstarttai,
  725. procendtai : tai;
  726. skpara: pointer;
  727. personality: tprocdef;
  728. forwarddef,
  729. interfacedef : boolean;
  730. hasforward : boolean;
  731. is_implemented : boolean;
  732. end;
  733. pimplprocdefinfo = ^timplprocdefinfo;
  734. { tprocdef }
  735. tprocdef = class(tabstractprocdef)
  736. protected
  737. {$ifdef symansistr}
  738. _mangledname : ansistring;
  739. {$else symansistr}
  740. _mangledname : pshortstring;
  741. {$endif}
  742. _parentfpsym : tsym;
  743. { information that is only required until the implementation of the
  744. procdef has been handled }
  745. implprocdefinfo : pimplprocdefinfo;
  746. function store_localst:boolean;
  747. function GetResultName: PShortString;
  748. procedure SetResultName(AValue: PShortString);
  749. function GetParentFPStruct: tsym;
  750. procedure SetParentFPStruct(AValue: tsym);
  751. function GetParentFPStructPtrType: tdef;
  752. procedure SetParentFPStructPtrType(AValue: tdef);
  753. function GetParentFPInitBlock: tnode;
  754. procedure SetParentFPInitBlock(AValue: tnode);
  755. function Getprocstarttai: tai;
  756. procedure Setprocstarttai(AValue: tai);
  757. function Getprocendtai: tai;
  758. procedure Setprocendtai(AValue: tai);
  759. function Getskpara: pointer;
  760. procedure Setskpara(AValue: pointer);
  761. function Getpersonality: tprocdef;
  762. procedure Setpersonality(AValue: tprocdef);
  763. function Getforwarddef: boolean;
  764. procedure Setforwarddef(AValue: boolean);
  765. function Getinterfacedef: boolean;
  766. procedure Setinterfacedef(AValue: boolean);virtual;
  767. function Gethasforward: boolean;
  768. procedure Sethasforward(AValue: boolean);
  769. function GetIsEmpty: boolean;
  770. procedure SetIsEmpty(AValue: boolean);
  771. function GetHasInliningInfo: boolean;
  772. procedure SetHasInliningInfo(AValue: boolean);
  773. function Getis_implemented: boolean;
  774. procedure Setis_implemented(AValue: boolean);
  775. function getparentfpsym: tsym;
  776. public
  777. messageinf : tmessageinf;
  778. dispid : longint;
  779. {$ifndef EXTDEBUG}
  780. { where is this function defined and what were the symbol
  781. flags, needed here because there
  782. is only one symbol for all overloaded functions
  783. EXTDEBUG has fileinfo in tdef (PFV) }
  784. fileinfo : tfileposinfo;
  785. {$endif}
  786. symoptions : tsymoptions;
  787. deprecatedmsg : pshortstring;
  788. { generic support }
  789. genericdecltokenbuf : tdynamicarray;
  790. { symbol owning this definition }
  791. procsym : tsym;
  792. procsymderef : tderef;
  793. { alias names }
  794. aliasnames : TCmdStrList;
  795. { symtables }
  796. localst : TSymtable;
  797. funcretsym : tsym;
  798. funcretsymderef : tderef;
  799. struct : tabstractrecorddef;
  800. structderef : tderef;
  801. implprocoptions: timplprocoptions;
  802. { import info }
  803. import_dll,
  804. import_name : pshortstring;
  805. { info for inlining the subroutine, if this pointer is nil,
  806. the procedure can't be inlined }
  807. inlininginfo : pinlininginfo;
  808. import_nr : word;
  809. extnumber : word;
  810. { set to a value different from tsk_none in case this procdef is for
  811. a routine that has to be internally generated by the compiler }
  812. synthetickind: tsynthetickind;
  813. visibility : tvisibility;
  814. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  815. seenmarker : pointer; // used for filtering in tcandidate
  816. {$endif}
  817. {$ifdef symansistr}
  818. section: ansistring;
  819. {$else symansistr}
  820. section: pshortstring;
  821. {$endif}
  822. constructor create(level:byte;doregister:boolean);virtual;
  823. constructor ppuload(ppufile:tcompilerppufile);
  824. destructor destroy;override;
  825. procedure freeimplprocdefinfo;
  826. { do not override this routine in platform-specific subclasses,
  827. override ppuwrite_platform instead }
  828. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  829. procedure buildderef;override;
  830. procedure buildderefimpl;override;
  831. procedure deref;override;
  832. procedure derefimpl;override;
  833. function GetSymtable(t:tGetSymtable):TSymtable;override;
  834. { warnings:
  835. * the symtablestack top has to be the symtable to which the copy
  836. should be added
  837. * getcopy does not create a finished/ready-to-use procdef; it
  838. needs to be finalised afterwards by calling
  839. symcreat.finish_copied_procdef() afterwards
  840. }
  841. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp; const paraprefix: string): tstoreddef; override;
  842. function getcopy: tstoreddef; override;
  843. function GetTypeName : string;override;
  844. function mangledname : TSymStr; virtual;
  845. procedure setmangledname(const s : TSymStr);
  846. function needsglobalasmsym: boolean;
  847. procedure setcompilerprocname;
  848. function fullprocname(showhidden:boolean):string;
  849. function customprocname(pno: tprocnameoptions):ansistring;
  850. function defaultmangledname: TSymStr;
  851. function cplusplusmangledname : TSymStr;
  852. function objcmangledname : TSymStr;
  853. function is_methodpointer:boolean;override;
  854. function is_addressonly:boolean;override;
  855. procedure make_external;
  856. procedure init_genericdecl;
  857. function get_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean; virtual;
  858. function get_safecall_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean; virtual;
  859. { returns whether the mangled name or any of its aliases is equal to
  860. s }
  861. function has_alias_name(const s: TSymStr):boolean;
  862. { aliases to fields only required when a function is implemented in
  863. the current unit }
  864. property resultname: PShortString read GetResultName write SetResultName;
  865. { temporary reference to structure containing copies of all local
  866. variables and parameters accessed by nested routines; reference to
  867. this structure is passed as "parent frame pointer" on targets that
  868. lack this concept (at least JVM and LLVM); no need to save to/
  869. restore from ppu, since nested routines are always in the same
  870. unit }
  871. property parentfpstruct: tsym read GetParentFPStruct write SetParentFPStruct;
  872. { pointer to parentfpstruct's type (not yet valid during parsing, so
  873. cannot be used for $parentfp parameter) (no need to save to ppu) }
  874. property parentfpstructptrtype: tdef read GetParentFPStructPtrType write SetParentFPStructPtrType;
  875. { code to copy the parameters accessed from nested routines into the
  876. parentfpstruct (no need to save to ppu) }
  877. property parentfpinitblock: tnode read GetParentFPInitBlock write SetParentFPInitBlock;
  878. { First/last assembler symbol/instruction in aasmoutput list.
  879. Note: initialised after compiling the code for the procdef, but
  880. not saved to/restored from ppu. Used when inserting debug info }
  881. property procstarttai: tai read Getprocstarttai write Setprocstarttai;
  882. property procendtai: tai read Getprocendtai write Setprocendtai;
  883. { optional parameter for the synthetic routine generation logic }
  884. property skpara: pointer read Getskpara write Setskpara;
  885. { ABI-conformant exception handling personality function }
  886. property personality: tprocdef read Getpersonality write Setpersonality;
  887. { true, if the procedure is only declared
  888. (forward procedure) }
  889. property forwarddef: boolean read Getforwarddef write Setforwarddef;
  890. { true if the procedure is declared in the interface }
  891. property interfacedef: boolean read Getinterfacedef write Setinterfacedef;
  892. { true if the procedure has a forward declaration }
  893. property hasforward: boolean read Gethasforward write Sethasforward;
  894. { true if the routine's body is empty }
  895. property isempty: boolean read GetIsEmpty write SetIsEmpty;
  896. { true if all information required to inline this routine is available }
  897. property has_inlininginfo: boolean read GetHasInliningInfo write SetHasInliningInfo;
  898. { returns the $parentfp parameter for nested routines }
  899. property parentfpsym: tsym read getparentfpsym;
  900. { true if the implementation part for this procdef has been handled }
  901. property is_implemented: boolean read Getis_implemented write Setis_implemented;
  902. end;
  903. tprocdefclass = class of tprocdef;
  904. { single linked list of overloaded procs }
  905. pprocdeflist = ^tprocdeflist;
  906. tprocdeflist = record
  907. def : tprocdef;
  908. defderef : tderef;
  909. next : pprocdeflist;
  910. end;
  911. tstringdef = class(tstoreddef)
  912. encoding : tstringencoding;
  913. stringtype : tstringtype;
  914. len : asizeint;
  915. constructor createshort(l: byte; doregister: boolean);virtual;
  916. constructor loadshort(ppufile:tcompilerppufile);
  917. constructor createlong(l: asizeint; doregister: boolean);virtual;
  918. constructor loadlong(ppufile:tcompilerppufile);
  919. constructor createansi(aencoding: tstringencoding; doregister: boolean);virtual;
  920. constructor loadansi(ppufile:tcompilerppufile);
  921. constructor createwide(doregister: boolean);virtual;
  922. constructor loadwide(ppufile:tcompilerppufile);
  923. constructor createunicode(doregister: boolean);virtual;
  924. constructor loadunicode(ppufile:tcompilerppufile);virtual;
  925. function getcopy : tstoreddef;override;
  926. function stringtypname:string;
  927. { do not override this routine in platform-specific subclasses,
  928. override ppuwrite_platform instead }
  929. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  930. function GetTypeName:string;override;
  931. function getmangledparaname:TSymStr;override;
  932. function is_publishable : boolean;override;
  933. function size:asizeint;override;
  934. function alignment : shortint;override;
  935. function needs_inittable : boolean;override;
  936. function getvardef:longint;override;
  937. { returns the default char type def for the string }
  938. function get_default_char_type: tdef;
  939. { returns the default string type }
  940. function get_default_string_type: tdef;
  941. end;
  942. tstringdefclass = class of tstringdef;
  943. { tenumdef }
  944. tenumdef = class(tstoreddef)
  945. minval,
  946. maxval : asizeint;
  947. basedef : tenumdef;
  948. basedefderef : tderef;
  949. symtable : TSymtable;
  950. has_jumps : boolean;
  951. constructor create;virtual;
  952. constructor create_subrange(_basedef:tenumdef;_min,_max:asizeint);virtual;
  953. constructor ppuload(ppufile:tcompilerppufile);
  954. destructor destroy;override;
  955. function getcopy : tstoreddef;override;
  956. { do not override this routine in platform-specific subclasses,
  957. override ppuwrite_platform instead }
  958. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  959. procedure buildderef;override;
  960. procedure deref;override;
  961. function GetTypeName:string;override;
  962. function is_publishable : boolean;override;
  963. procedure calcsavesize(packenum: shortint);
  964. function packedbitsize: asizeint; override;
  965. procedure setmax(_max:asizeint);
  966. procedure setmin(_min:asizeint);
  967. function min:asizeint;
  968. function max:asizeint;
  969. function getfirstsym:tsym;
  970. function int2enumsym(l: asizeint): tsym;
  971. { returns basedef if assigned, otherwise self }
  972. function getbasedef: tenumdef;
  973. end;
  974. tenumdefclass = class of tenumdef;
  975. tsetdef = class(tstoreddef)
  976. elementdef : tdef;
  977. elementdefderef : tderef;
  978. setbase,
  979. setmax : asizeint;
  980. constructor create(def: tdef; low, high: asizeint; doregister: boolean);virtual;
  981. constructor ppuload(ppufile:tcompilerppufile);
  982. function getcopy : tstoreddef;override;
  983. { do not override this routine in platform-specific subclasses,
  984. override ppuwrite_platform instead }
  985. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  986. procedure buildderef;override;
  987. procedure deref;override;
  988. function GetTypeName:string;override;
  989. function is_publishable : boolean;override;
  990. function alignment: shortint; override;
  991. end;
  992. tsetdefclass = class of tsetdef;
  993. tgenericdummyentry = class
  994. dummysym : tsym;
  995. resolvedsym : tsym;
  996. end;
  997. tdefawaresymtablestack = class(TSymtablestack)
  998. private
  999. procedure add_helpers_and_generics(st:tsymtable;addgenerics:boolean);
  1000. procedure remove_helpers_and_generics(st:tsymtable);inline;
  1001. procedure remove_helpers(st:tsymtable);
  1002. procedure remove_generics(st:tsymtable);
  1003. procedure pushcommon(st:tsymtable);inline;
  1004. public
  1005. procedure push(st: TSymtable); override;
  1006. procedure pushafter(st,afterst:TSymtable); override;
  1007. procedure pop(st: TSymtable); override;
  1008. end;
  1009. var
  1010. current_structdef: tabstractrecorddef; { used for private functions check !! }
  1011. current_genericdef: tstoreddef; { used to reject declaration of generic class inside generic class }
  1012. current_specializedef: tstoreddef; { used to implement usage of generic class in itself }
  1013. cfiledef: tfiledefclass;
  1014. cvariantdef: tvariantdefclass;
  1015. cformaldef: tformaldefclass;
  1016. cforwarddef: tforwarddefclass;
  1017. cundefineddef: tundefineddefclass;
  1018. cerrordef: terrordefclass;
  1019. cpointerdef: tpointerdefclass;
  1020. crecorddef: trecorddefclass;
  1021. cimplementedinterface: timplementedinterfaceclass;
  1022. cobjectdef: tobjectdefclass;
  1023. cclassrefdef: tclassrefdefclass;
  1024. carraydef: tarraydefclass;
  1025. corddef: torddefclass;
  1026. cfloatdef: tfloatdefclass;
  1027. cprocvardef: tprocvardefclass;
  1028. cprocdef: tprocdefclass;
  1029. cstringdef: tstringdefclass;
  1030. cenumdef: tenumdefclass;
  1031. csetdef: tsetdefclass;
  1032. { default types }
  1033. generrordef, { error in definition }
  1034. voidpointertype, { pointer for Void-pointeddef }
  1035. charpointertype, { pointer for Char-pointeddef }
  1036. widecharpointertype, { pointer for WideChar-pointeddef }
  1037. voidcodepointertype, { pointer to code; corresponds to System.CodePointer }
  1038. voidstackpointertype, { the pointer type used for accessing parameters and local vars on the stack }
  1039. parentfpvoidpointertype, { void pointer with the size of the hidden parentfp parameter, passed to nested functions }
  1040. {$ifdef x86}
  1041. voidnearpointertype,
  1042. voidnearcspointertype,
  1043. voidneardspointertype,
  1044. voidnearsspointertype,
  1045. voidnearespointertype,
  1046. voidnearfspointertype,
  1047. voidneargspointertype,
  1048. {$ifdef i8086}
  1049. voidfarpointertype,
  1050. voidhugepointertype,
  1051. charnearpointertype,
  1052. charfarpointertype,
  1053. charhugepointertype,
  1054. bytefarpointertype, { used for Mem[] }
  1055. wordfarpointertype, { used for MemW[] }
  1056. longintfarpointertype, { used for MemL[] }
  1057. {$endif i8086}
  1058. {$endif x86}
  1059. cundefinedtype,
  1060. cformaltype, { unique formal definition }
  1061. ctypedformaltype, { unique typed formal definition }
  1062. voidtype, { Void (procedure) }
  1063. cansichartype, { Char }
  1064. cwidechartype, { WideChar }
  1065. pasbool1type, { boolean type }
  1066. pasbool8type,
  1067. pasbool16type,
  1068. pasbool32type,
  1069. pasbool64type,
  1070. bool8type,
  1071. bool16type,
  1072. bool32type,
  1073. bool64type, { implement me }
  1074. {$ifdef llvm}
  1075. llvmbool1type, { LLVM i1 type }
  1076. {$endif llvm}
  1077. u8inttype, { 8-Bit unsigned integer }
  1078. s8inttype, { 8-Bit signed integer }
  1079. u16inttype, { 16-Bit unsigned integer }
  1080. s16inttype, { 16-Bit signed integer }
  1081. u24inttype, { 24-Bit unsigned integer }
  1082. s24inttype, { 24-Bit signed integer }
  1083. u32inttype, { 32-Bit unsigned integer }
  1084. s32inttype, { 32-Bit signed integer }
  1085. u40inttype, { 40-Bit unsigned integer }
  1086. s40inttype, { 40-Bit signed integer }
  1087. u48inttype, { 48-Bit unsigned integer }
  1088. s48inttype, { 48-Bit signed integer }
  1089. u56inttype, { 56-Bit unsigned integer }
  1090. s56inttype, { 56-Bit signed integer }
  1091. u64inttype, { 64-bit unsigned integer }
  1092. s64inttype, { 64-bit signed integer }
  1093. u128inttype, { 128-bit unsigned integer }
  1094. s128inttype, { 128-bit signed integer }
  1095. s32floattype, { 32 bit floating point number }
  1096. s64floattype, { 64 bit floating point number }
  1097. s80floattype, { 80 bit floating point number }
  1098. sc80floattype, { 80 bit floating point number but stored like in C }
  1099. s64currencytype, { pointer to a currency type }
  1100. cshortstringtype, { pointer to type of short string const }
  1101. clongstringtype, { pointer to type of long string const }
  1102. cansistringtype, { pointer to type of ansi string const }
  1103. cwidestringtype, { pointer to type of wide string const }
  1104. cunicodestringtype,
  1105. openshortstringtype, { pointer to type of an open shortstring,
  1106. needed for readln() }
  1107. openchararraytype, { pointer to type of an open array of char,
  1108. needed for readln() }
  1109. cfiletype, { get the same definition for all file }
  1110. { used for stabs }
  1111. methodpointertype, { typecasting of methodpointers to extract self }
  1112. nestedprocpointertype, { typecasting of nestedprocpointers to extract parentfp }
  1113. hresultdef,
  1114. typekindtype, { def of TTypeKind for correct handling of GetTypeKind parameters }
  1115. { we use only one variant def for every variant class }
  1116. cvarianttype,
  1117. colevarianttype,
  1118. { default integer type, normally s32inttype on 32 bit systems and s64bittype on 64 bit systems }
  1119. sinttype,
  1120. uinttype,
  1121. { integer types corresponding to OS_SINT/OS_INT }
  1122. ossinttype,
  1123. osuinttype,
  1124. { integer types corresponding to the ALU size, sizeof(aint) and the ALUSInt/ALUUInt types in the system unit }
  1125. alusinttype,
  1126. aluuinttype,
  1127. { integer types corresponding to SizeInt and SizeUInt for the target platform }
  1128. sizeuinttype,
  1129. sizesinttype,
  1130. { unsigned and signed ord type with the same size as a pointer }
  1131. ptruinttype,
  1132. ptrsinttype,
  1133. { unsigned and signed ord type with the same size as a codepointer }
  1134. codeptruinttype,
  1135. codeptrsinttype,
  1136. { several types to simulate more or less C++ objects for GDB }
  1137. vmttype,
  1138. vmtarraytype,
  1139. { type of classrefs, used for stabs }
  1140. pvmttype,
  1141. { return type of the setjmp function }
  1142. exceptionreasontype : tdef;
  1143. { pointer to the anchestor of all classes }
  1144. class_tobject : tobjectdef;
  1145. { pointer to the base type for custom attributes }
  1146. class_tcustomattribute : tobjectdef;
  1147. { pointer to the ancestor of all COM interfaces }
  1148. interface_iunknown : tobjectdef;
  1149. { pointer to the ancestor of all dispinterfaces }
  1150. interface_idispatch : tobjectdef;
  1151. { pointer to the TGUID type
  1152. of all interfaces }
  1153. rec_tguid : trecorddef;
  1154. { jump buffer type, used by setjmp }
  1155. rec_jmp_buf : trecorddef;
  1156. { system.texceptaddr type, used by fpc_pushexceptaddr }
  1157. rec_exceptaddr: trecorddef;
  1158. { Objective-C base types }
  1159. objc_metaclasstype,
  1160. objc_superclasstype,
  1161. objc_idtype,
  1162. objc_seltype : tpointerdef;
  1163. objc_objecttype : trecorddef;
  1164. { base type of @protocol(protocolname) Objective-C statements }
  1165. objc_protocoltype : tobjectdef;
  1166. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  1167. objc_fastenumeration : tobjectdef;
  1168. objc_fastenumerationstate : trecorddef;
  1169. {$ifdef llvm}
  1170. { llvm types }
  1171. { a unique def to identify any kind of metadata }
  1172. llvm_metadatatype : tdef;
  1173. {$endif llvm}
  1174. { Java base types }
  1175. { java.lang.Object }
  1176. java_jlobject : tobjectdef;
  1177. { java.lang.Throwable }
  1178. java_jlthrowable : tobjectdef;
  1179. { FPC base type for records }
  1180. java_fpcbaserecordtype : tobjectdef;
  1181. { java.lang.String }
  1182. java_jlstring : tobjectdef;
  1183. { java.lang.Enum }
  1184. java_jlenum : tobjectdef;
  1185. { java.util.EnumSet }
  1186. java_juenumset : tobjectdef;
  1187. { java.util.BitSet }
  1188. java_jubitset : tobjectdef;
  1189. { FPC java implementation of ansistrings }
  1190. java_ansistring : tobjectdef;
  1191. { FPC java implementation of shortstrings }
  1192. java_shortstring : tobjectdef;
  1193. { FPC java procvar base class }
  1194. java_procvarbase : tobjectdef;
  1195. { x86 vector types }
  1196. x86_m64type,
  1197. x86_m128type,
  1198. x86_m128dtype,
  1199. x86_m128itype,
  1200. x86_m256type,
  1201. x86_m256dtype,
  1202. x86_m256itype : tdef;
  1203. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  1204. function make_dllmangledname(const dllname,importname:TSymStr;
  1205. import_nr : word; pco : tproccalloption):TSymStr;
  1206. { should be in the types unit, but the types unit uses the node stuff :( }
  1207. function is_interfacecom(def: tdef): boolean;
  1208. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  1209. function is_any_interface_kind(def: tdef): boolean;
  1210. function is_interfacecorba(def: tdef): boolean;
  1211. function is_interface(def: tdef): boolean;
  1212. function is_dispinterface(def: tdef): boolean;
  1213. function is_object(def: tdef): boolean;
  1214. function is_class(def: tdef): boolean;
  1215. function is_cppclass(def: tdef): boolean;
  1216. function is_objectpascal_helper(def: tdef): boolean;
  1217. function is_objcclass(def: tdef): boolean;
  1218. function is_objcclassref(def: tdef): boolean;
  1219. function is_objcprotocol(def: tdef): boolean;
  1220. function is_objccategory(def: tdef): boolean;
  1221. function is_objc_class_or_protocol(def: tdef): boolean;
  1222. function is_objc_protocol_or_category(def: tdef): boolean;
  1223. function is_classhelper(def: tdef): boolean;
  1224. function is_class_or_interface(def: tdef): boolean;
  1225. function is_class_or_interface_or_objc(def: tdef): boolean;
  1226. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  1227. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  1228. function is_class_or_interface_or_object(def: tdef): boolean;
  1229. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  1230. function is_implicit_pointer_object_type(def: tdef): boolean;
  1231. { returns true, if def is a type which is an implicit pointer to an array (dyn. array or dyn. string) }
  1232. function is_implicit_array_pointer(def: tdef): boolean;
  1233. function is_class_or_object(def: tdef): boolean;
  1234. function is_record(def: tdef): boolean;
  1235. function is_javaclass(def: tdef): boolean;
  1236. function is_javaclassref(def: tdef): boolean;
  1237. function is_javainterface(def: tdef): boolean;
  1238. function is_java_class_or_interface(def: tdef): boolean;
  1239. procedure loadobjctypes;
  1240. procedure maybeloadcocoatypes;
  1241. function use_vectorfpu(def : tdef) : boolean;
  1242. function getansistringcodepage:tstringencoding; inline;
  1243. function getansistringdef:tstringdef;
  1244. function getparaencoding(def:tdef):tstringencoding; inline;
  1245. function get_threadvar_record(def: tdef; out index_field, non_mt_data_field: tsym): trecorddef;
  1246. function get_recorddef(prefix:tinternaltypeprefix;const fields:array of tdef; packrecords:shortint): trecorddef;
  1247. { get a table def of the form
  1248. record
  1249. count: countdef;
  1250. elements: array[0..count-1] of elementdef
  1251. end;
  1252. Returns both the outer record and the inner arraydef
  1253. }
  1254. procedure get_tabledef(prefix:tinternaltypeprefix;countdef,elementdef:tdef;count:longint;packrecords:shortint;out recdef:trecorddef;out arrdef:tarraydef);
  1255. implementation
  1256. uses
  1257. SysUtils,
  1258. cutils,
  1259. { global }
  1260. verbose,
  1261. { target }
  1262. systems,paramgr,
  1263. { symtable }
  1264. symsym,symtable,defutil,objcdef,
  1265. { parser }
  1266. pgenutil,
  1267. { module }
  1268. fmodule,ppu,
  1269. { other }
  1270. aasmbase,
  1271. gendef,
  1272. fpchash,
  1273. entfile
  1274. ;
  1275. {****************************************************************************
  1276. Helpers
  1277. ****************************************************************************}
  1278. function getansistringcodepage:tstringencoding; inline;
  1279. begin
  1280. if ([cs_explicit_codepage,cs_system_codepage]*current_settings.moduleswitches)<>[] then
  1281. result:=current_settings.sourcecodepage
  1282. else
  1283. result:=0;
  1284. end;
  1285. function getansistringdef:tstringdef;
  1286. var
  1287. symtable:tsymtable;
  1288. oldstack : tsymtablestack;
  1289. begin
  1290. { if a codepage is explicitly defined in this mudule we need to return
  1291. a replacement for ansistring def }
  1292. if ([cs_explicit_codepage,cs_system_codepage]*current_settings.moduleswitches)<>[] then
  1293. begin
  1294. if not assigned(current_module) then
  1295. internalerror(2011101301);
  1296. { codepage can be redeclared only once per unit so we don't need a list of
  1297. redefined ansistring but only one pointer }
  1298. if not assigned(current_module.ansistrdef) then
  1299. begin
  1300. { if we did not create it yet we need to do this now }
  1301. if current_module.in_interface then
  1302. symtable:=current_module.globalsymtable
  1303. else
  1304. symtable:=current_module.localsymtable;
  1305. { create a temporary stack as it's not good (TM) to mess around
  1306. with the order if the unit contains generics or helpers; don't
  1307. use a def aware symtablestack though }
  1308. oldstack:=symtablestack;
  1309. symtablestack:=tsymtablestack.create;
  1310. symtablestack.push(symtable);
  1311. current_module.ansistrdef:=cstringdef.createansi(current_settings.sourcecodepage,true);
  1312. symtablestack.pop(symtable);
  1313. symtablestack.free;
  1314. symtablestack:=oldstack;
  1315. end;
  1316. result:=tstringdef(current_module.ansistrdef);
  1317. end
  1318. else
  1319. result:=tstringdef(cansistringtype);
  1320. end;
  1321. function getparaencoding(def:tdef):tstringencoding; inline;
  1322. begin
  1323. { don't pass CP_NONE encoding to internal functions
  1324. they expect 0 encoding instead
  1325. exception: result of string concatenation, because if you pass the
  1326. result of a string concatenation to a rawbytestring, the result of
  1327. that concatenation shouldn't be converted to defaultsystemcodepage
  1328. if all strings have the same type }
  1329. result:=tstringdef(def).encoding;
  1330. if result=globals.CP_NONE then
  1331. result:=0
  1332. end;
  1333. function get_threadvar_record(def: tdef; out index_field, non_mt_data_field: tsym): trecorddef;
  1334. var
  1335. typ: ttypesym;
  1336. name: string;
  1337. begin
  1338. name:=internaltypeprefixName[itp_threadvar_record]+def.unique_id_str;
  1339. typ:=try_search_current_module_type(name);
  1340. if assigned(typ) then
  1341. begin
  1342. result:=trecorddef(ttypesym(typ).typedef);
  1343. index_field:=tsym(result.symtable.symlist[0]);
  1344. non_mt_data_field:=tsym(result.symtable.symlist[1]);
  1345. exit;
  1346. end;
  1347. { set recordalinmin to sizeof(pint), so the second field gets put at
  1348. offset = sizeof(pint) as expected }
  1349. result:=crecorddef.create_global_internal(
  1350. name,sizeof(pint),sizeof(pint));
  1351. {$ifdef cpu16bitaddr}
  1352. index_field:=result.add_field_by_def('',u16inttype);
  1353. {$else cpu16bitaddr}
  1354. index_field:=result.add_field_by_def('',u32inttype);
  1355. {$endif cpu16bitaddr}
  1356. non_mt_data_field:=result.add_field_by_def('',def);
  1357. { no need to add alignment padding, we won't create arrays of these }
  1358. end;
  1359. function get_recorddef(prefix:tinternaltypeprefix; const fields:array of tdef; packrecords:shortint): trecorddef;
  1360. var
  1361. fieldlist: tfplist;
  1362. srsym: tsym;
  1363. srsymtable: tsymtable;
  1364. i: longint;
  1365. name : TIDString;
  1366. begin
  1367. name:=copy(internaltypeprefixName[prefix],2,length(internaltypeprefixName[prefix]));
  1368. if searchsym_type(name,srsym,srsymtable) then
  1369. begin
  1370. result:=trecorddef(ttypesym(srsym).typedef);
  1371. exit
  1372. end;
  1373. { also always search in the current module (symtables are popped for
  1374. RTTI related code already) }
  1375. if searchsym_in_module(pointer(current_module),name,srsym,srsymtable) then
  1376. begin
  1377. result:=trecorddef(ttypesym(srsym).typedef);
  1378. exit;
  1379. end;
  1380. fieldlist:=tfplist.create;
  1381. for i:=low(fields) to high(fields) do
  1382. fieldlist.add(fields[i]);
  1383. result:=crecorddef.create_global_internal(internaltypeprefixName[prefix],packrecords,
  1384. targetinfos[target_info.system]^.alignment.recordalignmin);
  1385. result.add_fields_from_deflist(fieldlist);
  1386. fieldlist.free;
  1387. end;
  1388. procedure get_tabledef(prefix:tinternaltypeprefix;countdef,elementdef:tdef;count:longint;packrecords:shortint;out recdef:trecorddef;out arrdef:tarraydef);
  1389. var
  1390. fields: tfplist;
  1391. name: TIDString;
  1392. srsym: tsym;
  1393. srsymtable: tsymtable;
  1394. begin
  1395. { already created a message string table with this number of elements
  1396. in this unit -> reuse the def }
  1397. name:=internaltypeprefixName[prefix]+tostr(count);
  1398. if searchsym_type(copy(name,2,length(name)),srsym,srsymtable) then
  1399. begin
  1400. recdef:=trecorddef(ttypesym(srsym).typedef);
  1401. arrdef:=tarraydef(trecordsymtable(recdef.symtable).findfieldbyoffset(countdef.size).vardef);
  1402. exit
  1403. end;
  1404. { also always search in the current module (symtables are popped for
  1405. RTTI related code already) }
  1406. if searchsym_in_module(pointer(current_module),copy(name,2,length(name)),srsym,srsymtable) then
  1407. begin
  1408. recdef:=trecorddef(ttypesym(srsym).typedef);
  1409. arrdef:=tarraydef(trecordsymtable(recdef.symtable).findfieldbyoffset(countdef.size).vardef);
  1410. exit;
  1411. end;
  1412. recdef:=crecorddef.create_global_internal(name,packrecords,
  1413. targetinfos[target_info.system]^.alignment.recordalignmin);
  1414. fields:=tfplist.create;
  1415. fields.add(countdef);
  1416. if count>0 then
  1417. begin
  1418. arrdef:=carraydef.create(0,count-1,sizeuinttype);
  1419. arrdef.elementdef:=elementdef;
  1420. fields.add(arrdef);
  1421. end
  1422. else
  1423. arrdef:=nil;
  1424. recdef.add_fields_from_deflist(fields);
  1425. fields.free;
  1426. end;
  1427. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  1428. var
  1429. s,
  1430. prefix : TSymStr;
  1431. hash : qword;
  1432. begin
  1433. prefix:='';
  1434. if not assigned(st) then
  1435. internalerror(200204212);
  1436. repeat
  1437. { sub procedures }
  1438. while (st.symtabletype in [localsymtable,parasymtable]) do
  1439. begin
  1440. if st.defowner.typ<>procdef then
  1441. internalerror(200204173);
  1442. { Add the full mangledname of the routine to prevent
  1443. conflicts with two overloads both having a local entity
  1444. -- routine (tb0314), class, interface -- with the same name }
  1445. s:=tprocdef(st.defowner).procsym.name;
  1446. s:=s+tprocdef(st.defowner).mangledprocparanames(Length(s));
  1447. if prefix<>'' then
  1448. prefix:=s+'_'+prefix
  1449. else
  1450. prefix:=s;
  1451. if length(prefix)>100 then
  1452. begin
  1453. hash:=0;
  1454. hash:=UpdateFnv64(hash,prefix[1],length(prefix));
  1455. prefix:='$H'+Base64Mangle(hash);
  1456. end;
  1457. st:=st.defowner.owner;
  1458. end;
  1459. { object/classes symtable, nested type definitions in classes require the while loop }
  1460. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  1461. begin
  1462. if not (st.defowner.typ in [objectdef,recorddef]) then
  1463. internalerror(200204174);
  1464. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  1465. st:=st.defowner.owner;
  1466. end;
  1467. { local classes & interfaces are possible (because of closures) }
  1468. if st.symtabletype<>localsymtable then
  1469. break;
  1470. prefix:='$'+prefix;
  1471. until false;
  1472. { symtable must now be static or global }
  1473. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  1474. internalerror(200204175);
  1475. { The mangled name is made out of at most 4 parts:
  1476. 1) Optional typeprefix given as first parameter
  1477. with '_$' appended if not empty
  1478. 2) Unit name or 'P$'+program name (never empty)
  1479. 3) optional prefix variable that contains a unique
  1480. name for the local symbol table (prepended with '$_$'
  1481. if not empty)
  1482. 4) suffix as given as third parameter,
  1483. also optional (i.e. can be empty)
  1484. prepended by '_$$_' if not empty }
  1485. result:='';
  1486. if typeprefix<>'' then
  1487. result:=result+typeprefix+'_$';
  1488. { Add P$ for program, which can have the same name as
  1489. a unit }
  1490. if (TSymtable(main_module.localsymtable)=st) and
  1491. (not main_module.is_unit) then
  1492. result:=result+'P$'+st.name^
  1493. else
  1494. result:=result+st.name^;
  1495. if prefix<>'' then
  1496. result:=result+'$_$'+prefix;
  1497. if suffix<>'' then
  1498. result:=result+'_$$_'+suffix;
  1499. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  1500. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  1501. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  1502. { those in the debug map, leading to troubles with dsymutil). So always }
  1503. { add an underscore on darwin. }
  1504. if (target_info.system in systems_darwin) then
  1505. result := '_' + result;
  1506. end;
  1507. function make_dllmangledname(const dllname,importname:TSymStr;import_nr : word; pco : tproccalloption):TSymStr;
  1508. var
  1509. crc : cardinal;
  1510. i : longint;
  1511. use_crc : boolean;
  1512. dllprefix : TSymStr;
  1513. begin
  1514. if (target_info.system in (systems_all_windows + systems_nativent +
  1515. [system_i386_emx, system_i386_os2]))
  1516. and (dllname <> '') then
  1517. begin
  1518. dllprefix:=lower(ExtractFileName(dllname));
  1519. { Remove .dll suffix if present }
  1520. if copy(dllprefix,length(dllprefix)-3,length(dllprefix))='.dll' then
  1521. dllprefix:=copy(dllprefix,1,length(dllprefix)-4);
  1522. use_crc:=false;
  1523. for i:=1 to length(dllprefix) do
  1524. if not (dllprefix[i] in ['a'..'z','A'..'Z','_','0'..'9']) then
  1525. begin
  1526. use_crc:=true;
  1527. break;
  1528. end;
  1529. if use_crc then
  1530. begin
  1531. crc:=0;
  1532. crc:=UpdateCrc32(crc,dllprefix[1],length(dllprefix));
  1533. dllprefix:='_$dll$crc$'+hexstr(crc,8)+'$';
  1534. end
  1535. else
  1536. dllprefix:='_$dll$'+dllprefix+'$';
  1537. if importname<>'' then
  1538. result:=dllprefix+importname
  1539. else
  1540. result:=dllprefix+'_index_'+tostr(import_nr);
  1541. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  1542. { This allows to import VC++ mangled names from DLLs. }
  1543. { Do not perform replacement, if external symbol is not imported from DLL. }
  1544. if (dllname<>'') then
  1545. begin
  1546. Replace(result,'?','__q$$');
  1547. {$ifdef arm}
  1548. { @ symbol is not allowed in ARM assembler only }
  1549. Replace(result,'@','__a$$');
  1550. {$endif arm}
  1551. end;
  1552. end
  1553. else
  1554. begin
  1555. if importname<>'' then
  1556. begin
  1557. if not(pco in [pocall_cdecl,pocall_cppdecl]) then
  1558. result:=importname
  1559. else
  1560. result:=target_info.Cprefix+importname;
  1561. end
  1562. else
  1563. result:='_index_'+tostr(import_nr);
  1564. end;
  1565. end;
  1566. {****************************************************************************
  1567. TDEFAWARESYMTABLESTACK
  1568. (symtablestack descendant that does some special actions on
  1569. the pushed/popped symtables)
  1570. ****************************************************************************}
  1571. procedure tdefawaresymtablestack.add_helpers_and_generics(st:tsymtable;addgenerics:boolean);
  1572. var
  1573. i: integer;
  1574. s: string;
  1575. list: TFPObjectList;
  1576. def: tdef;
  1577. sym : tsym;
  1578. begin
  1579. { search the symtable from first to last; the helper to use will be the
  1580. last one in the list }
  1581. for i:=0 to st.symlist.count-1 do
  1582. begin
  1583. sym:=tsym(st.symlist[i]);
  1584. if not (sym.typ in [typesym,procsym]) then
  1585. continue;
  1586. if sym.typ=typesym then
  1587. def:=ttypesym(st.SymList[i]).typedef
  1588. else
  1589. def:=nil;
  1590. if is_objectpascal_helper(def) then
  1591. begin
  1592. s:=generate_objectpascal_helper_key(tobjectdef(def).extendeddef);
  1593. Message1(sym_d_adding_helper_for,s);
  1594. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1595. if not assigned(list) then
  1596. begin
  1597. list:=TFPObjectList.Create(false);
  1598. current_module.extendeddefs.Add(s,list);
  1599. end;
  1600. list.Add(def);
  1601. end
  1602. else
  1603. begin
  1604. if addgenerics then
  1605. add_generic_dummysym(sym);
  1606. { add nested helpers as well }
  1607. if assigned(def) and
  1608. (def.typ in [recorddef,objectdef]) and
  1609. (sto_has_helper in tabstractrecorddef(def).symtable.tableoptions) then
  1610. add_helpers_and_generics(tabstractrecorddef(def).symtable,false);
  1611. end;
  1612. end;
  1613. end;
  1614. procedure tdefawaresymtablestack.remove_helpers_and_generics(st:tsymtable);
  1615. begin
  1616. if sto_has_helper in st.tableoptions then
  1617. remove_helpers(st);
  1618. if sto_has_generic in st.tableoptions then
  1619. remove_generics(st);
  1620. end;
  1621. procedure tdefawaresymtablestack.remove_helpers(st:TSymtable);
  1622. var
  1623. i, j: integer;
  1624. tmpst: TSymtable;
  1625. list: TFPObjectList;
  1626. begin
  1627. for i:=current_module.extendeddefs.count-1 downto 0 do
  1628. begin
  1629. list:=TFPObjectList(current_module.extendeddefs[i]);
  1630. for j:=list.count-1 downto 0 do
  1631. begin
  1632. if not (list[j] is tobjectdef) then
  1633. Internalerror(2011031501);
  1634. tmpst:=tobjectdef(list[j]).owner;
  1635. repeat
  1636. if tmpst=st then
  1637. begin
  1638. list.delete(j);
  1639. break;
  1640. end
  1641. else
  1642. begin
  1643. if assigned(tmpst.defowner) then
  1644. tmpst:=tmpst.defowner.owner
  1645. else
  1646. tmpst:=nil;
  1647. end;
  1648. until not assigned(tmpst) or (tmpst.symtabletype in [globalsymtable,staticsymtable]);
  1649. end;
  1650. if list.count=0 then
  1651. current_module.extendeddefs.delete(i);
  1652. end;
  1653. end;
  1654. procedure tdefawaresymtablestack.remove_generics(st:tsymtable);
  1655. var
  1656. i,j : longint;
  1657. entry : tgenericdummyentry;
  1658. list : tfpobjectlist;
  1659. begin
  1660. for i:=current_module.genericdummysyms.count-1 downto 0 do
  1661. begin
  1662. list:=tfpobjectlist(current_module.genericdummysyms[i]);
  1663. if not assigned(list) then
  1664. continue;
  1665. for j:=list.count-1 downto 0 do
  1666. begin
  1667. entry:=tgenericdummyentry(list[j]);
  1668. if entry.dummysym.owner=st then
  1669. list.delete(j);
  1670. end;
  1671. if list.count=0 then
  1672. current_module.genericdummysyms.delete(i);
  1673. end;
  1674. end;
  1675. procedure tdefawaresymtablestack.pushcommon(st:tsymtable);
  1676. begin
  1677. if (sto_has_generic in st.tableoptions) or
  1678. (
  1679. (st.symtabletype in [globalsymtable,staticsymtable]) and
  1680. (sto_has_helper in st.tableoptions)
  1681. ) then
  1682. { nested helpers will be added as well }
  1683. add_helpers_and_generics(st,true);
  1684. end;
  1685. procedure tdefawaresymtablestack.push(st: TSymtable);
  1686. begin
  1687. pushcommon(st);
  1688. inherited push(st);
  1689. end;
  1690. procedure tdefawaresymtablestack.pushafter(st,afterst:TSymtable);
  1691. begin
  1692. pushcommon(st);
  1693. inherited pushafter(st,afterst);
  1694. end;
  1695. procedure tdefawaresymtablestack.pop(st: TSymtable);
  1696. begin
  1697. inherited pop(st);
  1698. if (sto_has_generic in st.tableoptions) or
  1699. (
  1700. (st.symtabletype in [globalsymtable,staticsymtable]) and
  1701. (sto_has_helper in st.tableoptions)
  1702. ) then
  1703. { nested helpers will be removed as well }
  1704. remove_helpers_and_generics(st);
  1705. end;
  1706. {****************************************************************************
  1707. TDEF (base class for definitions)
  1708. ****************************************************************************}
  1709. constructor tgenericconstraintdata.create;
  1710. begin
  1711. interfaces:=tfpobjectlist.create(false);
  1712. interfacesderef:=tfplist.create;
  1713. end;
  1714. destructor tgenericconstraintdata.destroy;
  1715. var
  1716. i : longint;
  1717. begin
  1718. for i:=0 to interfacesderef.count-1 do
  1719. dispose(pderef(interfacesderef[i]));
  1720. interfacesderef.free;
  1721. interfaces.free;
  1722. inherited destroy;
  1723. end;
  1724. procedure tgenericconstraintdata.ppuload(ppufile: tcompilerppufile);
  1725. var
  1726. cnt,i : longint;
  1727. intfderef : pderef;
  1728. begin
  1729. ppufile.getset(tppuset1(flags));
  1730. cnt:=ppufile.getlongint;
  1731. for i:=0 to cnt-1 do
  1732. begin
  1733. new(intfderef);
  1734. ppufile.getderef(intfderef^);
  1735. interfacesderef.add(intfderef);
  1736. end;
  1737. end;
  1738. procedure tgenericconstraintdata.ppuwrite(ppufile: tcompilerppufile);
  1739. var
  1740. i : longint;
  1741. begin
  1742. ppufile.putset(tppuset1(flags));
  1743. ppufile.putlongint(interfacesderef.count);
  1744. for i:=0 to interfacesderef.count-1 do
  1745. ppufile.putderef(pderef(interfacesderef[i])^);
  1746. end;
  1747. procedure tgenericconstraintdata.buildderef;
  1748. var
  1749. intfderef : pderef;
  1750. i : longint;
  1751. begin
  1752. for i:=0 to interfaces.count-1 do
  1753. begin
  1754. new(intfderef);
  1755. intfderef^.build(tobjectdef(interfaces[i]));
  1756. interfacesderef.add(intfderef);
  1757. end;
  1758. end;
  1759. procedure tgenericconstraintdata.deref;
  1760. var
  1761. i : longint;
  1762. begin
  1763. for i:=0 to interfacesderef.count-1 do
  1764. interfaces.add(pderef(interfacesderef[i])^.resolve);
  1765. end;
  1766. procedure tstoreddef.writeentry(ppufile: tcompilerppufile; ibnr: byte);
  1767. begin
  1768. ppuwrite_platform(ppufile);
  1769. ppufile.writeentry(ibnr);
  1770. end;
  1771. procedure tstoreddef.ppuwrite_platform(ppufile: tcompilerppufile);
  1772. begin
  1773. { by default: do nothing }
  1774. end;
  1775. procedure tstoreddef.ppuload_platform(ppufile: tcompilerppufile);
  1776. begin
  1777. { by default: do nothing }
  1778. end;
  1779. class procedure tstoreddef.setup_reusable_def(origdef, newdef: tdef; res: PHashSetItem; oldsymtablestack: tsymtablestack);
  1780. var
  1781. reusablesymtab: tsymtable;
  1782. begin
  1783. { must not yet belong to a symtable }
  1784. if assigned(newdef.owner) then
  1785. internalerror(2015111503);
  1786. reusablesymtab:=origdef.getreusablesymtab;
  1787. res^.Data:=newdef;
  1788. reusablesymtab.insertdef(newdef);
  1789. symtablestack:=oldsymtablestack;
  1790. end;
  1791. constructor tstoreddef.create(dt:tdeftyp;doregister:boolean);
  1792. begin
  1793. inherited create(dt);
  1794. savesize := 0;
  1795. {$ifdef EXTDEBUG}
  1796. fileinfo := current_filepos;
  1797. {$endif}
  1798. generictokenbuf:=nil;
  1799. genericdef:=nil;
  1800. typesymderef.reset;
  1801. genericdefderef.reset;
  1802. { Don't register forwarddefs, they are disposed at the
  1803. end of an type block }
  1804. if (dt=forwarddef) then
  1805. exit;
  1806. { Register in symtable stack }
  1807. if doregister then
  1808. begin
  1809. { immediately register interface defs, as they will always be
  1810. written to the ppu, their defid inlfuences the interface crc and
  1811. if we wait, depending on e.g. compiler defines they may get a
  1812. different defid (e.g. when a function is called, its procdef is
  1813. registered, so depending on whether or not, or when, an interface
  1814. procedure is called in the implementation, that may change its
  1815. defid otherwise) }
  1816. if assigned(current_module) and
  1817. current_module.in_interface then
  1818. register_def
  1819. else
  1820. maybe_put_in_symtable_stack;
  1821. end;
  1822. end;
  1823. destructor tstoreddef.destroy;
  1824. var
  1825. i : longint;
  1826. begin
  1827. { Direct calls are not allowed, use symtable.deletedef() }
  1828. if assigned(owner) then
  1829. internalerror(200612311);
  1830. if assigned(generictokenbuf) then
  1831. begin
  1832. generictokenbuf.free;
  1833. generictokenbuf:=nil;
  1834. end;
  1835. rtti_attribute_list.free;
  1836. genericparas.free;
  1837. if assigned(genericparaderefs) then
  1838. for i:=0 to genericparaderefs.count-1 do
  1839. dispose(pderef(genericparaderefs[i]));
  1840. genericparaderefs.free;
  1841. genconstraintdata.free;
  1842. {$ifndef symansistr}
  1843. stringdispose(_fullownerhierarchyname);
  1844. {$endif not symansistr}
  1845. inherited destroy;
  1846. end;
  1847. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1848. var
  1849. sizeleft,i,cnt : longint;
  1850. buf : array[0..255] of byte;
  1851. symderef : pderef;
  1852. begin
  1853. inherited create(dt);
  1854. DefId:=ppufile.getlongint;
  1855. current_module.deflist[DefId]:=self;
  1856. {$ifdef EXTDEBUG}
  1857. fillchar(fileinfo,sizeof(fileinfo),0);
  1858. {$endif}
  1859. { load }
  1860. ppufile.getderef(typesymderef);
  1861. ppufile.getset(tppuset2(defoptions));
  1862. ppufile.getset(tppuset1(defstates));
  1863. if df_genconstraint in defoptions then
  1864. begin
  1865. genconstraintdata:=tgenericconstraintdata.create;
  1866. genconstraintdata.ppuload(ppufile);
  1867. end;
  1868. if [df_generic,df_specialization]*defoptions<>[] then
  1869. begin
  1870. cnt:=ppufile.getlongint;
  1871. if cnt>0 then
  1872. begin
  1873. genericparas:=tfphashobjectlist.create(false);
  1874. genericparaderefs:=tfplist.create;
  1875. for i:=0 to cnt-1 do
  1876. begin
  1877. genericparas.add(ppufile.getstring,nil);
  1878. New(symderef);
  1879. ppufile.getderef(symderef^);
  1880. genericparaderefs.add(symderef);
  1881. end;
  1882. end;
  1883. end;
  1884. if df_generic in defoptions then
  1885. begin
  1886. sizeleft:=ppufile.getlongint;
  1887. initgeneric;
  1888. while sizeleft>0 do
  1889. begin
  1890. if sizeleft>sizeof(buf) then
  1891. i:=sizeof(buf)
  1892. else
  1893. i:=sizeleft;
  1894. ppufile.getdata(buf,i);
  1895. generictokenbuf.write(buf,i);
  1896. dec(sizeleft,i);
  1897. end;
  1898. end;
  1899. if df_specialization in defoptions then
  1900. ppufile.getderef(genericdefderef);
  1901. rtti_attribute_list:=trtti_attribute_list.ppuload(ppufile);
  1902. end;
  1903. function tstoreddef.needs_separate_initrtti:boolean;
  1904. begin
  1905. result:=false;
  1906. end;
  1907. function tstoreddef.rtti_mangledname(rt : trttitype) : TSymStr;
  1908. var
  1909. prefix : string[4];
  1910. begin
  1911. if (rt=fullrtti) or (not needs_separate_initrtti) then
  1912. begin
  1913. prefix:='RTTI';
  1914. include(defstates,ds_rtti_table_used);
  1915. end
  1916. else
  1917. begin
  1918. prefix:='INIT';
  1919. include(defstates,ds_init_table_used);
  1920. end;
  1921. if assigned(typesym) and
  1922. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  1923. result:=make_mangledname(prefix,typesym.owner,typesym.name)
  1924. else
  1925. result:=make_mangledname(prefix,findunitsymtable(owner),'def'+unique_id_str)
  1926. end;
  1927. function tstoreddef.OwnerHierarchyName: string;
  1928. var
  1929. tmp: tdef;
  1930. begin
  1931. tmp:=self;
  1932. result:='';
  1933. repeat
  1934. { can be not assigned in case of a forwarddef }
  1935. if assigned(tmp.owner) and
  1936. (tmp.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1937. tmp:=tdef(tmp.owner.defowner)
  1938. else
  1939. break;
  1940. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  1941. until tmp=nil;
  1942. end;
  1943. function tstoreddef.fullownerhierarchyname(skipprocparams:boolean): TSymStr;
  1944. var
  1945. lastowner: tsymtable;
  1946. tmp: tdef;
  1947. pno: tprocnameoptions;
  1948. begin
  1949. {$ifdef symansistr}
  1950. if not skipprocparams and (_fullownerhierarchyname<>'') then
  1951. exit(_fullownerhierarchyname);
  1952. {$else symansistr}
  1953. if not skipprocparams and assigned(_fullownerhierarchyname) then
  1954. exit(_fullownerhierarchyname^);
  1955. {$endif symansistr}
  1956. { the def can only reside inside structured types or
  1957. procedures/functions/methods }
  1958. tmp:=self;
  1959. result:='';
  1960. repeat
  1961. lastowner:=tmp.owner;
  1962. { can be not assigned in case of a forwarddef }
  1963. if not assigned(lastowner) then
  1964. break
  1965. else
  1966. tmp:=tdef(lastowner.defowner);
  1967. if not assigned(tmp) then
  1968. break;
  1969. if tmp.typ in [recorddef,objectdef] then
  1970. result:=tabstractrecorddef(tmp).objrealname^+'.'+result
  1971. else
  1972. if tmp.typ=procdef then
  1973. begin
  1974. pno:=[pno_paranames,pno_proctypeoption];
  1975. if skipprocparams then
  1976. include(pno,pno_noparams);
  1977. result:=tprocdef(tmp).customprocname(pno)+'.'+result;
  1978. end;
  1979. until tmp=nil;
  1980. { add the unit name }
  1981. if assigned(lastowner) and
  1982. assigned(lastowner.realname) then
  1983. result:=lastowner.realname^+'.'+result;
  1984. if not skipprocparams then
  1985. { don't store the name in this case }
  1986. {$ifdef symansistr}
  1987. _fullownerhierarchyname:=result;
  1988. {$else symansistr}
  1989. _fullownerhierarchyname:=stringdup(result);
  1990. {$endif symansistr}
  1991. end;
  1992. function tstoreddef.in_currentunit: boolean;
  1993. var
  1994. st: tsymtable;
  1995. begin
  1996. st:=owner;
  1997. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  1998. st:=st.defowner.owner;
  1999. result:=st.iscurrentunit;
  2000. end;
  2001. function tstoreddef.getcopy : tstoreddef;
  2002. begin
  2003. Message(sym_e_cant_create_unique_type);
  2004. getcopy:=cerrordef.create;
  2005. end;
  2006. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  2007. var
  2008. sizeleft,i : longint;
  2009. buf : array[0..255] of byte;
  2010. oldintfcrc : boolean;
  2011. begin
  2012. if defid<0 then
  2013. internalerror(2015101401);
  2014. ppufile.putlongint(DefId);
  2015. ppufile.putderef(typesymderef);
  2016. ppufile.putset(tppuset2(defoptions));
  2017. oldintfcrc:=ppufile.do_crc;
  2018. ppufile.do_crc:=false;
  2019. ppufile.putset(tppuset1(defstates));
  2020. if df_genconstraint in defoptions then
  2021. genconstraintdata.ppuwrite(ppufile);
  2022. if [df_generic,df_specialization]*defoptions<>[] then
  2023. begin
  2024. if not assigned(genericparas) then
  2025. ppufile.putlongint(0)
  2026. else
  2027. begin
  2028. if not assigned(genericparaderefs) then
  2029. internalerror(2014052305);
  2030. ppufile.putlongint(genericparas.count);
  2031. for i:=0 to genericparas.count-1 do
  2032. begin
  2033. ppufile.putstring(genericparas.nameofindex(i));
  2034. ppufile.putderef(pderef(genericparaderefs[i])^);
  2035. end;
  2036. end;
  2037. end;
  2038. if df_generic in defoptions then
  2039. begin
  2040. if assigned(generictokenbuf) then
  2041. begin
  2042. sizeleft:=generictokenbuf.size;
  2043. generictokenbuf.seek(0);
  2044. end
  2045. else
  2046. sizeleft:=0;
  2047. ppufile.putlongint(sizeleft);
  2048. while sizeleft>0 do
  2049. begin
  2050. if sizeleft>sizeof(buf) then
  2051. i:=sizeof(buf)
  2052. else
  2053. i:=sizeleft;
  2054. generictokenbuf.read(buf,i);
  2055. ppufile.putdata(buf,i);
  2056. dec(sizeleft,i);
  2057. end;
  2058. end;
  2059. ppufile.do_crc:=oldintfcrc;
  2060. if df_specialization in defoptions then
  2061. ppufile.putderef(genericdefderef);
  2062. trtti_attribute_list.ppuwrite(rtti_attribute_list,ppufile);
  2063. end;
  2064. procedure tstoreddef.ppuload_subentries(ppufile: tcompilerppufile);
  2065. begin
  2066. trtti_attribute_list.ppuload_subentries(rtti_attribute_list,ppufile);
  2067. end;
  2068. procedure tstoreddef.ppuwrite_subentries(ppufile: tcompilerppufile);
  2069. begin
  2070. trtti_attribute_list.ppuwrite_subentries(rtti_attribute_list,ppufile);
  2071. end;
  2072. procedure tstoreddef.buildderef;
  2073. var
  2074. i : longint;
  2075. sym : tsym;
  2076. symderef : pderef;
  2077. begin
  2078. if not registered then
  2079. register_def;
  2080. typesymderef.build(typesym);
  2081. genericdefderef.build(genericdef);
  2082. if assigned(rtti_attribute_list) then
  2083. rtti_attribute_list.buildderef;
  2084. if assigned(genconstraintdata) then
  2085. genconstraintdata.buildderef;
  2086. if assigned(genericparas) then
  2087. begin
  2088. if not assigned(genericparaderefs) then
  2089. genericparaderefs:=tfplist.create;
  2090. for i:=0 to genericparas.count-1 do
  2091. begin
  2092. sym:=tsym(genericparas.items[i]);
  2093. new(symderef);
  2094. symderef^.build(sym);
  2095. genericparaderefs.add(symderef);
  2096. end;
  2097. end;
  2098. end;
  2099. procedure tstoreddef.buildderefimpl;
  2100. begin
  2101. end;
  2102. procedure tstoreddef.deref;
  2103. var
  2104. symderef : pderef;
  2105. i : longint;
  2106. begin
  2107. typesym:=ttypesym(typesymderef.resolve);
  2108. if df_specialization in defoptions then
  2109. genericdef:=tstoreddef(genericdefderef.resolve);
  2110. if assigned(rtti_attribute_list) then
  2111. rtti_attribute_list.deref;
  2112. if assigned(genconstraintdata) then
  2113. genconstraintdata.deref;
  2114. if assigned(genericparas) then
  2115. begin
  2116. if not assigned(genericparaderefs) then
  2117. internalerror(2014052302);
  2118. if genericparas.count<>genericparaderefs.count then
  2119. internalerror(2014052303);
  2120. for i:=0 to genericparaderefs.count-1 do
  2121. begin
  2122. symderef:=pderef(genericparaderefs[i]);
  2123. genericparas.items[i]:=symderef^.resolve;
  2124. end;
  2125. end;
  2126. end;
  2127. procedure tstoreddef.derefimpl;
  2128. begin
  2129. end;
  2130. function tstoreddef.size : asizeint;
  2131. begin
  2132. size:=savesize;
  2133. end;
  2134. function tstoreddef.getvardef:longint;
  2135. begin
  2136. result:=varUndefined;
  2137. end;
  2138. function tstoreddef.alignment : shortint;
  2139. begin
  2140. { natural alignment by default }
  2141. alignment:=size_2_align(savesize);
  2142. { can happen if savesize = 0, e.g. for voiddef or
  2143. an empty record
  2144. }
  2145. if (alignment=0) then
  2146. alignment:=1;
  2147. end;
  2148. { returns true, if the definition can be published }
  2149. function tstoreddef.is_publishable : boolean;
  2150. begin
  2151. is_publishable:=false;
  2152. end;
  2153. { needs an init table }
  2154. function tstoreddef.needs_inittable : boolean;
  2155. begin
  2156. needs_inittable:=false;
  2157. end;
  2158. function tstoreddef.has_non_trivial_init_child(check_parent:boolean):boolean;
  2159. begin
  2160. result:=false;
  2161. end;
  2162. function tstoreddef.is_intregable : boolean;
  2163. {$ifndef cpuhighleveltarget}
  2164. var
  2165. recsize,temp: longint;
  2166. {$endif cpuhighleveltarget}
  2167. begin
  2168. case typ of
  2169. orddef,
  2170. pointerdef,
  2171. enumdef,
  2172. classrefdef:
  2173. is_intregable:=true;
  2174. procvardef :
  2175. is_intregable:=tprocvardef(self).is_addressonly or (po_methodpointer in tprocvardef(self).procoptions);
  2176. objectdef:
  2177. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  2178. setdef:
  2179. is_intregable:=is_smallset(self);
  2180. arraydef:
  2181. {$ifdef cpuhighleveltarget}
  2182. is_intregable:=false
  2183. {$else cpuhighleveltarget}
  2184. is_intregable:=not(is_special_array(self)) and
  2185. (tarraydef(self).size<=8) and (tarraydef(self).size in [1,2,4,8]) and
  2186. tstoreddef(tarraydef(self).elementdef).is_intregable
  2187. {$ifdef SUPPORT_MMX}
  2188. and not((cs_mmx in current_settings.localswitches) and
  2189. is_mmx_able_array(self))
  2190. {$endif SUPPORT_MMX}
  2191. {$endif cpuhighleveltarget}
  2192. ;
  2193. recorddef:
  2194. begin
  2195. {$ifdef cpuhighleveltarget}
  2196. is_intregable:=false;
  2197. {$else cpuhighleveltarget}
  2198. recsize:=size;
  2199. is_intregable:=
  2200. ispowerof2(recsize,temp) and
  2201. ((recsize<=sizeof(aint)*2) and
  2202. not trecorddef(self).contains_cross_aword_field and
  2203. { records cannot go into registers on 16 bit targets for now }
  2204. (sizeof(aint)>2) and
  2205. not trecorddef(self).contains_float_field
  2206. ) and
  2207. not needs_inittable;
  2208. {$endif cpuhighleveltarget}
  2209. end;
  2210. else
  2211. is_intregable:=false;
  2212. end;
  2213. end;
  2214. function tstoreddef.is_fpuregable : boolean;
  2215. begin
  2216. {$ifdef x86}
  2217. result:=use_vectorfpu(self);
  2218. {$else x86}
  2219. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches)
  2220. {$ifdef xtensa}
  2221. and (FPUXTENSA_SINGLE in fpu_capabilities[init_settings.fputype]) and (tfloatdef(self).floattype=s32real)
  2222. {$endif xtensa}
  2223. {$ifdef arm}
  2224. and (((FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[init_settings.fputype]) and (tfloatdef(self).floattype=s32real)) or
  2225. (FPUARM_HAS_VFP_DOUBLE in fpu_capabilities[init_settings.fputype]))
  2226. {$endif arm}
  2227. ;
  2228. {$endif x86}
  2229. end;
  2230. function tstoreddef.is_const_intregable : boolean;
  2231. begin
  2232. case typ of
  2233. stringdef:
  2234. result:=tstringdef(self).stringtype in [st_ansistring,st_unicodestring,st_widestring];
  2235. arraydef:
  2236. result:=is_dynamic_array(self);
  2237. objectdef:
  2238. result:=is_interface(self);
  2239. else
  2240. result:=false;
  2241. end;
  2242. end;
  2243. procedure tstoreddef.initgeneric;
  2244. begin
  2245. if assigned(generictokenbuf) then
  2246. internalerror(200512131);
  2247. generictokenbuf:=tdynamicarray.create(256);
  2248. end;
  2249. function tstoreddef.is_generic: boolean;
  2250. var
  2251. sym: tsym;
  2252. i: longint;
  2253. begin
  2254. result:=assigned(genericparas) and
  2255. (genericparas.count>0) and
  2256. (df_generic in defoptions);
  2257. if result then
  2258. { if any of the type parameters does *not* belong to as (meaning it was passed
  2259. in from outside) then we aren't a generic, but a specialization }
  2260. for i:=0 to genericparas.count-1 do
  2261. begin
  2262. sym:=tsym(genericparas[i]);
  2263. { sym must be either a type or const }
  2264. if not (sym.typ in [symconst.typesym,symconst.constsym]) then
  2265. internalerror(2014050903);
  2266. if sym.owner.defowner<>self then
  2267. exit(false);
  2268. if (sym.typ=symconst.constsym) and (sp_generic_const in sym.symoptions) then
  2269. exit(false);
  2270. end;
  2271. end;
  2272. function tstoreddef.is_generic_param_const(index:integer):boolean;
  2273. begin
  2274. result:=tsym(genericparas[index]).typ=constsym;
  2275. end;
  2276. function tstoreddef.get_generic_param_def(index:integer):tdef;
  2277. begin
  2278. if tsym(genericparas[index]).typ=constsym then
  2279. result:=tconstsym(genericparas[index]).constdef
  2280. else
  2281. result:=ttypesym(genericparas[index]).typedef;
  2282. end;
  2283. function tstoreddef.is_specialization: boolean;
  2284. var
  2285. i : longint;
  2286. sym : tsym;
  2287. begin
  2288. result:=assigned(genericparas) and
  2289. (genericparas.count>0) and
  2290. (df_specialization in defoptions);
  2291. if result then
  2292. begin
  2293. { if at least one of the generic parameters is not owned by us (meaning it was
  2294. passed in from outside) then we have a specialization, otherwise we have a generic }
  2295. for i:=0 to genericparas.count-1 do
  2296. begin
  2297. sym:=tsym(genericparas[i]);
  2298. { sym must be either a type or const }
  2299. if not (sym.typ in [symconst.typesym,symconst.constsym]) then
  2300. internalerror(2014050904);
  2301. if sym.owner.defowner<>self then
  2302. exit(true);
  2303. if (sym.typ=symconst.constsym) and (sp_generic_const in sym.symoptions) then
  2304. exit(true);
  2305. end;
  2306. result:=false;
  2307. end;
  2308. end;
  2309. procedure tstoreddef.register_def;
  2310. var
  2311. gst : tgetsymtable;
  2312. st : tsymtable;
  2313. begin
  2314. if registered then
  2315. exit;
  2316. { Register in current_module }
  2317. if assigned(current_module) then
  2318. begin
  2319. exclude(defoptions,df_not_registered_no_free);
  2320. for gst:=low(tgetsymtable) to high(tgetsymtable) do
  2321. begin
  2322. st:=getsymtable(gst);
  2323. if assigned(st) then
  2324. tstoredsymtable(st).register_children;
  2325. end;
  2326. if defid<defid_not_registered then
  2327. defid:=deflist_index
  2328. else
  2329. begin
  2330. current_module.deflist.Add(self);
  2331. defid:=current_module.deflist.Count-1;
  2332. registered_in_module:=current_module;
  2333. end;
  2334. maybe_put_in_symtable_stack;
  2335. end
  2336. else
  2337. DefId:=defid_registered_nost;
  2338. end;
  2339. procedure tstoreddef.maybe_put_in_symtable_stack;
  2340. var
  2341. insertstack: psymtablestackitem;
  2342. begin
  2343. if assigned(symtablestack) and
  2344. not assigned(self.owner) then
  2345. begin
  2346. insertstack:=symtablestack.stack;
  2347. { don't insert defs in exception symtables, as they are freed before
  2348. the module is compiled, so we can get crashes on high level targets
  2349. if they still need it while e.g. writing assembler code }
  2350. while assigned(insertstack) and
  2351. (insertstack^.symtable.symtabletype in [stt_exceptsymtable,withsymtable]) do
  2352. insertstack:=insertstack^.next;
  2353. if not assigned(insertstack) then
  2354. internalerror(200602044);
  2355. if insertstack^.symtable.sealed then
  2356. internalerror(2015022301);
  2357. insertstack^.symtable.insertdef(self);
  2358. end;
  2359. end;
  2360. {****************************************************************************
  2361. Tstringdef
  2362. ****************************************************************************}
  2363. constructor tstringdef.createshort(l: byte; doregister: boolean);
  2364. begin
  2365. inherited create(stringdef,doregister);
  2366. stringtype:=st_shortstring;
  2367. encoding:=0;
  2368. len:=l;
  2369. end;
  2370. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  2371. begin
  2372. inherited ppuload(stringdef,ppufile);
  2373. stringtype:=st_shortstring;
  2374. encoding:=0;
  2375. len:=ppufile.getbyte;
  2376. ppuload_platform(ppufile);
  2377. end;
  2378. constructor tstringdef.createlong(l: asizeint; doregister: boolean);
  2379. begin
  2380. inherited create(stringdef,doregister);
  2381. stringtype:=st_longstring;
  2382. encoding:=0;
  2383. len:=l;
  2384. end;
  2385. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  2386. begin
  2387. inherited ppuload(stringdef,ppufile);
  2388. stringtype:=st_longstring;
  2389. encoding:=0;
  2390. len:=ppufile.getasizeint;
  2391. ppuload_platform(ppufile);
  2392. end;
  2393. constructor tstringdef.createansi(aencoding: tstringencoding; doregister: boolean);
  2394. begin
  2395. inherited create(stringdef,doregister);
  2396. stringtype:=st_ansistring;
  2397. encoding:=aencoding;
  2398. len:=-1;
  2399. end;
  2400. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  2401. begin
  2402. inherited ppuload(stringdef,ppufile);
  2403. stringtype:=st_ansistring;
  2404. len:=ppufile.getasizeint;
  2405. encoding:=ppufile.getword;
  2406. ppuload_platform(ppufile);
  2407. end;
  2408. constructor tstringdef.createwide(doregister: boolean);
  2409. begin
  2410. inherited create(stringdef,doregister);
  2411. stringtype:=st_widestring;
  2412. if target_info.endian=endian_little then
  2413. encoding:=CP_UTF16LE
  2414. else
  2415. encoding:=CP_UTF16BE;
  2416. len:=-1;
  2417. end;
  2418. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  2419. begin
  2420. inherited ppuload(stringdef,ppufile);
  2421. stringtype:=st_widestring;
  2422. if target_info.endian=endian_little then
  2423. encoding:=CP_UTF16LE
  2424. else
  2425. encoding:=CP_UTF16BE;
  2426. len:=ppufile.getasizeint;
  2427. ppuload_platform(ppufile);
  2428. end;
  2429. constructor tstringdef.createunicode(doregister: boolean);
  2430. begin
  2431. inherited create(stringdef,doregister);
  2432. stringtype:=st_unicodestring;
  2433. if target_info.endian=endian_little then
  2434. encoding:=CP_UTF16LE
  2435. else
  2436. encoding:=CP_UTF16BE;
  2437. len:=-1;
  2438. end;
  2439. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  2440. begin
  2441. inherited ppuload(stringdef,ppufile);
  2442. stringtype:=st_unicodestring;
  2443. len:=ppufile.getasizeint;
  2444. encoding:=ppufile.getword;
  2445. ppuload_platform(ppufile);
  2446. end;
  2447. function tstringdef.getcopy : tstoreddef;
  2448. begin
  2449. result:=cstringdef.create(typ,true);
  2450. result.typ:=stringdef;
  2451. tstringdef(result).stringtype:=stringtype;
  2452. tstringdef(result).encoding:=encoding;
  2453. tstringdef(result).len:=len;
  2454. end;
  2455. function tstringdef.stringtypname:string;
  2456. const
  2457. typname:array[tstringtype] of string[10]=(
  2458. 'shortstr','longstr','ansistr','widestr','unicodestr'
  2459. );
  2460. begin
  2461. stringtypname:=typname[stringtype];
  2462. end;
  2463. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  2464. begin
  2465. inherited ppuwrite(ppufile);
  2466. if stringtype=st_shortstring then
  2467. begin
  2468. {$ifdef extdebug}
  2469. if len > 255 then internalerror(12122002);
  2470. {$endif}
  2471. ppufile.putbyte(byte(len))
  2472. end
  2473. else
  2474. ppufile.putasizeint(len);
  2475. if stringtype in [st_ansistring,st_unicodestring] then
  2476. ppufile.putword(encoding);
  2477. case stringtype of
  2478. st_shortstring : writeentry(ppufile,ibshortstringdef);
  2479. st_longstring : writeentry(ppufile,iblongstringdef);
  2480. st_ansistring : writeentry(ppufile,ibansistringdef);
  2481. st_widestring : writeentry(ppufile,ibwidestringdef);
  2482. st_unicodestring : writeentry(ppufile,ibunicodestringdef);
  2483. end;
  2484. end;
  2485. function tstringdef.needs_inittable : boolean;
  2486. begin
  2487. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  2488. end;
  2489. function tstringdef.GetTypeName : string;
  2490. const
  2491. names : array[tstringtype] of string[15] = (
  2492. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  2493. begin
  2494. GetTypeName:=names[stringtype];
  2495. case stringtype of
  2496. st_ansistring,st_unicodestring:
  2497. GetTypeName:=GetTypeName+'('+tostr(encoding)+')';
  2498. st_shortstring:
  2499. GetTypeName:=GetTypeName+'['+tostr(len)+']';
  2500. else
  2501. ;
  2502. end;
  2503. end;
  2504. function tstringdef.getvardef : longint;
  2505. const
  2506. vardef : array[tstringtype] of longint = (
  2507. varUndefined,varUndefined,varString,varOleStr,varUString);
  2508. begin
  2509. result:=vardef[stringtype];
  2510. end;
  2511. function tstringdef.get_default_char_type: tdef;
  2512. begin
  2513. case stringtype of
  2514. st_shortstring,
  2515. st_longstring,
  2516. st_ansistring:
  2517. result:=cansichartype;
  2518. st_unicodestring,
  2519. st_widestring:
  2520. result:=cwidechartype;
  2521. end;
  2522. end;
  2523. function tstringdef.get_default_string_type: tdef;
  2524. begin
  2525. case stringtype of
  2526. st_shortstring:
  2527. result:=cshortstringtype;
  2528. { st_longstring is currently not supported but
  2529. when it is this case will need to be supplied }
  2530. st_longstring:
  2531. internalerror(2021040801);
  2532. st_ansistring:
  2533. result:=cansistringtype;
  2534. st_widestring:
  2535. result:=cwidestringtype;
  2536. st_unicodestring:
  2537. result:=cunicodestringtype;
  2538. end
  2539. end;
  2540. function tstringdef.alignment : shortint;
  2541. begin
  2542. case stringtype of
  2543. st_unicodestring,
  2544. st_widestring,
  2545. st_ansistring:
  2546. alignment:=voidpointertype.alignment;
  2547. st_longstring,
  2548. st_shortstring:
  2549. { char to string accesses byte 0 and 1 with one word access }
  2550. if (tf_requires_proper_alignment in target_info.flags) or
  2551. { macpas needs an alignment of 2 (MetroWerks compatible) }
  2552. (m_mac in current_settings.modeswitches) then
  2553. alignment:=size_2_align(2)
  2554. else
  2555. alignment:=size_2_align(1);
  2556. end;
  2557. end;
  2558. function tstringdef.getmangledparaname : TSymStr;
  2559. begin
  2560. getmangledparaname:='STRING';
  2561. end;
  2562. function tstringdef.is_publishable : boolean;
  2563. begin
  2564. is_publishable:=true;
  2565. end;
  2566. function tstringdef.size: asizeint;
  2567. begin
  2568. case stringtype of
  2569. st_shortstring:
  2570. Result:=len+1;
  2571. st_longstring,
  2572. st_ansistring,
  2573. st_widestring,
  2574. st_unicodestring:
  2575. Result:=voidpointertype.size;
  2576. end;
  2577. end;
  2578. {****************************************************************************
  2579. TENUMDEF
  2580. ****************************************************************************}
  2581. constructor tenumdef.create;
  2582. begin
  2583. inherited create(enumdef,true);
  2584. minval:=0;
  2585. maxval:=0;
  2586. calcsavesize(current_settings.packenum);
  2587. has_jumps:=false;
  2588. basedef:=nil;
  2589. basedefderef.reset;
  2590. symtable:=tenumsymtable.create(self);
  2591. end;
  2592. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  2593. begin
  2594. inherited create(enumdef,true);
  2595. minval:=_min;
  2596. maxval:=_max;
  2597. basedef:=_basedef;
  2598. calcsavesize(current_settings.packenum);
  2599. has_jumps:=false;
  2600. symtable:=basedef.symtable.getcopy;
  2601. include(defoptions, df_copied_def);
  2602. end;
  2603. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  2604. begin
  2605. inherited ppuload(enumdef,ppufile);
  2606. minval:=ppufile.getaint;
  2607. maxval:=ppufile.getaint;
  2608. savesize:=ppufile.getaint;
  2609. has_jumps:=false;
  2610. if df_copied_def in defoptions then
  2611. begin
  2612. symtable:=nil;
  2613. ppufile.getderef(basedefderef);
  2614. ppuload_platform(ppufile);
  2615. end
  2616. else
  2617. begin
  2618. ppuload_platform(ppufile);
  2619. // create with nil defowner first to prevent values changes on insert
  2620. symtable:=tenumsymtable.create(nil);
  2621. tenumsymtable(symtable).ppuload(ppufile);
  2622. symtable.defowner:=self;
  2623. end;
  2624. end;
  2625. destructor tenumdef.destroy;
  2626. begin
  2627. symtable.free;
  2628. symtable:=nil;
  2629. inherited destroy;
  2630. end;
  2631. function tenumdef.getcopy : tstoreddef;
  2632. begin
  2633. if assigned(basedef) then
  2634. result:=cenumdef.create_subrange(basedef,minval,maxval)
  2635. else
  2636. begin
  2637. result:=cenumdef.create;
  2638. tenumdef(result).minval:=minval;
  2639. tenumdef(result).maxval:=maxval;
  2640. tenumdef(result).symtable.free;
  2641. tenumdef(result).symtable:=symtable.getcopy;
  2642. tenumdef(result).basedef:=self;
  2643. end;
  2644. tenumdef(result).has_jumps:=has_jumps;
  2645. tenumdef(result).basedefderef:=basedefderef;
  2646. include(tenumdef(result).defoptions,df_copied_def);
  2647. end;
  2648. procedure tenumdef.calcsavesize(packenum: shortint);
  2649. begin
  2650. {$IFNDEF cpu64bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2651. if (packenum=8) or (int64(min)<low(longint)) or (int64(max)>high(cardinal)) then
  2652. savesize:=8
  2653. {$IFNDEF cpu64bitaddr} {$pop} {$ENDIF}
  2654. else
  2655. {$IFDEF cpu16bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2656. if (packenum=4) or (min<low(smallint)) or (max>high(word)) then
  2657. savesize:=4
  2658. {$IFDEF cpu16bitaddr} {$pop} {$ENDIF}
  2659. else
  2660. if (packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  2661. savesize:=2
  2662. else
  2663. savesize:=1;
  2664. end;
  2665. function tenumdef.packedbitsize: asizeint;
  2666. var
  2667. sizeval: tconstexprint;
  2668. power: longint;
  2669. begin
  2670. result := 0;
  2671. if (minval >= 0) and
  2672. (maxval <= 1) then
  2673. result := 1
  2674. else
  2675. begin
  2676. if (minval>=0) then
  2677. sizeval:=maxval
  2678. else
  2679. { don't count 0 twice, but take into account that range goes from -n-1..n }
  2680. sizeval:=(cutils.max(-minval,maxval+1)*2)-1;
  2681. { 256 must become 512 etc. }
  2682. nextpowerof2(sizeval+1,power);
  2683. result := power;
  2684. end;
  2685. end;
  2686. procedure tenumdef.setmax(_max:asizeint);
  2687. begin
  2688. maxval:=_max;
  2689. calcsavesize(current_settings.packenum);
  2690. end;
  2691. procedure tenumdef.setmin(_min:asizeint);
  2692. begin
  2693. minval:=_min;
  2694. calcsavesize(current_settings.packenum);
  2695. end;
  2696. function tenumdef.min:asizeint;
  2697. begin
  2698. min:=minval;
  2699. end;
  2700. function tenumdef.max:asizeint;
  2701. begin
  2702. max:=maxval;
  2703. end;
  2704. function tenumdef.getfirstsym: tsym;
  2705. var
  2706. i:integer;
  2707. begin
  2708. for i := 0 to symtable.SymList.Count - 1 do
  2709. begin
  2710. result:=tsym(symtable.SymList[i]);
  2711. if tenumsym(result).value=minval then
  2712. exit;
  2713. end;
  2714. result:=nil;
  2715. end;
  2716. function tenumdef.int2enumsym(l: asizeint): tsym;
  2717. var
  2718. i: longint;
  2719. sym: tsym;
  2720. bdef: tenumdef;
  2721. begin
  2722. result:=nil;
  2723. if (l<minval) or
  2724. (l>maxval) then
  2725. exit;
  2726. bdef:=getbasedef;
  2727. for i:=0 to bdef.symtable.symlist.count-1 do
  2728. begin
  2729. sym:=tsym(bdef.symtable.symlist[i]);
  2730. if (sym.typ=enumsym) and
  2731. (tenumsym(sym).value=l) then
  2732. begin
  2733. result:=sym;
  2734. exit;
  2735. end;
  2736. end;
  2737. end;
  2738. function tenumdef.getbasedef: tenumdef;
  2739. begin
  2740. if not assigned(basedef) then
  2741. result:=self
  2742. else
  2743. result:=basedef;
  2744. end;
  2745. procedure tenumdef.buildderef;
  2746. begin
  2747. inherited buildderef;
  2748. if df_copied_def in defoptions then
  2749. basedefderef.build(basedef)
  2750. else
  2751. tenumsymtable(symtable).buildderef;
  2752. end;
  2753. procedure tenumdef.deref;
  2754. begin
  2755. inherited deref;
  2756. if df_copied_def in defoptions then
  2757. begin
  2758. basedef:=tenumdef(basedefderef.resolve);
  2759. symtable:=basedef.symtable.getcopy;
  2760. end
  2761. else
  2762. tenumsymtable(symtable).deref(false);
  2763. end;
  2764. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  2765. begin
  2766. inherited ppuwrite(ppufile);
  2767. ppufile.putaint(min);
  2768. ppufile.putaint(max);
  2769. ppufile.putaint(savesize);
  2770. if df_copied_def in defoptions then
  2771. ppufile.putderef(basedefderef);
  2772. writeentry(ppufile,ibenumdef);
  2773. if not (df_copied_def in defoptions) then
  2774. tenumsymtable(symtable).ppuwrite(ppufile);
  2775. end;
  2776. function tenumdef.is_publishable : boolean;
  2777. begin
  2778. is_publishable:=true;
  2779. end;
  2780. function tenumdef.GetTypeName : string;
  2781. begin
  2782. GetTypeName:='<enumeration type>';
  2783. end;
  2784. {****************************************************************************
  2785. TRTTI_ATTRIBUTE_LIST
  2786. ****************************************************************************}
  2787. constructor trtti_attribute.ppuload(ppufile: tcompilerppufile);
  2788. begin
  2789. ppufile.getderef(typesymderef);
  2790. ppufile.getderef(typeconstrderef);
  2791. setlength(paras,ppufile.getlongint);
  2792. end;
  2793. procedure trtti_attribute.ppuwrite(ppufile: tcompilerppufile);
  2794. begin
  2795. ppufile.putderef(typesymderef);
  2796. ppufile.putderef(typeconstrderef);
  2797. ppufile.putlongint(length(paras));
  2798. end;
  2799. procedure trtti_attribute.ppuload_subentries(ppufile: tcompilerppufile);
  2800. var
  2801. i : sizeint;
  2802. begin
  2803. for i:=0 to high(paras) do
  2804. paras[i]:=ppuloadnodetree(ppufile);
  2805. end;
  2806. procedure trtti_attribute.ppuwrite_subentries(ppufile: tcompilerppufile);
  2807. var
  2808. i : sizeint;
  2809. begin
  2810. for i:=0 to high(paras) do
  2811. ppuwritenodetree(ppufile,paras[i]);
  2812. end;
  2813. destructor trtti_attribute.destroy;
  2814. var
  2815. n : tnode;
  2816. begin
  2817. constructorcall.free;
  2818. for n in paras do
  2819. n.free;
  2820. inherited destroy;
  2821. end;
  2822. procedure trtti_attribute.buildderef;
  2823. var
  2824. i : sizeint;
  2825. begin
  2826. typesymderef.build(typesym);
  2827. typeconstrderef.build(typeconstr);
  2828. for i:=0 to high(paras) do
  2829. paras[i].buildderefimpl;
  2830. end;
  2831. procedure trtti_attribute.deref;
  2832. var
  2833. i : sizeint;
  2834. begin
  2835. typesym:=tsym(typesymderef.resolve);
  2836. typeconstr:=tdef(typeconstrderef.resolve);
  2837. for i:=0 to high(paras) do
  2838. paras[i].derefimpl;
  2839. end;
  2840. class procedure trtti_attribute_list.bind(var dangling,owned:trtti_attribute_list);
  2841. begin
  2842. if assigned(owned) then
  2843. internalerror(2019071001);
  2844. if not assigned(dangling) then
  2845. exit;
  2846. if dangling.is_bound then
  2847. internalerror(2019071002);
  2848. current_module.used_rtti_attrs.concatlistcopy(dangling.rtti_attributes);
  2849. dangling.is_bound:=true;
  2850. owned:=dangling;
  2851. dangling:=nil;
  2852. end;
  2853. procedure trtti_attribute_list.addattribute(atypesym:tsym;typeconstr:tdef;constructorcall:tnode;constref paras:array of tnode);
  2854. var
  2855. newattribute : trtti_attribute;
  2856. i : sizeint;
  2857. begin
  2858. if not assigned(rtti_attributes) then
  2859. rtti_attributes:=TFPObjectList.Create(true);
  2860. newattribute:=trtti_attribute.Create;
  2861. newattribute.typesym:=atypesym;
  2862. newattribute.typeconstr:=typeconstr;
  2863. newattribute.constructorcall:=constructorcall;
  2864. setlength(newattribute.paras,length(paras));
  2865. for i:=0 to high(paras) do
  2866. newattribute.paras[i]:=paras[i];
  2867. rtti_attributes.Add(newattribute);
  2868. end;
  2869. procedure trtti_attribute_list.addattribute(attr:trtti_attribute);
  2870. begin
  2871. if not assigned(rtti_attributes) then
  2872. rtti_attributes:=TFPObjectList.Create(true);
  2873. rtti_attributes.add(attr);
  2874. end;
  2875. destructor trtti_attribute_list.destroy;
  2876. begin
  2877. rtti_attributes.Free;
  2878. inherited destroy;
  2879. end;
  2880. function trtti_attribute_list.get_attribute_count:longint;
  2881. begin
  2882. if assigned(rtti_attributes) then
  2883. result:=rtti_attributes.Count
  2884. else
  2885. result:=0;
  2886. end;
  2887. procedure trtti_attribute_list.buildderef;
  2888. var
  2889. i : sizeint;
  2890. begin
  2891. if not assigned(rtti_attributes) then
  2892. exit;
  2893. for i:=0 to rtti_attributes.count-1 do
  2894. trtti_attribute(rtti_attributes[i]).buildderef;
  2895. end;
  2896. procedure trtti_attribute_list.deref;
  2897. var
  2898. i : sizeint;
  2899. begin
  2900. if not assigned(rtti_attributes) then
  2901. exit;
  2902. for i:=0 to rtti_attributes.count-1 do
  2903. trtti_attribute(rtti_attributes[i]).deref;
  2904. end;
  2905. class procedure trtti_attribute_list.ppuload_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  2906. var
  2907. i : sizeint;
  2908. begin
  2909. if assigned(attrlist) then
  2910. begin
  2911. if not assigned(attrlist.rtti_attributes) then
  2912. internalerror(2019071101);
  2913. for i:=0 to attrlist.rtti_attributes.count-1 do
  2914. trtti_attribute(attrlist.rtti_attributes[i]).ppuload_subentries(ppufile);
  2915. end;
  2916. end;
  2917. class procedure trtti_attribute_list.ppuwrite_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  2918. var
  2919. i : sizeint;
  2920. begin
  2921. if assigned(attrlist) and assigned(attrlist.rtti_attributes) then
  2922. begin
  2923. for i:=0 to attrlist.rtti_attributes.count-1 do
  2924. trtti_attribute(attrlist.rtti_attributes[i]).ppuwrite_subentries(ppufile);
  2925. end;
  2926. end;
  2927. class function trtti_attribute_list.ppuload(ppufile:tcompilerppufile):trtti_attribute_list;
  2928. var
  2929. cnt,i : longint;
  2930. begin
  2931. cnt:=ppufile.getlongint;
  2932. if cnt>0 then
  2933. begin
  2934. result:=trtti_attribute_list.create;
  2935. for i:=0 to cnt-1 do
  2936. result.addattribute(trtti_attribute.ppuload(ppufile));
  2937. end
  2938. else
  2939. result:=nil;
  2940. end;
  2941. class procedure trtti_attribute_list.ppuwrite(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  2942. var
  2943. i : longint;
  2944. begin
  2945. if assigned(attrlist) and assigned(attrlist.rtti_attributes) then
  2946. begin
  2947. ppufile.putlongint(attrlist.rtti_attributes.count);
  2948. for i:=0 to attrlist.rtti_attributes.count-1 do
  2949. trtti_attribute(attrlist.rtti_attributes[i]).ppuwrite(ppufile);
  2950. end
  2951. else
  2952. ppufile.putlongint(0);
  2953. end;
  2954. {****************************************************************************
  2955. TORDDEF
  2956. ****************************************************************************}
  2957. constructor torddef.create(t : tordtype;v,b : TConstExprInt; doregister: boolean);
  2958. begin
  2959. inherited create(orddef,doregister);
  2960. low:=v;
  2961. high:=b;
  2962. ordtype:=t;
  2963. setsize;
  2964. end;
  2965. constructor torddef.ppuload(ppufile:tcompilerppufile);
  2966. begin
  2967. inherited ppuload(orddef,ppufile);
  2968. ordtype:=tordtype(ppufile.getbyte);
  2969. low:=ppufile.getexprint;
  2970. high:=ppufile.getexprint;
  2971. setsize;
  2972. ppuload_platform(ppufile);
  2973. end;
  2974. function torddef.getcopy : tstoreddef;
  2975. begin
  2976. result:=corddef.create(ordtype,low,high,true);
  2977. result.typ:=orddef;
  2978. torddef(result).low:=low;
  2979. torddef(result).high:=high;
  2980. torddef(result).ordtype:=ordtype;
  2981. torddef(result).savesize:=savesize;
  2982. end;
  2983. function torddef.alignment:shortint;
  2984. begin
  2985. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_ios]) and
  2986. (ordtype in [s64bit,u64bit]) then
  2987. result := 4
  2988. else
  2989. result := inherited alignment;
  2990. end;
  2991. procedure torddef.setsize;
  2992. const
  2993. sizetbl : array[tordtype] of longint = (
  2994. 0,
  2995. 1,2,4,8,16,
  2996. 1,2,4,8,16,
  2997. 1,1,2,4,8,
  2998. 1,2,4,8,
  2999. 1,2,8,system.high(longint)
  3000. );
  3001. begin
  3002. savesize:=sizetbl[ordtype];
  3003. if savesize=system.high(longint) then
  3004. savesize:=packedbitsize div 8;
  3005. end;
  3006. function torddef.packedbitsize: asizeint;
  3007. var
  3008. sizeval: tconstexprint;
  3009. power: longint;
  3010. begin
  3011. result := 0;
  3012. if ordtype = uvoid then
  3013. exit;
  3014. {$ifndef cpu64bitalu}
  3015. if (ordtype in [s64bit,u64bit]) then
  3016. {$else not cpu64bitalu}
  3017. if ((ordtype = u64bit) and
  3018. (high > system.high(int64))) or
  3019. ((ordtype = s64bit) and
  3020. ((low <= (system.low(int64) div 2)) or
  3021. ((low < 0) and
  3022. (high > (system.high(int64) div 2))))) then
  3023. {$endif cpu64bitalu}
  3024. result := 64
  3025. else if (
  3026. (low >= 0) and
  3027. (high <= 1)
  3028. ) or (
  3029. ordtype in [pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]
  3030. ) then
  3031. result := 1
  3032. else
  3033. begin
  3034. if (low>=0) then
  3035. sizeval:=high
  3036. else
  3037. { don't count 0 twice, but take into account that range goes from -n-1..n }
  3038. sizeval:=(cutils.max(-low,high+1)*2)-1;
  3039. { 256 must become 512 etc. }
  3040. nextpowerof2(sizeval+1,power);
  3041. result := power;
  3042. end;
  3043. end;
  3044. function torddef.getvardef : longint;
  3045. const
  3046. basetype2vardef : array[tordtype] of longint = (
  3047. varUndefined,
  3048. varbyte,varword,varlongword,varqword,varUndefined,
  3049. varshortint,varsmallint,varinteger,varint64,varUndefined,
  3050. varboolean,varboolean,varboolean,varboolean,varboolean,
  3051. varboolean,varboolean,varUndefined,varUndefined,
  3052. varUndefined,varUndefined,varCurrency,varEmpty);
  3053. begin
  3054. result:=basetype2vardef[ordtype];
  3055. if result=varEmpty then
  3056. result:=basetype2vardef[range_to_basetype(low,high)];
  3057. end;
  3058. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  3059. begin
  3060. inherited ppuwrite(ppufile);
  3061. ppufile.putbyte(byte(ordtype));
  3062. ppufile.putexprint(low);
  3063. ppufile.putexprint(high);
  3064. writeentry(ppufile,iborddef);
  3065. end;
  3066. function torddef.is_publishable : boolean;
  3067. begin
  3068. is_publishable:=(ordtype<>uvoid);
  3069. end;
  3070. function torddef.GetTypeName : string;
  3071. const
  3072. names : array[tordtype] of string[20] = (
  3073. 'untyped',
  3074. 'Byte','Word','DWord','QWord','UInt128',
  3075. 'ShortInt','SmallInt','LongInt','Int64','Int128',
  3076. 'Boolean','Boolean8','Boolean16','Boolean32','Boolean64',
  3077. 'ByteBool','WordBool','LongBool','QWordBool',
  3078. 'Char','WideChar','Currency','CustomRange');
  3079. begin
  3080. GetTypeName:=names[ordtype];
  3081. end;
  3082. {****************************************************************************
  3083. TFLOATDEF
  3084. ****************************************************************************}
  3085. constructor tfloatdef.create(t: tfloattype; doregister: boolean);
  3086. begin
  3087. inherited create(floatdef,doregister);
  3088. floattype:=t;
  3089. setsize;
  3090. end;
  3091. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  3092. begin
  3093. inherited ppuload(floatdef,ppufile);
  3094. floattype:=tfloattype(ppufile.getbyte);
  3095. setsize;
  3096. ppuload_platform(ppufile);
  3097. end;
  3098. function tfloatdef.getcopy : tstoreddef;
  3099. begin
  3100. result:=cfloatdef.create(floattype,true);
  3101. result.typ:=floatdef;
  3102. tfloatdef(result).savesize:=savesize;
  3103. end;
  3104. function tfloatdef.alignment:shortint;
  3105. begin
  3106. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_ios]) then
  3107. case floattype of
  3108. sc80real,
  3109. s80real: result:=16;
  3110. s64real,
  3111. s64currency,
  3112. s64comp : result:=4;
  3113. else
  3114. result := inherited alignment;
  3115. end
  3116. else
  3117. result := inherited alignment;
  3118. end;
  3119. function tfloatdef.structalignment: shortint;
  3120. begin
  3121. { aix is really annoying: the recommended scalar alignment for both
  3122. int64 and double is 64 bits, but in structs int64 has to be aligned
  3123. to 8 bytes and double to 4 bytes }
  3124. if (target_info.system in systems_aix) and
  3125. (floattype=s64real) then
  3126. result:=4
  3127. else
  3128. result:=alignment;
  3129. end;
  3130. procedure tfloatdef.setsize;
  3131. begin
  3132. case floattype of
  3133. s32real : savesize:=4;
  3134. s80real : savesize:=10;
  3135. sc80real:
  3136. if target_info.system in [system_i386_darwin,
  3137. system_i386_iphonesim,system_x86_64_darwin,
  3138. system_x86_64_iphonesim,
  3139. system_x86_64_linux,system_x86_64_freebsd,
  3140. system_x86_64_openbsd,system_x86_64_netbsd,
  3141. system_x86_64_solaris,system_x86_64_embedded,
  3142. system_x86_64_dragonfly,system_x86_64_haiku] then
  3143. savesize:=16
  3144. else
  3145. savesize:=12;
  3146. s64real,
  3147. s64currency,
  3148. s64comp : savesize:=8;
  3149. else
  3150. savesize:=0;
  3151. end;
  3152. end;
  3153. function tfloatdef.getvardef : longint;
  3154. const
  3155. floattype2vardef : array[tfloattype] of longint = (
  3156. varSingle,varDouble,varUndefined,varUndefined,
  3157. varUndefined,varCurrency,varUndefined);
  3158. begin
  3159. if (upper(typename)='TDATETIME') and
  3160. assigned(owner) and
  3161. assigned(owner.name) and
  3162. (owner.name^='SYSTEM') then
  3163. result:=varDate
  3164. else
  3165. result:=floattype2vardef[floattype];
  3166. end;
  3167. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  3168. begin
  3169. inherited ppuwrite(ppufile);
  3170. ppufile.putbyte(byte(floattype));
  3171. writeentry(ppufile,ibfloatdef);
  3172. end;
  3173. function tfloatdef.is_publishable : boolean;
  3174. begin
  3175. is_publishable:=true;
  3176. end;
  3177. function tfloatdef.GetTypeName : string;
  3178. const
  3179. names : array[tfloattype] of string[20] = (
  3180. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  3181. begin
  3182. GetTypeName:=names[floattype];
  3183. end;
  3184. {****************************************************************************
  3185. TFILEDEF
  3186. ****************************************************************************}
  3187. constructor tfiledef.createtext;
  3188. begin
  3189. inherited create(filedef,true);
  3190. filetyp:=ft_text;
  3191. typedfiledef:=nil;
  3192. typedfiledefderef.reset;
  3193. end;
  3194. constructor tfiledef.createuntyped;
  3195. begin
  3196. inherited create(filedef,true);
  3197. filetyp:=ft_untyped;
  3198. typedfiledef:=nil;
  3199. end;
  3200. constructor tfiledef.createtyped(def:tdef);
  3201. begin
  3202. inherited create(filedef,true);
  3203. filetyp:=ft_typed;
  3204. typedfiledef:=def;
  3205. end;
  3206. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  3207. begin
  3208. inherited ppuload(filedef,ppufile);
  3209. filetyp:=tfiletyp(ppufile.getbyte);
  3210. if filetyp=ft_typed then
  3211. ppufile.getderef(typedfiledefderef)
  3212. else
  3213. typedfiledef:=nil;
  3214. ppuload_platform(ppufile);
  3215. end;
  3216. function tfiledef.getcopy : tstoreddef;
  3217. begin
  3218. case filetyp of
  3219. ft_typed:
  3220. result:=cfiledef.createtyped(typedfiledef);
  3221. ft_untyped:
  3222. result:=cfiledef.createuntyped;
  3223. ft_text:
  3224. result:=cfiledef.createtext;
  3225. end;
  3226. end;
  3227. procedure tfiledef.buildderef;
  3228. begin
  3229. inherited buildderef;
  3230. if filetyp=ft_typed then
  3231. typedfiledefderef.build(typedfiledef);
  3232. end;
  3233. procedure tfiledef.deref;
  3234. begin
  3235. inherited deref;
  3236. if filetyp=ft_typed then
  3237. typedfiledef:=tdef(typedfiledefderef.resolve);
  3238. end;
  3239. function tfiledef.size:asizeint;
  3240. begin
  3241. if savesize=0 then
  3242. setsize;
  3243. size:=savesize;
  3244. end;
  3245. procedure tfiledef.setsize;
  3246. begin
  3247. case filetyp of
  3248. ft_text:
  3249. savesize:=search_system_type('TEXTREC').typedef.size;
  3250. ft_typed:
  3251. begin
  3252. savesize:=search_system_type('FILEREC').typedef.size;
  3253. { allocate put/get buffer in iso mode }
  3254. if m_isolike_io in current_settings.modeswitches then
  3255. inc(savesize,typedfiledef.size);
  3256. end;
  3257. ft_untyped:
  3258. savesize:=search_system_type('FILEREC').typedef.size;
  3259. end;
  3260. end;
  3261. function tfiledef.alignment: shortint;
  3262. begin
  3263. case filetyp of
  3264. ft_text:
  3265. result:=search_system_type('TEXTREC').typedef.alignment;
  3266. ft_typed,
  3267. ft_untyped:
  3268. result:=search_system_type('FILEREC').typedef.alignment;
  3269. end;
  3270. end;
  3271. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  3272. begin
  3273. inherited ppuwrite(ppufile);
  3274. ppufile.putbyte(byte(filetyp));
  3275. if filetyp=ft_typed then
  3276. ppufile.putderef(typedfiledefderef);
  3277. writeentry(ppufile,ibfiledef);
  3278. end;
  3279. function tfiledef.GetTypeName : string;
  3280. begin
  3281. case filetyp of
  3282. ft_untyped:
  3283. GetTypeName:='File';
  3284. ft_typed:
  3285. GetTypeName:='File Of '+typedfiledef.typename;
  3286. ft_text:
  3287. GetTypeName:='Text'
  3288. end;
  3289. end;
  3290. function tfiledef.getmangledparaname : TSymStr;
  3291. begin
  3292. case filetyp of
  3293. ft_untyped:
  3294. getmangledparaname:='FILE';
  3295. ft_typed:
  3296. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  3297. ft_text:
  3298. getmangledparaname:='TEXT'
  3299. end;
  3300. end;
  3301. {****************************************************************************
  3302. TVARIANTDEF
  3303. ****************************************************************************}
  3304. constructor tvariantdef.create(v : tvarianttype);
  3305. begin
  3306. inherited create(variantdef,true);
  3307. varianttype:=v;
  3308. setsize;
  3309. end;
  3310. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  3311. begin
  3312. inherited ppuload(variantdef,ppufile);
  3313. varianttype:=tvarianttype(ppufile.getbyte);
  3314. setsize;
  3315. ppuload_platform(ppufile);
  3316. end;
  3317. function tvariantdef.getcopy : tstoreddef;
  3318. begin
  3319. result:=cvariantdef.create(varianttype);
  3320. end;
  3321. function tvariantdef.alignment: shortint;
  3322. begin
  3323. result:=search_system_type('TVARDATA').typedef.alignment;
  3324. end;
  3325. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  3326. begin
  3327. inherited ppuwrite(ppufile);
  3328. ppufile.putbyte(byte(varianttype));
  3329. writeentry(ppufile,ibvariantdef);
  3330. end;
  3331. function tvariantdef.getvardef : longint;
  3332. begin
  3333. Result:=varVariant;
  3334. end;
  3335. procedure tvariantdef.setsize;
  3336. begin
  3337. {$ifdef cpu64bitaddr}
  3338. savesize:=24;
  3339. {$else cpu64bitaddr}
  3340. savesize:=16;
  3341. {$endif cpu64bitaddr}
  3342. end;
  3343. function tvariantdef.GetTypeName : string;
  3344. begin
  3345. case varianttype of
  3346. vt_normalvariant:
  3347. GetTypeName:='Variant';
  3348. vt_olevariant:
  3349. GetTypeName:='OleVariant';
  3350. end;
  3351. end;
  3352. function tvariantdef.needs_inittable : boolean;
  3353. begin
  3354. needs_inittable:=true;
  3355. end;
  3356. function tvariantdef.is_publishable : boolean;
  3357. begin
  3358. is_publishable:=true;
  3359. end;
  3360. {****************************************************************************
  3361. TABSTRACtpointerdef
  3362. ****************************************************************************}
  3363. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  3364. begin
  3365. inherited create(dt,true);
  3366. pointeddef:=def;
  3367. pointeddefderef.reset;
  3368. if df_generic in pointeddef.defoptions then
  3369. include(defoptions,df_generic);
  3370. if df_specialization in pointeddef.defoptions then
  3371. include(defoptions,df_specialization);
  3372. end;
  3373. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3374. begin
  3375. inherited ppuload(dt,ppufile);
  3376. ppufile.getderef(pointeddefderef);
  3377. end;
  3378. procedure tabstractpointerdef.buildderef;
  3379. begin
  3380. inherited buildderef;
  3381. pointeddefderef.build(pointeddef);
  3382. end;
  3383. procedure tabstractpointerdef.deref;
  3384. begin
  3385. inherited deref;
  3386. pointeddef:=tdef(pointeddefderef.resolve);
  3387. end;
  3388. function tabstractpointerdef.size: asizeint;
  3389. begin
  3390. Result:=voidpointertype.size;
  3391. end;
  3392. function tabstractpointerdef.alignment: shortint;
  3393. begin
  3394. alignment:=size_2_align(voidpointertype.size);
  3395. end;
  3396. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  3397. begin
  3398. inherited ppuwrite(ppufile);
  3399. ppufile.putderef(pointeddefderef);
  3400. end;
  3401. {****************************************************************************
  3402. tpointerdef
  3403. ****************************************************************************}
  3404. constructor tpointerdef.create(def:tdef);
  3405. begin
  3406. inherited create(pointerdef,def);
  3407. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  3408. if (df_specialization in tstoreddef(def).defoptions)
  3409. {$ifndef genericdef_for_nested}
  3410. { currently, nested procdefs of generic routines get df_specialization,
  3411. but no genericdef }
  3412. and assigned(tstoreddef(def).genericdef)
  3413. {$endif}
  3414. then
  3415. genericdef:=cpointerdef.getreusable(tstoreddef(def).genericdef);
  3416. end;
  3417. class function tpointerdef.getreusable(def: tdef): tpointerdef;
  3418. var
  3419. res: PHashSetItem;
  3420. oldsymtablestack: tsymtablestack;
  3421. begin
  3422. if not assigned(current_module) then
  3423. internalerror(2011071101);
  3424. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  3425. if not assigned(res^.Data) then
  3426. begin
  3427. { since these pointerdefs can be reused anywhere in the current
  3428. unit, add them to the global/staticsymtable (or local symtable
  3429. if they're a local def, because otherwise they'll be saved
  3430. to the ppu referencing a local symtable entry that doesn't
  3431. exist in the ppu) }
  3432. oldsymtablestack:=symtablestack;
  3433. { do not simply push/pop current_module.localsymtable, because
  3434. that can have side-effects (e.g., it removes helpers) }
  3435. symtablestack:=nil;
  3436. result:=cpointerdef.create(def);
  3437. setup_reusable_def(def,result,res,oldsymtablestack);
  3438. { res^.Data may still be nil -> don't overwrite result }
  3439. exit;
  3440. end;
  3441. result:=tpointerdef(res^.Data);
  3442. end;
  3443. class function tpointerdef.getreusable_no_free(def: tdef): tpointerdef;
  3444. begin
  3445. result:=getreusable(def);
  3446. if not result.is_registered then
  3447. include(result.defoptions,df_not_registered_no_free);
  3448. end;
  3449. function tpointerdef.size: asizeint;
  3450. begin
  3451. result:=sizeof(pint);
  3452. end;
  3453. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  3454. begin
  3455. inherited ppuload(pointerdef,ppufile);
  3456. has_pointer_math:=(ppufile.getbyte<>0);
  3457. ppuload_platform(ppufile);
  3458. end;
  3459. function tpointerdef.getcopy : tstoreddef;
  3460. begin
  3461. { don't use direct pointeddef if it is a forwarddef because in other case
  3462. one of them will be destroyed on forward type resolve and the second will
  3463. point to garbage }
  3464. if pointeddef.typ=forwarddef then
  3465. result:=cpointerdef.create(tforwarddef(pointeddef).getcopy)
  3466. else
  3467. result:=cpointerdef.create(pointeddef);
  3468. tpointerdef(result).has_pointer_math:=has_pointer_math;
  3469. tpointerdef(result).savesize:=savesize;
  3470. end;
  3471. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  3472. begin
  3473. inherited ppuwrite(ppufile);
  3474. ppufile.putbyte(byte(has_pointer_math));
  3475. writeentry(ppufile,ibpointerdef);
  3476. end;
  3477. function tpointerdef.GetTypeName : string;
  3478. begin
  3479. { parameter types and the resultdef of a procvardef can contain a
  3480. pointer to this procvardef itself, resulting in endless recursion ->
  3481. use the typesym's name instead if it exists (if it doesn't, such as
  3482. for anynonymous procedure types in macpas/iso mode, then there cannot
  3483. be any recursive references to it either) }
  3484. if (pointeddef.typ<>procvardef) or
  3485. not assigned(pointeddef.typesym) then
  3486. GetTypeName:='^'+pointeddef.typename
  3487. else
  3488. GetTypeName:='^'+pointeddef.typesym.realname;
  3489. end;
  3490. function tpointerdef.pointer_arithmetic_int_type:tdef;
  3491. begin
  3492. result:=ptrsinttype;
  3493. end;
  3494. function tpointerdef.pointer_arithmetic_uint_type:tdef;
  3495. begin
  3496. result:=ptruinttype;
  3497. end;
  3498. function tpointerdef.pointer_subtraction_result_type:tdef;
  3499. begin
  3500. result:=ptrsinttype;
  3501. end;
  3502. function tpointerdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  3503. begin
  3504. result:=true;
  3505. end;
  3506. function tpointerdef.converted_pointer_to_array_range_type:tdef;
  3507. begin
  3508. result:=ptrsinttype;
  3509. end;
  3510. {****************************************************************************
  3511. TCLASSREFDEF
  3512. ****************************************************************************}
  3513. constructor tclassrefdef.create(def:tdef);
  3514. begin
  3515. while (def.typ=objectdef) and tobjectdef(def).is_unique_objpasdef do
  3516. def:=tobjectdef(def).childof;
  3517. inherited create(classrefdef,def);
  3518. if df_specialization in tstoreddef(def).defoptions then
  3519. genericdef:=cclassrefdef.create(tstoreddef(def).genericdef);
  3520. end;
  3521. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  3522. begin
  3523. inherited ppuload(classrefdef,ppufile);
  3524. ppuload_platform(ppufile);
  3525. end;
  3526. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  3527. begin
  3528. inherited ppuwrite(ppufile);
  3529. writeentry(ppufile,ibclassrefdef);
  3530. end;
  3531. function tclassrefdef.getcopy:tstoreddef;
  3532. begin
  3533. if pointeddef.typ=forwarddef then
  3534. result:=cclassrefdef.create(tforwarddef(pointeddef).getcopy)
  3535. else
  3536. result:=cclassrefdef.create(pointeddef);
  3537. end;
  3538. function tclassrefdef.GetTypeName : string;
  3539. begin
  3540. GetTypeName:='Class Of '+pointeddef.typename;
  3541. end;
  3542. function tclassrefdef.is_publishable : boolean;
  3543. begin
  3544. result:=true;
  3545. end;
  3546. function tclassrefdef.rtti_mangledname(rt: trttitype): TSymStr;
  3547. begin
  3548. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  3549. result:=inherited rtti_mangledname(rt)
  3550. else
  3551. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  3552. end;
  3553. procedure tclassrefdef.register_created_object_type;
  3554. begin
  3555. tobjectdef(pointeddef).register_created_classref_type;
  3556. end;
  3557. {***************************************************************************
  3558. TSETDEF
  3559. ***************************************************************************}
  3560. constructor tsetdef.create(def: tdef; low, high: asizeint; doregister: boolean);
  3561. var
  3562. setallocbits: aint;
  3563. packedsavesize: aint;
  3564. actual_setalloc: ShortInt;
  3565. begin
  3566. inherited create(setdef,doregister);
  3567. elementdef:=def;
  3568. elementdefderef.reset;
  3569. setmax:=high;
  3570. actual_setalloc:=current_settings.setalloc;
  3571. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  3572. if actual_setalloc=0 then
  3573. actual_setalloc:=1;
  3574. {$endif}
  3575. if (actual_setalloc=0) then
  3576. begin
  3577. setbase:=0;
  3578. if (high<32) then
  3579. savesize:=Sizeof(longint)
  3580. else if (high<256) then
  3581. savesize:=32
  3582. else
  3583. savesize:=(high+7) div 8
  3584. end
  3585. else
  3586. begin
  3587. setallocbits:=actual_setalloc*8;
  3588. setbase:=low and not(setallocbits-1);
  3589. packedsavesize:=actual_setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  3590. savesize:=packedsavesize;
  3591. {$if not defined(cpu8bitalu) and not defined(cpu16bitalu)}
  3592. if savesize=3 then
  3593. savesize:=4;
  3594. {$endif}
  3595. end;
  3596. end;
  3597. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  3598. begin
  3599. inherited ppuload(setdef,ppufile);
  3600. ppufile.getderef(elementdefderef);
  3601. savesize:=ppufile.getasizeint;
  3602. setbase:=ppufile.getasizeint;
  3603. setmax:=ppufile.getasizeint;
  3604. ppuload_platform(ppufile);
  3605. end;
  3606. function tsetdef.getcopy : tstoreddef;
  3607. begin
  3608. result:=csetdef.create(elementdef,setbase,setmax,true);
  3609. { the copy might have been created with a different setalloc setting }
  3610. tsetdef(result).savesize:=savesize;
  3611. end;
  3612. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  3613. begin
  3614. inherited ppuwrite(ppufile);
  3615. ppufile.putderef(elementdefderef);
  3616. ppufile.putasizeint(savesize);
  3617. ppufile.putasizeint(setbase);
  3618. ppufile.putasizeint(setmax);
  3619. writeentry(ppufile,ibsetdef);
  3620. end;
  3621. procedure tsetdef.buildderef;
  3622. begin
  3623. inherited buildderef;
  3624. elementdefderef.build(elementdef);
  3625. end;
  3626. procedure tsetdef.deref;
  3627. begin
  3628. inherited deref;
  3629. elementdef:=tdef(elementdefderef.resolve);
  3630. end;
  3631. function tsetdef.is_publishable : boolean;
  3632. begin
  3633. is_publishable:=savesize in [1,2,4];
  3634. end;
  3635. function tsetdef.alignment: shortint;
  3636. begin
  3637. Result:=inherited;
  3638. if result>sizeof(aint) then
  3639. result:=sizeof(aint);
  3640. end;
  3641. function tsetdef.GetTypeName : string;
  3642. begin
  3643. if assigned(elementdef) then
  3644. GetTypeName:='Set Of '+elementdef.typename
  3645. else
  3646. GetTypeName:='Empty Set';
  3647. end;
  3648. {***************************************************************************
  3649. TFORMALDEF
  3650. ***************************************************************************}
  3651. constructor tformaldef.create(Atyped:boolean);
  3652. begin
  3653. inherited create(formaldef,true);
  3654. typed:=Atyped;
  3655. savesize:=0;
  3656. end;
  3657. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  3658. begin
  3659. inherited ppuload(formaldef,ppufile);
  3660. typed:=boolean(ppufile.getbyte);
  3661. savesize:=0;
  3662. ppuload_platform(ppufile);
  3663. end;
  3664. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  3665. begin
  3666. inherited ppuwrite(ppufile);
  3667. ppufile.putbyte(byte(typed));
  3668. writeentry(ppufile,ibformaldef);
  3669. end;
  3670. function tformaldef.GetTypeName : string;
  3671. begin
  3672. if typed then
  3673. GetTypeName:='<Typed formal type>'
  3674. else
  3675. GetTypeName:='<Formal type>';
  3676. end;
  3677. {***************************************************************************
  3678. TARRAYDEF
  3679. ***************************************************************************}
  3680. constructor tarraydef.create(l, h: asizeint; def: tdef);
  3681. begin
  3682. inherited create(arraydef,true);
  3683. lowrange:=l;
  3684. highrange:=h;
  3685. rangedef:=def;
  3686. rangedefderef.reset;
  3687. _elementdef:=nil;
  3688. _elementdefderef.reset;
  3689. arrayoptions:=[];
  3690. symtable:=tarraysymtable.create(self);
  3691. end;
  3692. constructor tarraydef.create_vector(l ,h: asizeint; def: tdef);
  3693. begin
  3694. self.create(l,h,def);
  3695. include(arrayoptions,ado_IsVector);
  3696. end;
  3697. constructor tarraydef.create_openarray;
  3698. begin
  3699. self.create(0,-1,sizesinttype);
  3700. include(arrayoptions,ado_OpenArray);
  3701. end;
  3702. class function tarraydef.getreusable_intern(def: tdef; elems: asizeint; const options: tarraydefoptions): tarraydef;
  3703. var
  3704. res: PHashSetItem;
  3705. oldsymtablestack: tsymtablestack;
  3706. arrdesc: packed record
  3707. def: tdef;
  3708. elecount: asizeint;
  3709. options: tarraydefoptions
  3710. end;
  3711. begin
  3712. if not assigned(current_module) then
  3713. internalerror(2011081301);
  3714. arrdesc.def:=def;
  3715. arrdesc.elecount:=elems;
  3716. arrdesc.options:=options;
  3717. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  3718. if not assigned(res^.Data) then
  3719. begin
  3720. { since these pointerdefs can be reused anywhere in the current
  3721. unit, add them to the global/staticsymtable (or local symtable
  3722. if they're a local def, because otherwise they'll be saved
  3723. to the ppu referencing a local symtable entry that doesn't
  3724. exist in the ppu) }
  3725. oldsymtablestack:=symtablestack;
  3726. { do not simply push/pop current_module.localsymtable, because
  3727. that can have side-effects (e.g., it removes helpers) }
  3728. symtablestack:=nil;
  3729. result:=carraydef.create(0,elems-1,sizesinttype);
  3730. result.elementdef:=def;
  3731. result.arrayoptions:=options;
  3732. setup_reusable_def(def,result,res,oldsymtablestack);
  3733. { res^.Data may still be nil -> don't overwrite result }
  3734. exit;
  3735. end;
  3736. result:=tarraydef(res^.Data);
  3737. end;
  3738. class function tarraydef.getreusable(def: tdef; elems: asizeint): tarraydef;
  3739. begin
  3740. result:=getreusable_intern(def,elems,[]);
  3741. end;
  3742. class function tarraydef.getreusable_vector(def: tdef; elems: asizeint): tarraydef;
  3743. begin
  3744. result:=getreusable_intern(def,elems,[ado_IsVector]);
  3745. end;
  3746. class function tarraydef.getreusable_no_free(def: tdef; elems: asizeint): tarraydef;
  3747. begin
  3748. result:=getreusable(def,elems);
  3749. if not result.is_registered then
  3750. include(result.defoptions,df_not_registered_no_free);
  3751. end;
  3752. class function tarraydef.getreusable_no_free_vector(def: tdef; elems: asizeint): tarraydef;
  3753. begin
  3754. result:=getreusable_vector(def,elems);
  3755. if not result.is_registered then
  3756. include(result.defoptions,df_not_registered_no_free);
  3757. end;
  3758. destructor tarraydef.destroy;
  3759. begin
  3760. symtable.free;
  3761. symtable:=nil;
  3762. inherited;
  3763. end;
  3764. constructor tarraydef.create_from_pointer(def:tpointerdef);
  3765. begin
  3766. { divide by the element size and do -1 so the array will have a valid size,
  3767. further, the element size might be 0 e.g. for empty records, so use max(...,1)
  3768. to avoid a division by zero }
  3769. self.create(0,(high(asizeint) div max(def.pointeddef.size,1))-1,
  3770. def.converted_pointer_to_array_range_type);
  3771. arrayoptions:=[ado_IsConvertedPointer];
  3772. setelementdef(def.pointeddef);
  3773. end;
  3774. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  3775. begin
  3776. inherited ppuload(arraydef,ppufile);
  3777. { the addresses are calculated later }
  3778. ppufile.getderef(_elementdefderef);
  3779. ppufile.getderef(rangedefderef);
  3780. lowrange:=ppufile.getasizeint;
  3781. highrange:=ppufile.getasizeint;
  3782. ppufile.getset(tppuset2(arrayoptions));
  3783. ppuload_platform(ppufile);
  3784. symtable:=tarraysymtable.create(self);
  3785. tarraysymtable(symtable).ppuload(ppufile)
  3786. end;
  3787. function tarraydef.getcopy : tstoreddef;
  3788. begin
  3789. result:=carraydef.create(lowrange,highrange,rangedef);
  3790. tarraydef(result).arrayoptions:=arrayoptions;
  3791. tarraydef(result)._elementdef:=_elementdef;
  3792. end;
  3793. procedure tarraydef.buildderef;
  3794. begin
  3795. inherited buildderef;
  3796. tarraysymtable(symtable).buildderef;
  3797. _elementdefderef.build(_elementdef);
  3798. rangedefderef.build(rangedef);
  3799. end;
  3800. procedure tarraydef.deref;
  3801. begin
  3802. inherited deref;
  3803. tarraysymtable(symtable).deref(false);
  3804. _elementdef:=tdef(_elementdefderef.resolve);
  3805. rangedef:=tdef(rangedefderef.resolve);
  3806. end;
  3807. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  3808. begin
  3809. inherited ppuwrite(ppufile);
  3810. ppufile.putderef(_elementdefderef);
  3811. ppufile.putderef(rangedefderef);
  3812. ppufile.putasizeint(lowrange);
  3813. ppufile.putasizeint(highrange);
  3814. ppufile.putset(tppuset2(arrayoptions));
  3815. writeentry(ppufile,ibarraydef);
  3816. tarraysymtable(symtable).ppuwrite(ppufile);
  3817. end;
  3818. function tarraydef.elesize : asizeint;
  3819. begin
  3820. if (ado_IsBitPacked in arrayoptions) then
  3821. internalerror(2006080101);
  3822. if assigned(_elementdef) then
  3823. result:=_elementdef.size
  3824. else
  3825. result:=0;
  3826. end;
  3827. function tarraydef.elepackedbitsize : asizeint;
  3828. begin
  3829. if not(ado_IsBitPacked in arrayoptions) then
  3830. internalerror(2006080102);
  3831. if assigned(_elementdef) then
  3832. result:=_elementdef.packedbitsize
  3833. else
  3834. result:=0;
  3835. end;
  3836. function tarraydef.elecount : asizeuint;
  3837. var
  3838. qhigh,qlow : qword;
  3839. begin
  3840. if ado_IsDynamicArray in arrayoptions then
  3841. begin
  3842. result:=0;
  3843. exit;
  3844. end;
  3845. if (highrange>0) and (lowrange<0) then
  3846. begin
  3847. qhigh:=highrange;
  3848. if lowrange=low(asizeint) then
  3849. qlow:=high(asizeint) + 1
  3850. else
  3851. qlow:=qword(-lowrange);
  3852. { prevent overflow, return 0 to indicate overflow }
  3853. if qhigh+qlow>qword(high(asizeint)-1) then
  3854. result:=0
  3855. else
  3856. result:=qhigh+qlow+1;
  3857. end
  3858. else
  3859. result:=int64(highrange)-lowrange+1;
  3860. end;
  3861. function tarraydef.size : asizeint;
  3862. var
  3863. cachedelecount : asizeuint;
  3864. cachedelesize : asizeint;
  3865. begin
  3866. if ado_IsDynamicArray in arrayoptions then
  3867. begin
  3868. size:=voidpointertype.size;
  3869. exit;
  3870. end;
  3871. { Tarraydef.size may never be called for an open array! }
  3872. if ado_OpenArray in arrayoptions then
  3873. internalerror(99080501);
  3874. if not (ado_IsBitPacked in arrayoptions) then
  3875. cachedelesize:=elesize
  3876. else
  3877. cachedelesize := elepackedbitsize;
  3878. cachedelecount:=elecount;
  3879. if (cachedelesize = 0) then
  3880. begin
  3881. size := 0;
  3882. exit;
  3883. end;
  3884. if (cachedelecount = 0) then
  3885. begin
  3886. if ado_isconststring in arrayoptions then
  3887. size := 0
  3888. else
  3889. size := -1;
  3890. exit;
  3891. end;
  3892. { prevent overflow, return -1 to indicate overflow }
  3893. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  3894. if (cachedelecount > asizeuint(high(asizeint))) or
  3895. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  3896. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  3897. accessing the array, see ncgmem (PFV) }
  3898. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  3899. begin
  3900. result:=-1;
  3901. exit;
  3902. end;
  3903. result:=cachedelesize*asizeint(cachedelecount);
  3904. if (ado_IsBitPacked in arrayoptions) then
  3905. { can't just add 7 and divide by 8, because that may overflow }
  3906. result:=result div 8 + ord((result mod 8)<>0);
  3907. {$ifdef cpu16bitaddr}
  3908. if result>65535 then
  3909. begin
  3910. result:=-1;
  3911. exit;
  3912. end;
  3913. {$endif cpu16bitaddr}
  3914. end;
  3915. procedure tarraydef.setelementdef(def:tdef);
  3916. begin
  3917. _elementdef:=def;
  3918. if not(
  3919. (ado_IsDynamicArray in arrayoptions) or
  3920. (ado_IsConvertedPointer in arrayoptions) or
  3921. (ado_IsConstructor in arrayoptions) or
  3922. (ado_IsGeneric in arrayoptions) or
  3923. (highrange<lowrange)
  3924. ) and
  3925. (size=-1) then
  3926. Message(sym_e_segment_too_large);
  3927. end;
  3928. function tarraydef.alignment : shortint;
  3929. begin
  3930. if ado_IsVector in arrayoptions then
  3931. alignment:=size
  3932. { alignment of dyn. arrays doesn't depend on the element size }
  3933. else if ado_IsDynamicArray in arrayoptions then
  3934. alignment:=voidpointertype.alignment
  3935. { alignment is the target alignment for the used load size }
  3936. else if (ado_IsBitPacked in arrayoptions) and
  3937. (elementdef.typ in [enumdef,orddef]) then
  3938. alignment:=cgsize_orddef(int_cgsize(packedbitsloadsize(elepackedbitsize))).alignment
  3939. { alignment is the alignment of the elements }
  3940. else
  3941. alignment:=elementdef.alignment
  3942. end;
  3943. function tarraydef.needs_inittable : boolean;
  3944. begin
  3945. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  3946. end;
  3947. function tarraydef.needs_separate_initrtti : boolean;
  3948. begin
  3949. if ado_IsBitPacked in arrayoptions then
  3950. result:=false
  3951. else
  3952. result:=elementdef.needs_separate_initrtti;
  3953. end;
  3954. function tarraydef.GetTypeName : string;
  3955. begin
  3956. if (ado_IsConstString in arrayoptions) then
  3957. result:='Constant String'
  3958. else if (ado_isarrayofconst in arrayoptions) or
  3959. (ado_isConstructor in arrayoptions) then
  3960. begin
  3961. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  3962. GetTypeName:='Array Of Const'
  3963. else
  3964. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  3965. end
  3966. else if (ado_IsDynamicArray in arrayoptions) then
  3967. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  3968. else if (ado_OpenArray in arrayoptions) then
  3969. GetTypeName:='{Open} Array Of '+elementdef.typename
  3970. else
  3971. begin
  3972. result := '';
  3973. if (ado_IsBitPacked in arrayoptions) then
  3974. result:='BitPacked ';
  3975. if rangedef.typ=enumdef then
  3976. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  3977. else
  3978. result:=result+'Array['+tostr(lowrange)+'..'+
  3979. tostr(highrange)+'] Of '+elementdef.typename
  3980. end;
  3981. end;
  3982. function tarraydef.getmangledparaname : TSymStr;
  3983. begin
  3984. if ado_isarrayofconst in arrayoptions then
  3985. getmangledparaname:='array_of_const'
  3986. else
  3987. if ((highrange=-1) and (lowrange=0)) then
  3988. getmangledparaname:='array_of_'+elementdef.mangledparaname
  3989. else
  3990. internalerror(200204176);
  3991. end;
  3992. function tarraydef.is_publishable : boolean;
  3993. begin
  3994. Result:=ado_IsDynamicArray in arrayoptions;
  3995. end;
  3996. function tarraydef.is_hwvector: boolean;
  3997. begin
  3998. result:=ado_IsVector in arrayoptions;
  3999. end;
  4000. {***************************************************************************
  4001. tabstractrecorddef
  4002. ***************************************************************************}
  4003. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp;doregister:boolean);
  4004. begin
  4005. inherited create(dt,doregister);
  4006. objname:=stringdup(upper(n));
  4007. objrealname:=stringdup(n);
  4008. objectoptions:=[];
  4009. if assigned(current_module.namespace) then
  4010. begin
  4011. import_lib:=stringdup(current_module.namespace^);
  4012. replace(import_lib^,'.','/');
  4013. end;
  4014. end;
  4015. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  4016. begin
  4017. inherited ppuload(dt,ppufile);
  4018. objrealname:=ppufile.getpshortstring;
  4019. objname:=stringdup(upper(objrealname^));
  4020. import_lib:=ppufile.getpshortstring;
  4021. { only used for external C++ classes and Java classes/records }
  4022. if (import_lib^='') then
  4023. stringdispose(import_lib);
  4024. ppufile.getset(tppuset4(objectoptions));
  4025. end;
  4026. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  4027. begin
  4028. inherited ppuwrite(ppufile);
  4029. ppufile.putstring(objrealname^);
  4030. if assigned(import_lib) then
  4031. ppufile.putstring(import_lib^)
  4032. else
  4033. ppufile.putstring('');
  4034. ppufile.putset(tppuset4(objectoptions));
  4035. end;
  4036. destructor tabstractrecorddef.destroy;
  4037. begin
  4038. stringdispose(objname);
  4039. stringdispose(objrealname);
  4040. stringdispose(import_lib);
  4041. tcinitcode.free;
  4042. inherited destroy;
  4043. end;
  4044. procedure tabstractrecorddef.buildderefimpl;
  4045. begin
  4046. inherited buildderefimpl;
  4047. if not (df_copied_def in defoptions) then
  4048. tstoredsymtable(symtable).buildderefimpl;
  4049. end;
  4050. procedure tabstractrecorddef.derefimpl;
  4051. begin
  4052. inherited derefimpl;
  4053. if not (df_copied_def in defoptions) then
  4054. tstoredsymtable(symtable).derefimpl(false);
  4055. end;
  4056. procedure tabstractrecorddef.check_forwards;
  4057. begin
  4058. { the defs of a copied def are defined for the original type only }
  4059. if not(df_copied_def in defoptions) then
  4060. tstoredsymtable(symtable).check_forwards;
  4061. end;
  4062. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  4063. var
  4064. i: longint;
  4065. sym: tsym;
  4066. begin
  4067. for i:=0 to symtable.SymList.Count-1 do
  4068. begin
  4069. sym:=tsym(symtable.SymList[i]);
  4070. if sym.typ=procsym then
  4071. begin
  4072. result:=tprocsym(sym).find_procdef_bytype(pt);
  4073. if assigned(result) then
  4074. exit;
  4075. end;
  4076. end;
  4077. result:=nil;
  4078. end;
  4079. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  4080. begin
  4081. if t=gs_record then
  4082. GetSymtable:=symtable
  4083. else
  4084. GetSymtable:=nil;
  4085. end;
  4086. function tabstractrecorddef.is_packed:boolean;
  4087. begin
  4088. result:=tabstractrecordsymtable(symtable).is_packed;
  4089. end;
  4090. function tabstractrecorddef.RttiName: string;
  4091. function generate_full_paramname(maxlength:longint):string;
  4092. const
  4093. commacount : array[boolean] of longint = (0,1);
  4094. var
  4095. fullparas,
  4096. paramname : ansistring;
  4097. module : tmodule;
  4098. sym : tsym;
  4099. def : tdef;
  4100. i : longint;
  4101. begin
  4102. { we want at least enough space for an ellipsis }
  4103. if maxlength<3 then
  4104. internalerror(2014121203);
  4105. fullparas:='';
  4106. for i:=0 to genericparas.count-1 do
  4107. begin
  4108. sym:=tsym(genericparas[i]);
  4109. module:=find_module_from_symtable(sym.owner);
  4110. if not assigned(module) then
  4111. internalerror(2014121202);
  4112. if not (sym.typ in [constsym,symconst.typesym]) then
  4113. internalerror(2020042501);
  4114. if sym.typ=constsym then
  4115. def:=tconstsym(sym).constdef
  4116. else
  4117. def:=ttypesym(sym).typedef;
  4118. paramname:=module.realmodulename^;
  4119. if def.typ in [objectdef,recorddef] then
  4120. paramname:=paramname+'.'+tabstractrecorddef(def).rttiname
  4121. else
  4122. paramname:=paramname+'.'+def.typename;
  4123. if length(fullparas)+commacount[i>0]+length(paramname)>maxlength then
  4124. begin
  4125. if i>0 then
  4126. fullparas:=fullparas+',...'
  4127. else
  4128. fullparas:=fullparas+'...';
  4129. break;
  4130. end;
  4131. { could we fit an ellipsis after this parameter if it should be too long? }
  4132. if (maxlength-(length(fullparas)+commacount[i>0]+length(paramname))<4) and (i<genericparas.count-1) then
  4133. begin
  4134. { then omit already this parameter }
  4135. if i>0 then
  4136. fullparas:=fullparas+',...'
  4137. else
  4138. fullparas:=fullparas+'...';
  4139. break;
  4140. end;
  4141. if i>0 then
  4142. fullparas:=fullparas+',';
  4143. fullparas:=fullparas+paramname;
  4144. end;
  4145. result:=fullparas;
  4146. end;
  4147. var
  4148. nongeneric,
  4149. basename : string;
  4150. i,
  4151. remlength,
  4152. paramcount,
  4153. crcidx : longint;
  4154. begin
  4155. if rttistring='' then
  4156. begin
  4157. if is_specialization then
  4158. begin
  4159. rttistring:=OwnerHierarchyName;
  4160. { there should be two $ characters, one before the CRC and one before the count }
  4161. crcidx:=-1;
  4162. for i:=length(objrealname^) downto 1 do
  4163. if objrealname^[i]='$' then
  4164. begin
  4165. crcidx:=i;
  4166. break;
  4167. end;
  4168. if crcidx<0 then
  4169. internalerror(2014121201);
  4170. basename:=copy(objrealname^,1,crcidx-1);
  4171. split_generic_name(basename,nongeneric,paramcount);
  4172. rttistring:=rttistring+nongeneric+'<';
  4173. remlength:=255-length(rttistring)-1;
  4174. if remlength<4 then
  4175. rttistring:=rttistring+'>'
  4176. else
  4177. rttistring:=rttistring+generate_full_paramname(remlength)+'>';
  4178. end
  4179. else
  4180. if is_generic then
  4181. begin
  4182. rttistring:=OwnerHierarchyName;
  4183. split_generic_name(objrealname^,nongeneric,paramcount);
  4184. rttistring:=rttistring+nongeneric+'<';
  4185. { we don't want any ',' if there is only one parameter }
  4186. for i:=0 to paramcount-0 do
  4187. rttistring:=rttistring+',';
  4188. rttistring:=rttistring+'>';
  4189. end
  4190. else
  4191. rttistring:=OwnerHierarchyName+objrealname^;
  4192. end;
  4193. result:=rttistring;
  4194. end;
  4195. function tabstractrecorddef.search_enumerator_get: tprocdef;
  4196. var
  4197. sym : tsym;
  4198. i : integer;
  4199. pd : tprocdef;
  4200. hashedid : THashedIDString;
  4201. begin
  4202. result:=nil;
  4203. hashedid.id:='GETENUMERATOR';
  4204. sym:=tsym(symtable.FindWithHash(hashedid));
  4205. if assigned(sym) and (sym.typ=procsym) then
  4206. begin
  4207. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  4208. begin
  4209. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4210. if (pd.proctypeoption = potype_function) and
  4211. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  4212. (pd.visibility >= vis_public) then
  4213. begin
  4214. result:=pd;
  4215. exit;
  4216. end;
  4217. end;
  4218. end;
  4219. end;
  4220. function tabstractrecorddef.search_enumerator_move: tprocdef;
  4221. var
  4222. sym : tsym;
  4223. i : integer;
  4224. pd : tprocdef;
  4225. hashedid : THashedIDString;
  4226. begin
  4227. result:=nil;
  4228. // first search for po_enumerator_movenext method modifier
  4229. // then search for public function MoveNext: Boolean
  4230. for i:=0 to symtable.SymList.Count-1 do
  4231. begin
  4232. sym:=TSym(symtable.SymList[i]);
  4233. if (sym.typ=procsym) then
  4234. begin
  4235. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  4236. if assigned(pd) then
  4237. begin
  4238. result:=pd;
  4239. exit;
  4240. end;
  4241. end;
  4242. end;
  4243. hashedid.id:='MOVENEXT';
  4244. sym:=tsym(symtable.FindWithHash(hashedid));
  4245. if assigned(sym) and (sym.typ=procsym) then
  4246. begin
  4247. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  4248. begin
  4249. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4250. if (pd.proctypeoption = potype_function) and
  4251. is_boolean(pd.returndef) and
  4252. (pd.minparacount = 0) and
  4253. (pd.visibility >= vis_public) then
  4254. begin
  4255. result:=pd;
  4256. exit;
  4257. end;
  4258. end;
  4259. end;
  4260. end;
  4261. function tabstractrecorddef.search_enumerator_current: tsym;
  4262. var
  4263. sym: tsym;
  4264. i: integer;
  4265. hashedid : THashedIDString;
  4266. begin
  4267. result:=nil;
  4268. // first search for ppo_enumerator_current property modifier
  4269. // then search for public property Current
  4270. for i:=0 to symtable.SymList.Count-1 do
  4271. begin
  4272. sym:=TSym(symtable.SymList[i]);
  4273. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  4274. begin
  4275. result:=sym;
  4276. exit;
  4277. end;
  4278. end;
  4279. hashedid.id:='CURRENT';
  4280. sym:=tsym(symtable.FindWithHash(hashedid));
  4281. if assigned(sym) and (sym.typ=propertysym) and
  4282. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  4283. begin
  4284. result:=sym;
  4285. exit;
  4286. end;
  4287. end;
  4288. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  4289. var
  4290. st: tsymtable;
  4291. enclosingdef: tdef;
  4292. begin
  4293. if typ=objectdef then
  4294. result:=tobjectdef(self).objextname^
  4295. else if assigned(typesym) then
  4296. result:=typesym.realname
  4297. { have to generate anonymous nested type in current unit/class/record }
  4298. else
  4299. internalerror(2011032601);
  4300. { in case of specializations, add some extras to prevent name conflicts
  4301. with nested classes }
  4302. if df_specialization in defoptions then
  4303. result:='$'+result+'$specialization$';
  4304. st:=owner;
  4305. while assigned(st) and
  4306. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  4307. begin
  4308. { nested classes are named as "OuterClass$InnerClass" }
  4309. enclosingdef:=tdef(st.defowner);
  4310. if enclosingdef.typ=procdef then
  4311. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  4312. else if enclosingdef.typ=objectdef then
  4313. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  4314. else if assigned(enclosingdef.typesym) then
  4315. result:=enclosingdef.typesym.realname+'$'+result
  4316. else
  4317. internalerror(2011060305);
  4318. st:=enclosingdef.owner;
  4319. end;
  4320. if with_package_name and
  4321. assigned(import_lib) then
  4322. result:=import_lib^+'/'+result;
  4323. end;
  4324. function tabstractrecorddef.contains_float_field: boolean;
  4325. var
  4326. i : longint;
  4327. begin
  4328. result:=true;
  4329. for i:=0 to symtable.symlist.count-1 do
  4330. begin
  4331. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  4332. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  4333. continue;
  4334. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  4335. begin
  4336. if tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).is_fpuregable then
  4337. exit;
  4338. { search recursively }
  4339. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  4340. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_float_field) then
  4341. exit;
  4342. end;
  4343. end;
  4344. result:=false;
  4345. end;
  4346. function tabstractrecorddef.contains_cross_aword_field: boolean;
  4347. var
  4348. i : longint;
  4349. foffset, fsize: aword;
  4350. begin
  4351. result:=true;
  4352. for i:=0 to symtable.symlist.count-1 do
  4353. begin
  4354. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  4355. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  4356. continue;
  4357. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  4358. begin
  4359. if is_packed then
  4360. begin
  4361. foffset:=tfieldvarsym(symtable.symlist[i]).fieldoffset;
  4362. fsize:=tfieldvarsym(symtable.symlist[i]).vardef.packedbitsize;
  4363. end
  4364. else
  4365. begin
  4366. foffset:=tfieldvarsym(symtable.symlist[i]).fieldoffset*8;
  4367. fsize:=tfieldvarsym(symtable.symlist[i]).vardef.size*8;
  4368. end;
  4369. if (fsize>0) and ((foffset div (sizeof(aword)*8)) <> ((foffset+fsize-1) div (sizeof(aword)*8))) then
  4370. exit;
  4371. { search recursively }
  4372. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  4373. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_cross_aword_field) then
  4374. exit;
  4375. end;
  4376. end;
  4377. result:=false;
  4378. end;
  4379. {$ifdef DEBUG_NODE_XML}
  4380. procedure tabstractrecorddef.XMLPrintDefData(var T: Text; Sym: TSym);
  4381. procedure WriteSymOptions(SourceSym: TSym);
  4382. var
  4383. i: TSymOption;
  4384. first: Boolean;
  4385. begin
  4386. First := True;
  4387. for i := Low(TSymOption) to High(TSymOption) do
  4388. if i in SourceSym.symoptions then
  4389. begin
  4390. if First then
  4391. begin
  4392. Write(T, '" symoptions="', i);
  4393. First := False;
  4394. end
  4395. else
  4396. Write(T, ',', i)
  4397. end;
  4398. end;
  4399. var
  4400. List: TFPHashObjectList;
  4401. i: Integer;
  4402. begin
  4403. WriteLn(T, PrintNodeIndention, '<size>', size, '</size>');
  4404. if (alignment = structalignment) and (alignment = aggregatealignment) then
  4405. begin
  4406. { Straightforward and simple }
  4407. WriteLn(T, PrintNodeIndention, '<alignment>', alignment, '</alignment>');
  4408. end
  4409. else
  4410. begin
  4411. WriteLn(T, PrintNodeIndention, '<alignment>');
  4412. printnodeindent;
  4413. WriteLn(T, PrintNodeIndention, '<basic>', alignment, '</basic>');
  4414. if (structalignment <> alignment) then
  4415. WriteLn(T, PrintNodeIndention, '<struct>', structalignment, '</struct>');
  4416. if (aggregatealignment <> alignment) and (aggregatealignment <> structalignment) then
  4417. WriteLn(T, PrintNodeIndention, '<aggregate>', aggregatealignment, '</aggregate>');
  4418. printnodeunindent;
  4419. WriteLn(T, PrintNodeIndention, '</alignment>');
  4420. end;
  4421. { List the fields }
  4422. List := Symtable.SymList;
  4423. for i := 0 to List.Count - 1 do
  4424. case TSym(List[i]).typ of
  4425. { staticvarsym,localvarsym,paravarsym,fieldvarsym,
  4426. typesym,procsym,unitsym,}
  4427. constsym:
  4428. with TConstSym(List[i]) do
  4429. begin
  4430. Write(T, PrintNodeIndention, '<const name="', RealName, '" pos="', fileinfo.line, ',', fileinfo.column);
  4431. WriteSymOptions(TSym(List[i]));
  4432. WriteLn(T, '">');
  4433. PrintNodeIndent;
  4434. XMLPrintConstData(T);
  4435. PrintNodeUnindent;
  4436. WriteLn(T, PrintNodeIndention, '</const>');
  4437. end;
  4438. {
  4439. errorsym,syssym,labelsym,absolutevarsym,propertysym,
  4440. macrosym,namespacesym,undefinedsym,programparasym
  4441. }
  4442. fieldvarsym:
  4443. with TFieldVarSym(List[i]) do
  4444. begin
  4445. Write(T, PrintNodeIndention, '<field name="', RealName, '" pos="', fileinfo.line, ',', fileinfo.column);
  4446. WriteSymOptions(TSym(List[i]));
  4447. WriteLn(T, '">');
  4448. PrintNodeIndent;
  4449. XMLPrintFieldData(T);
  4450. PrintNodeUnindent;
  4451. WriteLn(T, PrintNodeIndention, '</field>');
  4452. end;
  4453. else
  4454. ;
  4455. end;
  4456. end;
  4457. {$endif DEBUG_NODE_XML}
  4458. {***************************************************************************
  4459. trecorddef
  4460. ***************************************************************************}
  4461. constructor trecorddef.create(const n:string; p:TSymtable);
  4462. begin
  4463. inherited create(n,recorddef,true);
  4464. symtable:=p;
  4465. { we can own the symtable only if nobody else owns a copy so far }
  4466. if symtable.refcount=1 then
  4467. symtable.defowner:=self;
  4468. isunion:=false;
  4469. cloneddefderef.reset;
  4470. end;
  4471. constructor trecorddef.create_global_internal(const n: string; packrecords, recordalignmin: shortint);
  4472. var
  4473. name : string;
  4474. pname : pshortstring;
  4475. oldsymtablestack: tsymtablestack;
  4476. where : tsymtable;
  4477. ts: ttypesym;
  4478. begin
  4479. { construct name }
  4480. if n<>'' then
  4481. pname:=@n
  4482. else
  4483. begin
  4484. init_defid;
  4485. name:='$InternalRec'+unique_id_str;
  4486. pname:=@name;
  4487. end;
  4488. oldsymtablestack:=symtablestack;
  4489. { do not simply push/pop current_module.localsymtable, because
  4490. that can have side-effects (e.g., it removes helpers) }
  4491. symtablestack:=nil;
  4492. symtable:=trecordsymtable.create(pname^,packrecords,recordalignmin);
  4493. symtable.defowner:=self;
  4494. isunion:=false;
  4495. inherited create(pname^,recorddef,true);
  4496. where:=current_module.localsymtable;
  4497. if not assigned(where) then
  4498. where:=current_module.globalsymtable;
  4499. where.insertdef(self);
  4500. { if we specified a name, then we'll probably want to look up the
  4501. type again by name too -> create typesym }
  4502. if n<>'' then
  4503. begin
  4504. ts:=ctypesym.create(n,self);
  4505. { avoid hints about unused types (these may only be used for
  4506. typed constant data) }
  4507. ts.increfcount;
  4508. where.insert(ts);
  4509. end;
  4510. symtablestack:=oldsymtablestack;
  4511. { don't create RTTI for internal types, these are not exported }
  4512. defstates:=defstates+[ds_rtti_table_written,ds_init_table_written];
  4513. include(defoptions,df_internal);
  4514. end;
  4515. function trecorddef.add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
  4516. var
  4517. sym: tfieldvarsym;
  4518. name: TIDString;
  4519. pname: ^TIDString;
  4520. begin
  4521. if optionalname='' then
  4522. begin
  4523. name:='$f'+tostr(trecordsymtable(symtable).symlist.count);
  4524. pname:=@name
  4525. end
  4526. else
  4527. pname:=@optionalname;
  4528. sym:=cfieldvarsym.create(pname^,vs_value,def,[]);
  4529. symtable.insert(sym);
  4530. trecordsymtable(symtable).addfield(sym,vis_hidden);
  4531. result:=sym;
  4532. end;
  4533. procedure trecorddef.add_fields_from_deflist(fieldtypes: tfplist);
  4534. var
  4535. i: longint;
  4536. begin
  4537. for i:=0 to fieldtypes.count-1 do
  4538. add_field_by_def('',tdef(fieldtypes[i]));
  4539. end;
  4540. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  4541. procedure readvariantrecdesc(var variantrecdesc : pvariantrecdesc);
  4542. var
  4543. i,j : longint;
  4544. begin
  4545. if ppufile.getbyte=1 then
  4546. begin
  4547. new(variantrecdesc);
  4548. ppufile.getderef(variantrecdesc^.variantselectorderef);
  4549. SetLength(variantrecdesc^.branches,ppufile.getasizeint);
  4550. for i:=0 to high(variantrecdesc^.branches) do
  4551. begin
  4552. SetLength(variantrecdesc^.branches[i].values,ppufile.getasizeint);
  4553. for j:=0 to high(variantrecdesc^.branches[i].values) do
  4554. variantrecdesc^.branches[i].values[j]:=ppufile.getexprint;
  4555. readvariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4556. end;
  4557. end
  4558. else
  4559. variantrecdesc:=nil;
  4560. end;
  4561. begin
  4562. inherited ppuload(recorddef,ppufile);
  4563. if df_copied_def in defoptions then
  4564. begin
  4565. ppufile.getderef(cloneddefderef);
  4566. ppuload_platform(ppufile);
  4567. end
  4568. else
  4569. begin
  4570. symtable:=trecordsymtable.create(objrealname^,0,0);
  4571. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  4572. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  4573. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  4574. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  4575. trecordsymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  4576. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  4577. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  4578. ppufile.getset(tppuset1(trecordsymtable(symtable).managementoperators));
  4579. { position of ppuload_platform call must correspond
  4580. to position of writeentry in ppuwrite method }
  4581. ppuload_platform(ppufile);
  4582. trecordsymtable(symtable).ppuload(ppufile);
  4583. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  4584. but because iso mode supports no units, there is no need to store the variantrecdesc
  4585. in the ppu
  4586. }
  4587. // readvariantrecdesc(variantrecdesc);
  4588. { requires usefieldalignment to be set }
  4589. symtable.defowner:=self;
  4590. end;
  4591. isunion:=false;
  4592. end;
  4593. destructor trecorddef.destroy;
  4594. procedure free_variantrecdesc(var variantrecdesc : pvariantrecdesc);
  4595. var
  4596. i : longint;
  4597. begin
  4598. while assigned(variantrecdesc) do
  4599. begin
  4600. for i:=0 to high(variantrecdesc^.branches) do
  4601. begin
  4602. free_variantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4603. SetLength(variantrecdesc^.branches[i].values,0);
  4604. end;
  4605. SetLength(variantrecdesc^.branches,0);
  4606. dispose(variantrecdesc);
  4607. variantrecdesc:=nil;
  4608. end;
  4609. end;
  4610. begin
  4611. if assigned(variantrecdesc) then
  4612. free_variantrecdesc(variantrecdesc);
  4613. if assigned(symtable) then
  4614. begin
  4615. symtable.free;
  4616. symtable:=nil;
  4617. end;
  4618. inherited destroy;
  4619. end;
  4620. function trecorddef.getcopy : tstoreddef;
  4621. begin
  4622. result:=crecorddef.create(objrealname^,symtable.getcopy);
  4623. trecorddef(result).isunion:=isunion;
  4624. include(trecorddef(result).defoptions,df_copied_def);
  4625. if assigned(tcinitcode) then
  4626. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  4627. if assigned(import_lib) then
  4628. trecorddef(result).import_lib:=stringdup(import_lib^);
  4629. end;
  4630. function trecorddef.needs_inittable : boolean;
  4631. begin
  4632. { each record with managed field or with any management operator needs
  4633. init table }
  4634. needs_inittable:=(trecordsymtable(symtable).managementoperators<>[]) or
  4635. trecordsymtable(symtable).needs_init_final
  4636. end;
  4637. function trecorddef.needs_separate_initrtti : boolean;
  4638. begin
  4639. result:=true;
  4640. end;
  4641. function trecorddef.has_non_trivial_init_child(check_parent:boolean):boolean;
  4642. begin
  4643. result:=trecordsymtable(symtable).has_non_trivial_init;
  4644. end;
  4645. procedure trecorddef.buildderef;
  4646. begin
  4647. inherited buildderef;
  4648. if df_copied_def in defoptions then
  4649. cloneddefderef.build(symtable.defowner)
  4650. else
  4651. tstoredsymtable(symtable).buildderef;
  4652. end;
  4653. procedure trecorddef.deref;
  4654. begin
  4655. inherited deref;
  4656. { now dereference the definitions }
  4657. if df_copied_def in defoptions then
  4658. begin
  4659. cloneddef:=trecorddef(cloneddefderef.resolve);
  4660. symtable:=cloneddef.symtable.getcopy;
  4661. end
  4662. else
  4663. tstoredsymtable(symtable).deref(false);
  4664. { internal types, only load from the system unit }
  4665. if assigned(owner) and
  4666. assigned(owner.name) and
  4667. (owner.name^='SYSTEM') then
  4668. begin
  4669. { TGUID }
  4670. if not assigned(rec_tguid) and
  4671. (upper(typename)='TGUID') then
  4672. rec_tguid:=self
  4673. { JMP_BUF }
  4674. else if not assigned(rec_jmp_buf) and
  4675. (upper(typename)='JMP_BUF') then
  4676. rec_jmp_buf:=self
  4677. else if not assigned(rec_exceptaddr) and
  4678. (upper(typename)='TEXCEPTADDR') then
  4679. rec_exceptaddr:=self;
  4680. end;
  4681. end;
  4682. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  4683. procedure writevariantrecdesc(variantrecdesc : pvariantrecdesc);
  4684. var
  4685. i,j : longint;
  4686. begin
  4687. if assigned(variantrecdesc) then
  4688. begin
  4689. ppufile.putbyte(1);
  4690. ppufile.putderef(variantrecdesc^.variantselectorderef);
  4691. ppufile.putasizeint(length(variantrecdesc^.branches));
  4692. for i:=0 to high(variantrecdesc^.branches) do
  4693. begin
  4694. ppufile.putasizeint(length(variantrecdesc^.branches[i].values));
  4695. for j:=0 to high(variantrecdesc^.branches[i].values) do
  4696. ppufile.putexprint(variantrecdesc^.branches[i].values[j]);
  4697. writevariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4698. end;
  4699. end
  4700. else
  4701. ppufile.putbyte(0);
  4702. end;
  4703. begin
  4704. inherited ppuwrite(ppufile);
  4705. if df_copied_def in defoptions then
  4706. ppufile.putderef(cloneddefderef)
  4707. else
  4708. begin
  4709. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  4710. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  4711. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  4712. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  4713. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignmin));
  4714. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  4715. ppufile.putword(trecordsymtable(symtable).paddingsize);
  4716. ppufile.putset(tppuset1(trecordsymtable(symtable).managementoperators));
  4717. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  4718. but because iso mode supports no units, there is no need to store the variantrecdesc
  4719. in the ppu
  4720. }
  4721. // writevariantrecdesc(variantrecdesc);
  4722. end;
  4723. writeentry(ppufile,ibrecorddef);
  4724. if not(df_copied_def in defoptions) then
  4725. trecordsymtable(symtable).ppuwrite(ppufile);
  4726. end;
  4727. function trecorddef.size:asizeint;
  4728. begin
  4729. result:=trecordsymtable(symtable).datasize;
  4730. end;
  4731. function trecorddef.alignment:shortint;
  4732. begin
  4733. alignment:=trecordsymtable(symtable).recordalignment;
  4734. end;
  4735. function trecorddef.padalignment:shortint;
  4736. begin
  4737. padalignment := trecordsymtable(symtable).padalignment;
  4738. end;
  4739. function trecorddef.GetTypeName : string;
  4740. begin
  4741. GetTypeName:='<record type>'
  4742. end;
  4743. {$ifdef DEBUG_NODE_XML}
  4744. function TRecordDef.XMLPrintType: ansistring;
  4745. begin
  4746. Result := '&lt;record&gt;';
  4747. end;
  4748. {$endif DEBUG_NODE_XML}
  4749. {***************************************************************************
  4750. TABSTRACTPROCDEF
  4751. ***************************************************************************}
  4752. constructor tabstractprocdef.create(dt:tdeftyp;level:byte;doregister:boolean);
  4753. begin
  4754. inherited create(dt,doregister);
  4755. parast:=tparasymtable.create(self,level);
  4756. paras:=nil;
  4757. minparacount:=0;
  4758. maxparacount:=0;
  4759. proctypeoption:=potype_none;
  4760. proccalloption:=pocall_none;
  4761. procoptions:=[];
  4762. returndef:=voidtype;
  4763. returndefderef.reset;
  4764. savesize:=sizeof(pint);
  4765. callerargareasize:=0;
  4766. calleeargareasize:=0;
  4767. has_paraloc_info:=callnoside;
  4768. funcretloc[callerside].init;
  4769. funcretloc[calleeside].init;
  4770. check_mark_as_nested;
  4771. end;
  4772. destructor tabstractprocdef.destroy;
  4773. begin
  4774. if assigned(paras) then
  4775. begin
  4776. {$ifdef MEMDEBUG}
  4777. memprocpara.start;
  4778. {$endif MEMDEBUG}
  4779. paras.free;
  4780. paras:=nil;
  4781. {$ifdef MEMDEBUG}
  4782. memprocpara.stop;
  4783. {$endif MEMDEBUG}
  4784. end;
  4785. if assigned(parast) then
  4786. begin
  4787. {$ifdef MEMDEBUG}
  4788. memprocparast.start;
  4789. {$endif MEMDEBUG}
  4790. parast.free;
  4791. parast:=nil;
  4792. {$ifdef MEMDEBUG}
  4793. memprocparast.stop;
  4794. {$endif MEMDEBUG}
  4795. end;
  4796. funcretloc[callerside].done;
  4797. funcretloc[calleeside].done;
  4798. inherited destroy;
  4799. end;
  4800. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  4801. begin
  4802. if (tsym(p).typ<>paravarsym) then
  4803. exit;
  4804. inc(plongint(arg)^);
  4805. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  4806. begin
  4807. if not assigned(tparavarsym(p).defaultconstsym) then
  4808. inc(minparacount);
  4809. inc(maxparacount);
  4810. end;
  4811. end;
  4812. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  4813. begin
  4814. if (tsym(p).typ<>paravarsym) then
  4815. exit;
  4816. paras.add(p);
  4817. end;
  4818. procedure tabstractprocdef.calcparas;
  4819. var
  4820. paracount : longint;
  4821. begin
  4822. { This can already be assigned when
  4823. we need to reresolve this unit (PFV) }
  4824. if assigned(paras) then
  4825. paras.free;
  4826. paras:=tparalist.create(false);
  4827. paracount:=0;
  4828. minparacount:=0;
  4829. maxparacount:=0;
  4830. parast.SymList.ForEachCall(@count_para,@paracount);
  4831. paras.capacity:=paracount;
  4832. { Insert parameters in table }
  4833. parast.SymList.ForEachCall(@insert_para,nil);
  4834. { Order parameters }
  4835. paras.sortparas;
  4836. end;
  4837. function tabstractprocdef.mangledprocparanames(oldlen : longint) : string;
  4838. var
  4839. hash : qword;
  4840. hp : TParavarsym;
  4841. hs : TSymStr;
  4842. newlen,
  4843. i : integer;
  4844. begin
  4845. result:='';
  4846. hp:=nil;
  4847. { add parameter types }
  4848. for i:=0 to paras.count-1 do
  4849. begin
  4850. hp:=tparavarsym(paras[i]);
  4851. if not(vo_is_hidden_para in hp.varoptions) then
  4852. result:=result+'$'+hp.vardef.mangledparaname;
  4853. end;
  4854. { add resultdef, add $$ as separator to make it unique from a
  4855. parameter separator }
  4856. if not is_void(returndef) then
  4857. result:=result+'$$'+returndef.mangledparaname;
  4858. newlen:=length(result)+oldlen;
  4859. { Replace with hash if the parameter line is very long }
  4860. if (newlen-oldlen>12) and
  4861. ((newlen>100) or (newlen-oldlen>64)) then
  4862. begin
  4863. hash:=0;
  4864. for i:=0 to paras.count-1 do
  4865. begin
  4866. hp:=tparavarsym(paras[i]);
  4867. if not(vo_is_hidden_para in hp.varoptions) then
  4868. begin
  4869. hs:=hp.vardef.mangledparaname;
  4870. hash:=UpdateFnv64(hash,hs[1],length(hs));
  4871. end;
  4872. end;
  4873. if not is_void(returndef) then
  4874. begin
  4875. { add a little prefix so that x(integer; integer) is different from x(integer):integer }
  4876. hs:='$$'+returndef.mangledparaname;
  4877. hash:=UpdateFnv64(hash,hs[1],length(hs));
  4878. end;
  4879. result:='$h'+Base64Mangle(hash);
  4880. end;
  4881. end;
  4882. procedure tabstractprocdef.buildderef;
  4883. begin
  4884. { released procdef? }
  4885. if not assigned(parast) then
  4886. exit;
  4887. inherited buildderef;
  4888. returndefderef.build(returndef);
  4889. if po_explicitparaloc in procoptions then
  4890. funcretloc[callerside].buildderef;
  4891. { parast }
  4892. tparasymtable(parast).buildderef;
  4893. end;
  4894. procedure tabstractprocdef.deref;
  4895. begin
  4896. inherited deref;
  4897. returndef:=tdef(returndefderef.resolve);
  4898. if po_explicitparaloc in procoptions then
  4899. begin
  4900. funcretloc[callerside].deref;
  4901. has_paraloc_info:=callerside;
  4902. end
  4903. else
  4904. begin
  4905. { deref is called after loading from a ppu, but also after another
  4906. unit has been reloaded/recompiled and all references must be
  4907. re-resolved. Since the funcretloc contains a reference to a tdef,
  4908. reset it so that we won't try to access the stale def }
  4909. funcretloc[callerside].init;
  4910. has_paraloc_info:=callnoside;
  4911. end;
  4912. { parast }
  4913. tparasymtable(parast).deref(false);
  4914. { recalculated parameters }
  4915. calcparas;
  4916. end;
  4917. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  4918. begin
  4919. inherited ppuload(dt,ppufile);
  4920. parast:=nil;
  4921. Paras:=nil;
  4922. minparacount:=0;
  4923. maxparacount:=0;
  4924. ppufile.getderef(returndefderef);
  4925. proctypeoption:=tproctypeoption(ppufile.getbyte);
  4926. proccalloption:=tproccalloption(ppufile.getbyte);
  4927. ppufile.getset(tppuset8(procoptions));
  4928. funcretloc[callerside].init;
  4929. if po_explicitparaloc in procoptions then
  4930. funcretloc[callerside].ppuload(ppufile);
  4931. savesize:=sizeof(pint);
  4932. if (po_explicitparaloc in procoptions) then
  4933. has_paraloc_info:=callerside;
  4934. end;
  4935. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  4936. var
  4937. oldintfcrc : boolean;
  4938. begin
  4939. { released procdef? }
  4940. if not assigned(parast) then
  4941. exit;
  4942. inherited ppuwrite(ppufile);
  4943. ppufile.putderef(returndefderef);
  4944. oldintfcrc:=ppufile.do_interface_crc;
  4945. ppufile.do_interface_crc:=false;
  4946. ppufile.putbyte(ord(proctypeoption));
  4947. ppufile.putbyte(ord(proccalloption));
  4948. ppufile.putset(tppuset8(procoptions));
  4949. ppufile.do_interface_crc:=oldintfcrc;
  4950. if (po_explicitparaloc in procoptions) then
  4951. funcretloc[callerside].ppuwrite(ppufile);
  4952. end;
  4953. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  4954. var
  4955. hs,s : ansistring;
  4956. hp : TParavarsym;
  4957. hpc : tconstsym;
  4958. first : boolean;
  4959. i,j : integer;
  4960. begin
  4961. s:='';
  4962. first:=true;
  4963. for i:=0 to paras.count-1 do
  4964. begin
  4965. hp:=tparavarsym(paras[i]);
  4966. if not(vo_is_hidden_para in hp.varoptions) or
  4967. (pno_showhidden in pno) then
  4968. begin
  4969. if first then
  4970. begin
  4971. s:=s+'(';
  4972. first:=false;
  4973. end
  4974. else
  4975. s:=s+';';
  4976. if vo_is_hidden_para in hp.varoptions then
  4977. s:=s+'<';
  4978. case hp.varspez of
  4979. vs_var :
  4980. s:=s+'var ';
  4981. vs_const :
  4982. s:=s+'const ';
  4983. vs_out :
  4984. s:=s+'out ';
  4985. vs_constref :
  4986. s:=s+'constref ';
  4987. else
  4988. ;
  4989. end;
  4990. if (pno_paranames in pno) then
  4991. s:=s+hp.realname+':';
  4992. if hp.univpara then
  4993. s:=s+'univ ';
  4994. if assigned(hp.vardef.typesym) then
  4995. begin
  4996. hs:=hp.vardef.typesym.realname;
  4997. if hs[1]<>'$' then
  4998. s:=s+hp.vardef.OwnerHierarchyName+hs
  4999. else
  5000. s:=s+hp.vardef.GetTypeName;
  5001. end
  5002. else
  5003. s:=s+hp.vardef.GetTypeName;
  5004. { default value }
  5005. if assigned(hp.defaultconstsym) then
  5006. begin
  5007. hpc:=tconstsym(hp.defaultconstsym);
  5008. hs:='';
  5009. case hpc.consttyp of
  5010. constwstring:
  5011. begin
  5012. if pcompilerwidestring(hpc.value.valueptr)^.len>0 then
  5013. begin
  5014. setlength(hs,pcompilerwidestring(hpc.value.valueptr)^.len);
  5015. for j:=0 to pcompilerwidestring(hpc.value.valueptr)^.len-1 do
  5016. begin
  5017. if (ord(pcompilerwidestring(hpc.value.valueptr)^.data[j])<127) and
  5018. not(byte(pcompilerwidestring(hpc.value.valueptr)^.data[j]) in [0,10,13]) then
  5019. hs[j+1]:=char(pcompilerwidestring(hpc.value.valueptr)^.data[j])
  5020. else
  5021. hs[j+1]:='.';
  5022. end;
  5023. end;
  5024. end;
  5025. conststring,
  5026. constresourcestring :
  5027. begin
  5028. if hpc.value.len>0 then
  5029. begin
  5030. setLength(hs,hpc.value.len);
  5031. { don't write past the end of hs if the constant
  5032. is > 255 chars }
  5033. move(hpc.value.valueptr^,hs[1],length(hs));
  5034. { make sure that constant strings with newline chars
  5035. don't create a linebreak in the assembler code,
  5036. since comments are line-based. Also remove nulls
  5037. because the comments are written as a pchar. }
  5038. ReplaceCase(hs,#0,'.');
  5039. ReplaceCase(hs,#10,'.');
  5040. ReplaceCase(hs,#13,'.');
  5041. end;
  5042. end;
  5043. constreal :
  5044. str(pbestreal(hpc.value.valueptr)^,hs);
  5045. constpointer :
  5046. hs:=tostr(hpc.value.valueordptr);
  5047. constord :
  5048. begin
  5049. if is_boolean(hpc.constdef) then
  5050. begin
  5051. if hpc.value.valueord<>0 then
  5052. hs:='TRUE'
  5053. else
  5054. hs:='FALSE';
  5055. end
  5056. else
  5057. hs:=tostr(hpc.value.valueord);
  5058. end;
  5059. constnil :
  5060. hs:='nil';
  5061. constset :
  5062. hs:='<set>';
  5063. constguid:
  5064. hs:=guid2string(pguid(hpc.value.valueptr)^);
  5065. constnone:
  5066. internalerror(2019050704);
  5067. end;
  5068. if hs<>'' then
  5069. s:=s+'=`'+hs+'`';
  5070. end;
  5071. if vo_is_hidden_para in hp.varoptions then
  5072. s:=s+'>';
  5073. end;
  5074. end;
  5075. if not first then
  5076. s:=s+')';
  5077. if (po_varargs in procoptions) then
  5078. s:=s+';VarArgs';
  5079. typename_paras:=s;
  5080. end;
  5081. function tabstractprocdef.is_methodpointer:boolean;
  5082. begin
  5083. result:=false;
  5084. end;
  5085. function tabstractprocdef.is_addressonly:boolean;
  5086. begin
  5087. result:=true;
  5088. end;
  5089. function tabstractprocdef.no_self_node: boolean;
  5090. begin
  5091. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  5092. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  5093. end;
  5094. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp; const paraprefix: string): tstoreddef;
  5095. var
  5096. j, nestinglevel: longint;
  5097. pvs, npvs: tparavarsym;
  5098. begin
  5099. nestinglevel:=parast.symtablelevel;
  5100. if newtyp=procdef then
  5101. begin
  5102. if (copytyp<>pc_bareproc) then
  5103. result:=cprocdef.create(nestinglevel,true)
  5104. else
  5105. result:=cprocdef.create(normal_function_level,true);
  5106. tprocdef(result).visibility:=vis_public;
  5107. end
  5108. else
  5109. begin
  5110. result:=cprocvardef.create(nestinglevel);
  5111. end;
  5112. tabstractprocdef(result).returndef:=returndef;
  5113. tabstractprocdef(result).returndefderef:=returndefderef;
  5114. pvs:=nil;
  5115. npvs:=nil;
  5116. for j:=0 to parast.symlist.count-1 do
  5117. begin
  5118. case tsym(parast.symlist[j]).typ of
  5119. paravarsym:
  5120. begin
  5121. pvs:=tparavarsym(parast.symlist[j]);
  5122. { in case of bare proc, don't copy self, vmt or framepointer
  5123. parameters }
  5124. if (copytyp in [pc_bareproc,pc_normal_no_hidden]) and
  5125. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result,vo_is_funcret]*pvs.varoptions)<>[]) then
  5126. continue;
  5127. if paraprefix='' then
  5128. npvs:=cparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  5129. pvs.vardef,pvs.varoptions)
  5130. else if not(vo_is_high_para in pvs.varoptions) then
  5131. npvs:=cparavarsym.create(paraprefix+pvs.realname,pvs.paranr,pvs.varspez,
  5132. pvs.vardef,pvs.varoptions)
  5133. else
  5134. npvs:=cparavarsym.create('$high'+paraprefix+copy(pvs.name,5,length(pvs.name)),pvs.paranr,pvs.varspez,
  5135. pvs.vardef,pvs.varoptions);
  5136. npvs.defaultconstsym:=pvs.defaultconstsym;
  5137. tabstractprocdef(result).parast.insert(npvs);
  5138. end;
  5139. constsym:
  5140. begin
  5141. // ignore, reuse original constym. Should also be duplicated
  5142. // be safe though
  5143. end;
  5144. symconst.typesym:
  5145. begin
  5146. // reuse original, part of generic declaration
  5147. end
  5148. else
  5149. internalerror(201160604);
  5150. end;
  5151. end;
  5152. tabstractprocdef(result).savesize:=savesize;
  5153. if (typ<>procvardef) and
  5154. (newtyp=procvardef) then
  5155. begin
  5156. { procvars can't be (class)constructures/destructors etc }
  5157. if proctypeoption=potype_constructor then
  5158. begin
  5159. tabstractprocdef(result).returndef:=tdef(owner.defowner);
  5160. if not(is_implicit_pointer_object_type(returndef) or
  5161. (returndef.typ<>objectdef)) then
  5162. tabstractprocdef(result).returndef:=cpointerdef.getreusable(tabstractprocdef(result).returndef);
  5163. tabstractprocdef(result).proctypeoption:=potype_function;
  5164. end
  5165. else if is_void(returndef) then
  5166. tabstractprocdef(result).proctypeoption:=potype_procedure
  5167. else
  5168. tabstractprocdef(result).proctypeoption:=potype_function;
  5169. end
  5170. else
  5171. tabstractprocdef(result).proctypeoption:=proctypeoption;
  5172. tabstractprocdef(result).proccalloption:=proccalloption;
  5173. tabstractprocdef(result).procoptions:=procoptions;
  5174. if (copytyp=pc_bareproc) then
  5175. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  5176. if newtyp=procvardef then
  5177. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  5178. if copytyp=pc_address_only then
  5179. include(tabstractprocdef(result).procoptions,po_addressonly);
  5180. tabstractprocdef(result).callerargareasize:=callerargareasize;
  5181. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  5182. tabstractprocdef(result).maxparacount:=maxparacount;
  5183. tabstractprocdef(result).minparacount:=minparacount;
  5184. if po_explicitparaloc in procoptions then
  5185. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  5186. { recalculate parameter info }
  5187. tabstractprocdef(result).has_paraloc_info:=callnoside;
  5188. {$ifdef m68k}
  5189. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  5190. {$endif}
  5191. if (typ=procdef) and
  5192. (newtyp=procvardef) and
  5193. (owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  5194. include(tprocvardef(result).procoptions,po_methodpointer);
  5195. end;
  5196. function tabstractprocdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  5197. begin
  5198. result:=is_addressonly;
  5199. end;
  5200. procedure tabstractprocdef.check_mark_as_nested;
  5201. begin
  5202. { nested procvars require that nested functions use the Delphi-style
  5203. nested procedure calling convention }
  5204. if (parast.symtablelevel>normal_function_level) and
  5205. (m_nested_procvars in current_settings.modeswitches) then
  5206. include(procoptions,po_delphi_nested_cc);
  5207. end;
  5208. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  5209. begin
  5210. if (side in [callerside,callbothsides]) and
  5211. not(has_paraloc_info in [callerside,callbothsides]) then
  5212. begin
  5213. if not is_c_variadic(self) then
  5214. callerargareasize:=paramanager.create_paraloc_info(self,callerside)
  5215. else
  5216. callerargareasize:=paramanager.create_varargs_paraloc_info(self,callerside,nil);
  5217. if has_paraloc_info in [calleeside,callbothsides] then
  5218. has_paraloc_info:=callbothsides
  5219. else
  5220. has_paraloc_info:=callerside;
  5221. end;
  5222. if (side in [calleeside,callbothsides]) and
  5223. not(has_paraloc_info in [calleeside,callbothsides]) then
  5224. begin
  5225. if not is_c_variadic(self) then
  5226. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside)
  5227. else
  5228. callerargareasize:=paramanager.create_varargs_paraloc_info(self,calleeside,nil);
  5229. if has_paraloc_info in [callerside,callbothsides] then
  5230. has_paraloc_info:=callbothsides
  5231. else
  5232. has_paraloc_info:=calleeside;
  5233. end;
  5234. end;
  5235. procedure tabstractprocdef.done_paraloc_info(side: tcallercallee);
  5236. var
  5237. i: longint;
  5238. begin
  5239. if (side in [callerside,callbothsides]) and
  5240. (has_paraloc_info in [callerside,callbothsides]) then
  5241. begin
  5242. funcretloc[callerside].done;
  5243. for i:=0 to paras.count-1 do
  5244. tparavarsym(paras[i]).paraloc[callerside].done;
  5245. if has_paraloc_info=callerside then
  5246. has_paraloc_info:=callnoside
  5247. else
  5248. has_paraloc_info:=calleeside;
  5249. end;
  5250. if (side in [calleeside,callbothsides]) and
  5251. (has_paraloc_info in [calleeside,callbothsides]) then
  5252. begin
  5253. funcretloc[calleeside].done;
  5254. for i:=0 to paras.count-1 do
  5255. tparavarsym(paras[i]).paraloc[calleeside].done;
  5256. if has_paraloc_info=calleeside then
  5257. has_paraloc_info:=callnoside
  5258. else
  5259. has_paraloc_info:=callerside;
  5260. end;
  5261. end;
  5262. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  5263. var
  5264. p: tparavarsym;
  5265. ploc: PCGParalocation;
  5266. i: longint;
  5267. begin
  5268. result:=false;
  5269. init_paraloc_info(side);
  5270. for i:=0 to parast.SymList.Count-1 do
  5271. if tsym(parast.SymList[i]).typ=paravarsym then
  5272. begin
  5273. p:=tparavarsym(parast.SymList[i]);
  5274. if not p.is_used then
  5275. continue;
  5276. { check if no parameter is located on the stack }
  5277. if (is_open_array(p.vardef) or
  5278. is_array_of_const(p.vardef)) and (p.varspez=vs_value) then
  5279. begin
  5280. result:=true;
  5281. exit;
  5282. end;
  5283. ploc:=p.paraloc[side].location;
  5284. while assigned(ploc) do
  5285. begin
  5286. if (ploc^.loc=LOC_REFERENCE) then
  5287. begin
  5288. result:=true;
  5289. exit
  5290. end;
  5291. ploc:=ploc^.next;
  5292. end;
  5293. end;
  5294. end;
  5295. function tabstractprocdef.is_pushleftright: boolean;
  5296. begin
  5297. result:=false;
  5298. end;
  5299. function tabstractprocdef.address_type: tdef;
  5300. begin
  5301. result:=voidcodepointertype;
  5302. end;
  5303. function tabstractprocdef.ofs_address_type:tdef;
  5304. begin
  5305. result:=address_type;
  5306. end;
  5307. procedure tabstractprocdef.declared_far;
  5308. begin
  5309. Message1(parser_w_proc_directive_ignored,'FAR');
  5310. end;
  5311. procedure tabstractprocdef.declared_near;
  5312. begin
  5313. Message1(parser_w_proc_directive_ignored,'NEAR');
  5314. end;
  5315. function tabstractprocdef.generate_safecall_wrapper: boolean;
  5316. begin
  5317. {$ifdef SUPPORT_SAFECALL}
  5318. result:=
  5319. (proccalloption=pocall_safecall) and
  5320. not(po_assembler in procoptions) and
  5321. (tf_safecall_exceptions in target_info.flags);
  5322. {$else SUPPORT_SAFECALL}
  5323. result:=false;
  5324. {$endif}
  5325. end;
  5326. function tabstractprocdef.is_generic_param(def:tdef): boolean;
  5327. begin
  5328. if def.typ=undefineddef then
  5329. result:=def.owner=self.parast
  5330. else if def.typ=objectdef then
  5331. result:=(def.owner=self.parast) and (tstoreddef(def).genconstraintdata<>nil)
  5332. else
  5333. result:=false;
  5334. end;
  5335. {***************************************************************************
  5336. TPROCDEF
  5337. ***************************************************************************}
  5338. function tprocdef.getparentfpsym: tsym;
  5339. begin
  5340. if not assigned(_parentfpsym) then
  5341. begin
  5342. _parentfpsym:=tsym(parast.Find('parentfp'));
  5343. if not assigned(_parentfpsym) then
  5344. internalerror(200309281);
  5345. end;
  5346. result:=_parentfpsym;
  5347. end;
  5348. function tprocdef.Getis_implemented: boolean;
  5349. begin
  5350. result:=not assigned(implprocdefinfo) or implprocdefinfo^.is_implemented;
  5351. end;
  5352. procedure tprocdef.Setis_implemented(AValue: boolean);
  5353. begin
  5354. if not assigned(implprocdefinfo) then
  5355. internalerror(2020062101);
  5356. implprocdefinfo^.is_implemented:=AValue;
  5357. end;
  5358. function tprocdef.store_localst: boolean;
  5359. begin
  5360. result:=has_inlininginfo or (df_generic in defoptions);
  5361. end;
  5362. function tprocdef.GetResultName: PShortString;
  5363. begin
  5364. if not assigned(implprocdefinfo) then
  5365. internalerror(2014010301);
  5366. result:=implprocdefinfo^.resultname;
  5367. end;
  5368. procedure tprocdef.SetResultName(AValue: PShortString);
  5369. begin
  5370. if not assigned(implprocdefinfo) then
  5371. internalerror(2014010302);
  5372. implprocdefinfo^.resultname:=AValue;
  5373. end;
  5374. function tprocdef.GetParentFPInitBlock: tnode;
  5375. begin
  5376. if not assigned(implprocdefinfo) then
  5377. internalerror(2014010303);
  5378. result:=implprocdefinfo^.parentfpinitblock;
  5379. end;
  5380. function tprocdef.GetParentFPStruct: tsym;
  5381. begin
  5382. if not assigned(implprocdefinfo) then
  5383. internalerror(2014010304);
  5384. result:=implprocdefinfo^.parentfpstruct;
  5385. end;
  5386. function tprocdef.GetParentFPStructPtrType: tdef;
  5387. begin
  5388. if not assigned(implprocdefinfo) then
  5389. internalerror(2014010305);
  5390. result:=implprocdefinfo^.parentfpstructptrtype;
  5391. end;
  5392. procedure tprocdef.SetParentFPInitBlock(AValue: tnode);
  5393. begin
  5394. if not assigned(implprocdefinfo) then
  5395. internalerror(2014010306);
  5396. implprocdefinfo^.parentfpinitblock:=AValue;
  5397. end;
  5398. function tprocdef.Getprocendtai: tai;
  5399. begin
  5400. if not assigned(implprocdefinfo) then
  5401. internalerror(2014010307);
  5402. result:=implprocdefinfo^.procendtai;
  5403. end;
  5404. function tprocdef.Getprocstarttai: tai;
  5405. begin
  5406. if not assigned(implprocdefinfo) then
  5407. internalerror(2014010308);
  5408. result:=implprocdefinfo^.procstarttai;
  5409. end;
  5410. procedure tprocdef.Setprocendtai(AValue: tai);
  5411. begin
  5412. if not assigned(implprocdefinfo) then
  5413. internalerror(2014010309);
  5414. implprocdefinfo^.procendtai:=AValue;
  5415. end;
  5416. function tprocdef.Getskpara: pointer;
  5417. begin
  5418. if not assigned(implprocdefinfo) then
  5419. internalerror(2014010310);
  5420. result:=implprocdefinfo^.skpara;
  5421. end;
  5422. procedure tprocdef.Setskpara(AValue: pointer);
  5423. begin
  5424. if not assigned(implprocdefinfo) then
  5425. internalerror(2014010311);
  5426. implprocdefinfo^.skpara:=AValue;
  5427. end;
  5428. function tprocdef.Getpersonality: tprocdef;
  5429. begin
  5430. if not assigned(implprocdefinfo) then
  5431. internalerror(2016121704);
  5432. result:=implprocdefinfo^.personality;
  5433. end;
  5434. procedure tprocdef.Setpersonality(AValue: tprocdef);
  5435. begin
  5436. if not assigned(implprocdefinfo) then
  5437. internalerror(2016121705);
  5438. implprocdefinfo^.personality:=AValue;
  5439. end;
  5440. function tprocdef.Getforwarddef: boolean;
  5441. begin
  5442. if not assigned(implprocdefinfo) then
  5443. internalerror(2014010312);
  5444. result:=implprocdefinfo^.forwarddef;
  5445. end;
  5446. function tprocdef.Gethasforward: boolean;
  5447. begin
  5448. if not assigned(implprocdefinfo) then
  5449. internalerror(2014010313);
  5450. result:=implprocdefinfo^.hasforward;
  5451. end;
  5452. function tprocdef.Getinterfacedef: boolean;
  5453. begin
  5454. if not assigned(implprocdefinfo) then
  5455. internalerror(2014010314);
  5456. result:=implprocdefinfo^.interfacedef;
  5457. end;
  5458. procedure tprocdef.Setforwarddef(AValue: boolean);
  5459. begin
  5460. if not assigned(implprocdefinfo) then
  5461. internalerror(2014010315);
  5462. implprocdefinfo^.forwarddef:=AValue;
  5463. end;
  5464. procedure tprocdef.Sethasforward(AValue: boolean);
  5465. begin
  5466. if not assigned(implprocdefinfo) then
  5467. internalerror(2014010316);
  5468. implprocdefinfo^.hasforward:=AValue;
  5469. end;
  5470. function tprocdef.GetIsEmpty: boolean;
  5471. begin
  5472. result:=pio_empty in implprocoptions;
  5473. end;
  5474. procedure tprocdef.SetIsEmpty(AValue: boolean);
  5475. begin
  5476. if AValue then
  5477. include(implprocoptions,pio_empty)
  5478. else
  5479. exclude(implprocoptions,pio_empty);
  5480. end;
  5481. function tprocdef.GetHasInliningInfo: boolean;
  5482. begin
  5483. result:=pio_has_inlininginfo in implprocoptions;
  5484. end;
  5485. procedure tprocdef.SetHasInliningInfo(AValue: boolean);
  5486. begin
  5487. if AValue then
  5488. include(implprocoptions,pio_has_inlininginfo)
  5489. else
  5490. exclude(implprocoptions,pio_has_inlininginfo);
  5491. end;
  5492. procedure tprocdef.Setinterfacedef(AValue: boolean);
  5493. begin
  5494. if not assigned(implprocdefinfo) then
  5495. internalerror(2014010317);
  5496. implprocdefinfo^.interfacedef:=AValue;
  5497. end;
  5498. procedure tprocdef.Setprocstarttai(AValue: tai);
  5499. begin
  5500. if not assigned(implprocdefinfo) then
  5501. internalerror(2014010318);
  5502. implprocdefinfo^.procstarttai:=AValue;
  5503. end;
  5504. procedure tprocdef.SetParentFPStruct(AValue: tsym);
  5505. begin
  5506. if not assigned(implprocdefinfo) then
  5507. internalerror(2014010319);
  5508. implprocdefinfo^.parentfpstruct:=AValue;
  5509. end;
  5510. procedure tprocdef.SetParentFPStructPtrType(AValue: tdef);
  5511. begin
  5512. if not assigned(implprocdefinfo) then
  5513. internalerror(2014010320);
  5514. implprocdefinfo^.parentfpstructptrtype:=AValue;
  5515. end;
  5516. constructor tprocdef.create(level:byte;doregister:boolean);
  5517. begin
  5518. inherited create(procdef,level,doregister);
  5519. implprocdefinfo:=allocmem(sizeof(implprocdefinfo^));
  5520. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  5521. {$ifdef symansistr}
  5522. _mangledname:='';
  5523. {$else symansistr}
  5524. _mangledname:=nil;
  5525. {$endif symansistr}
  5526. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  5527. seenmarker := nil;
  5528. {$endif}
  5529. fileinfo:=current_filepos;
  5530. extnumber:=$ffff;
  5531. aliasnames:=TCmdStrList.create;
  5532. funcretsym:=nil;
  5533. funcretsymderef.reset;
  5534. procsymderef.reset;
  5535. forwarddef:=true;
  5536. interfacedef:=false;
  5537. hasforward:=false;
  5538. struct := nil;
  5539. structderef.reset;
  5540. import_dll:=nil;
  5541. import_name:=nil;
  5542. import_nr:=0;
  5543. inlininginfo:=nil;
  5544. deprecatedmsg:=nil;
  5545. genericdecltokenbuf:=nil;
  5546. if cs_opt_fastmath in current_settings.optimizerswitches then
  5547. include(implprocoptions, pio_fastmath);
  5548. end;
  5549. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  5550. var
  5551. i,aliasnamescount,sizeleft : longint;
  5552. level : byte;
  5553. buf : array[0..255] of byte;
  5554. begin
  5555. inherited ppuload(procdef,ppufile);
  5556. {$ifdef symansistr}
  5557. if po_has_mangledname in procoptions then
  5558. _mangledname:=ppufile.getansistring
  5559. else
  5560. _mangledname:='';
  5561. {$else symansistr}
  5562. if po_has_mangledname in procoptions then
  5563. _mangledname:=ppufile.getpshortstring
  5564. else
  5565. _mangledname:=nil;
  5566. {$endif symansistr}
  5567. extnumber:=ppufile.getword;
  5568. level:=ppufile.getbyte;
  5569. ppufile.getderef(structderef);
  5570. ppufile.getderef(procsymderef);
  5571. ppufile.getposinfo(fileinfo);
  5572. visibility:=tvisibility(ppufile.getbyte);
  5573. ppufile.getset(tppuset2(symoptions));
  5574. if sp_has_deprecated_msg in symoptions then
  5575. deprecatedmsg:=ppufile.getpshortstring
  5576. else
  5577. deprecatedmsg:=nil;
  5578. { import stuff }
  5579. if po_has_importdll in procoptions then
  5580. import_dll:=ppufile.getpshortstring
  5581. else
  5582. import_dll:=nil;
  5583. if po_has_importname in procoptions then
  5584. import_name:=ppufile.getpshortstring
  5585. else
  5586. import_name:=nil;
  5587. import_nr:=ppufile.getword;
  5588. if (po_msgint in procoptions) then
  5589. messageinf.i:=ppufile.getlongint;
  5590. if (po_msgstr in procoptions) then
  5591. messageinf.str:=ppufile.getpshortstring;
  5592. if (po_dispid in procoptions) then
  5593. dispid:=ppufile.getlongint;
  5594. { inline stuff }
  5595. ppufile.getset(tppuset1(implprocoptions));
  5596. if has_inlininginfo then
  5597. begin
  5598. ppufile.getderef(funcretsymderef);
  5599. new(inlininginfo);
  5600. ppufile.getset(tppuset4(inlininginfo^.flags));
  5601. end
  5602. else
  5603. begin
  5604. inlininginfo:=nil;
  5605. funcretsym:=nil;
  5606. end;
  5607. aliasnames:=TCmdStrList.create;
  5608. { count alias names }
  5609. aliasnamescount:=ppufile.getbyte;
  5610. for i:=1 to aliasnamescount do
  5611. aliasnames.insert(ppufile.getstring);
  5612. { load the token stream containing the declaration }
  5613. sizeleft:=ppufile.getlongint;
  5614. if sizeleft>0 then
  5615. begin
  5616. init_genericdecl;
  5617. while sizeleft>0 do
  5618. begin
  5619. if sizeleft>sizeof(buf) then
  5620. i:=sizeof(buf)
  5621. else
  5622. i:=sizeleft;
  5623. ppufile.getdata(buf,i);
  5624. genericdecltokenbuf.write(buf,i);
  5625. dec(sizeleft,i);
  5626. end;
  5627. end;
  5628. ppuload_platform(ppufile);
  5629. { load para symtable }
  5630. parast:=tparasymtable.create(self,level);
  5631. tparasymtable(parast).ppuload(ppufile);
  5632. { load local symtable }
  5633. if store_localst then
  5634. begin
  5635. localst:=tlocalsymtable.create(self,level);
  5636. tlocalsymtable(localst).ppuload(ppufile);
  5637. end
  5638. else
  5639. localst:=nil;
  5640. { inline stuff }
  5641. if has_inlininginfo then
  5642. inlininginfo^.code:=ppuloadnodetree(ppufile);
  5643. { default values for no persistent data }
  5644. if (cs_link_deffile in current_settings.globalswitches) and
  5645. (tf_need_export in target_info.flags) and
  5646. (po_exports in procoptions) then
  5647. deffile.AddExport(mangledname);
  5648. { Disable po_has_inlining until the derefimpl is done }
  5649. has_inlininginfo:=false;
  5650. end;
  5651. destructor tprocdef.destroy;
  5652. begin
  5653. aliasnames.free;
  5654. aliasnames:=nil;
  5655. if assigned(localst) and
  5656. (localst.symtabletype<>staticsymtable) then
  5657. begin
  5658. {$ifdef MEMDEBUG}
  5659. memproclocalst.start;
  5660. {$endif MEMDEBUG}
  5661. localst.free;
  5662. localst:=nil;
  5663. {$ifdef MEMDEBUG}
  5664. memproclocalst.start;
  5665. {$endif MEMDEBUG}
  5666. end;
  5667. if assigned(inlininginfo) then
  5668. begin
  5669. {$ifdef MEMDEBUG}
  5670. memprocnodetree.start;
  5671. {$endif MEMDEBUG}
  5672. tnode(inlininginfo^.code).free;
  5673. {$ifdef MEMDEBUG}
  5674. memprocnodetree.start;
  5675. {$endif MEMDEBUG}
  5676. dispose(inlininginfo);
  5677. inlininginfo:=nil;
  5678. end;
  5679. freeimplprocdefinfo;
  5680. genericdecltokenbuf.free;
  5681. genericdecltokenbuf:=nil;
  5682. stringdispose(import_dll);
  5683. stringdispose(import_name);
  5684. stringdispose(deprecatedmsg);
  5685. if (po_msgstr in procoptions) then
  5686. stringdispose(messageinf.str);
  5687. {$ifndef symansistr}
  5688. if assigned(_mangledname) then
  5689. begin
  5690. {$ifdef MEMDEBUG}
  5691. memmanglednames.start;
  5692. {$endif MEMDEBUG}
  5693. stringdispose(_mangledname);
  5694. {$ifdef MEMDEBUG}
  5695. memmanglednames.stop;
  5696. {$endif MEMDEBUG}
  5697. end;
  5698. {$endif symansistr}
  5699. inherited destroy;
  5700. end;
  5701. procedure tprocdef.freeimplprocdefinfo;
  5702. begin
  5703. if assigned(implprocdefinfo) then
  5704. begin
  5705. stringdispose(implprocdefinfo^.resultname);
  5706. freemem(implprocdefinfo);
  5707. implprocdefinfo:=nil;
  5708. end;
  5709. end;
  5710. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  5711. var
  5712. oldintfcrc : boolean;
  5713. aliasnamescount,i,sizeleft : longint;
  5714. item : TCmdStrListItem;
  5715. buf : array[0..255] of byte;
  5716. begin
  5717. { released procdef? }
  5718. if not assigned(parast) then
  5719. exit;
  5720. inherited ppuwrite(ppufile);
  5721. {$ifdef symansistr}
  5722. if po_has_mangledname in procoptions then
  5723. ppufile.putansistring(_mangledname);
  5724. {$else symansistr}
  5725. if po_has_mangledname in procoptions then
  5726. ppufile.putstring(_mangledname^);
  5727. {$endif symansistr}
  5728. ppufile.putword(extnumber);
  5729. ppufile.putbyte(parast.symtablelevel);
  5730. ppufile.putderef(structderef);
  5731. ppufile.putderef(procsymderef);
  5732. ppufile.putposinfo(fileinfo);
  5733. ppufile.putbyte(byte(visibility));
  5734. ppufile.putset(tppuset2(symoptions));
  5735. if sp_has_deprecated_msg in symoptions then
  5736. ppufile.putstring(deprecatedmsg^);
  5737. { import }
  5738. if po_has_importdll in procoptions then
  5739. ppufile.putstring(import_dll^);
  5740. if po_has_importname in procoptions then
  5741. ppufile.putstring(import_name^);
  5742. ppufile.putword(import_nr);
  5743. if (po_msgint in procoptions) then
  5744. ppufile.putlongint(messageinf.i);
  5745. if (po_msgstr in procoptions) then
  5746. ppufile.putstring(messageinf.str^);
  5747. if (po_dispid in procoptions) then
  5748. ppufile.putlongint(dispid);
  5749. { inline stuff }
  5750. oldintfcrc:=ppufile.do_crc;
  5751. ppufile.do_crc:=false;
  5752. ppufile.putset(tppuset1(implprocoptions));
  5753. if has_inlininginfo then
  5754. begin
  5755. ppufile.putderef(funcretsymderef);
  5756. ppufile.putset(tppuset4(inlininginfo^.flags));
  5757. end;
  5758. { count alias names }
  5759. aliasnamescount:=0;
  5760. item:=TCmdStrListItem(aliasnames.first);
  5761. while assigned(item) do
  5762. begin
  5763. inc(aliasnamescount);
  5764. item:=TCmdStrListItem(item.next);
  5765. end;
  5766. if aliasnamescount>255 then
  5767. internalerror(200711021);
  5768. ppufile.putbyte(aliasnamescount);
  5769. item:=TCmdStrListItem(aliasnames.first);
  5770. while assigned(item) do
  5771. begin
  5772. ppufile.putstring(item.str);
  5773. item:=TCmdStrListItem(item.next);
  5774. end;
  5775. ppufile.do_crc:=oldintfcrc;
  5776. { generic tokens for the declaration }
  5777. if assigned(genericdecltokenbuf) and (genericdecltokenbuf.size>0) then
  5778. begin
  5779. sizeleft:=genericdecltokenbuf.size;
  5780. genericdecltokenbuf.seek(0);
  5781. ppufile.putlongint(sizeleft);
  5782. while sizeleft>0 do
  5783. begin
  5784. if sizeleft>sizeof(buf) then
  5785. i:=sizeof(buf)
  5786. else
  5787. i:=sizeleft;
  5788. genericdecltokenbuf.read(buf,i);
  5789. ppufile.putdata(buf,i);
  5790. dec(sizeleft,i);
  5791. end;
  5792. end
  5793. else
  5794. ppufile.putlongint(0);
  5795. { write this entry }
  5796. writeentry(ppufile,ibprocdef);
  5797. { Save the para symtable, this is taken from the interface }
  5798. tparasymtable(parast).ppuwrite(ppufile);
  5799. { save localsymtable for inline procedures or when local
  5800. browser info is requested, this has no influence on the crc }
  5801. if store_localst and not ppufile.crc_only then
  5802. begin
  5803. oldintfcrc:=ppufile.do_crc;
  5804. ppufile.do_crc:=false;
  5805. tlocalsymtable(localst).ppuwrite(ppufile);
  5806. ppufile.do_crc:=oldintfcrc;
  5807. end;
  5808. { node tree for inlining }
  5809. oldintfcrc:=ppufile.do_crc;
  5810. ppufile.do_crc:=false;
  5811. if has_inlininginfo then
  5812. ppuwritenodetree(ppufile,inlininginfo^.code);
  5813. ppufile.do_crc:=oldintfcrc;
  5814. end;
  5815. function tprocdef.fullprocname(showhidden:boolean):string;
  5816. var
  5817. pno: tprocnameoptions;
  5818. begin
  5819. pno:=[];
  5820. if showhidden then
  5821. include(pno,pno_showhidden);
  5822. result:=customprocname(pno);
  5823. end;
  5824. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  5825. var
  5826. s, rn : ansistring;
  5827. t : ttoken;
  5828. syssym : tsyssym;
  5829. begin
  5830. {$ifdef EXTDEBUG}
  5831. include(pno,pno_showhidden);
  5832. {$endif EXTDEBUG}
  5833. s:='';
  5834. if proctypeoption=potype_operator then
  5835. begin
  5836. for t:=NOTOKEN to last_overloaded do
  5837. if procsym.realname='$'+overloaded_names[t] then
  5838. begin
  5839. s:='operator ';
  5840. if (pno_ownername in pno) and
  5841. assigned(struct) then
  5842. s:=s+struct.RttiName+'.';
  5843. s:=s+arraytokeninfo[t].str;
  5844. if not (pno_noparams in pno) then
  5845. s:=s+typename_paras(pno);
  5846. break;
  5847. end;
  5848. end
  5849. else
  5850. begin
  5851. if (po_classmethod in procoptions) and
  5852. not(pno_noclassmarker in pno) then
  5853. s:='class ';
  5854. case proctypeoption of
  5855. potype_constructor,
  5856. potype_class_constructor:
  5857. s:=s+'constructor ';
  5858. potype_class_destructor,
  5859. potype_destructor:
  5860. s:=s+'destructor ';
  5861. else
  5862. if (pno_proctypeoption in pno) then
  5863. begin
  5864. if assigned(returndef) and
  5865. not(is_void(returndef)) then
  5866. s:=s+'function '
  5867. else
  5868. s:=s+'procedure ';
  5869. end;
  5870. end;
  5871. if (pno_ownername in pno) and
  5872. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  5873. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  5874. if (po_compilerproc in procoptions) and (extnumber<>$ffff) then
  5875. begin
  5876. syssym:=tsyssym.find_by_number(extnumber);
  5877. if not assigned(syssym) then
  5878. internalerror(2016060305);
  5879. rn:=syssym.realname;
  5880. end
  5881. else
  5882. rn:=procsym.realname;
  5883. if (pno_noleadingdollar in pno) and
  5884. (rn[1]='$') then
  5885. delete(rn,1,1);
  5886. s:=s+rn;
  5887. if not (pno_noparams in pno) then
  5888. s:=s+typename_paras(pno);
  5889. end;
  5890. if not(proctypeoption in [potype_constructor,potype_destructor,
  5891. potype_class_constructor,potype_class_destructor]) and
  5892. assigned(returndef) and
  5893. not(is_void(returndef)) then
  5894. s:=s+':'+returndef.GetTypeName;
  5895. if assigned(owner) and (owner.symtabletype=localsymtable) then
  5896. s:=s+' is nested'
  5897. else if po_is_block in procoptions then
  5898. s:=s+' is block';
  5899. s:=s+';';
  5900. if po_far in procoptions then
  5901. s:=s+' far;';
  5902. { forced calling convention? }
  5903. if (po_hascallingconvention in procoptions) then
  5904. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  5905. if (po_staticmethod in procoptions) and
  5906. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  5907. s:=s+' Static;';
  5908. if pno_mangledname in pno then
  5909. s:=s+' -- mangled name: '+mangledname;
  5910. customprocname:=s;
  5911. end;
  5912. function tprocdef.is_methodpointer:boolean;
  5913. begin
  5914. { don't check assigned(_class), that's also the case for nested
  5915. procedures inside methods }
  5916. result:=(owner.symtabletype in [recordsymtable,ObjectSymtable]) and not no_self_node;
  5917. end;
  5918. function tprocdef.is_addressonly:boolean;
  5919. begin
  5920. result:=assigned(owner) and
  5921. not is_methodpointer and
  5922. (not(m_nested_procvars in current_settings.modeswitches) or
  5923. not is_nested_pd(self));
  5924. end;
  5925. procedure tprocdef.make_external;
  5926. begin
  5927. include(procoptions,po_external);
  5928. forwarddef:=false;
  5929. end;
  5930. procedure tprocdef.init_genericdecl;
  5931. begin
  5932. if assigned(genericdecltokenbuf) then
  5933. internalerror(2015061901);
  5934. genericdecltokenbuf:=tdynamicarray.create(256);
  5935. end;
  5936. function tprocdef.get_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean;
  5937. begin
  5938. result:=false;
  5939. if proctypeoption=potype_constructor then
  5940. begin
  5941. result:=true;
  5942. ressym:=tsym(parast.Find('self'));
  5943. resdef:=tabstractnormalvarsym(ressym).vardef;
  5944. { and TP-style constructors return a pointer to self }
  5945. if is_object(resdef) then
  5946. resdef:=cpointerdef.getreusable(resdef);
  5947. end
  5948. else if (proccalloption=pocall_safecall) and
  5949. (tf_safecall_exceptions in target_info.flags) then
  5950. begin
  5951. result:=true;
  5952. ressym:=tsym(localst.Find('safecallresult'));
  5953. resdef:=tabstractnormalvarsym(ressym).vardef;
  5954. end
  5955. else if not is_void(returndef) then
  5956. begin
  5957. result:=true;
  5958. ressym:=funcretsym;
  5959. resdef:=tabstractnormalvarsym(ressym).vardef;
  5960. end;
  5961. end;
  5962. function tprocdef.get_safecall_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean;
  5963. begin
  5964. result:=false;
  5965. if (proctypeoption<>potype_constructor) and
  5966. (proccalloption=pocall_safecall) and
  5967. (tf_safecall_exceptions in target_info.flags) then
  5968. begin
  5969. result:=true;
  5970. ressym:=tsym(localst.Find('safecallresult'));
  5971. resdef:=tabstractnormalvarsym(ressym).vardef;
  5972. end
  5973. end;
  5974. function tprocdef.has_alias_name(const s: TSymStr): boolean;
  5975. var
  5976. item : TCmdStrListItem;
  5977. begin
  5978. result:=true;
  5979. if mangledname=s then
  5980. exit;
  5981. item:=TCmdStrListItem(aliasnames.first);
  5982. while assigned(item) do
  5983. begin
  5984. if item.str=s then
  5985. exit;
  5986. item:=TCmdStrListItem(item.next);
  5987. end;
  5988. result:=false;
  5989. end;
  5990. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  5991. begin
  5992. case t of
  5993. gs_local :
  5994. GetSymtable:=localst;
  5995. gs_para :
  5996. GetSymtable:=parast;
  5997. else
  5998. GetSymtable:=nil;
  5999. end;
  6000. end;
  6001. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string): tstoreddef;
  6002. var
  6003. j : longint;
  6004. begin
  6005. result:=inherited;
  6006. if newtyp=procvardef then
  6007. begin
  6008. { create new paralist }
  6009. tprocvardef(result).calcparas;
  6010. exit;
  6011. end;
  6012. { don't copy mangled name, can be different }
  6013. tprocdef(result).messageinf:=messageinf;
  6014. tprocdef(result).dispid:=dispid;
  6015. if po_msgstr in procoptions then
  6016. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  6017. tprocdef(result).symoptions:=symoptions;
  6018. if assigned(deprecatedmsg) then
  6019. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  6020. { will have to be associated with appropriate procsym }
  6021. tprocdef(result).procsym:=nil;
  6022. { don't create aliases for bare copies, nor copy the funcretsym as
  6023. the function result parameter will be inserted again if necessary
  6024. (e.g. if the calling convention is changed) }
  6025. if not(copytyp in [pc_bareproc,pc_normal_no_hidden]) then
  6026. begin
  6027. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  6028. if assigned(funcretsym) then
  6029. begin
  6030. if funcretsym.owner=parast then
  6031. begin
  6032. j:=parast.symlist.indexof(funcretsym);
  6033. if j<0 then
  6034. internalerror(2011040606);
  6035. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  6036. end
  6037. else if funcretsym.owner=localst then
  6038. begin
  6039. { nothing to do, will be inserted for the new procdef while
  6040. parsing its body (by pdecsub.insert_funcret_local) }
  6041. end
  6042. else
  6043. internalerror(2011040605);
  6044. end;
  6045. end;
  6046. { will have to be associated with a new struct }
  6047. tprocdef(result).struct:=nil;
  6048. if assigned(implprocdefinfo) then
  6049. begin
  6050. if assigned(resultname) then
  6051. tprocdef(result).resultname:=stringdup(resultname^);
  6052. tprocdef(result).synthetickind:=synthetickind;
  6053. end;
  6054. if assigned(import_dll) then
  6055. tprocdef(result).import_dll:=stringdup(import_dll^);
  6056. if assigned(import_name) then
  6057. tprocdef(result).import_name:=stringdup(import_name^);
  6058. tprocdef(result).import_nr:=import_nr;
  6059. tprocdef(result).extnumber:=$ffff;
  6060. tprocdef(result).visibility:=visibility;
  6061. { we need a separate implementation for the copied def }
  6062. tprocdef(result).forwarddef:=true;
  6063. tprocdef(result).interfacedef:=true;
  6064. { create new paralist }
  6065. tprocdef(result).calcparas;
  6066. end;
  6067. function tprocdef.getcopy: tstoreddef;
  6068. begin
  6069. result:=getcopyas(procdef,pc_normal,'');
  6070. end;
  6071. procedure tprocdef.buildderef;
  6072. begin
  6073. inherited buildderef;
  6074. structderef.build(struct);
  6075. { procsym that originaly defined this definition, should be in the
  6076. same symtable }
  6077. procsymderef.build(procsym);
  6078. end;
  6079. procedure tprocdef.buildderefimpl;
  6080. begin
  6081. inherited buildderefimpl;
  6082. { Localst is not available for main/unit init }
  6083. if store_localst and assigned(localst) then
  6084. begin
  6085. tlocalsymtable(localst).buildderef;
  6086. tlocalsymtable(localst).buildderefimpl;
  6087. end;
  6088. { inline tree }
  6089. if has_inlininginfo then
  6090. begin
  6091. funcretsymderef.build(funcretsym);
  6092. inlininginfo^.code.buildderefimpl;
  6093. end;
  6094. end;
  6095. procedure tprocdef.deref;
  6096. begin
  6097. inherited deref;
  6098. struct:=tabstractrecorddef(structderef.resolve);
  6099. { procsym that originaly defined this definition, should be in the
  6100. same symtable }
  6101. procsym:=tprocsym(procsymderef.resolve);
  6102. end;
  6103. procedure tprocdef.derefimpl;
  6104. begin
  6105. { Enable has_inlininginfo when the inlininginfo
  6106. structure is available. The has_inlininginfo was disabled
  6107. after the load, since the data was invalid }
  6108. if assigned(inlininginfo) then
  6109. has_inlininginfo:=true;
  6110. { Locals }
  6111. if store_localst and assigned(localst) then
  6112. begin
  6113. tlocalsymtable(localst).deref(false);
  6114. tlocalsymtable(localst).derefimpl(false);
  6115. end;
  6116. { Inline }
  6117. if has_inlininginfo then
  6118. begin
  6119. inlininginfo^.code.derefimpl;
  6120. { funcretsym, this is always located in the localst }
  6121. funcretsym:=tsym(funcretsymderef.resolve);
  6122. end
  6123. else
  6124. begin
  6125. { safety }
  6126. { Not safe! A unit may be reresolved after its interface has been
  6127. parsed but before its implementation has been parsed, and in that
  6128. case the funcretsym is still required!
  6129. funcretsym:=nil; }
  6130. end;
  6131. end;
  6132. function tprocdef.GetTypeName : string;
  6133. begin
  6134. GetTypeName := FullProcName(false);
  6135. end;
  6136. function tprocdef.mangledname : TSymStr;
  6137. begin
  6138. {$ifdef symansistr}
  6139. if _mangledname='' then
  6140. begin
  6141. result:=defaultmangledname;
  6142. _mangledname:=result;
  6143. end
  6144. else
  6145. result:=_mangledname;
  6146. {$else symansistr}
  6147. if not assigned(_mangledname) then
  6148. begin
  6149. result:=defaultmangledname;
  6150. _mangledname:=stringdup(mangledname);
  6151. end
  6152. else
  6153. result:=_mangledname^;
  6154. {$endif symansistr}
  6155. end;
  6156. function tprocdef.defaultmangledname: TSymStr;
  6157. var
  6158. n : TSymStr;
  6159. begin
  6160. n:=procsym.name;
  6161. { make sure that the mangled names of these overloadable methods types is
  6162. unique even if it's made lowercase (e.g. for section names) }
  6163. if proctypeoption in [potype_operator,potype_class_constructor,potype_class_destructor] then
  6164. n:='$'+n;
  6165. { we need to use the symtable where the procsym is inserted,
  6166. because that is visible to the world }
  6167. defaultmangledname:=make_mangledname('',procsym.owner,n);
  6168. defaultmangledname:=defaultmangledname+mangledprocparanames(Length(defaultmangledname))
  6169. end;
  6170. function tprocdef.cplusplusmangledname : TSymStr;
  6171. function getcppparaname(p : tdef) : TSymStr;
  6172. const
  6173. {$ifdef NAMEMANGLING_GCC2}
  6174. ordtype2str : array[tordtype] of string[2] = (
  6175. '',
  6176. 'Uc','Us','Ui','Us','',
  6177. 'Sc','s','i','x','',
  6178. 'b','b','b','b','b','b',
  6179. 'c','w','x');
  6180. {$else NAMEMANGLING_GCC2}
  6181. ordtype2str : array[tordtype] of string[1] = (
  6182. 'v',
  6183. 'h','t','j','y','',
  6184. 'a','s','i','x','',
  6185. 'b','b','b','b','b',
  6186. 'b','b','b','b',
  6187. 'c','w','x','C');
  6188. floattype2str : array[tfloattype] of string[1] = (
  6189. 'f','d','e','e',
  6190. 'd','d','g');
  6191. {$endif NAMEMANGLING_GCC2}
  6192. var
  6193. s : TSymStr;
  6194. begin
  6195. case p.typ of
  6196. orddef:
  6197. begin
  6198. s:=ordtype2str[torddef(p).ordtype];
  6199. if s='C' then
  6200. s:=ordtype2str[range_to_basetype(torddef(p).low,torddef(p).high)];
  6201. end;
  6202. pointerdef:
  6203. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  6204. {$ifndef NAMEMANGLING_GCC2}
  6205. floatdef:
  6206. s:=floattype2str[tfloatdef(p).floattype];
  6207. {$endif NAMEMANGLING_GCC2}
  6208. else
  6209. internalerror(2103001);
  6210. end;
  6211. getcppparaname:=s;
  6212. end;
  6213. var
  6214. s,s2 : TSymStr;
  6215. hp : TParavarsym;
  6216. i : integer;
  6217. begin
  6218. {$ifdef NAMEMANGLING_GCC2}
  6219. { outdated gcc 2.x name mangling scheme }
  6220. s := procsym.realname;
  6221. if procsym.owner.symtabletype=ObjectSymtable then
  6222. begin
  6223. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  6224. case proctypeoption of
  6225. potype_destructor:
  6226. s:='_$_'+tostr(length(s2))+s2;
  6227. potype_constructor:
  6228. s:='___'+tostr(length(s2))+s2;
  6229. else
  6230. s:='_'+s+'__'+tostr(length(s2))+s2;
  6231. end;
  6232. end
  6233. else s:=s+'__';
  6234. s:=s+'F';
  6235. { concat modifiers }
  6236. { !!!!! }
  6237. { now we handle the parameters }
  6238. if maxparacount>0 then
  6239. begin
  6240. for i:=0 to paras.count-1 do
  6241. begin
  6242. hp:=tparavarsym(paras[i]);
  6243. { no hidden parameters form part of a C++ mangled name:
  6244. a) self is not included
  6245. b) there are no "high" or other hidden parameters
  6246. }
  6247. if vo_is_hidden_para in hp.varoptions then
  6248. continue;
  6249. s2:=getcppparaname(hp.vardef);
  6250. if hp.varspez in [vs_var,vs_out] then
  6251. s2:='R'+s2;
  6252. s:=s+s2;
  6253. end;
  6254. end
  6255. else
  6256. s:=s+'v';
  6257. cplusplusmangledname:=s;
  6258. {$else NAMEMANGLING_GCC2}
  6259. { gcc 3.x and 4.x name mangling scheme }
  6260. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  6261. if procsym.owner.symtabletype=ObjectSymtable then
  6262. begin
  6263. s:='_ZN';
  6264. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  6265. s:=s+tostr(length(s2))+s2;
  6266. case proctypeoption of
  6267. potype_constructor:
  6268. s:=s+'C1';
  6269. potype_destructor:
  6270. s:=s+'D1';
  6271. else
  6272. s:=s+tostr(length(procsym.realname))+procsym.realname;
  6273. end;
  6274. s:=s+'E';
  6275. end
  6276. else
  6277. s:=procsym.realname;
  6278. { now we handle the parameters }
  6279. if maxparacount>0 then
  6280. begin
  6281. for i:=0 to paras.count-1 do
  6282. begin
  6283. hp:=tparavarsym(paras[i]);
  6284. { no hidden parameters form part of a C++ mangled name:
  6285. a) self is not included
  6286. b) there are no "high" or other hidden parameters
  6287. }
  6288. if vo_is_hidden_para in hp.varoptions then
  6289. continue;
  6290. s2:=getcppparaname(hp.vardef);
  6291. if hp.varspez in [vs_var,vs_out] then
  6292. s2:='R'+s2;
  6293. s:=s+s2;
  6294. end;
  6295. end
  6296. else
  6297. s:=s+'v';
  6298. cplusplusmangledname:=s;
  6299. {$endif NAMEMANGLING_GCC2}
  6300. end;
  6301. function tprocdef.objcmangledname : TSymStr;
  6302. var
  6303. manglednamelen: longint;
  6304. iscatmethod : boolean;
  6305. begin
  6306. if not (po_msgstr in procoptions) then
  6307. internalerror(2009030901);
  6308. { we may very well need longer strings to handle these... }
  6309. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  6310. length('+"[ ]"')+length(messageinf.str^);
  6311. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  6312. if (iscatmethod) then
  6313. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  6314. if manglednamelen>255 then
  6315. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  6316. if not(po_classmethod in procoptions) then
  6317. result:='"-['
  6318. else
  6319. result:='"+[';
  6320. { quotes are necessary because the +/- otherwise confuse the assembler
  6321. into expecting a number
  6322. }
  6323. if iscatmethod then
  6324. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  6325. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  6326. if iscatmethod then
  6327. result:=result+')';
  6328. result:=result+' '+messageinf.str^+']"';
  6329. end;
  6330. procedure tprocdef.setmangledname(const s : TSymStr);
  6331. begin
  6332. { This is not allowed anymore, the forward declaration
  6333. already needs to create the correct mangledname, no changes
  6334. afterwards are allowed (PFV) }
  6335. { Exception: interface definitions in mode macpas, since in that }
  6336. { case no reference to the old name can exist yet (JM) }
  6337. {$ifdef symansistr}
  6338. if _mangledname<>'' then
  6339. if ((m_mac in current_settings.modeswitches) and
  6340. (interfacedef)) then
  6341. _mangledname:=''
  6342. else
  6343. internalerror(200411171);
  6344. {$else symansistr}
  6345. if assigned(_mangledname) then
  6346. if ((m_mac in current_settings.modeswitches) and
  6347. (interfacedef)) then
  6348. stringdispose(_mangledname)
  6349. else
  6350. internalerror(200411171);
  6351. {$endif symansistr}
  6352. {$ifdef jvm}
  6353. { this routine can be called for compilerproces. can't set mangled
  6354. name since it must be calculated, but it uses import_name when set
  6355. -> set that one }
  6356. import_name:=stringdup(s);
  6357. include(procoptions,po_has_importname);
  6358. include(procoptions,po_has_mangledname);
  6359. {$else}
  6360. {$ifdef symansistr}
  6361. _mangledname:=s;
  6362. {$else symansistr}
  6363. _mangledname:=stringdup(s);
  6364. {$endif symansistr}
  6365. {$endif jvm}
  6366. include(procoptions,po_has_mangledname);
  6367. end;
  6368. function tprocdef.needsglobalasmsym: boolean;
  6369. begin
  6370. result:=
  6371. (cs_profile in current_settings.moduleswitches) or
  6372. { smart linking using a library requires to promote
  6373. all non-nested procedures to AB_GLOBAL
  6374. otherwise you get undefined symbol error at linking
  6375. for msdos target with -CX option for instance }
  6376. (create_smartlink_library and not is_nested_pd(self)) or
  6377. (po_global in procoptions);
  6378. end;
  6379. procedure tprocdef.setcompilerprocname;
  6380. begin
  6381. procsym.realname:='$'+lower(procsym.name);
  6382. end;
  6383. {***************************************************************************
  6384. TPROCVARDEF
  6385. ***************************************************************************}
  6386. constructor tprocvardef.create(level:byte);
  6387. begin
  6388. inherited create(procvardef,level,true);
  6389. end;
  6390. class function tprocvardef.getreusableprocaddr(def: tabstractprocdef; copytyp: tcacheableproccopytyp): tprocvardef;
  6391. var
  6392. res: PHashSetItem;
  6393. oldsymtablestack: tsymtablestack;
  6394. key: packed record
  6395. def: tabstractprocdef;
  6396. copytyp: tcacheableproccopytyp;
  6397. end;
  6398. begin
  6399. if not assigned(current_module) then
  6400. internalerror(2011081302);
  6401. key.def:=def;
  6402. key.copytyp:=copytyp;
  6403. res:=current_module.procaddrdefs.FindOrAdd(@key,sizeof(key));
  6404. if not assigned(res^.Data) then
  6405. begin
  6406. { since these pointerdefs can be reused anywhere in the current
  6407. unit, add them to the global/staticsymtable (or local symtable
  6408. if they're a local def, because otherwise they'll be saved
  6409. to the ppu referencing a local symtable entry that doesn't
  6410. exist in the ppu) }
  6411. oldsymtablestack:=symtablestack;
  6412. { do not simply push/pop current_module.localsymtable, because
  6413. that can have side-effects (e.g., it removes helpers) }
  6414. symtablestack:=nil;
  6415. result:=tprocvardef(def.getcopyas(procvardef,copytyp,''));
  6416. setup_reusable_def(def,result,res,oldsymtablestack);
  6417. { res^.Data may still be nil -> don't overwrite result }
  6418. exit;
  6419. end;
  6420. result:=tprocvardef(res^.Data);
  6421. end;
  6422. class function tprocvardef.getreusableprocaddr_no_free(def: tabstractprocdef): tprocvardef;
  6423. begin
  6424. result:=getreusableprocaddr(def,pc_address_only);
  6425. if not result.is_registered then
  6426. include(result.defoptions,df_not_registered_no_free);
  6427. end;
  6428. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  6429. begin
  6430. inherited ppuload(procvardef,ppufile);
  6431. { load para symtable }
  6432. parast:=tparasymtable.create(self,ppufile.getbyte);
  6433. ppuload_platform(ppufile);
  6434. tparasymtable(parast).ppuload(ppufile);
  6435. end;
  6436. function tprocvardef.getcopy : tstoreddef;
  6437. var
  6438. i : tcallercallee;
  6439. j : longint;
  6440. begin
  6441. result:=cprocvardef.create(parast.symtablelevel);
  6442. tprocvardef(result).returndef:=returndef;
  6443. tprocvardef(result).returndefderef:=returndefderef;
  6444. tprocvardef(result).parast:=parast.getcopy;
  6445. tprocvardef(result).savesize:=savesize;
  6446. { create paralist copy }
  6447. calcparas;
  6448. tprocvardef(result).paras:=tparalist.create(false);
  6449. tprocvardef(result).paras.count:=paras.count;
  6450. for j:=0 to paras.count-1 do
  6451. tprocvardef(result).paras[j]:=paras[j];
  6452. tprocvardef(result).proctypeoption:=proctypeoption;
  6453. tprocvardef(result).proccalloption:=proccalloption;
  6454. tprocvardef(result).procoptions:=procoptions;
  6455. tprocvardef(result).callerargareasize:=callerargareasize;
  6456. tprocvardef(result).calleeargareasize:=calleeargareasize;
  6457. tprocvardef(result).maxparacount:=maxparacount;
  6458. tprocvardef(result).minparacount:=minparacount;
  6459. for i:=low(funcretloc) to high(funcretloc) do
  6460. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  6461. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  6462. {$ifdef m68k}
  6463. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  6464. {$endif}
  6465. end;
  6466. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  6467. begin
  6468. inherited ppuwrite(ppufile);
  6469. { Save the para symtable level (necessary to distinguish nested
  6470. procvars) }
  6471. ppufile.putbyte(parast.symtablelevel);
  6472. { Write this entry }
  6473. writeentry(ppufile,ibprocvardef);
  6474. { Save the para symtable, this is taken from the interface }
  6475. tparasymtable(parast).ppuwrite(ppufile);
  6476. end;
  6477. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  6478. begin
  6479. case t of
  6480. gs_para :
  6481. GetSymtable:=parast;
  6482. else
  6483. GetSymtable:=nil;
  6484. end;
  6485. end;
  6486. function tprocvardef.size : asizeint;
  6487. begin
  6488. { we return false for is_addressonly for a block (because it's not a
  6489. simple pointer to a function), but they are handled as implicit
  6490. pointers to a datastructure that contains everything ->
  6491. voidpointertype.size instead of voidcodepointertype.size }
  6492. if po_is_block in procoptions then
  6493. size:=voidpointertype.size
  6494. else if not is_addressonly then
  6495. begin
  6496. if is_nested_pd(self) then
  6497. size:=voidcodepointertype.size+parentfpvoidpointertype.size
  6498. else
  6499. size:=voidcodepointertype.size+voidpointertype.size;
  6500. end
  6501. else
  6502. size:=voidcodepointertype.size;
  6503. end;
  6504. function tprocvardef.is_methodpointer:boolean;
  6505. begin
  6506. result:=(po_methodpointer in procoptions);
  6507. end;
  6508. function tprocvardef.is_addressonly:boolean;
  6509. begin
  6510. result:=((not(po_methodpointer in procoptions) or (po_staticmethod in procoptions)) and
  6511. not(po_is_block in procoptions) and
  6512. not is_nested_pd(self)) or
  6513. (po_addressonly in procoptions);
  6514. end;
  6515. function tprocvardef.getmangledparaname:TSymStr;
  6516. begin
  6517. if not(po_methodpointer in procoptions) then
  6518. if not is_nested_pd(self) then
  6519. result:='procvar'
  6520. else
  6521. { we need the manglednames here, because nestedprocvars can be anonymous, e.g.
  6522. having not a type name or not an unique one, see webtbs/tw27515.pp
  6523. Further, use $_ ... _$ delimiters to avoid ambiguous names, see webtbs/tw27515.pp }
  6524. result:='$_nestedprovar'+mangledprocparanames(0)+'_$'
  6525. else
  6526. result:='procvarofobj'
  6527. end;
  6528. function tprocvardef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string): tstoreddef;
  6529. begin
  6530. result:=inherited;
  6531. tabstractprocdef(result).calcparas;
  6532. end;
  6533. function tprocvardef.is_publishable : boolean;
  6534. begin
  6535. is_publishable:=(po_methodpointer in procoptions);
  6536. end;
  6537. function tprocvardef.GetTypeName : string;
  6538. var
  6539. s: string;
  6540. pno : tprocnameoptions;
  6541. begin
  6542. {$ifdef EXTDEBUG}
  6543. pno:=[pno_showhidden];
  6544. {$else EXTDEBUG}
  6545. pno:=[];
  6546. {$endif EXTDEBUG}
  6547. s:='<';
  6548. if po_is_block in procoptions then
  6549. s := s+'reference to'
  6550. else if po_classmethod in procoptions then
  6551. s := s+'class method type of'
  6552. else
  6553. if po_addressonly in procoptions then
  6554. s := s+'address of'
  6555. else
  6556. s := s+'procedure variable type of';
  6557. if assigned(returndef) and
  6558. (returndef<>voidtype) then
  6559. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  6560. else
  6561. s:=s+' procedure'+typename_paras(pno);
  6562. if po_methodpointer in procoptions then
  6563. s := s+' of object';
  6564. if is_nested_pd(self) then
  6565. s := s+' is nested';
  6566. if po_far in procoptions then
  6567. s := s+';far';
  6568. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  6569. end;
  6570. {***************************************************************************
  6571. TOBJECTDEF
  6572. ***************************************************************************}
  6573. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef;doregister:boolean);
  6574. begin
  6575. inherited create(n,objectdef,doregister);
  6576. fcurrent_dispid:=0;
  6577. objecttype:=ot;
  6578. childof:=nil;
  6579. childofderef.reset;
  6580. vmt_fieldderef.reset;
  6581. extendeddefderef.reset;
  6582. cloneddefderef.reset;
  6583. if objecttype=odt_helper then
  6584. owner.includeoption(sto_has_helper);
  6585. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords,
  6586. current_settings.alignment.recordalignmin);
  6587. { create space for vmt !! }
  6588. vmtentries:=TFPList.Create;
  6589. set_parent(c);
  6590. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  6591. prepareguid;
  6592. { setup implemented interfaces }
  6593. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  6594. ImplementedInterfaces:=TFPObjectList.Create(true)
  6595. else
  6596. ImplementedInterfaces:=nil;
  6597. writing_class_record_dbginfo:=false;
  6598. end;
  6599. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  6600. var
  6601. i,
  6602. implintfcount : longint;
  6603. d, getterd : tderef;
  6604. ImplIntf : TImplementedInterface;
  6605. vmtentry : pvmtentry;
  6606. begin
  6607. inherited ppuload(objectdef,ppufile);
  6608. objecttype:=tobjecttyp(ppufile.getbyte);
  6609. helpertype:=thelpertype(ppufile.getbyte);
  6610. objextname:=ppufile.getpshortstring;
  6611. { only used for external Objective-C classes/protocols }
  6612. if (objextname^='') then
  6613. stringdispose(objextname);
  6614. symtable:=tObjectSymtable.create(self,objrealname^,0,0);
  6615. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  6616. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  6617. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  6618. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  6619. tObjectSymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  6620. ppufile.getderef(vmt_fieldderef);
  6621. ppufile.getderef(childofderef);
  6622. { load guid }
  6623. iidstr:=nil;
  6624. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  6625. begin
  6626. new(iidguid);
  6627. ppufile.getguid(iidguid^);
  6628. iidstr:=ppufile.getpshortstring;
  6629. end;
  6630. abstractcnt:=ppufile.getlongint;
  6631. if objecttype=odt_helper then
  6632. ppufile.getderef(extendeddefderef);
  6633. vmtentries:=TFPList.Create;
  6634. vmtentries.count:=ppufile.getlongint;
  6635. for i:=0 to vmtentries.count-1 do
  6636. begin
  6637. ppufile.getderef(d);
  6638. new(vmtentry);
  6639. vmtentry^.procdef:=nil;
  6640. vmtentry^.procdefderef:=d;
  6641. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  6642. vmtentries[i]:=vmtentry;
  6643. end;
  6644. { load implemented interfaces }
  6645. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  6646. begin
  6647. ImplementedInterfaces:=TFPObjectList.Create(true);
  6648. implintfcount:=ppufile.getlongint;
  6649. for i:=0 to implintfcount-1 do
  6650. begin
  6651. ppufile.getderef(d);
  6652. ppufile.getderef(getterd);
  6653. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  6654. ImplIntf.IOffset:=ppufile.getlongint;
  6655. byte(ImplIntf.IType):=ppufile.getbyte;
  6656. ImplementedInterfaces.Add(ImplIntf);
  6657. end;
  6658. end
  6659. else
  6660. ImplementedInterfaces:=nil;
  6661. if df_copied_def in defoptions then
  6662. begin
  6663. ppufile.getderef(cloneddefderef);
  6664. ppuload_platform(ppufile);
  6665. end
  6666. else
  6667. begin
  6668. ppuload_platform(ppufile);
  6669. tObjectSymtable(symtable).ppuload(ppufile);
  6670. end;
  6671. { handles the predefined class tobject }
  6672. { the last TOBJECT which is loaded gets }
  6673. { it ! }
  6674. if (childof=nil) and
  6675. (objecttype in [odt_class,odt_javaclass]) and
  6676. (objname^='TOBJECT') then
  6677. class_tobject:=self;
  6678. if (childof=nil) and
  6679. (objecttype=odt_interfacecom) then
  6680. if (objname^='IUNKNOWN') then
  6681. interface_iunknown:=self
  6682. else
  6683. if (objname^='IDISPATCH') then
  6684. interface_idispatch:=self;
  6685. if (childof=nil) and
  6686. (objecttype=odt_objcclass) and
  6687. (objname^='PROTOCOL') then
  6688. objc_protocoltype:=self;
  6689. if (objecttype=odt_javaclass) and
  6690. not(oo_is_formal in objectoptions) then
  6691. begin
  6692. if (objname^='JLOBJECT') then
  6693. java_jlobject:=self
  6694. else if (objname^='JLTHROWABLE') then
  6695. java_jlthrowable:=self
  6696. else if (objname^='FPCBASERECORDTYPE') then
  6697. java_fpcbaserecordtype:=self
  6698. else if (objname^='JLSTRING') then
  6699. java_jlstring:=self
  6700. else if (objname^='ANSISTRINGCLASS') then
  6701. java_ansistring:=self
  6702. else if (objname^='SHORTSTRINGCLASS') then
  6703. java_shortstring:=self
  6704. else if (objname^='JLENUM') then
  6705. java_jlenum:=self
  6706. else if (objname^='JUENUMSET') then
  6707. java_juenumset:=self
  6708. else if (objname^='FPCBITSET') then
  6709. java_jubitset:=self
  6710. else if (objname^='FPCBASEPROCVARTYPE') then
  6711. java_procvarbase:=self;
  6712. end;
  6713. writing_class_record_dbginfo:=false;
  6714. end;
  6715. destructor tobjectdef.destroy;
  6716. var
  6717. i: longint;
  6718. begin
  6719. if assigned(symtable) then
  6720. begin
  6721. symtable.free;
  6722. symtable:=nil;
  6723. end;
  6724. stringdispose(objextname);
  6725. stringdispose(iidstr);
  6726. if assigned(ImplementedInterfaces) then
  6727. begin
  6728. ImplementedInterfaces.free;
  6729. ImplementedInterfaces:=nil;
  6730. end;
  6731. if assigned(iidguid) then
  6732. begin
  6733. dispose(iidguid);
  6734. iidguid:=nil;
  6735. end;
  6736. if assigned(vmtentries) then
  6737. begin
  6738. for i:=0 to vmtentries.count-1 do
  6739. dispose(pvmtentry(vmtentries[i]));
  6740. vmtentries.free;
  6741. vmtentries:=nil;
  6742. end;
  6743. if assigned(vmcallstaticinfo) then
  6744. begin
  6745. freemem(vmcallstaticinfo);
  6746. vmcallstaticinfo:=nil;
  6747. end;
  6748. inherited destroy;
  6749. end;
  6750. function tobjectdef.getcopy : tstoreddef;
  6751. var
  6752. i : longint;
  6753. begin
  6754. result:=cobjectdef.create(objecttype,objrealname^,childof,true);
  6755. { the constructor allocates a symtable which we release to avoid memory leaks }
  6756. tobjectdef(result).symtable.free;
  6757. tobjectdef(result).symtable:=symtable.getcopy;
  6758. if assigned(objextname) then
  6759. tobjectdef(result).objextname:=stringdup(objextname^);
  6760. if assigned(import_lib) then
  6761. tobjectdef(result).import_lib:=stringdup(import_lib^);
  6762. tobjectdef(result).objectoptions:=objectoptions;
  6763. include(tobjectdef(result).defoptions,df_copied_def);
  6764. tobjectdef(result).extendeddef:=extendeddef;
  6765. if assigned(tcinitcode) then
  6766. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  6767. tobjectdef(result).vmt_field:=vmt_field;
  6768. if assigned(iidguid) then
  6769. begin
  6770. new(tobjectdef(result).iidguid);
  6771. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  6772. end;
  6773. if assigned(iidstr) then
  6774. tobjectdef(result).iidstr:=stringdup(iidstr^);
  6775. tobjectdef(result).abstractcnt:=abstractcnt;
  6776. if assigned(ImplementedInterfaces) then
  6777. begin
  6778. for i:=0 to ImplementedInterfaces.count-1 do
  6779. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  6780. end;
  6781. tobjectdef(result).copyvmtentries(self);
  6782. end;
  6783. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  6784. var
  6785. i : longint;
  6786. vmtentry : pvmtentry;
  6787. ImplIntf : TImplementedInterface;
  6788. old_do_indirect_crc: boolean;
  6789. begin
  6790. { if class1 in unit A changes, and class2 in unit B inherits from it
  6791. (so unit B uses unit A), then unit B with class2 will be recompiled.
  6792. However, if there is also a class3 in unit C that only depends on
  6793. unit B, then unit C will not be recompiled because nothing changed
  6794. to the interface of unit B. Nevertheless, unit C can indirectly
  6795. depend on unit A via derefs, and these must be updated -> the
  6796. indirect crc keeps track of such changes. }
  6797. old_do_indirect_crc:=ppufile.do_indirect_crc;
  6798. ppufile.do_indirect_crc:=true;
  6799. inherited ppuwrite(ppufile);
  6800. ppufile.putbyte(byte(objecttype));
  6801. ppufile.putbyte(byte(helpertype));
  6802. if assigned(objextname) then
  6803. ppufile.putstring(objextname^)
  6804. else
  6805. ppufile.putstring('');
  6806. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  6807. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  6808. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  6809. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  6810. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignmin));
  6811. ppufile.putderef(vmt_fieldderef);
  6812. ppufile.putderef(childofderef);
  6813. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  6814. begin
  6815. ppufile.putguid(iidguid^);
  6816. ppufile.putstring(iidstr^);
  6817. end;
  6818. ppufile.putlongint(abstractcnt);
  6819. if objecttype=odt_helper then
  6820. ppufile.putderef(extendeddefderef);
  6821. ppufile.putlongint(vmtentries.count);
  6822. for i:=0 to vmtentries.count-1 do
  6823. begin
  6824. vmtentry:=pvmtentry(vmtentries[i]);
  6825. ppufile.putderef(vmtentry^.procdefderef);
  6826. ppufile.putbyte(byte(vmtentry^.visibility));
  6827. end;
  6828. if assigned(ImplementedInterfaces) then
  6829. begin
  6830. ppufile.putlongint(ImplementedInterfaces.Count);
  6831. for i:=0 to ImplementedInterfaces.Count-1 do
  6832. begin
  6833. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  6834. ppufile.putderef(ImplIntf.intfdefderef);
  6835. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  6836. ppufile.putlongint(ImplIntf.Ioffset);
  6837. ppufile.putbyte(byte(ImplIntf.IType));
  6838. end;
  6839. end;
  6840. if df_copied_def in defoptions then
  6841. ppufile.putderef(cloneddefderef);
  6842. writeentry(ppufile,ibobjectdef);
  6843. if not(df_copied_def in defoptions) then
  6844. tObjectSymtable(symtable).ppuwrite(ppufile);
  6845. ppufile.do_indirect_crc:=old_do_indirect_crc;
  6846. end;
  6847. function tobjectdef.GetTypeName:string;
  6848. begin
  6849. { in this case we will go in endless recursion, because then }
  6850. { there is no tsym associated yet with the def. It can occur }
  6851. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  6852. { instead of the actual type name }
  6853. if not assigned(typesym) then
  6854. result:='<Currently Parsed Class>'
  6855. else
  6856. result:=typesymbolprettyname;
  6857. end;
  6858. procedure tobjectdef.buildderef;
  6859. var
  6860. i : longint;
  6861. vmtentry : pvmtentry;
  6862. begin
  6863. inherited buildderef;
  6864. vmt_fieldderef.build(vmt_field);
  6865. childofderef.build(childof);
  6866. if df_copied_def in defoptions then
  6867. cloneddefderef.build(symtable.defowner)
  6868. else
  6869. tstoredsymtable(symtable).buildderef;
  6870. if objecttype=odt_helper then
  6871. extendeddefderef.build(extendeddef);
  6872. for i:=0 to vmtentries.count-1 do
  6873. begin
  6874. vmtentry:=pvmtentry(vmtentries[i]);
  6875. vmtentry^.procdefderef.build(vmtentry^.procdef);
  6876. end;
  6877. if assigned(ImplementedInterfaces) then
  6878. begin
  6879. for i:=0 to ImplementedInterfaces.count-1 do
  6880. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  6881. end;
  6882. end;
  6883. procedure tobjectdef.deref;
  6884. var
  6885. i : longint;
  6886. vmtentry : pvmtentry;
  6887. begin
  6888. inherited deref;
  6889. vmt_field:=tsym(vmt_fieldderef.resolve);
  6890. childof:=tobjectdef(childofderef.resolve);
  6891. if df_copied_def in defoptions then
  6892. begin
  6893. cloneddef:=tobjectdef(cloneddefderef.resolve);
  6894. symtable:=cloneddef.symtable.getcopy;
  6895. end
  6896. else
  6897. tstoredsymtable(symtable).deref(false);
  6898. if objecttype=odt_helper then
  6899. extendeddef:=tdef(extendeddefderef.resolve);
  6900. for i:=0 to vmtentries.count-1 do
  6901. begin
  6902. vmtentry:=pvmtentry(vmtentries[i]);
  6903. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  6904. end;
  6905. if assigned(ImplementedInterfaces) then
  6906. begin
  6907. for i:=0 to ImplementedInterfaces.count-1 do
  6908. TImplementedInterface(ImplementedInterfaces[i]).deref;
  6909. end;
  6910. end;
  6911. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  6912. var
  6913. pd: tprocdef absolute def;
  6914. st: tsymtable;
  6915. psym: tsym;
  6916. nname: TIDString;
  6917. begin
  6918. if (tdef(def).typ<>procdef) then
  6919. exit;
  6920. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  6921. owner = symtable in which objcclassdef is defined
  6922. }
  6923. st:=pd.owner.defowner.owner;
  6924. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  6925. { check for an existing procsym with our special name }
  6926. psym:=tsym(st.find(nname));
  6927. if not assigned(psym) then
  6928. begin
  6929. psym:=cprocsym.create(nname);
  6930. { avoid warning about this symbol being unused }
  6931. psym.IncRefCount;
  6932. { don't check for duplicates:
  6933. a) we checked above
  6934. b) in case we are in the implementation section of a unit, this
  6935. will also check for this symbol in the interface section
  6936. (since you normally cannot have symbols with the same name
  6937. both interface and implementation), and it's possible to
  6938. have class helpers for the same class in the interface and
  6939. in the implementation, and they cannot be merged since only
  6940. the once in the interface must be saved to the ppu/visible
  6941. from other units }
  6942. st.insert(psym,false);
  6943. end
  6944. else if (psym.typ<>procsym) then
  6945. internalerror(2009111501);
  6946. { add ourselves to this special procsym }
  6947. tprocsym(psym).procdeflist.add(def);
  6948. end;
  6949. procedure tobjectdef.derefimpl;
  6950. begin
  6951. inherited derefimpl;
  6952. { the procdefs are not owned by the class helper procsyms, so they
  6953. are not stored/restored either -> re-add them here }
  6954. if (objecttype in [odt_objcclass,odt_objcprotocol]) or
  6955. (oo_is_classhelper in objectoptions) then
  6956. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  6957. end;
  6958. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  6959. var
  6960. i : longint;
  6961. vmtentry : pvmtentry;
  6962. begin
  6963. if vmtentries.count<>0 then
  6964. internalerror(2019081401);
  6965. vmtentries.count:=objdef.vmtentries.count;
  6966. for i:=0 to objdef.vmtentries.count-1 do
  6967. begin
  6968. new(vmtentry);
  6969. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  6970. vmtentries[i]:=vmtentry;
  6971. end;
  6972. end;
  6973. function tobjectdef.getparentdef:tdef;
  6974. begin
  6975. { TODO: Remove getparentdef hack}
  6976. { With 2 forward declared classes with the child class before the
  6977. parent class the child class is written earlier to the ppu. Leaving it
  6978. possible to have a reference to the parent class for property overriding,
  6979. but the parent class still has the childof not resolved yet (PFV) }
  6980. if childof=nil then
  6981. childof:=tobjectdef(childofderef.resolve);
  6982. result:=childof;
  6983. end;
  6984. procedure tobjectdef.prepareguid;
  6985. begin
  6986. { set up guid }
  6987. if not assigned(iidguid) then
  6988. begin
  6989. new(iidguid);
  6990. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  6991. end;
  6992. { setup iidstring }
  6993. if not assigned(iidstr) then
  6994. iidstr:=stringdup(''); { default is empty string }
  6995. end;
  6996. procedure tobjectdef.set_parent( c : tobjectdef);
  6997. begin
  6998. if assigned(childof) then
  6999. exit;
  7000. childof:=c;
  7001. if not assigned(c) then
  7002. exit;
  7003. { inherit options and status }
  7004. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  7005. { initially has the same number of abstract methods as the parent }
  7006. abstractcnt:=c.abstractcnt;
  7007. { add the data of the anchestor class/object }
  7008. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  7009. begin
  7010. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  7011. { inherit recordalignment }
  7012. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  7013. { if both the parent and this record use C-alignment, also inherit
  7014. the current field alignment }
  7015. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  7016. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  7017. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  7018. { the padding is not inherited for Objective-C classes (maybe not
  7019. for cppclass either?) }
  7020. if objecttype=odt_objcclass then
  7021. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  7022. if (oo_has_vmt in objectoptions) and
  7023. (oo_has_vmt in c.objectoptions) then
  7024. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  7025. { if parent has a vmt field then the offset is the same for the child PM }
  7026. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  7027. begin
  7028. vmt_field:=c.vmt_field;
  7029. include(objectoptions,oo_has_vmt);
  7030. end;
  7031. end;
  7032. end;
  7033. procedure tobjectdef.insertvmt;
  7034. begin
  7035. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  7036. exit;
  7037. if (oo_has_vmt in objectoptions) then
  7038. internalerror(2020100816)
  7039. else
  7040. begin
  7041. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  7042. tObjectSymtable(symtable).fieldalignment);
  7043. if (tf_requires_proper_alignment in target_info.flags) then
  7044. begin
  7045. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  7046. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  7047. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  7048. end;
  7049. vmt_field:=cfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  7050. hidesym(vmt_field);
  7051. tObjectSymtable(symtable).insert(vmt_field);
  7052. tObjectSymtable(symtable).addfield(tfieldvarsym(vmt_field),vis_hidden);
  7053. include(objectoptions,oo_has_vmt);
  7054. end;
  7055. end;
  7056. function tobjectdef.vmt_offset: asizeint;
  7057. begin
  7058. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  7059. result:=0
  7060. else if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  7061. result:=tfieldvarsym(vmt_field).fieldoffset
  7062. else
  7063. result:=tfieldvarsym(vmt_field).fieldoffset div 8;
  7064. end;
  7065. procedure tobjectdef.check_forwards;
  7066. begin
  7067. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  7068. inherited;
  7069. if (oo_is_forward in objectoptions) then
  7070. begin
  7071. { ok, in future, the forward can be resolved }
  7072. Message1(sym_e_class_forward_not_resolved,objrealname^);
  7073. exclude(objectoptions,oo_is_forward);
  7074. end;
  7075. end;
  7076. function tobjectdef.find_destructor: tprocdef;
  7077. var
  7078. objdef: tobjectdef;
  7079. begin
  7080. objdef:=self;
  7081. while assigned(objdef) do
  7082. begin
  7083. result:=objdef.find_procdef_bytype(potype_destructor);
  7084. if assigned(result) then
  7085. exit;
  7086. objdef:=objdef.childof;
  7087. end;
  7088. result:=nil;
  7089. end;
  7090. function tobjectdef.implements_any_interfaces: boolean;
  7091. begin
  7092. result := (ImplementedInterfaces.Count > 0) or
  7093. (assigned(childof) and childof.implements_any_interfaces);
  7094. end;
  7095. function tobjectdef.size : asizeint;
  7096. begin
  7097. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  7098. result:=voidpointertype.size
  7099. else
  7100. result:=tObjectSymtable(symtable).datasize;
  7101. end;
  7102. function tobjectdef.alignment:shortint;
  7103. begin
  7104. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  7105. alignment:=voidpointertype.alignment
  7106. else
  7107. alignment:=tObjectSymtable(symtable).recordalignment;
  7108. end;
  7109. function tobjectdef.vmtmethodoffset(index:longint):longint;
  7110. begin
  7111. { for offset of methods for classes, see rtl/inc/objpash.inc }
  7112. case objecttype of
  7113. odt_class:
  7114. { the +2*sizeof(pint) is size and -size }
  7115. vmtmethodoffset:=index*voidcodepointertype.size+10*voidpointertype.size+2*sizeof(pint);
  7116. odt_helper,
  7117. odt_objcclass,
  7118. odt_objcprotocol:
  7119. vmtmethodoffset:=0;
  7120. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  7121. vmtmethodoffset:=index*voidcodepointertype.size;
  7122. odt_javaclass,
  7123. odt_interfacejava:
  7124. { invalid }
  7125. vmtmethodoffset:=-1;
  7126. else
  7127. { the +2*sizeof(pint) is size and -size }
  7128. {$ifdef WITHDMT}
  7129. vmtmethodoffset:=index*voidcodepointertype.size+2*voidpointertype.size+2*sizeof(pint);
  7130. {$else WITHDMT}
  7131. vmtmethodoffset:=index*voidcodepointertype.size+1*voidpointertype.size+2*sizeof(pint);
  7132. {$endif WITHDMT}
  7133. end;
  7134. end;
  7135. function tobjectdef.vmt_mangledname : TSymStr;
  7136. begin
  7137. if not(oo_has_vmt in objectoptions) then
  7138. Message1(parser_n_object_has_no_vmt,objrealname^);
  7139. if not is_unique_objpasdef then
  7140. vmt_mangledname:=make_mangledname('VMT',owner,objname^)
  7141. else
  7142. vmt_mangledname:=childof.vmt_mangledname;
  7143. end;
  7144. function tobjectdef.vmt_def: trecorddef;
  7145. var
  7146. where: tsymtable;
  7147. vmttypesym: tsymentry;
  7148. begin
  7149. if not is_unique_objpasdef then
  7150. begin
  7151. where:=get_top_level_symtable(true);
  7152. vmttypesym:=where.Find('vmtdef$'+mangledparaname);
  7153. if not assigned(vmttypesym) or
  7154. (vmttypesym.typ<>symconst.typesym) or
  7155. (ttypesym(vmttypesym).typedef.typ<>recorddef) then
  7156. internalerror(2015052501);
  7157. result:=trecorddef(ttypesym(vmttypesym).typedef);
  7158. end
  7159. else
  7160. result:=childof.vmt_def;
  7161. end;
  7162. function tobjectdef.needs_inittable : boolean;
  7163. var
  7164. hp : tobjectdef;
  7165. begin
  7166. case objecttype of
  7167. odt_helper,
  7168. odt_class :
  7169. needs_inittable:=false;
  7170. odt_dispinterface,
  7171. odt_interfacecom:
  7172. needs_inittable:=true;
  7173. odt_interfacecorba:
  7174. begin
  7175. hp:=childof;
  7176. while assigned(hp) do
  7177. begin
  7178. if hp=interface_iunknown then
  7179. begin
  7180. needs_inittable:=true;
  7181. exit;
  7182. end;
  7183. hp:=hp.childof;
  7184. end;
  7185. needs_inittable:=false;
  7186. end;
  7187. odt_object:
  7188. needs_inittable:=
  7189. tObjectSymtable(symtable).needs_init_final or
  7190. (assigned(childof) and
  7191. childof.needs_inittable);
  7192. odt_cppclass,
  7193. odt_objcclass,
  7194. odt_objcprotocol,
  7195. odt_javaclass,
  7196. odt_interfacejava:
  7197. needs_inittable:=false;
  7198. else
  7199. internalerror(200108267);
  7200. end;
  7201. end;
  7202. function tobjectdef.needs_separate_initrtti : boolean;
  7203. begin
  7204. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  7205. end;
  7206. function tobjectdef.has_non_trivial_init_child(check_parent:boolean):boolean;
  7207. begin
  7208. if objecttype in [odt_class,odt_object] then
  7209. begin
  7210. result:=tobjectsymtable(symtable).has_non_trivial_init or
  7211. (check_parent and assigned(childof) and childof.has_non_trivial_init_child(true));
  7212. end
  7213. else
  7214. result:=false;
  7215. end;
  7216. function tobjectdef.rtti_mangledname(rt: trttitype): TSymStr;
  7217. begin
  7218. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  7219. begin
  7220. if not is_unique_objpasdef then
  7221. result:=inherited rtti_mangledname(rt)
  7222. else
  7223. result:=childof.rtti_mangledname(rt)
  7224. end
  7225. else
  7226. begin
  7227. { necessary in case of a dynamic array of nsobject, or
  7228. if an nsobject field appears in a record that needs
  7229. init/finalisation }
  7230. if rt=initrtti then
  7231. begin
  7232. result:=voidpointertype.rtti_mangledname(rt);
  7233. exit;
  7234. end;
  7235. if not(target_info.system in systems_objc_nfabi) then
  7236. begin
  7237. result:=target_asm.labelprefix;
  7238. case objecttype of
  7239. odt_objcclass:
  7240. begin
  7241. case rt of
  7242. objcclassrtti:
  7243. if not(oo_is_classhelper in objectoptions) then
  7244. result:=result+'_OBJC_CLASS_'
  7245. else
  7246. result:=result+'_OBJC_CATEGORY_';
  7247. objcmetartti:
  7248. if not(oo_is_classhelper in objectoptions) then
  7249. result:=result+'_OBJC_METACLASS_'
  7250. else
  7251. internalerror(2009111511);
  7252. else
  7253. internalerror(2009092302);
  7254. end;
  7255. end;
  7256. odt_objcprotocol:
  7257. result:=result+'_OBJC_PROTOCOL_';
  7258. else
  7259. ;
  7260. end;
  7261. end
  7262. else
  7263. begin
  7264. case objecttype of
  7265. odt_objcclass:
  7266. begin
  7267. if (oo_is_classhelper in objectoptions) and
  7268. (rt<>objcclassrtti) then
  7269. internalerror(2009111512);
  7270. case rt of
  7271. objcclassrtti:
  7272. if not(oo_is_classhelper in objectoptions) then
  7273. result:='_OBJC_CLASS_$_'
  7274. else
  7275. result:='_OBJC_$_CATEGORY_';
  7276. objcmetartti:
  7277. result:='_OBJC_METACLASS_$_';
  7278. objcclassrortti:
  7279. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  7280. objcmetarortti:
  7281. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  7282. else
  7283. internalerror(2009092303);
  7284. end;
  7285. end;
  7286. odt_objcprotocol:
  7287. begin
  7288. result:=lower(target_asm.labelprefix);
  7289. case rt of
  7290. objcclassrtti:
  7291. result:=result+'_OBJC_PROTOCOL_$_';
  7292. objcmetartti:
  7293. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  7294. else
  7295. internalerror(2009092501);
  7296. end;
  7297. end;
  7298. else
  7299. internalerror(2013113005);
  7300. end;
  7301. end;
  7302. result:=result+objextname^;
  7303. end;
  7304. end;
  7305. function tobjectdef.is_unique_objpasdef: boolean;
  7306. begin
  7307. result:=
  7308. (df_unique in defoptions) and
  7309. is_class_or_interface_or_dispinterface(self)
  7310. end;
  7311. function tobjectdef.members_need_inittable : boolean;
  7312. begin
  7313. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  7314. end;
  7315. function tobjectdef.is_publishable : boolean;
  7316. begin
  7317. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  7318. end;
  7319. function tobjectdef.get_next_dispid: longint;
  7320. begin
  7321. inc(fcurrent_dispid);
  7322. result:=fcurrent_dispid;
  7323. end;
  7324. function tobjectdef.search_enumerator_get: tprocdef;
  7325. begin
  7326. result:=inherited;
  7327. if not assigned(result) and assigned(childof) then
  7328. result:=childof.search_enumerator_get;
  7329. end;
  7330. function tobjectdef.search_enumerator_move: tprocdef;
  7331. begin
  7332. result:=inherited;
  7333. if not assigned(result) and assigned(childof) then
  7334. result:=childof.search_enumerator_move;
  7335. end;
  7336. function tobjectdef.search_enumerator_current: tsym;
  7337. begin
  7338. result:=inherited;
  7339. if not assigned(result) and assigned(childof) then
  7340. result:=childof.search_enumerator_current;
  7341. end;
  7342. procedure tobjectdef.register_created_classref_type;
  7343. begin
  7344. if not classref_created_in_current_module then
  7345. begin
  7346. classref_created_in_current_module:=true;
  7347. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  7348. end;
  7349. end;
  7350. procedure tobjectdef.register_created_object_type;
  7351. begin
  7352. if not created_in_current_module then
  7353. begin
  7354. created_in_current_module:=true;
  7355. current_module.wpoinfo.addcreatedobjtype(self);
  7356. end;
  7357. end;
  7358. procedure tobjectdef.register_maybe_created_object_type;
  7359. begin
  7360. { if we know it has been created for sure, no need
  7361. to also record that it maybe can be created in
  7362. this module
  7363. }
  7364. if not (created_in_current_module) and
  7365. not (maybe_created_in_current_module) then
  7366. begin
  7367. maybe_created_in_current_module:=true;
  7368. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  7369. end;
  7370. end;
  7371. procedure tobjectdef.register_vmt_call(index: longint);
  7372. begin
  7373. if (is_object(self) or is_class(self)) then
  7374. current_module.wpoinfo.addcalledvmtentry(self,index);
  7375. end;
  7376. procedure check_and_finish_msg(data: tobject; arg: pointer);
  7377. var
  7378. def: tdef absolute data;
  7379. pd: tprocdef absolute data;
  7380. i,
  7381. paracount: longint;
  7382. begin
  7383. if (def.typ=procdef) then
  7384. begin
  7385. { add all messages also under a dummy name to the symtable in
  7386. which the objcclass/protocol/category is declared, so they can
  7387. be called via id.<name>
  7388. }
  7389. create_class_helper_for_procdef(pd,nil);
  7390. { we have to wait until now to set the mangled name because it
  7391. depends on the (possibly external) class name, which is defined
  7392. at the very end. }
  7393. if not(po_msgstr in pd.procoptions) then
  7394. begin
  7395. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  7396. { recover to avoid internalerror later on }
  7397. include(pd.procoptions,po_msgstr);
  7398. pd.messageinf.str:=stringdup('MissingDeclaration');
  7399. end;
  7400. { Mangled name is already set in case this is a copy of
  7401. another type. }
  7402. if not(po_has_mangledname in pd.procoptions) then
  7403. begin
  7404. { check whether the number of formal parameters is correct,
  7405. and whether they have valid Objective-C types }
  7406. paracount:=0;
  7407. for i:=1 to length(pd.messageinf.str^) do
  7408. if pd.messageinf.str^[i]=':' then
  7409. inc(paracount);
  7410. for i:=0 to pd.paras.count-1 do
  7411. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  7412. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  7413. dec(paracount);
  7414. if (paracount<>0) then
  7415. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  7416. pd.setmangledname(pd.objcmangledname);
  7417. end
  7418. else
  7419. { all checks already done }
  7420. exit;
  7421. if not(oo_is_external in pd.struct.objectoptions) then
  7422. begin
  7423. if (po_varargs in pd.procoptions) then
  7424. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  7425. else
  7426. begin
  7427. { check for "array of const" parameters }
  7428. for i:=0 to pd.parast.symlist.count-1 do
  7429. begin
  7430. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  7431. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  7432. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  7433. end;
  7434. end;
  7435. end;
  7436. end;
  7437. end;
  7438. procedure mark_private_fields_used(data: tobject; arg: pointer);
  7439. var
  7440. sym: tsym absolute data;
  7441. begin
  7442. if (sym.typ=fieldvarsym) and
  7443. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  7444. sym.IncRefCount;
  7445. end;
  7446. procedure tobjectdef.finish_objc_data;
  7447. begin
  7448. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  7449. if (oo_is_external in objectoptions) then
  7450. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  7451. end;
  7452. procedure verify_objc_vardef(data: tobject; arg: pointer);
  7453. var
  7454. sym: tabstractvarsym absolute data;
  7455. res: pboolean absolute arg;
  7456. founderrordef: tdef;
  7457. begin
  7458. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  7459. exit;
  7460. if (sym.typ=paravarsym) and
  7461. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  7462. is_array_of_const(tparavarsym(sym).vardef)) then
  7463. exit;
  7464. if not objcchecktype(sym.vardef,founderrordef) then
  7465. begin
  7466. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  7467. res^:=false;
  7468. end;
  7469. end;
  7470. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  7471. var
  7472. def: tdef absolute data;
  7473. res: pboolean absolute arg;
  7474. founderrordef: tdef;
  7475. begin
  7476. if (def.typ<>procdef) then
  7477. exit;
  7478. { check parameter types for validity }
  7479. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  7480. { check the result type for validity }
  7481. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  7482. begin
  7483. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  7484. res^:=false;
  7485. end;
  7486. end;
  7487. function tobjectdef.check_objc_types: boolean;
  7488. begin
  7489. { done in separate step from finish_objc_data, because when
  7490. finish_objc_data is called, not all forwarddefs have been resolved
  7491. yet and we need to know all types here }
  7492. result:=true;
  7493. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  7494. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  7495. end;
  7496. procedure do_cpp_import_info(data: tobject; arg: pointer);
  7497. var
  7498. def: tdef absolute data;
  7499. pd: tprocdef absolute data;
  7500. begin
  7501. if (def.typ=procdef) then
  7502. begin
  7503. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  7504. if (oo_is_external in pd.struct.objectoptions) then
  7505. begin
  7506. { copied from psub.read_proc }
  7507. if assigned(tobjectdef(pd.struct).import_lib) then
  7508. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  7509. else
  7510. begin
  7511. { add import name to external list for DLL scanning }
  7512. if tf_has_dllscanner in target_info.flags then
  7513. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  7514. end;
  7515. end;
  7516. end;
  7517. end;
  7518. procedure tobjectdef.finish_cpp_data;
  7519. begin
  7520. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  7521. end;
  7522. {$ifdef DEBUG_NODE_XML}
  7523. function TObjectDef.XMLPrintType: ansistring;
  7524. begin
  7525. if (oo_is_forward in objectoptions) then
  7526. Result := '&lt;class prototype&gt;'
  7527. else
  7528. Result := '&lt;class&gt;';
  7529. end;
  7530. procedure TObjectDef.XMLPrintDefInfo(var T: Text; Sym: TSym);
  7531. var
  7532. i: TObjectOption;
  7533. first: Boolean;
  7534. begin
  7535. inherited XMLPrintDefInfo(T, Sym);
  7536. First := True;
  7537. for i := Low(TObjectOption) to High(TObjectOption) do
  7538. if i in objectoptions then
  7539. begin
  7540. if First then
  7541. begin
  7542. Write(T, ' objectoptions="', i);
  7543. First := False;
  7544. end
  7545. else
  7546. Write(T, ',', i)
  7547. end;
  7548. if not first then
  7549. Write(T, '"');
  7550. end;
  7551. procedure TObjectDef.XMLPrintDefData(var T: Text; Sym: TSym);
  7552. begin
  7553. { There's nothing useful yet if the type is only forward-declared }
  7554. if not (oo_is_forward in objectoptions) then
  7555. begin
  7556. if Assigned(childof) then
  7557. WriteLn(T, printnodeindention, '<ancestor>', SanitiseXMLString(childof.typesym.RealName), '</ancestor>');
  7558. inherited XMLPrintDefData(T, Sym);
  7559. end;
  7560. end;
  7561. {$endif DEBUG_NODE_XML}
  7562. {****************************************************************************
  7563. TImplementedInterface
  7564. ****************************************************************************}
  7565. function TImplementedInterface.GetIOffset: longint;
  7566. begin
  7567. if (fIOffset=-1) and
  7568. (IType in [etFieldValue,etFieldValueClass]) then
  7569. result:=tfieldvarsym(ImplementsField).fieldoffset
  7570. else
  7571. result:=fIOffset;
  7572. end;
  7573. constructor TImplementedInterface.create(aintf: tobjectdef);
  7574. begin
  7575. inherited create;
  7576. intfdef:=aintf;
  7577. intfdefderef.reset;
  7578. IOffset:=-1;
  7579. IType:=etStandard;
  7580. NameMappings:=nil;
  7581. procdefs:=nil;
  7582. end;
  7583. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  7584. begin
  7585. inherited create;
  7586. intfdef:=nil;
  7587. intfdefderef:=intfd;
  7588. ImplementsGetterDeref:=getterd;
  7589. IOffset:=-1;
  7590. IType:=etStandard;
  7591. NameMappings:=nil;
  7592. procdefs:=nil;
  7593. end;
  7594. destructor TImplementedInterface.destroy;
  7595. var
  7596. i : longint;
  7597. mappedname : pshortstring;
  7598. begin
  7599. if assigned(NameMappings) then
  7600. begin
  7601. for i:=0 to NameMappings.Count-1 do
  7602. begin
  7603. mappedname:=pshortstring(NameMappings[i]);
  7604. stringdispose(mappedname);
  7605. end;
  7606. NameMappings.free;
  7607. NameMappings:=nil;
  7608. end;
  7609. if assigned(procdefs) then
  7610. begin
  7611. procdefs.free;
  7612. procdefs:=nil;
  7613. end;
  7614. inherited destroy;
  7615. end;
  7616. procedure TImplementedInterface.buildderef;
  7617. begin
  7618. intfdefderef.build(intfdef);
  7619. ImplementsGetterDeref.build(ImplementsGetter);
  7620. end;
  7621. procedure TImplementedInterface.deref;
  7622. begin
  7623. intfdef:=tobjectdef(intfdefderef.resolve);
  7624. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  7625. end;
  7626. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  7627. begin
  7628. if not assigned(NameMappings) then
  7629. NameMappings:=TFPHashList.Create;
  7630. NameMappings.Add(origname,stringdup(newname));
  7631. end;
  7632. function TImplementedInterface.GetMapping(const origname: string):string;
  7633. var
  7634. mappedname : pshortstring;
  7635. begin
  7636. result:='';
  7637. if not assigned(NameMappings) then
  7638. exit;
  7639. mappedname:=PShortstring(NameMappings.Find(origname));
  7640. if assigned(mappedname) then
  7641. result:=mappedname^;
  7642. end;
  7643. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  7644. begin
  7645. if not assigned(procdefs) then
  7646. procdefs:=TFPObjectList.Create(false);
  7647. { duplicate entries must be stored, because multiple }
  7648. { interfaces can declare methods with the same name }
  7649. { and all of these get their own VMT entry }
  7650. procdefs.Add(pd);
  7651. end;
  7652. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  7653. var
  7654. i : longint;
  7655. begin
  7656. result:=false;
  7657. { interfaces being implemented through delegation are not mergable (FK) }
  7658. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  7659. exit;
  7660. weight:=0;
  7661. { empty interface is mergeable }
  7662. if ProcDefs.Count=0 then
  7663. begin
  7664. result:=true;
  7665. exit;
  7666. end;
  7667. { The interface to merge must at least the number of
  7668. procedures of this interface }
  7669. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  7670. exit;
  7671. for i:=0 to ProcDefs.Count-1 do
  7672. begin
  7673. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  7674. exit;
  7675. end;
  7676. weight:=ProcDefs.Count;
  7677. result:=true;
  7678. end;
  7679. function TImplementedInterface.getcopy:TImplementedInterface;
  7680. begin
  7681. Result:=TImplementedInterface.Create(nil);
  7682. { 1) the procdefs list will be freed once for each copy
  7683. 2) since the procdefs list owns its elements, those will also be freed for each copy
  7684. 3) idem for the name mappings
  7685. }
  7686. { warning: this is completely wrong on so many levels...
  7687. Move(pointer(self)^,pointer(result)^,InstanceSize);
  7688. We need to make clean copies of the different fields
  7689. this is not implemented yet, and thus we generate an internal
  7690. error instead PM 2011-06-14 }
  7691. internalerror(2011061401);
  7692. end;
  7693. {****************************************************************************
  7694. TFORWARDDEF
  7695. ****************************************************************************}
  7696. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  7697. begin
  7698. inherited create(forwarddef,true);
  7699. tosymname:=stringdup(s);
  7700. forwardpos:=pos;
  7701. end;
  7702. function tforwarddef.GetTypeName:string;
  7703. begin
  7704. GetTypeName:='unresolved forward to '+tosymname^;
  7705. end;
  7706. destructor tforwarddef.destroy;
  7707. begin
  7708. stringdispose(tosymname);
  7709. inherited destroy;
  7710. end;
  7711. function tforwarddef.getcopy:tstoreddef;
  7712. begin
  7713. result:=cforwarddef.create(tosymname^, forwardpos);
  7714. end;
  7715. {****************************************************************************
  7716. TUNDEFINEDDEF
  7717. ****************************************************************************}
  7718. constructor tundefineddef.create(doregister:boolean);
  7719. begin
  7720. inherited create(undefineddef,doregister);
  7721. end;
  7722. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  7723. begin
  7724. inherited ppuload(undefineddef,ppufile);
  7725. ppuload_platform(ppufile);
  7726. end;
  7727. function tundefineddef.GetTypeName:string;
  7728. begin
  7729. GetTypeName:='<undefined type>';
  7730. end;
  7731. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  7732. begin
  7733. inherited ppuwrite(ppufile);
  7734. writeentry(ppufile,ibundefineddef);
  7735. end;
  7736. {****************************************************************************
  7737. TERRORDEF
  7738. ****************************************************************************}
  7739. constructor terrordef.create;
  7740. begin
  7741. inherited create(errordef,true);
  7742. { prevent consecutive faults }
  7743. savesize:=1;
  7744. end;
  7745. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  7746. begin
  7747. { Can't write errordefs to ppu }
  7748. internalerror(200411063);
  7749. end;
  7750. function terrordef.GetTypeName:string;
  7751. begin
  7752. GetTypeName:='<erroneous type>';
  7753. end;
  7754. function terrordef.getmangledparaname:TSymStr;
  7755. begin
  7756. getmangledparaname:='error';
  7757. end;
  7758. {****************************************************************************
  7759. Definition Helpers
  7760. ****************************************************************************}
  7761. function is_interfacecom(def: tdef): boolean;
  7762. begin
  7763. is_interfacecom:=
  7764. assigned(def) and
  7765. (def.typ=objectdef) and
  7766. (tobjectdef(def).objecttype=odt_interfacecom);
  7767. end;
  7768. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  7769. begin
  7770. is_interfacecom_or_dispinterface:=
  7771. assigned(def) and
  7772. (def.typ=objectdef) and
  7773. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  7774. end;
  7775. function is_any_interface_kind(def: tdef): boolean;
  7776. begin
  7777. result:=
  7778. assigned(def) and
  7779. (def.typ=objectdef) and
  7780. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  7781. is_objccategory(def));
  7782. end;
  7783. function is_interfacecorba(def: tdef): boolean;
  7784. begin
  7785. is_interfacecorba:=
  7786. assigned(def) and
  7787. (def.typ=objectdef) and
  7788. (tobjectdef(def).objecttype=odt_interfacecorba);
  7789. end;
  7790. function is_interface(def: tdef): boolean;
  7791. begin
  7792. is_interface:=
  7793. assigned(def) and
  7794. (def.typ=objectdef) and
  7795. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  7796. end;
  7797. function is_dispinterface(def: tdef): boolean;
  7798. begin
  7799. result:=
  7800. assigned(def) and
  7801. (def.typ=objectdef) and
  7802. (tobjectdef(def).objecttype=odt_dispinterface);
  7803. end;
  7804. function is_class(def: tdef): boolean;
  7805. begin
  7806. is_class:=
  7807. assigned(def) and
  7808. (def.typ=objectdef) and
  7809. (tobjectdef(def).objecttype=odt_class);
  7810. end;
  7811. function is_object(def: tdef): boolean;
  7812. begin
  7813. is_object:=
  7814. assigned(def) and
  7815. (def.typ=objectdef) and
  7816. (tobjectdef(def).objecttype=odt_object);
  7817. end;
  7818. function is_cppclass(def: tdef): boolean;
  7819. begin
  7820. is_cppclass:=
  7821. assigned(def) and
  7822. (def.typ=objectdef) and
  7823. (tobjectdef(def).objecttype=odt_cppclass);
  7824. end;
  7825. function is_objcclass(def: tdef): boolean;
  7826. begin
  7827. is_objcclass:=
  7828. assigned(def) and
  7829. (def.typ=objectdef) and
  7830. (tobjectdef(def).objecttype=odt_objcclass);
  7831. end;
  7832. function is_objectpascal_helper(def: tdef): boolean;
  7833. begin
  7834. result:=
  7835. assigned(def) and
  7836. (def.typ=objectdef) and
  7837. (tobjectdef(def).objecttype=odt_helper);
  7838. end;
  7839. function is_objcclassref(def: tdef): boolean;
  7840. begin
  7841. is_objcclassref:=
  7842. assigned(def) and
  7843. (def.typ=classrefdef) and
  7844. is_objcclass(tclassrefdef(def).pointeddef);
  7845. end;
  7846. function is_objcprotocol(def: tdef): boolean;
  7847. begin
  7848. result:=
  7849. assigned(def) and
  7850. (def.typ=objectdef) and
  7851. (tobjectdef(def).objecttype=odt_objcprotocol);
  7852. end;
  7853. function is_objccategory(def: tdef): boolean;
  7854. begin
  7855. result:=
  7856. assigned(def) and
  7857. (def.typ=objectdef) and
  7858. { if used as a forward type }
  7859. ((tobjectdef(def).objecttype=odt_objccategory) or
  7860. { if used as after it has been resolved }
  7861. ((tobjectdef(def).objecttype=odt_objcclass) and
  7862. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  7863. end;
  7864. function is_objc_class_or_protocol(def: tdef): boolean;
  7865. begin
  7866. result:=
  7867. assigned(def) and
  7868. (def.typ=objectdef) and
  7869. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  7870. end;
  7871. function is_objc_protocol_or_category(def: tdef): boolean;
  7872. begin
  7873. result:=
  7874. assigned(def) and
  7875. (def.typ=objectdef) and
  7876. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  7877. ((tobjectdef(def).objecttype = odt_objcclass) and
  7878. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  7879. end;
  7880. function is_classhelper(def: tdef): boolean;
  7881. begin
  7882. result:=
  7883. is_objectpascal_helper(def) or
  7884. is_objccategory(def);
  7885. end;
  7886. function is_class_or_interface(def: tdef): boolean;
  7887. begin
  7888. result:=
  7889. assigned(def) and
  7890. (def.typ=objectdef) and
  7891. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  7892. end;
  7893. function is_class_or_interface_or_objc(def: tdef): boolean;
  7894. begin
  7895. result:=
  7896. assigned(def) and
  7897. (def.typ=objectdef) and
  7898. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  7899. end;
  7900. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  7901. begin
  7902. result:=
  7903. assigned(def) and
  7904. (def.typ=objectdef) and
  7905. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  7906. end;
  7907. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  7908. begin
  7909. result:=
  7910. assigned(def) and
  7911. (def.typ=objectdef) and
  7912. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  7913. end;
  7914. function is_class_or_interface_or_object(def: tdef): boolean;
  7915. begin
  7916. result:=
  7917. assigned(def) and
  7918. (def.typ=objectdef) and
  7919. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  7920. end;
  7921. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  7922. begin
  7923. result:=
  7924. assigned(def) and
  7925. (def.typ=objectdef) and
  7926. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  7927. end;
  7928. function is_implicit_pointer_object_type(def: tdef): boolean;
  7929. begin
  7930. result:=
  7931. assigned(def) and
  7932. (((def.typ=objectdef) and
  7933. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  7934. ((target_info.system in systems_jvm) and
  7935. (def.typ=recorddef)));
  7936. end;
  7937. function is_implicit_array_pointer(def: tdef): boolean;
  7938. begin
  7939. result:=is_dynamic_array(def) or is_dynamicstring(def);
  7940. end;
  7941. function is_class_or_object(def: tdef): boolean;
  7942. begin
  7943. result:=
  7944. assigned(def) and
  7945. (def.typ=objectdef) and
  7946. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  7947. end;
  7948. function is_record(def: tdef): boolean;
  7949. begin
  7950. result:=
  7951. assigned(def) and
  7952. (def.typ=recorddef);
  7953. end;
  7954. function is_javaclass(def: tdef): boolean;
  7955. begin
  7956. result:=
  7957. assigned(def) and
  7958. (def.typ=objectdef) and
  7959. (tobjectdef(def).objecttype=odt_javaclass);
  7960. end;
  7961. function is_javaclassref(def: tdef): boolean;
  7962. begin
  7963. is_javaclassref:=
  7964. assigned(def) and
  7965. (def.typ=classrefdef) and
  7966. is_javaclass(tclassrefdef(def).pointeddef);
  7967. end;
  7968. function is_javainterface(def: tdef): boolean;
  7969. begin
  7970. result:=
  7971. assigned(def) and
  7972. (def.typ=objectdef) and
  7973. (tobjectdef(def).objecttype=odt_interfacejava);
  7974. end;
  7975. function is_java_class_or_interface(def: tdef): boolean;
  7976. begin
  7977. result:=
  7978. assigned(def) and
  7979. (def.typ=objectdef) and
  7980. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  7981. end;
  7982. procedure loadobjctypes;
  7983. begin
  7984. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  7985. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  7986. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  7987. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  7988. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  7989. end;
  7990. procedure maybeloadcocoatypes;
  7991. var
  7992. tsym: ttypesym;
  7993. cocoaunit: string[15];
  7994. begin
  7995. if assigned(objc_fastenumeration) then
  7996. exit;
  7997. if not(target_info.system in [system_arm_ios,system_i386_iphonesim,system_aarch64_ios,system_x86_64_iphonesim]) then
  7998. cocoaunit:='COCOAALL'
  7999. else
  8000. cocoaunit:='IPHONEALL';
  8001. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  8002. if assigned(tsym) then
  8003. objc_fastenumeration:=tobjectdef(tsym.typedef)
  8004. else
  8005. objc_fastenumeration:=nil;
  8006. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  8007. if assigned(tsym) then
  8008. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  8009. else
  8010. objc_fastenumerationstate:=nil;
  8011. end;
  8012. function use_vectorfpu(def : tdef) : boolean;
  8013. begin
  8014. {$ifdef x86}
  8015. {$define use_vectorfpuimplemented}
  8016. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  8017. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  8018. {$endif x86}
  8019. {$ifdef arm}
  8020. {$define use_vectorfpuimplemented}
  8021. use_vectorfpu:=FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[current_settings.fputype];
  8022. {$endif arm}
  8023. {$ifdef aarch64}
  8024. {$define use_vectorfpuimplemented}
  8025. use_vectorfpu:=true;
  8026. {$endif aarch64}
  8027. {$ifndef use_vectorfpuimplemented}
  8028. use_vectorfpu:=false;
  8029. {$endif}
  8030. end;
  8031. end.