symdef.pas 304 KB

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