symdef.pas 265 KB

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