symdef.pas 304 KB

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