symdef.pas 304 KB

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