symdef.pas 265 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976
  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. end;
  1895. end;
  1896. end;
  1897. procedure tstoreddef.derefimpl;
  1898. begin
  1899. end;
  1900. function tstoreddef.size : asizeint;
  1901. begin
  1902. size:=savesize;
  1903. end;
  1904. function tstoreddef.getvardef:longint;
  1905. begin
  1906. result:=varUndefined;
  1907. end;
  1908. function tstoreddef.alignment : shortint;
  1909. begin
  1910. { natural alignment by default }
  1911. alignment:=size_2_align(savesize);
  1912. { can happen if savesize = 0, e.g. for voiddef or
  1913. an empty record
  1914. }
  1915. if (alignment=0) then
  1916. alignment:=1;
  1917. end;
  1918. { returns true, if the definition can be published }
  1919. function tstoreddef.is_publishable : boolean;
  1920. begin
  1921. is_publishable:=false;
  1922. end;
  1923. { needs an init table }
  1924. function tstoreddef.needs_inittable : boolean;
  1925. begin
  1926. needs_inittable:=false;
  1927. end;
  1928. function tstoreddef.is_intregable : boolean;
  1929. var
  1930. recsize,temp: longint;
  1931. begin
  1932. is_intregable:=false;
  1933. case typ of
  1934. orddef,
  1935. pointerdef,
  1936. enumdef,
  1937. classrefdef:
  1938. is_intregable:=true;
  1939. procvardef :
  1940. is_intregable:=tprocvardef(self).is_addressonly or (po_methodpointer in tprocvardef(self).procoptions);
  1941. objectdef:
  1942. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  1943. setdef:
  1944. is_intregable:=is_smallset(self);
  1945. recorddef:
  1946. begin
  1947. {$ifdef llvm}
  1948. is_intregable:=false;
  1949. {$else llvm}
  1950. recsize:=size;
  1951. is_intregable:=
  1952. ispowerof2(recsize,temp) and
  1953. (((recsize <= sizeof(aint)*2) and
  1954. { records cannot go into registers on 16 bit targets for now }
  1955. (sizeof(aint)>2) and
  1956. not trecorddef(self).contains_float_field) or
  1957. (recsize <= sizeof(aint))) and
  1958. not needs_inittable;
  1959. {$endif llvm}
  1960. end;
  1961. end;
  1962. end;
  1963. function tstoreddef.is_fpuregable : boolean;
  1964. begin
  1965. {$ifdef x86}
  1966. result:=use_vectorfpu(self);
  1967. {$else x86}
  1968. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1969. {$endif x86}
  1970. end;
  1971. function tstoreddef.is_const_intregable : boolean;
  1972. begin
  1973. case typ of
  1974. stringdef:
  1975. result:=tstringdef(self).stringtype in [st_ansistring,st_unicodestring,st_widestring];
  1976. arraydef:
  1977. result:=is_dynamic_array(self);
  1978. objectdef:
  1979. result:=is_interface(self);
  1980. else
  1981. result:=false;
  1982. end;
  1983. end;
  1984. procedure tstoreddef.initgeneric;
  1985. begin
  1986. if assigned(generictokenbuf) then
  1987. internalerror(200512131);
  1988. generictokenbuf:=tdynamicarray.create(256);
  1989. end;
  1990. function tstoreddef.is_generic: boolean;
  1991. var
  1992. sym: tsym;
  1993. i: longint;
  1994. begin
  1995. result:=assigned(genericparas) and
  1996. (genericparas.count>0) and
  1997. (df_generic in defoptions);
  1998. if result then
  1999. { if any of the type parameters does *not* belong to as (meaning it was passed
  2000. in from outside) then we aren't a generic, but a specialization }
  2001. for i:=0 to genericparas.count-1 do
  2002. begin
  2003. sym:=tsym(genericparas[i]);
  2004. if sym.typ<>symconst.typesym then
  2005. internalerror(2014050903);
  2006. if sym.owner.defowner<>self then
  2007. exit(false);
  2008. end;
  2009. end;
  2010. function tstoreddef.is_specialization: boolean;
  2011. var
  2012. i : longint;
  2013. sym : tsym;
  2014. begin
  2015. result:=assigned(genericparas) and
  2016. (genericparas.count>0) and
  2017. (df_specialization in defoptions);
  2018. if result then
  2019. begin
  2020. { if at least one of the generic parameters is not owned by us (meaning it was
  2021. passed in from outside) then we have a specialization, otherwise we have a generic }
  2022. for i:=0 to genericparas.count-1 do
  2023. begin
  2024. sym:=tsym(genericparas[i]);
  2025. if sym.typ<>symconst.typesym then
  2026. internalerror(2014050904);
  2027. if sym.owner.defowner<>self then
  2028. exit(true);
  2029. end;
  2030. result:=false;
  2031. end;
  2032. end;
  2033. procedure tstoreddef.register_def;
  2034. begin
  2035. if registered then
  2036. exit;
  2037. { Register in current_module }
  2038. if assigned(current_module) then
  2039. begin
  2040. exclude(defoptions,df_not_registered_no_free);
  2041. if defid<defid_not_registered then
  2042. defid:=deflist_index
  2043. else
  2044. begin
  2045. current_module.deflist.Add(self);
  2046. defid:=current_module.deflist.Count-1;
  2047. end;
  2048. maybe_put_in_symtable_stack;
  2049. end
  2050. else
  2051. DefId:=defid_registered_nost;
  2052. end;
  2053. procedure tstoreddef.maybe_put_in_symtable_stack;
  2054. var
  2055. insertstack: psymtablestackitem;
  2056. begin
  2057. if assigned(symtablestack) and
  2058. not assigned(self.owner) then
  2059. begin
  2060. insertstack:=symtablestack.stack;
  2061. { don't insert defs in exception symtables, as they are freed before
  2062. the module is compiled, so we can get crashes on high level targets
  2063. if they still need it while e.g. writing assembler code }
  2064. while assigned(insertstack) and
  2065. (insertstack^.symtable.symtabletype in [stt_exceptsymtable,withsymtable]) do
  2066. insertstack:=insertstack^.next;
  2067. if not assigned(insertstack) then
  2068. internalerror(200602044);
  2069. if insertstack^.symtable.sealed then
  2070. internalerror(2015022301);
  2071. insertstack^.symtable.insertdef(self);
  2072. end;
  2073. end;
  2074. {****************************************************************************
  2075. Tstringdef
  2076. ****************************************************************************}
  2077. constructor tstringdef.createshort(l: byte; doregister: boolean);
  2078. begin
  2079. inherited create(stringdef,doregister);
  2080. stringtype:=st_shortstring;
  2081. encoding:=0;
  2082. len:=l;
  2083. end;
  2084. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  2085. begin
  2086. inherited ppuload(stringdef,ppufile);
  2087. stringtype:=st_shortstring;
  2088. encoding:=0;
  2089. len:=ppufile.getbyte;
  2090. ppuload_platform(ppufile);
  2091. end;
  2092. constructor tstringdef.createlong(l: asizeint; doregister: boolean);
  2093. begin
  2094. inherited create(stringdef,doregister);
  2095. stringtype:=st_longstring;
  2096. encoding:=0;
  2097. len:=l;
  2098. end;
  2099. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  2100. begin
  2101. inherited ppuload(stringdef,ppufile);
  2102. stringtype:=st_longstring;
  2103. encoding:=0;
  2104. len:=ppufile.getasizeint;
  2105. ppuload_platform(ppufile);
  2106. end;
  2107. constructor tstringdef.createansi(aencoding: tstringencoding; doregister: boolean);
  2108. begin
  2109. inherited create(stringdef,doregister);
  2110. stringtype:=st_ansistring;
  2111. encoding:=aencoding;
  2112. len:=-1;
  2113. end;
  2114. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  2115. begin
  2116. inherited ppuload(stringdef,ppufile);
  2117. stringtype:=st_ansistring;
  2118. len:=ppufile.getaint;
  2119. encoding:=ppufile.getword;
  2120. ppuload_platform(ppufile);
  2121. end;
  2122. constructor tstringdef.createwide(doregister: boolean);
  2123. begin
  2124. inherited create(stringdef,doregister);
  2125. stringtype:=st_widestring;
  2126. if target_info.endian=endian_little then
  2127. encoding:=CP_UTF16LE
  2128. else
  2129. encoding:=CP_UTF16BE;
  2130. len:=-1;
  2131. end;
  2132. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  2133. begin
  2134. inherited ppuload(stringdef,ppufile);
  2135. stringtype:=st_widestring;
  2136. if target_info.endian=endian_little then
  2137. encoding:=CP_UTF16LE
  2138. else
  2139. encoding:=CP_UTF16BE;
  2140. len:=ppufile.getaint;
  2141. ppuload_platform(ppufile);
  2142. end;
  2143. constructor tstringdef.createunicode(doregister: boolean);
  2144. begin
  2145. inherited create(stringdef,doregister);
  2146. stringtype:=st_unicodestring;
  2147. if target_info.endian=endian_little then
  2148. encoding:=CP_UTF16LE
  2149. else
  2150. encoding:=CP_UTF16BE;
  2151. len:=-1;
  2152. end;
  2153. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  2154. begin
  2155. inherited ppuload(stringdef,ppufile);
  2156. stringtype:=st_unicodestring;
  2157. len:=ppufile.getaint;
  2158. encoding:=ppufile.getword;
  2159. ppuload_platform(ppufile);
  2160. end;
  2161. function tstringdef.getcopy : tstoreddef;
  2162. begin
  2163. result:=cstringdef.create(typ,true);
  2164. result.typ:=stringdef;
  2165. tstringdef(result).stringtype:=stringtype;
  2166. tstringdef(result).encoding:=encoding;
  2167. tstringdef(result).len:=len;
  2168. end;
  2169. function tstringdef.stringtypname:string;
  2170. const
  2171. typname:array[tstringtype] of string[10]=(
  2172. 'shortstr','longstr','ansistr','widestr','unicodestr'
  2173. );
  2174. begin
  2175. stringtypname:=typname[stringtype];
  2176. end;
  2177. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  2178. begin
  2179. inherited ppuwrite(ppufile);
  2180. if stringtype=st_shortstring then
  2181. begin
  2182. {$ifdef extdebug}
  2183. if len > 255 then internalerror(12122002);
  2184. {$endif}
  2185. ppufile.putbyte(byte(len))
  2186. end
  2187. else
  2188. ppufile.putaint(len);
  2189. if stringtype in [st_ansistring,st_unicodestring] then
  2190. ppufile.putword(encoding);
  2191. case stringtype of
  2192. st_shortstring : writeentry(ppufile,ibshortstringdef);
  2193. st_longstring : writeentry(ppufile,iblongstringdef);
  2194. st_ansistring : writeentry(ppufile,ibansistringdef);
  2195. st_widestring : writeentry(ppufile,ibwidestringdef);
  2196. st_unicodestring : writeentry(ppufile,ibunicodestringdef);
  2197. end;
  2198. end;
  2199. function tstringdef.needs_inittable : boolean;
  2200. begin
  2201. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  2202. end;
  2203. function tstringdef.GetTypeName : string;
  2204. const
  2205. names : array[tstringtype] of string[15] = (
  2206. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  2207. begin
  2208. GetTypeName:=names[stringtype];
  2209. end;
  2210. function tstringdef.getvardef : longint;
  2211. const
  2212. vardef : array[tstringtype] of longint = (
  2213. varUndefined,varUndefined,varString,varOleStr,varUString);
  2214. begin
  2215. result:=vardef[stringtype];
  2216. end;
  2217. function tstringdef.alignment : shortint;
  2218. begin
  2219. case stringtype of
  2220. st_unicodestring,
  2221. st_widestring,
  2222. st_ansistring:
  2223. alignment:=voidpointertype.alignment;
  2224. st_longstring,
  2225. st_shortstring:
  2226. { char to string accesses byte 0 and 1 with one word access }
  2227. if (tf_requires_proper_alignment in target_info.flags) or
  2228. { macpas needs an alignment of 2 (MetroWerks compatible) }
  2229. (m_mac in current_settings.modeswitches) then
  2230. alignment:=size_2_align(2)
  2231. else
  2232. alignment:=size_2_align(1);
  2233. else
  2234. internalerror(200412301);
  2235. end;
  2236. end;
  2237. function tstringdef.getmangledparaname : TSymStr;
  2238. begin
  2239. getmangledparaname:='STRING';
  2240. end;
  2241. function tstringdef.is_publishable : boolean;
  2242. begin
  2243. is_publishable:=true;
  2244. end;
  2245. function tstringdef.size: asizeint;
  2246. begin
  2247. case stringtype of
  2248. st_shortstring:
  2249. Result:=len+1;
  2250. st_longstring,
  2251. st_ansistring,
  2252. st_widestring,
  2253. st_unicodestring:
  2254. Result:=voidpointertype.size;
  2255. else
  2256. internalerror(2014032301);
  2257. end;
  2258. end;
  2259. {****************************************************************************
  2260. TENUMDEF
  2261. ****************************************************************************}
  2262. constructor tenumdef.create;
  2263. begin
  2264. inherited create(enumdef,true);
  2265. minval:=0;
  2266. maxval:=0;
  2267. calcsavesize;
  2268. has_jumps:=false;
  2269. basedef:=nil;
  2270. symtable:=tenumsymtable.create(self);
  2271. end;
  2272. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  2273. begin
  2274. inherited create(enumdef,true);
  2275. minval:=_min;
  2276. maxval:=_max;
  2277. basedef:=_basedef;
  2278. calcsavesize;
  2279. has_jumps:=false;
  2280. symtable:=basedef.symtable.getcopy;
  2281. include(defoptions, df_copied_def);
  2282. end;
  2283. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  2284. begin
  2285. inherited ppuload(enumdef,ppufile);
  2286. minval:=ppufile.getaint;
  2287. maxval:=ppufile.getaint;
  2288. savesize:=ppufile.getaint;
  2289. has_jumps:=false;
  2290. if df_copied_def in defoptions then
  2291. begin
  2292. symtable:=nil;
  2293. ppufile.getderef(basedefderef);
  2294. ppuload_platform(ppufile);
  2295. end
  2296. else
  2297. begin
  2298. ppuload_platform(ppufile);
  2299. // create with nil defowner first to prevent values changes on insert
  2300. symtable:=tenumsymtable.create(nil);
  2301. tenumsymtable(symtable).ppuload(ppufile);
  2302. symtable.defowner:=self;
  2303. end;
  2304. end;
  2305. destructor tenumdef.destroy;
  2306. begin
  2307. symtable.free;
  2308. symtable:=nil;
  2309. inherited destroy;
  2310. end;
  2311. function tenumdef.getcopy : tstoreddef;
  2312. begin
  2313. if assigned(basedef) then
  2314. result:=cenumdef.create_subrange(basedef,minval,maxval)
  2315. else
  2316. begin
  2317. result:=cenumdef.create;
  2318. tenumdef(result).minval:=minval;
  2319. tenumdef(result).maxval:=maxval;
  2320. tenumdef(result).symtable.free;
  2321. tenumdef(result).symtable:=symtable.getcopy;
  2322. tenumdef(result).basedef:=self;
  2323. end;
  2324. tenumdef(result).has_jumps:=has_jumps;
  2325. tenumdef(result).basedefderef:=basedefderef;
  2326. include(tenumdef(result).defoptions,df_copied_def);
  2327. end;
  2328. procedure tenumdef.calcsavesize;
  2329. begin
  2330. {$IFNDEF cpu64bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2331. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  2332. savesize:=8
  2333. {$IFNDEF cpu64bitaddr} {$pop} {$ENDIF}
  2334. else
  2335. {$IFDEF cpu16bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2336. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  2337. savesize:=4
  2338. {$IFDEF cpu16bitaddr} {$pop} {$ENDIF}
  2339. else
  2340. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  2341. savesize:=2
  2342. else
  2343. savesize:=1;
  2344. end;
  2345. function tenumdef.packedbitsize: asizeint;
  2346. var
  2347. sizeval: tconstexprint;
  2348. power: longint;
  2349. begin
  2350. result := 0;
  2351. if (minval >= 0) and
  2352. (maxval <= 1) then
  2353. result := 1
  2354. else
  2355. begin
  2356. if (minval>=0) then
  2357. sizeval:=maxval
  2358. else
  2359. { don't count 0 twice }
  2360. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  2361. { 256 must become 512 etc. }
  2362. nextpowerof2(sizeval+1,power);
  2363. result := power;
  2364. end;
  2365. end;
  2366. procedure tenumdef.setmax(_max:asizeint);
  2367. begin
  2368. maxval:=_max;
  2369. calcsavesize;
  2370. end;
  2371. procedure tenumdef.setmin(_min:asizeint);
  2372. begin
  2373. minval:=_min;
  2374. calcsavesize;
  2375. end;
  2376. function tenumdef.min:asizeint;
  2377. begin
  2378. min:=minval;
  2379. end;
  2380. function tenumdef.max:asizeint;
  2381. begin
  2382. max:=maxval;
  2383. end;
  2384. function tenumdef.getfirstsym: tsym;
  2385. var
  2386. i:integer;
  2387. begin
  2388. for i := 0 to symtable.SymList.Count - 1 do
  2389. begin
  2390. result:=tsym(symtable.SymList[i]);
  2391. if tenumsym(result).value=minval then
  2392. exit;
  2393. end;
  2394. result:=nil;
  2395. end;
  2396. function tenumdef.int2enumsym(l: asizeint): tsym;
  2397. var
  2398. i: longint;
  2399. sym: tsym;
  2400. bdef: tenumdef;
  2401. begin
  2402. result:=nil;
  2403. if (l<minval) or
  2404. (l>maxval) then
  2405. exit;
  2406. bdef:=getbasedef;
  2407. for i:=0 to bdef.symtable.symlist.count-1 do
  2408. begin
  2409. sym:=tsym(bdef.symtable.symlist[i]);
  2410. if (sym.typ=enumsym) and
  2411. (tenumsym(sym).value=l) then
  2412. begin
  2413. result:=sym;
  2414. exit;
  2415. end;
  2416. end;
  2417. end;
  2418. function tenumdef.getbasedef: tenumdef;
  2419. begin
  2420. if not assigned(basedef) then
  2421. result:=self
  2422. else
  2423. result:=basedef;
  2424. end;
  2425. procedure tenumdef.buildderef;
  2426. begin
  2427. inherited buildderef;
  2428. if df_copied_def in defoptions then
  2429. basedefderef.build(basedef)
  2430. else
  2431. tenumsymtable(symtable).buildderef;
  2432. end;
  2433. procedure tenumdef.deref;
  2434. begin
  2435. inherited deref;
  2436. if df_copied_def in defoptions then
  2437. begin
  2438. basedef:=tenumdef(basedefderef.resolve);
  2439. symtable:=basedef.symtable.getcopy;
  2440. end
  2441. else
  2442. tenumsymtable(symtable).deref(false);
  2443. end;
  2444. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  2445. begin
  2446. inherited ppuwrite(ppufile);
  2447. ppufile.putaint(min);
  2448. ppufile.putaint(max);
  2449. ppufile.putaint(savesize);
  2450. if df_copied_def in defoptions then
  2451. ppufile.putderef(basedefderef);
  2452. writeentry(ppufile,ibenumdef);
  2453. if not (df_copied_def in defoptions) then
  2454. tenumsymtable(symtable).ppuwrite(ppufile);
  2455. end;
  2456. function tenumdef.is_publishable : boolean;
  2457. begin
  2458. is_publishable:=true;
  2459. end;
  2460. function tenumdef.GetTypeName : string;
  2461. begin
  2462. GetTypeName:='<enumeration type>';
  2463. end;
  2464. {****************************************************************************
  2465. TORDDEF
  2466. ****************************************************************************}
  2467. constructor torddef.create(t : tordtype;v,b : TConstExprInt; doregister: boolean);
  2468. begin
  2469. inherited create(orddef,doregister);
  2470. low:=v;
  2471. high:=b;
  2472. ordtype:=t;
  2473. setsize;
  2474. end;
  2475. constructor torddef.ppuload(ppufile:tcompilerppufile);
  2476. begin
  2477. inherited ppuload(orddef,ppufile);
  2478. ordtype:=tordtype(ppufile.getbyte);
  2479. low:=ppufile.getexprint;
  2480. high:=ppufile.getexprint;
  2481. setsize;
  2482. ppuload_platform(ppufile);
  2483. end;
  2484. function torddef.getcopy : tstoreddef;
  2485. begin
  2486. result:=corddef.create(ordtype,low,high,true);
  2487. result.typ:=orddef;
  2488. torddef(result).low:=low;
  2489. torddef(result).high:=high;
  2490. torddef(result).ordtype:=ordtype;
  2491. torddef(result).savesize:=savesize;
  2492. end;
  2493. function torddef.alignment:shortint;
  2494. begin
  2495. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) and
  2496. (ordtype in [s64bit,u64bit]) then
  2497. result := 4
  2498. else
  2499. result := inherited alignment;
  2500. end;
  2501. procedure torddef.setsize;
  2502. const
  2503. sizetbl : array[tordtype] of longint = (
  2504. 0,
  2505. 1,2,4,8,16,
  2506. 1,2,4,8,16,
  2507. 1,2,4,8,
  2508. 1,2,4,8,
  2509. 1,2,8
  2510. );
  2511. begin
  2512. savesize:=sizetbl[ordtype];
  2513. end;
  2514. function torddef.packedbitsize: asizeint;
  2515. var
  2516. sizeval: tconstexprint;
  2517. power: longint;
  2518. begin
  2519. result := 0;
  2520. if ordtype = uvoid then
  2521. exit;
  2522. {$ifndef cpu64bitalu}
  2523. if (ordtype in [s64bit,u64bit]) then
  2524. {$else not cpu64bitalu}
  2525. if (ordtype = u64bit) or
  2526. ((ordtype = s64bit) and
  2527. ((low <= (system.low(int64) div 2)) or
  2528. (high > (system.high(int64) div 2)))) then
  2529. {$endif cpu64bitalu}
  2530. result := 64
  2531. else if (low >= 0) and
  2532. (high <= 1) then
  2533. result := 1
  2534. else
  2535. begin
  2536. if (low>=0) then
  2537. sizeval:=high
  2538. else
  2539. { don't count 0 twice }
  2540. sizeval:=(cutils.max(-low,high)*2)-1;
  2541. { 256 must become 512 etc. }
  2542. nextpowerof2(sizeval+1,power);
  2543. result := power;
  2544. end;
  2545. end;
  2546. function torddef.getvardef : longint;
  2547. const
  2548. basetype2vardef : array[tordtype] of longint = (
  2549. varUndefined,
  2550. varbyte,varword,varlongword,varqword,varUndefined,
  2551. varshortint,varsmallint,varinteger,varint64,varUndefined,
  2552. varboolean,varboolean,varboolean,varboolean,
  2553. varboolean,varboolean,varUndefined,varUndefined,
  2554. varUndefined,varUndefined,varCurrency);
  2555. begin
  2556. result:=basetype2vardef[ordtype];
  2557. end;
  2558. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  2559. begin
  2560. inherited ppuwrite(ppufile);
  2561. ppufile.putbyte(byte(ordtype));
  2562. ppufile.putexprint(low);
  2563. ppufile.putexprint(high);
  2564. writeentry(ppufile,iborddef);
  2565. end;
  2566. function torddef.is_publishable : boolean;
  2567. begin
  2568. is_publishable:=(ordtype<>uvoid);
  2569. end;
  2570. function torddef.GetTypeName : string;
  2571. const
  2572. names : array[tordtype] of string[20] = (
  2573. 'untyped',
  2574. 'Byte','Word','DWord','QWord','UInt128',
  2575. 'ShortInt','SmallInt','LongInt','Int64','Int128',
  2576. 'Boolean','Boolean16','Boolean32','Boolean64',
  2577. 'ByteBool','WordBool','LongBool','QWordBool',
  2578. 'Char','WideChar','Currency');
  2579. begin
  2580. GetTypeName:=names[ordtype];
  2581. end;
  2582. {****************************************************************************
  2583. TFLOATDEF
  2584. ****************************************************************************}
  2585. constructor tfloatdef.create(t: tfloattype; doregister: boolean);
  2586. begin
  2587. inherited create(floatdef,doregister);
  2588. floattype:=t;
  2589. setsize;
  2590. end;
  2591. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  2592. begin
  2593. inherited ppuload(floatdef,ppufile);
  2594. floattype:=tfloattype(ppufile.getbyte);
  2595. setsize;
  2596. ppuload_platform(ppufile);
  2597. end;
  2598. function tfloatdef.getcopy : tstoreddef;
  2599. begin
  2600. result:=cfloatdef.create(floattype,true);
  2601. result.typ:=floatdef;
  2602. tfloatdef(result).savesize:=savesize;
  2603. end;
  2604. function tfloatdef.alignment:shortint;
  2605. begin
  2606. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) then
  2607. case floattype of
  2608. sc80real,
  2609. s80real: result:=16;
  2610. s64real,
  2611. s64currency,
  2612. s64comp : result:=4;
  2613. else
  2614. result := inherited alignment;
  2615. end
  2616. else
  2617. result := inherited alignment;
  2618. end;
  2619. function tfloatdef.structalignment: shortint;
  2620. begin
  2621. { aix is really annoying: the recommended scalar alignment for both
  2622. int64 and double is 64 bits, but in structs int64 has to be aligned
  2623. to 8 bytes and double to 4 bytes }
  2624. if (target_info.system in systems_aix) and
  2625. (floattype=s64real) then
  2626. result:=4
  2627. else
  2628. result:=alignment;
  2629. end;
  2630. procedure tfloatdef.setsize;
  2631. begin
  2632. case floattype of
  2633. s32real : savesize:=4;
  2634. s80real : savesize:=10;
  2635. sc80real:
  2636. if target_info.system in [system_i386_darwin,
  2637. system_i386_iphonesim,system_x86_64_darwin,
  2638. system_x86_64_iphonesim,
  2639. system_x86_64_linux,system_x86_64_freebsd,
  2640. system_x86_64_openbsd,system_x86_64_netbsd,
  2641. system_x86_64_solaris,system_x86_64_embedded,
  2642. system_x86_64_dragonfly] then
  2643. savesize:=16
  2644. else
  2645. savesize:=12;
  2646. s64real,
  2647. s64currency,
  2648. s64comp : savesize:=8;
  2649. else
  2650. savesize:=0;
  2651. end;
  2652. end;
  2653. function tfloatdef.getvardef : longint;
  2654. const
  2655. floattype2vardef : array[tfloattype] of longint = (
  2656. varSingle,varDouble,varUndefined,varUndefined,
  2657. varUndefined,varCurrency,varUndefined);
  2658. begin
  2659. if (upper(typename)='TDATETIME') and
  2660. assigned(owner) and
  2661. assigned(owner.name) and
  2662. (owner.name^='SYSTEM') then
  2663. result:=varDate
  2664. else
  2665. result:=floattype2vardef[floattype];
  2666. end;
  2667. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  2668. begin
  2669. inherited ppuwrite(ppufile);
  2670. ppufile.putbyte(byte(floattype));
  2671. writeentry(ppufile,ibfloatdef);
  2672. end;
  2673. function tfloatdef.is_publishable : boolean;
  2674. begin
  2675. is_publishable:=true;
  2676. end;
  2677. function tfloatdef.GetTypeName : string;
  2678. const
  2679. names : array[tfloattype] of string[20] = (
  2680. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  2681. begin
  2682. GetTypeName:=names[floattype];
  2683. end;
  2684. {****************************************************************************
  2685. TFILEDEF
  2686. ****************************************************************************}
  2687. constructor tfiledef.createtext;
  2688. begin
  2689. inherited create(filedef,true);
  2690. filetyp:=ft_text;
  2691. typedfiledef:=nil;
  2692. end;
  2693. constructor tfiledef.createuntyped;
  2694. begin
  2695. inherited create(filedef,true);
  2696. filetyp:=ft_untyped;
  2697. typedfiledef:=nil;
  2698. end;
  2699. constructor tfiledef.createtyped(def:tdef);
  2700. begin
  2701. inherited create(filedef,true);
  2702. filetyp:=ft_typed;
  2703. typedfiledef:=def;
  2704. end;
  2705. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  2706. begin
  2707. inherited ppuload(filedef,ppufile);
  2708. filetyp:=tfiletyp(ppufile.getbyte);
  2709. if filetyp=ft_typed then
  2710. ppufile.getderef(typedfiledefderef)
  2711. else
  2712. typedfiledef:=nil;
  2713. ppuload_platform(ppufile);
  2714. end;
  2715. function tfiledef.getcopy : tstoreddef;
  2716. begin
  2717. case filetyp of
  2718. ft_typed:
  2719. result:=cfiledef.createtyped(typedfiledef);
  2720. ft_untyped:
  2721. result:=cfiledef.createuntyped;
  2722. ft_text:
  2723. result:=cfiledef.createtext;
  2724. else
  2725. internalerror(2004121201);
  2726. end;
  2727. end;
  2728. procedure tfiledef.buildderef;
  2729. begin
  2730. inherited buildderef;
  2731. if filetyp=ft_typed then
  2732. typedfiledefderef.build(typedfiledef);
  2733. end;
  2734. procedure tfiledef.deref;
  2735. begin
  2736. inherited deref;
  2737. if filetyp=ft_typed then
  2738. typedfiledef:=tdef(typedfiledefderef.resolve);
  2739. end;
  2740. function tfiledef.size:asizeint;
  2741. begin
  2742. if savesize=0 then
  2743. setsize;
  2744. size:=savesize;
  2745. end;
  2746. procedure tfiledef.setsize;
  2747. begin
  2748. case filetyp of
  2749. ft_text:
  2750. savesize:=search_system_type('TEXTREC').typedef.size;
  2751. ft_typed:
  2752. begin
  2753. savesize:=search_system_type('FILEREC').typedef.size;
  2754. { allocate put/get buffer in iso mode }
  2755. if m_isolike_io in current_settings.modeswitches then
  2756. inc(savesize,typedfiledef.size);
  2757. end;
  2758. ft_untyped:
  2759. savesize:=search_system_type('FILEREC').typedef.size;
  2760. else
  2761. internalerror(2013113001);
  2762. end;
  2763. end;
  2764. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  2765. begin
  2766. inherited ppuwrite(ppufile);
  2767. ppufile.putbyte(byte(filetyp));
  2768. if filetyp=ft_typed then
  2769. ppufile.putderef(typedfiledefderef);
  2770. writeentry(ppufile,ibfiledef);
  2771. end;
  2772. function tfiledef.GetTypeName : string;
  2773. begin
  2774. case filetyp of
  2775. ft_untyped:
  2776. GetTypeName:='File';
  2777. ft_typed:
  2778. GetTypeName:='File Of '+typedfiledef.typename;
  2779. ft_text:
  2780. GetTypeName:='Text'
  2781. else
  2782. internalerror(2013113002);
  2783. end;
  2784. end;
  2785. function tfiledef.getmangledparaname : TSymStr;
  2786. begin
  2787. case filetyp of
  2788. ft_untyped:
  2789. getmangledparaname:='FILE';
  2790. ft_typed:
  2791. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  2792. ft_text:
  2793. getmangledparaname:='TEXT'
  2794. else
  2795. internalerror(2013113003);
  2796. end;
  2797. end;
  2798. {****************************************************************************
  2799. TVARIANTDEF
  2800. ****************************************************************************}
  2801. constructor tvariantdef.create(v : tvarianttype);
  2802. begin
  2803. inherited create(variantdef,true);
  2804. varianttype:=v;
  2805. setsize;
  2806. end;
  2807. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  2808. begin
  2809. inherited ppuload(variantdef,ppufile);
  2810. varianttype:=tvarianttype(ppufile.getbyte);
  2811. setsize;
  2812. ppuload_platform(ppufile);
  2813. end;
  2814. function tvariantdef.getcopy : tstoreddef;
  2815. begin
  2816. result:=cvariantdef.create(varianttype);
  2817. end;
  2818. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  2819. begin
  2820. inherited ppuwrite(ppufile);
  2821. ppufile.putbyte(byte(varianttype));
  2822. writeentry(ppufile,ibvariantdef);
  2823. end;
  2824. function tvariantdef.getvardef : longint;
  2825. begin
  2826. Result:=varVariant;
  2827. end;
  2828. procedure tvariantdef.setsize;
  2829. begin
  2830. {$ifdef cpu64bitaddr}
  2831. savesize:=24;
  2832. {$else cpu64bitaddr}
  2833. savesize:=16;
  2834. {$endif cpu64bitaddr}
  2835. end;
  2836. function tvariantdef.GetTypeName : string;
  2837. begin
  2838. case varianttype of
  2839. vt_normalvariant:
  2840. GetTypeName:='Variant';
  2841. vt_olevariant:
  2842. GetTypeName:='OleVariant';
  2843. else
  2844. internalerror(2013113004);
  2845. end;
  2846. end;
  2847. function tvariantdef.needs_inittable : boolean;
  2848. begin
  2849. needs_inittable:=true;
  2850. end;
  2851. function tvariantdef.is_publishable : boolean;
  2852. begin
  2853. is_publishable:=true;
  2854. end;
  2855. {****************************************************************************
  2856. TABSTRACtpointerdef
  2857. ****************************************************************************}
  2858. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  2859. begin
  2860. inherited create(dt,true);
  2861. pointeddef:=def;
  2862. end;
  2863. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2864. begin
  2865. inherited ppuload(dt,ppufile);
  2866. ppufile.getderef(pointeddefderef);
  2867. end;
  2868. procedure tabstractpointerdef.buildderef;
  2869. begin
  2870. inherited buildderef;
  2871. pointeddefderef.build(pointeddef);
  2872. end;
  2873. procedure tabstractpointerdef.deref;
  2874. begin
  2875. inherited deref;
  2876. pointeddef:=tdef(pointeddefderef.resolve);
  2877. end;
  2878. function tabstractpointerdef.size: asizeint;
  2879. begin
  2880. Result:=voidpointertype.size;
  2881. end;
  2882. function tabstractpointerdef.alignment: shortint;
  2883. begin
  2884. alignment:=size_2_align(voidpointertype.size);
  2885. end;
  2886. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2887. begin
  2888. inherited ppuwrite(ppufile);
  2889. ppufile.putderef(pointeddefderef);
  2890. end;
  2891. {****************************************************************************
  2892. tpointerdef
  2893. ****************************************************************************}
  2894. constructor tpointerdef.create(def:tdef);
  2895. begin
  2896. inherited create(pointerdef,def);
  2897. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  2898. end;
  2899. class function tpointerdef.getreusable(def: tdef): tpointerdef;
  2900. var
  2901. res: PHashSetItem;
  2902. oldsymtablestack: tsymtablestack;
  2903. begin
  2904. if not assigned(current_module) then
  2905. internalerror(2011071101);
  2906. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  2907. if not assigned(res^.Data) then
  2908. begin
  2909. { since these pointerdefs can be reused anywhere in the current
  2910. unit, add them to the global/staticsymtable (or local symtable
  2911. if they're a local def, because otherwise they'll be saved
  2912. to the ppu referencing a local symtable entry that doesn't
  2913. exist in the ppu) }
  2914. oldsymtablestack:=symtablestack;
  2915. { do not simply push/pop current_module.localsymtable, because
  2916. that can have side-effects (e.g., it removes helpers) }
  2917. symtablestack:=nil;
  2918. result:=cpointerdef.create(def);
  2919. setup_reusable_def(def,result,res,oldsymtablestack);
  2920. { res^.Data may still be nil -> don't overwrite result }
  2921. exit;
  2922. end;
  2923. result:=tpointerdef(res^.Data);
  2924. end;
  2925. class function tpointerdef.getreusable_no_free(def: tdef): tpointerdef;
  2926. begin
  2927. result:=getreusable(def);
  2928. if not result.is_registered then
  2929. include(result.defoptions,df_not_registered_no_free);
  2930. end;
  2931. function tpointerdef.size: asizeint;
  2932. begin
  2933. result:=sizeof(pint);
  2934. end;
  2935. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  2936. begin
  2937. inherited ppuload(pointerdef,ppufile);
  2938. has_pointer_math:=(ppufile.getbyte<>0);
  2939. ppuload_platform(ppufile);
  2940. end;
  2941. function tpointerdef.getcopy : tstoreddef;
  2942. begin
  2943. { don't use direct pointeddef if it is a forwarddef because in other case
  2944. one of them will be destroyed on forward type resolve and the second will
  2945. point to garbage }
  2946. if pointeddef.typ=forwarddef then
  2947. result:=cpointerdef.create(tforwarddef(pointeddef).getcopy)
  2948. else
  2949. result:=cpointerdef.create(pointeddef);
  2950. tpointerdef(result).has_pointer_math:=has_pointer_math;
  2951. tpointerdef(result).savesize:=savesize;
  2952. end;
  2953. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2954. begin
  2955. inherited ppuwrite(ppufile);
  2956. ppufile.putbyte(byte(has_pointer_math));
  2957. writeentry(ppufile,ibpointerdef);
  2958. end;
  2959. function tpointerdef.GetTypeName : string;
  2960. begin
  2961. { parameter types and the resultdef of a procvardef can contain a
  2962. pointer to this procvardef itself, resulting in endless recursion ->
  2963. use the typesym's name instead if it exists (if it doesn't, such as
  2964. for anynonymous procedure types in macpas/iso mode, then there cannot
  2965. be any recursive references to it either) }
  2966. if (pointeddef.typ<>procvardef) or
  2967. not assigned(pointeddef.typesym) then
  2968. GetTypeName:='^'+pointeddef.typename
  2969. else
  2970. GetTypeName:='^'+pointeddef.typesym.realname;
  2971. end;
  2972. function tpointerdef.pointer_arithmetic_int_type:tdef;
  2973. begin
  2974. result:=ptrsinttype;
  2975. end;
  2976. function tpointerdef.pointer_subtraction_result_type:tdef;
  2977. begin
  2978. result:=ptrsinttype;
  2979. end;
  2980. function tpointerdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  2981. begin
  2982. result:=true;
  2983. end;
  2984. {****************************************************************************
  2985. TCLASSREFDEF
  2986. ****************************************************************************}
  2987. constructor tclassrefdef.create(def:tdef);
  2988. begin
  2989. inherited create(classrefdef,def);
  2990. end;
  2991. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  2992. begin
  2993. inherited ppuload(classrefdef,ppufile);
  2994. ppuload_platform(ppufile);
  2995. end;
  2996. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  2997. begin
  2998. inherited ppuwrite(ppufile);
  2999. writeentry(ppufile,ibclassrefdef);
  3000. end;
  3001. function tclassrefdef.getcopy:tstoreddef;
  3002. begin
  3003. if pointeddef.typ=forwarddef then
  3004. result:=cclassrefdef.create(tforwarddef(pointeddef).getcopy)
  3005. else
  3006. result:=cclassrefdef.create(pointeddef);
  3007. end;
  3008. function tclassrefdef.GetTypeName : string;
  3009. begin
  3010. GetTypeName:='Class Of '+pointeddef.typename;
  3011. end;
  3012. function tclassrefdef.is_publishable : boolean;
  3013. begin
  3014. result:=true;
  3015. end;
  3016. function tclassrefdef.rtti_mangledname(rt: trttitype): TSymStr;
  3017. begin
  3018. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  3019. result:=inherited rtti_mangledname(rt)
  3020. else
  3021. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  3022. end;
  3023. procedure tclassrefdef.register_created_object_type;
  3024. begin
  3025. tobjectdef(pointeddef).register_created_classref_type;
  3026. end;
  3027. {***************************************************************************
  3028. TSETDEF
  3029. ***************************************************************************}
  3030. constructor tsetdef.create(def: tdef; low, high: asizeint; doregister: boolean);
  3031. var
  3032. setallocbits: aint;
  3033. packedsavesize: aint;
  3034. actual_setalloc: ShortInt;
  3035. begin
  3036. inherited create(setdef,doregister);
  3037. elementdef:=def;
  3038. setmax:=high;
  3039. actual_setalloc:=current_settings.setalloc;
  3040. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  3041. if actual_setalloc=0 then
  3042. actual_setalloc:=1;
  3043. {$endif}
  3044. if (actual_setalloc=0) then
  3045. begin
  3046. setbase:=0;
  3047. if (high<32) then
  3048. savesize:=Sizeof(longint)
  3049. else if (high<256) then
  3050. savesize:=32
  3051. else
  3052. savesize:=(high+7) div 8
  3053. end
  3054. else
  3055. begin
  3056. setallocbits:=actual_setalloc*8;
  3057. setbase:=low and not(setallocbits-1);
  3058. packedsavesize:=actual_setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  3059. savesize:=packedsavesize;
  3060. {$if not defined(cpu8bitalu) and not defined(cpu16bitalu)}
  3061. if savesize=3 then
  3062. savesize:=4;
  3063. {$endif}
  3064. end;
  3065. end;
  3066. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  3067. begin
  3068. inherited ppuload(setdef,ppufile);
  3069. ppufile.getderef(elementdefderef);
  3070. savesize:=ppufile.getasizeint;
  3071. setbase:=ppufile.getasizeint;
  3072. setmax:=ppufile.getasizeint;
  3073. ppuload_platform(ppufile);
  3074. end;
  3075. function tsetdef.getcopy : tstoreddef;
  3076. begin
  3077. result:=csetdef.create(elementdef,setbase,setmax,true);
  3078. { the copy might have been created with a different setalloc setting }
  3079. tsetdef(result).savesize:=savesize;
  3080. end;
  3081. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  3082. begin
  3083. inherited ppuwrite(ppufile);
  3084. ppufile.putderef(elementdefderef);
  3085. ppufile.putasizeint(savesize);
  3086. ppufile.putasizeint(setbase);
  3087. ppufile.putasizeint(setmax);
  3088. writeentry(ppufile,ibsetdef);
  3089. end;
  3090. procedure tsetdef.buildderef;
  3091. begin
  3092. inherited buildderef;
  3093. elementdefderef.build(elementdef);
  3094. end;
  3095. procedure tsetdef.deref;
  3096. begin
  3097. inherited deref;
  3098. elementdef:=tdef(elementdefderef.resolve);
  3099. end;
  3100. function tsetdef.is_publishable : boolean;
  3101. begin
  3102. is_publishable:=savesize in [1,2,4];
  3103. end;
  3104. function tsetdef.GetTypeName : string;
  3105. begin
  3106. if assigned(elementdef) then
  3107. GetTypeName:='Set Of '+elementdef.typename
  3108. else
  3109. GetTypeName:='Empty Set';
  3110. end;
  3111. {***************************************************************************
  3112. TFORMALDEF
  3113. ***************************************************************************}
  3114. constructor tformaldef.create(Atyped:boolean);
  3115. begin
  3116. inherited create(formaldef,true);
  3117. typed:=Atyped;
  3118. savesize:=0;
  3119. end;
  3120. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  3121. begin
  3122. inherited ppuload(formaldef,ppufile);
  3123. typed:=boolean(ppufile.getbyte);
  3124. savesize:=0;
  3125. ppuload_platform(ppufile);
  3126. end;
  3127. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  3128. begin
  3129. inherited ppuwrite(ppufile);
  3130. ppufile.putbyte(byte(typed));
  3131. writeentry(ppufile,ibformaldef);
  3132. end;
  3133. function tformaldef.GetTypeName : string;
  3134. begin
  3135. if typed then
  3136. GetTypeName:='<Typed formal type>'
  3137. else
  3138. GetTypeName:='<Formal type>';
  3139. end;
  3140. {***************************************************************************
  3141. TARRAYDEF
  3142. ***************************************************************************}
  3143. constructor tarraydef.create(l,h:asizeint;def:tdef);
  3144. begin
  3145. inherited create(arraydef,true);
  3146. lowrange:=l;
  3147. highrange:=h;
  3148. rangedef:=def;
  3149. _elementdef:=nil;
  3150. arrayoptions:=[];
  3151. symtable:=tarraysymtable.create(self);
  3152. end;
  3153. class function tarraydef.getreusable(def: tdef; elems: asizeint): tarraydef;
  3154. var
  3155. res: PHashSetItem;
  3156. oldsymtablestack: tsymtablestack;
  3157. arrdesc: packed record
  3158. def: tdef;
  3159. elecount: asizeint;
  3160. end;
  3161. begin
  3162. if not assigned(current_module) then
  3163. internalerror(2011081301);
  3164. arrdesc.def:=def;
  3165. arrdesc.elecount:=elems;
  3166. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  3167. if not assigned(res^.Data) then
  3168. begin
  3169. { since these pointerdefs can be reused anywhere in the current
  3170. unit, add them to the global/staticsymtable (or local symtable
  3171. if they're a local def, because otherwise they'll be saved
  3172. to the ppu referencing a local symtable entry that doesn't
  3173. exist in the ppu) }
  3174. oldsymtablestack:=symtablestack;
  3175. { do not simply push/pop current_module.localsymtable, because
  3176. that can have side-effects (e.g., it removes helpers) }
  3177. symtablestack:=nil;
  3178. result:=carraydef.create(0,elems-1,ptrsinttype);
  3179. result.elementdef:=def;
  3180. setup_reusable_def(def,result,res,oldsymtablestack);
  3181. { res^.Data may still be nil -> don't overwrite result }
  3182. exit;
  3183. end;
  3184. result:=tarraydef(res^.Data);
  3185. end;
  3186. class function tarraydef.getreusable_no_free(def: tdef; elems: asizeint): tarraydef;
  3187. begin
  3188. result:=getreusable(def,elems);
  3189. if not result.is_registered then
  3190. include(result.defoptions,df_not_registered_no_free);
  3191. end;
  3192. destructor tarraydef.destroy;
  3193. begin
  3194. symtable.free;
  3195. symtable:=nil;
  3196. inherited;
  3197. end;
  3198. constructor tarraydef.create_from_pointer(def:tpointerdef);
  3199. begin
  3200. { divide by the element size and do -1 so the array will have a valid size,
  3201. further, the element size might be 0 e.g. for empty records, so use max(...,1)
  3202. to avoid a division by zero }
  3203. self.create(0,(high(asizeint) div max(def.pointeddef.size,1))-1,ptrsinttype);
  3204. arrayoptions:=[ado_IsConvertedPointer];
  3205. setelementdef(def.pointeddef);
  3206. end;
  3207. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  3208. begin
  3209. inherited ppuload(arraydef,ppufile);
  3210. { the addresses are calculated later }
  3211. ppufile.getderef(_elementdefderef);
  3212. ppufile.getderef(rangedefderef);
  3213. lowrange:=ppufile.getasizeint;
  3214. highrange:=ppufile.getasizeint;
  3215. ppufile.getsmallset(arrayoptions);
  3216. ppuload_platform(ppufile);
  3217. symtable:=tarraysymtable.create(self);
  3218. tarraysymtable(symtable).ppuload(ppufile)
  3219. end;
  3220. function tarraydef.getcopy : tstoreddef;
  3221. begin
  3222. result:=carraydef.create(lowrange,highrange,rangedef);
  3223. tarraydef(result).arrayoptions:=arrayoptions;
  3224. tarraydef(result)._elementdef:=_elementdef;
  3225. end;
  3226. procedure tarraydef.buildderef;
  3227. begin
  3228. inherited buildderef;
  3229. tarraysymtable(symtable).buildderef;
  3230. _elementdefderef.build(_elementdef);
  3231. rangedefderef.build(rangedef);
  3232. end;
  3233. procedure tarraydef.deref;
  3234. begin
  3235. inherited deref;
  3236. tarraysymtable(symtable).deref(false);
  3237. _elementdef:=tdef(_elementdefderef.resolve);
  3238. rangedef:=tdef(rangedefderef.resolve);
  3239. end;
  3240. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  3241. begin
  3242. inherited ppuwrite(ppufile);
  3243. ppufile.putderef(_elementdefderef);
  3244. ppufile.putderef(rangedefderef);
  3245. ppufile.putasizeint(lowrange);
  3246. ppufile.putasizeint(highrange);
  3247. ppufile.putsmallset(arrayoptions);
  3248. writeentry(ppufile,ibarraydef);
  3249. tarraysymtable(symtable).ppuwrite(ppufile);
  3250. end;
  3251. function tarraydef.elesize : asizeint;
  3252. begin
  3253. if (ado_IsBitPacked in arrayoptions) then
  3254. internalerror(2006080101);
  3255. if assigned(_elementdef) then
  3256. result:=_elementdef.size
  3257. else
  3258. result:=0;
  3259. end;
  3260. function tarraydef.elepackedbitsize : asizeint;
  3261. begin
  3262. if not(ado_IsBitPacked in arrayoptions) then
  3263. internalerror(2006080102);
  3264. if assigned(_elementdef) then
  3265. result:=_elementdef.packedbitsize
  3266. else
  3267. result:=0;
  3268. end;
  3269. function tarraydef.elecount : asizeuint;
  3270. var
  3271. qhigh,qlow : qword;
  3272. begin
  3273. if ado_IsDynamicArray in arrayoptions then
  3274. begin
  3275. result:=0;
  3276. exit;
  3277. end;
  3278. if (highrange>0) and (lowrange<0) then
  3279. begin
  3280. qhigh:=highrange;
  3281. qlow:=qword(-lowrange);
  3282. { prevent overflow, return 0 to indicate overflow }
  3283. if qhigh+qlow>qword(high(asizeint)-1) then
  3284. result:=0
  3285. else
  3286. result:=qhigh+qlow+1;
  3287. end
  3288. else
  3289. result:=int64(highrange)-lowrange+1;
  3290. end;
  3291. function tarraydef.size : asizeint;
  3292. var
  3293. cachedelecount : asizeuint;
  3294. cachedelesize : asizeint;
  3295. begin
  3296. if ado_IsDynamicArray in arrayoptions then
  3297. begin
  3298. size:=voidpointertype.size;
  3299. exit;
  3300. end;
  3301. { Tarraydef.size may never be called for an open array! }
  3302. if highrange<lowrange then
  3303. internalerror(99080501);
  3304. if not (ado_IsBitPacked in arrayoptions) then
  3305. cachedelesize:=elesize
  3306. else
  3307. cachedelesize := elepackedbitsize;
  3308. cachedelecount:=elecount;
  3309. if (cachedelesize = 0) then
  3310. begin
  3311. size := 0;
  3312. exit;
  3313. end;
  3314. if (cachedelecount = 0) then
  3315. begin
  3316. size := -1;
  3317. exit;
  3318. end;
  3319. { prevent overflow, return -1 to indicate overflow }
  3320. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  3321. if (cachedelecount > asizeuint(high(asizeint))) or
  3322. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  3323. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  3324. accessing the array, see ncgmem (PFV) }
  3325. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  3326. begin
  3327. result:=-1;
  3328. exit;
  3329. end;
  3330. result:=cachedelesize*asizeint(cachedelecount);
  3331. if (ado_IsBitPacked in arrayoptions) then
  3332. { can't just add 7 and divide by 8, because that may overflow }
  3333. result:=result div 8 + ord((result mod 8)<>0);
  3334. {$ifdef cpu16bitaddr}
  3335. if result>65535 then
  3336. begin
  3337. result:=-1;
  3338. exit;
  3339. end;
  3340. {$endif cpu16bitaddr}
  3341. end;
  3342. procedure tarraydef.setelementdef(def:tdef);
  3343. begin
  3344. _elementdef:=def;
  3345. if not(
  3346. (ado_IsDynamicArray in arrayoptions) or
  3347. (ado_IsConvertedPointer in arrayoptions) or
  3348. (highrange<lowrange)
  3349. ) and
  3350. (size=-1) then
  3351. Message(sym_e_segment_too_large);
  3352. end;
  3353. function tarraydef.alignment : shortint;
  3354. begin
  3355. { alignment of dyn. arrays doesn't depend on the element size }
  3356. if (ado_IsDynamicArray in arrayoptions) then
  3357. alignment:=voidpointertype.alignment
  3358. { alignment is the target alignment for the used load size }
  3359. else if (ado_IsBitPacked in arrayoptions) and
  3360. (elementdef.typ in [enumdef,orddef]) then
  3361. alignment:=cgsize_orddef(int_cgsize(packedbitsloadsize(elepackedbitsize))).alignment
  3362. { alignment is the alignment of the elements }
  3363. else
  3364. alignment:=elementdef.alignment
  3365. end;
  3366. function tarraydef.needs_inittable : boolean;
  3367. begin
  3368. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  3369. end;
  3370. function tarraydef.needs_separate_initrtti : boolean;
  3371. begin
  3372. if ado_IsBitPacked in arrayoptions then
  3373. result:=false
  3374. else
  3375. result:=elementdef.needs_separate_initrtti;
  3376. end;
  3377. function tarraydef.GetTypeName : string;
  3378. begin
  3379. if (ado_IsConstString in arrayoptions) then
  3380. result:='Constant String'
  3381. else if (ado_isarrayofconst in arrayoptions) or
  3382. (ado_isConstructor in arrayoptions) then
  3383. begin
  3384. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  3385. GetTypeName:='Array Of Const'
  3386. else
  3387. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  3388. end
  3389. else if (ado_IsDynamicArray in arrayoptions) then
  3390. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  3391. else if ((highrange=-1) and (lowrange=0)) then
  3392. GetTypeName:='{Open} Array Of '+elementdef.typename
  3393. else
  3394. begin
  3395. result := '';
  3396. if (ado_IsBitPacked in arrayoptions) then
  3397. result:='BitPacked ';
  3398. if rangedef.typ=enumdef then
  3399. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  3400. else
  3401. result:=result+'Array['+tostr(lowrange)+'..'+
  3402. tostr(highrange)+'] Of '+elementdef.typename
  3403. end;
  3404. end;
  3405. function tarraydef.getmangledparaname : TSymStr;
  3406. begin
  3407. if ado_isarrayofconst in arrayoptions then
  3408. getmangledparaname:='array_of_const'
  3409. else
  3410. if ((highrange=-1) and (lowrange=0)) then
  3411. getmangledparaname:='array_of_'+elementdef.mangledparaname
  3412. else
  3413. internalerror(200204176);
  3414. end;
  3415. function tarraydef.is_publishable : boolean;
  3416. begin
  3417. Result:=ado_IsDynamicArray in arrayoptions;
  3418. end;
  3419. {***************************************************************************
  3420. tabstractrecorddef
  3421. ***************************************************************************}
  3422. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp;doregister:boolean);
  3423. begin
  3424. inherited create(dt,doregister);
  3425. objname:=stringdup(upper(n));
  3426. objrealname:=stringdup(n);
  3427. objectoptions:=[];
  3428. if assigned(current_module.namespace) then
  3429. begin
  3430. import_lib:=stringdup(current_module.namespace^);
  3431. replace(import_lib^,'.','/');
  3432. end;
  3433. end;
  3434. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3435. begin
  3436. inherited ppuload(dt,ppufile);
  3437. objrealname:=stringdup(ppufile.getstring);
  3438. objname:=stringdup(upper(objrealname^));
  3439. import_lib:=stringdup(ppufile.getstring);
  3440. { only used for external C++ classes and Java classes/records }
  3441. if (import_lib^='') then
  3442. stringdispose(import_lib);
  3443. ppufile.getsmallset(objectoptions);
  3444. end;
  3445. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  3446. begin
  3447. inherited ppuwrite(ppufile);
  3448. ppufile.putstring(objrealname^);
  3449. if assigned(import_lib) then
  3450. ppufile.putstring(import_lib^)
  3451. else
  3452. ppufile.putstring('');
  3453. ppufile.putsmallset(objectoptions);
  3454. end;
  3455. destructor tabstractrecorddef.destroy;
  3456. begin
  3457. stringdispose(objname);
  3458. stringdispose(objrealname);
  3459. stringdispose(import_lib);
  3460. tcinitcode.free;
  3461. inherited destroy;
  3462. end;
  3463. procedure tabstractrecorddef.buildderefimpl;
  3464. begin
  3465. inherited buildderefimpl;
  3466. if not (df_copied_def in defoptions) then
  3467. tstoredsymtable(symtable).buildderefimpl;
  3468. end;
  3469. procedure tabstractrecorddef.derefimpl;
  3470. begin
  3471. inherited derefimpl;
  3472. if not (df_copied_def in defoptions) then
  3473. tstoredsymtable(symtable).derefimpl(false);
  3474. end;
  3475. procedure tabstractrecorddef.check_forwards;
  3476. begin
  3477. { the defs of a copied def are defined for the original type only }
  3478. if not(df_copied_def in defoptions) then
  3479. tstoredsymtable(symtable).check_forwards;
  3480. end;
  3481. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  3482. var
  3483. i: longint;
  3484. sym: tsym;
  3485. begin
  3486. for i:=0 to symtable.SymList.Count-1 do
  3487. begin
  3488. sym:=tsym(symtable.SymList[i]);
  3489. if sym.typ=procsym then
  3490. begin
  3491. result:=tprocsym(sym).find_procdef_bytype(pt);
  3492. if assigned(result) then
  3493. exit;
  3494. end;
  3495. end;
  3496. result:=nil;
  3497. end;
  3498. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  3499. begin
  3500. if t=gs_record then
  3501. GetSymtable:=symtable
  3502. else
  3503. GetSymtable:=nil;
  3504. end;
  3505. function tabstractrecorddef.is_packed:boolean;
  3506. begin
  3507. result:=tabstractrecordsymtable(symtable).is_packed;
  3508. end;
  3509. function tabstractrecorddef.RttiName: string;
  3510. function generate_full_paramname(maxlength:longint):string;
  3511. const
  3512. commacount : array[boolean] of longint = (0,1);
  3513. var
  3514. fullparas,
  3515. paramname : ansistring;
  3516. module : tmodule;
  3517. sym : ttypesym;
  3518. i : longint;
  3519. begin
  3520. { we want at least enough space for an ellipsis }
  3521. if maxlength<3 then
  3522. internalerror(2014121203);
  3523. fullparas:='';
  3524. for i:=0 to genericparas.count-1 do
  3525. begin
  3526. sym:=ttypesym(genericparas[i]);
  3527. module:=find_module_from_symtable(sym.owner);
  3528. if not assigned(module) then
  3529. internalerror(2014121202);
  3530. paramname:=module.realmodulename^;
  3531. if sym.typedef.typ in [objectdef,recorddef] then
  3532. paramname:=paramname+'.'+tabstractrecorddef(sym.typedef).rttiname
  3533. else
  3534. paramname:=paramname+'.'+sym.typedef.typename;
  3535. if length(fullparas)+commacount[i>0]+length(paramname)>maxlength then
  3536. begin
  3537. if i>0 then
  3538. fullparas:=fullparas+',...'
  3539. else
  3540. fullparas:=fullparas+'...';
  3541. break;
  3542. end;
  3543. { could we fit an ellipsis after this parameter if it should be too long? }
  3544. if (maxlength-(length(fullparas)+commacount[i>0]+length(paramname))<4) and (i<genericparas.count-1) then
  3545. begin
  3546. { then omit already this parameter }
  3547. if i>0 then
  3548. fullparas:=fullparas+',...'
  3549. else
  3550. fullparas:=fullparas+'...';
  3551. break;
  3552. end;
  3553. if i>0 then
  3554. fullparas:=fullparas+',';
  3555. fullparas:=fullparas+paramname;
  3556. end;
  3557. result:=fullparas;
  3558. end;
  3559. var
  3560. nongeneric,
  3561. basename : string;
  3562. i,
  3563. remlength,
  3564. paramcount,
  3565. crcidx : longint;
  3566. begin
  3567. if rttistring='' then
  3568. begin
  3569. if is_specialization then
  3570. begin
  3571. rttistring:=OwnerHierarchyName;
  3572. { there should be two $ characters, one before the CRC and one before the count }
  3573. crcidx:=-1;
  3574. for i:=length(objrealname^) downto 1 do
  3575. if objrealname^[i]='$' then
  3576. begin
  3577. crcidx:=i;
  3578. break;
  3579. end;
  3580. if crcidx<0 then
  3581. internalerror(2014121201);
  3582. basename:=copy(objrealname^,1,crcidx-1);
  3583. split_generic_name(basename,nongeneric,paramcount);
  3584. rttistring:=rttistring+nongeneric+'<';
  3585. remlength:=255-length(rttistring)-1;
  3586. if remlength<4 then
  3587. rttistring:=rttistring+'>'
  3588. else
  3589. rttistring:=rttistring+generate_full_paramname(remlength)+'>';
  3590. end
  3591. else
  3592. if is_generic then
  3593. begin
  3594. rttistring:=OwnerHierarchyName;
  3595. split_generic_name(objrealname^,nongeneric,paramcount);
  3596. rttistring:=rttistring+nongeneric+'<';
  3597. { we don't want any ',' if there is only one parameter }
  3598. for i:=0 to paramcount-0 do
  3599. rttistring:=rttistring+',';
  3600. rttistring:=rttistring+'>';
  3601. end
  3602. else
  3603. rttistring:=OwnerHierarchyName+objrealname^;
  3604. end;
  3605. result:=rttistring;
  3606. end;
  3607. function tabstractrecorddef.search_enumerator_get: tprocdef;
  3608. var
  3609. sym : tsym;
  3610. i : integer;
  3611. pd : tprocdef;
  3612. hashedid : THashedIDString;
  3613. begin
  3614. result:=nil;
  3615. hashedid.id:='GETENUMERATOR';
  3616. sym:=tsym(symtable.FindWithHash(hashedid));
  3617. if assigned(sym) and (sym.typ=procsym) then
  3618. begin
  3619. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3620. begin
  3621. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3622. if (pd.proctypeoption = potype_function) and
  3623. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  3624. (pd.visibility >= vis_public) then
  3625. begin
  3626. result:=pd;
  3627. exit;
  3628. end;
  3629. end;
  3630. end;
  3631. end;
  3632. function tabstractrecorddef.search_enumerator_move: tprocdef;
  3633. var
  3634. sym : tsym;
  3635. i : integer;
  3636. pd : tprocdef;
  3637. hashedid : THashedIDString;
  3638. begin
  3639. result:=nil;
  3640. // first search for po_enumerator_movenext method modifier
  3641. // then search for public function MoveNext: Boolean
  3642. for i:=0 to symtable.SymList.Count-1 do
  3643. begin
  3644. sym:=TSym(symtable.SymList[i]);
  3645. if (sym.typ=procsym) then
  3646. begin
  3647. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  3648. if assigned(pd) then
  3649. begin
  3650. result:=pd;
  3651. exit;
  3652. end;
  3653. end;
  3654. end;
  3655. hashedid.id:='MOVENEXT';
  3656. sym:=tsym(symtable.FindWithHash(hashedid));
  3657. if assigned(sym) and (sym.typ=procsym) then
  3658. begin
  3659. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  3660. begin
  3661. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3662. if (pd.proctypeoption = potype_function) and
  3663. is_boolean(pd.returndef) and
  3664. (pd.minparacount = 0) and
  3665. (pd.visibility >= vis_public) then
  3666. begin
  3667. result:=pd;
  3668. exit;
  3669. end;
  3670. end;
  3671. end;
  3672. end;
  3673. function tabstractrecorddef.search_enumerator_current: tsym;
  3674. var
  3675. sym: tsym;
  3676. i: integer;
  3677. hashedid : THashedIDString;
  3678. begin
  3679. result:=nil;
  3680. // first search for ppo_enumerator_current property modifier
  3681. // then search for public property Current
  3682. for i:=0 to symtable.SymList.Count-1 do
  3683. begin
  3684. sym:=TSym(symtable.SymList[i]);
  3685. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  3686. begin
  3687. result:=sym;
  3688. exit;
  3689. end;
  3690. end;
  3691. hashedid.id:='CURRENT';
  3692. sym:=tsym(symtable.FindWithHash(hashedid));
  3693. if assigned(sym) and (sym.typ=propertysym) and
  3694. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  3695. begin
  3696. result:=sym;
  3697. exit;
  3698. end;
  3699. end;
  3700. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  3701. var
  3702. st: tsymtable;
  3703. enclosingdef: tdef;
  3704. begin
  3705. if typ=objectdef then
  3706. result:=tobjectdef(self).objextname^
  3707. else if assigned(typesym) then
  3708. result:=typesym.realname
  3709. { have to generate anonymous nested type in current unit/class/record }
  3710. else
  3711. internalerror(2011032601);
  3712. { in case of specializations, add some extras to prevent name conflicts
  3713. with nested classes }
  3714. if df_specialization in defoptions then
  3715. result:='$'+result+'$specialization$';
  3716. st:=owner;
  3717. while assigned(st) and
  3718. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  3719. begin
  3720. { nested classes are named as "OuterClass$InnerClass" }
  3721. enclosingdef:=tdef(st.defowner);
  3722. if enclosingdef.typ=procdef then
  3723. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  3724. else if enclosingdef.typ=objectdef then
  3725. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  3726. else if assigned(enclosingdef.typesym) then
  3727. result:=enclosingdef.typesym.realname+'$'+result
  3728. else
  3729. internalerror(2011060305);
  3730. st:=enclosingdef.owner;
  3731. end;
  3732. if with_package_name and
  3733. assigned(import_lib) then
  3734. result:=import_lib^+'/'+result;
  3735. end;
  3736. function tabstractrecorddef.contains_float_field: boolean;
  3737. var
  3738. i : longint;
  3739. begin
  3740. result:=true;
  3741. for i:=0 to symtable.symlist.count-1 do
  3742. begin
  3743. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  3744. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  3745. continue;
  3746. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  3747. begin
  3748. if tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).is_fpuregable then
  3749. exit;
  3750. { search recursively }
  3751. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  3752. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_float_field) then
  3753. exit;
  3754. end;
  3755. end;
  3756. result:=false;
  3757. end;
  3758. {***************************************************************************
  3759. trecorddef
  3760. ***************************************************************************}
  3761. constructor trecorddef.create(const n:string; p:TSymtable);
  3762. begin
  3763. inherited create(n,recorddef,true);
  3764. symtable:=p;
  3765. { we can own the symtable only if nobody else owns a copy so far }
  3766. if symtable.refcount=1 then
  3767. symtable.defowner:=self;
  3768. isunion:=false;
  3769. end;
  3770. constructor trecorddef.create_global_internal(n: string; packrecords, recordalignmin, maxCrecordalign: shortint);
  3771. var
  3772. oldsymtablestack: tsymtablestack;
  3773. ts: ttypesym;
  3774. definedname: boolean;
  3775. begin
  3776. { construct name }
  3777. definedname:=n<>'';
  3778. if not definedname then
  3779. n:='$InternalRec'+tostr(current_module.deflist.count);
  3780. oldsymtablestack:=symtablestack;
  3781. { do not simply push/pop current_module.localsymtable, because
  3782. that can have side-effects (e.g., it removes helpers) }
  3783. symtablestack:=nil;
  3784. symtable:=trecordsymtable.create(n,packrecords,recordalignmin,maxCrecordalign);
  3785. symtable.defowner:=self;
  3786. isunion:=false;
  3787. inherited create(n,recorddef,true);
  3788. { if we specified a name, then we'll probably want to look up the
  3789. type again by name too -> create typesym }
  3790. ts:=nil;
  3791. if definedname then
  3792. begin
  3793. ts:=ctypesym.create(n,self,true);
  3794. { avoid hints about unused types (these may only be used for
  3795. typed constant data) }
  3796. ts.increfcount;
  3797. end;
  3798. if assigned(current_module.localsymtable) then
  3799. begin
  3800. current_module.localsymtable.insertdef(self);
  3801. if definedname then
  3802. current_module.localsymtable.insert(ts);
  3803. end
  3804. else
  3805. begin
  3806. current_module.globalsymtable.insertdef(self);
  3807. if definedname then
  3808. current_module.globalsymtable.insert(ts);
  3809. end;
  3810. symtablestack:=oldsymtablestack;
  3811. { don't create RTTI for internal types, these are not exported }
  3812. defstates:=defstates+[ds_rtti_table_written,ds_init_table_written];
  3813. end;
  3814. function trecorddef.add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
  3815. var
  3816. sym: tfieldvarsym;
  3817. name: TIDString;
  3818. pname: ^TIDString;
  3819. begin
  3820. if optionalname='' then
  3821. begin
  3822. name:='$f'+tostr(trecordsymtable(symtable).symlist.count);
  3823. pname:=@name
  3824. end
  3825. else
  3826. pname:=@optionalname;
  3827. sym:=cfieldvarsym.create(pname^,vs_value,def,[],true);
  3828. symtable.insert(sym);
  3829. trecordsymtable(symtable).addfield(sym,vis_hidden);
  3830. result:=sym;
  3831. end;
  3832. procedure trecorddef.add_fields_from_deflist(fieldtypes: tfplist);
  3833. var
  3834. i: longint;
  3835. begin
  3836. for i:=0 to fieldtypes.count-1 do
  3837. add_field_by_def('',tdef(fieldtypes[i]));
  3838. end;
  3839. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  3840. procedure readvariantrecdesc(var variantrecdesc : pvariantrecdesc);
  3841. var
  3842. i,j : longint;
  3843. begin
  3844. if ppufile.getbyte=1 then
  3845. begin
  3846. new(variantrecdesc);
  3847. ppufile.getderef(variantrecdesc^.variantselectorderef);
  3848. SetLength(variantrecdesc^.branches,ppufile.getasizeint);
  3849. for i:=0 to high(variantrecdesc^.branches) do
  3850. begin
  3851. SetLength(variantrecdesc^.branches[i].values,ppufile.getasizeint);
  3852. for j:=0 to high(variantrecdesc^.branches[i].values) do
  3853. variantrecdesc^.branches[i].values[j]:=ppufile.getexprint;
  3854. readvariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  3855. end;
  3856. end
  3857. else
  3858. variantrecdesc:=nil;
  3859. end;
  3860. begin
  3861. inherited ppuload(recorddef,ppufile);
  3862. if df_copied_def in defoptions then
  3863. begin
  3864. ppufile.getderef(cloneddefderef);
  3865. ppuload_platform(ppufile);
  3866. end
  3867. else
  3868. begin
  3869. ppuload_platform(ppufile);
  3870. symtable:=trecordsymtable.create(objrealname^,0,0,0);
  3871. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  3872. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  3873. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  3874. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  3875. trecordsymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  3876. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  3877. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  3878. trecordsymtable(symtable).ppuload(ppufile);
  3879. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  3880. but because iso mode supports no units, there is no need to store the variantrecdesc
  3881. in the ppu
  3882. }
  3883. // readvariantrecdesc(variantrecdesc);
  3884. { requires usefieldalignment to be set }
  3885. symtable.defowner:=self;
  3886. end;
  3887. isunion:=false;
  3888. end;
  3889. destructor trecorddef.destroy;
  3890. begin
  3891. if assigned(symtable) then
  3892. begin
  3893. symtable.free;
  3894. symtable:=nil;
  3895. end;
  3896. inherited destroy;
  3897. end;
  3898. function trecorddef.getcopy : tstoreddef;
  3899. begin
  3900. result:=crecorddef.create(objrealname^,symtable.getcopy);
  3901. trecorddef(result).isunion:=isunion;
  3902. include(trecorddef(result).defoptions,df_copied_def);
  3903. if assigned(tcinitcode) then
  3904. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  3905. if assigned(import_lib) then
  3906. trecorddef(result).import_lib:=stringdup(import_lib^);
  3907. end;
  3908. function trecorddef.needs_inittable : boolean;
  3909. begin
  3910. needs_inittable:=trecordsymtable(symtable).needs_init_final
  3911. end;
  3912. function trecorddef.needs_separate_initrtti : boolean;
  3913. begin
  3914. result:=true;
  3915. end;
  3916. procedure trecorddef.buildderef;
  3917. begin
  3918. inherited buildderef;
  3919. if df_copied_def in defoptions then
  3920. cloneddefderef.build(symtable.defowner)
  3921. else
  3922. tstoredsymtable(symtable).buildderef;
  3923. end;
  3924. procedure trecorddef.deref;
  3925. begin
  3926. inherited deref;
  3927. { now dereference the definitions }
  3928. if df_copied_def in defoptions then
  3929. begin
  3930. cloneddef:=trecorddef(cloneddefderef.resolve);
  3931. symtable:=cloneddef.symtable.getcopy;
  3932. end
  3933. else
  3934. tstoredsymtable(symtable).deref(false);
  3935. { internal types, only load from the system unit }
  3936. if assigned(owner) and
  3937. assigned(owner.name) and
  3938. (owner.name^='SYSTEM') then
  3939. begin
  3940. { TGUID }
  3941. if not assigned(rec_tguid) and
  3942. (upper(typename)='TGUID') then
  3943. rec_tguid:=self
  3944. { JMP_BUF }
  3945. else if not assigned(rec_jmp_buf) and
  3946. (upper(typename)='JMP_BUF') then
  3947. rec_jmp_buf:=self
  3948. else if not assigned(rec_exceptaddr) and
  3949. (upper(typename)='TEXCEPTADDR') then
  3950. rec_exceptaddr:=self;
  3951. end;
  3952. end;
  3953. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  3954. procedure writevariantrecdesc(variantrecdesc : pvariantrecdesc);
  3955. var
  3956. i,j : longint;
  3957. begin
  3958. if assigned(variantrecdesc) then
  3959. begin
  3960. ppufile.putbyte(1);
  3961. ppufile.putderef(variantrecdesc^.variantselectorderef);
  3962. ppufile.putasizeint(length(variantrecdesc^.branches));
  3963. for i:=0 to high(variantrecdesc^.branches) do
  3964. begin
  3965. ppufile.putasizeint(length(variantrecdesc^.branches[i].values));
  3966. for j:=0 to high(variantrecdesc^.branches[i].values) do
  3967. ppufile.putexprint(variantrecdesc^.branches[i].values[j]);
  3968. writevariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  3969. end;
  3970. end
  3971. else
  3972. ppufile.putbyte(0);
  3973. end;
  3974. begin
  3975. inherited ppuwrite(ppufile);
  3976. if df_copied_def in defoptions then
  3977. ppufile.putderef(cloneddefderef)
  3978. else
  3979. begin
  3980. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  3981. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  3982. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  3983. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  3984. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignmin));
  3985. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  3986. ppufile.putword(trecordsymtable(symtable).paddingsize);
  3987. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  3988. but because iso mode supports no units, there is no need to store the variantrecdesc
  3989. in the ppu
  3990. }
  3991. // writevariantrecdesc(variantrecdesc);
  3992. end;
  3993. writeentry(ppufile,ibrecorddef);
  3994. if not(df_copied_def in defoptions) then
  3995. trecordsymtable(symtable).ppuwrite(ppufile);
  3996. end;
  3997. function trecorddef.size:asizeint;
  3998. begin
  3999. result:=trecordsymtable(symtable).datasize;
  4000. end;
  4001. function trecorddef.alignment:shortint;
  4002. begin
  4003. alignment:=trecordsymtable(symtable).recordalignment;
  4004. end;
  4005. function trecorddef.padalignment:shortint;
  4006. begin
  4007. padalignment := trecordsymtable(symtable).padalignment;
  4008. end;
  4009. function trecorddef.GetTypeName : string;
  4010. begin
  4011. GetTypeName:='<record type>'
  4012. end;
  4013. {***************************************************************************
  4014. TABSTRACTPROCDEF
  4015. ***************************************************************************}
  4016. constructor tabstractprocdef.create(dt:tdeftyp;level:byte;doregister:boolean);
  4017. begin
  4018. inherited create(dt,doregister);
  4019. parast:=tparasymtable.create(self,level);
  4020. paras:=nil;
  4021. minparacount:=0;
  4022. maxparacount:=0;
  4023. proctypeoption:=potype_none;
  4024. proccalloption:=pocall_none;
  4025. procoptions:=[];
  4026. returndef:=voidtype;
  4027. savesize:=sizeof(pint);
  4028. callerargareasize:=0;
  4029. calleeargareasize:=0;
  4030. has_paraloc_info:=callnoside;
  4031. funcretloc[callerside].init;
  4032. funcretloc[calleeside].init;
  4033. check_mark_as_nested;
  4034. end;
  4035. destructor tabstractprocdef.destroy;
  4036. begin
  4037. if assigned(paras) then
  4038. begin
  4039. {$ifdef MEMDEBUG}
  4040. memprocpara.start;
  4041. {$endif MEMDEBUG}
  4042. paras.free;
  4043. paras:=nil;
  4044. {$ifdef MEMDEBUG}
  4045. memprocpara.stop;
  4046. {$endif MEMDEBUG}
  4047. end;
  4048. if assigned(parast) then
  4049. begin
  4050. {$ifdef MEMDEBUG}
  4051. memprocparast.start;
  4052. {$endif MEMDEBUG}
  4053. parast.free;
  4054. parast:=nil;
  4055. {$ifdef MEMDEBUG}
  4056. memprocparast.stop;
  4057. {$endif MEMDEBUG}
  4058. end;
  4059. funcretloc[callerside].done;
  4060. funcretloc[calleeside].done;
  4061. inherited destroy;
  4062. end;
  4063. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  4064. begin
  4065. if (tsym(p).typ<>paravarsym) then
  4066. exit;
  4067. inc(plongint(arg)^);
  4068. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  4069. begin
  4070. if not assigned(tparavarsym(p).defaultconstsym) then
  4071. inc(minparacount);
  4072. inc(maxparacount);
  4073. end;
  4074. end;
  4075. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  4076. begin
  4077. if (tsym(p).typ<>paravarsym) then
  4078. exit;
  4079. paras.add(p);
  4080. end;
  4081. procedure tabstractprocdef.calcparas;
  4082. var
  4083. paracount : longint;
  4084. begin
  4085. { This can already be assigned when
  4086. we need to reresolve this unit (PFV) }
  4087. if assigned(paras) then
  4088. paras.free;
  4089. paras:=tparalist.create(false);
  4090. paracount:=0;
  4091. minparacount:=0;
  4092. maxparacount:=0;
  4093. parast.SymList.ForEachCall(@count_para,@paracount);
  4094. paras.capacity:=paracount;
  4095. { Insert parameters in table }
  4096. parast.SymList.ForEachCall(@insert_para,nil);
  4097. { Order parameters }
  4098. paras.sortparas;
  4099. end;
  4100. function tabstractprocdef.mangledprocparanames(oldlen : longint) : string;
  4101. var
  4102. crc : dword;
  4103. hp : TParavarsym;
  4104. hs : TSymStr;
  4105. newlen,
  4106. i : integer;
  4107. begin
  4108. result:='';
  4109. hp:=nil;
  4110. { add parameter types }
  4111. for i:=0 to paras.count-1 do
  4112. begin
  4113. hp:=tparavarsym(paras[i]);
  4114. if not(vo_is_hidden_para in hp.varoptions) then
  4115. result:=result+'$'+hp.vardef.mangledparaname;
  4116. end;
  4117. { add resultdef, add $$ as separator to make it unique from a
  4118. parameter separator }
  4119. if not is_void(returndef) then
  4120. result:=result+'$$'+returndef.mangledparaname;
  4121. newlen:=length(result)+oldlen;
  4122. { Replace with CRC if the parameter line is very long }
  4123. if (newlen-oldlen>12) and
  4124. ((newlen>100) or (newlen-oldlen>64)) then
  4125. begin
  4126. crc:=0;
  4127. for i:=0 to paras.count-1 do
  4128. begin
  4129. hp:=tparavarsym(paras[i]);
  4130. if not(vo_is_hidden_para in hp.varoptions) then
  4131. begin
  4132. hs:=hp.vardef.mangledparaname;
  4133. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4134. end;
  4135. end;
  4136. if not is_void(returndef) then
  4137. begin
  4138. { add a little prefix so that x(integer; integer) is different from x(integer):integer }
  4139. hs:='$$'+returndef.mangledparaname;
  4140. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4141. end;
  4142. result:='$crc'+hexstr(crc,8);
  4143. end;
  4144. end;
  4145. procedure tabstractprocdef.buildderef;
  4146. begin
  4147. { released procdef? }
  4148. if not assigned(parast) then
  4149. exit;
  4150. inherited buildderef;
  4151. returndefderef.build(returndef);
  4152. if po_explicitparaloc in procoptions then
  4153. funcretloc[callerside].buildderef;
  4154. { parast }
  4155. tparasymtable(parast).buildderef;
  4156. end;
  4157. procedure tabstractprocdef.deref;
  4158. begin
  4159. inherited deref;
  4160. returndef:=tdef(returndefderef.resolve);
  4161. if po_explicitparaloc in procoptions then
  4162. begin
  4163. funcretloc[callerside].deref;
  4164. has_paraloc_info:=callerside;
  4165. end
  4166. else
  4167. begin
  4168. { deref is called after loading from a ppu, but also after another
  4169. unit has been reloaded/recompiled and all references must be
  4170. re-resolved. Since the funcretloc contains a reference to a tdef,
  4171. reset it so that we won't try to access the stale def }
  4172. funcretloc[callerside].init;
  4173. has_paraloc_info:=callnoside;
  4174. end;
  4175. { parast }
  4176. tparasymtable(parast).deref(false);
  4177. { recalculated parameters }
  4178. calcparas;
  4179. end;
  4180. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  4181. begin
  4182. inherited ppuload(dt,ppufile);
  4183. parast:=nil;
  4184. Paras:=nil;
  4185. minparacount:=0;
  4186. maxparacount:=0;
  4187. ppufile.getderef(returndefderef);
  4188. proctypeoption:=tproctypeoption(ppufile.getbyte);
  4189. proccalloption:=tproccalloption(ppufile.getbyte);
  4190. ppufile.getnormalset(procoptions);
  4191. funcretloc[callerside].init;
  4192. if po_explicitparaloc in procoptions then
  4193. funcretloc[callerside].ppuload(ppufile);
  4194. savesize:=sizeof(pint);
  4195. if (po_explicitparaloc in procoptions) then
  4196. has_paraloc_info:=callerside;
  4197. end;
  4198. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  4199. var
  4200. oldintfcrc : boolean;
  4201. begin
  4202. { released procdef? }
  4203. if not assigned(parast) then
  4204. exit;
  4205. inherited ppuwrite(ppufile);
  4206. ppufile.putderef(returndefderef);
  4207. oldintfcrc:=ppufile.do_interface_crc;
  4208. ppufile.do_interface_crc:=false;
  4209. ppufile.putbyte(ord(proctypeoption));
  4210. ppufile.putbyte(ord(proccalloption));
  4211. ppufile.putnormalset(procoptions);
  4212. ppufile.do_interface_crc:=oldintfcrc;
  4213. if (po_explicitparaloc in procoptions) then
  4214. funcretloc[callerside].ppuwrite(ppufile);
  4215. end;
  4216. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  4217. var
  4218. hs,s : ansistring;
  4219. hp : TParavarsym;
  4220. hpc : tconstsym;
  4221. first : boolean;
  4222. i : integer;
  4223. begin
  4224. s:='';
  4225. first:=true;
  4226. for i:=0 to paras.count-1 do
  4227. begin
  4228. hp:=tparavarsym(paras[i]);
  4229. if not(vo_is_hidden_para in hp.varoptions) or
  4230. (pno_showhidden in pno) then
  4231. begin
  4232. if first then
  4233. begin
  4234. s:=s+'(';
  4235. first:=false;
  4236. end
  4237. else
  4238. s:=s+';';
  4239. if vo_is_hidden_para in hp.varoptions then
  4240. s:=s+'<';
  4241. case hp.varspez of
  4242. vs_var :
  4243. s:=s+'var ';
  4244. vs_const :
  4245. s:=s+'const ';
  4246. vs_out :
  4247. s:=s+'out ';
  4248. vs_constref :
  4249. s:=s+'constref ';
  4250. end;
  4251. if (pno_paranames in pno) then
  4252. s:=s+hp.realname+':';
  4253. if hp.univpara then
  4254. s:=s+'univ ';
  4255. if assigned(hp.vardef.typesym) then
  4256. begin
  4257. hs:=hp.vardef.typesym.realname;
  4258. if hs[1]<>'$' then
  4259. s:=s+hp.vardef.OwnerHierarchyName+hs
  4260. else
  4261. s:=s+hp.vardef.GetTypeName;
  4262. end
  4263. else
  4264. s:=s+hp.vardef.GetTypeName;
  4265. { default value }
  4266. if assigned(hp.defaultconstsym) then
  4267. begin
  4268. hpc:=tconstsym(hp.defaultconstsym);
  4269. hs:='';
  4270. case hpc.consttyp of
  4271. conststring,
  4272. constresourcestring :
  4273. begin
  4274. If hpc.value.len>0 then
  4275. begin
  4276. setLength(hs,hpc.value.len);
  4277. { don't write past the end of hs if the constant
  4278. is > 255 chars }
  4279. move(hpc.value.valueptr^,hs[1],length(hs));
  4280. { make sure that constant strings with newline chars
  4281. don't create a linebreak in the assembler code,
  4282. since comments are line-based. Also remove nulls
  4283. because the comments are written as a pchar. }
  4284. ReplaceCase(hs,#0,'.');
  4285. ReplaceCase(hs,#10,'.');
  4286. ReplaceCase(hs,#13,'.');
  4287. end;
  4288. end;
  4289. constreal :
  4290. str(pbestreal(hpc.value.valueptr)^,hs);
  4291. constpointer :
  4292. hs:=tostr(hpc.value.valueordptr);
  4293. constord :
  4294. begin
  4295. if is_boolean(hpc.constdef) then
  4296. begin
  4297. if hpc.value.valueord<>0 then
  4298. hs:='TRUE'
  4299. else
  4300. hs:='FALSE';
  4301. end
  4302. else
  4303. hs:=tostr(hpc.value.valueord);
  4304. end;
  4305. constnil :
  4306. hs:='nil';
  4307. constset :
  4308. hs:='<set>';
  4309. end;
  4310. if hs<>'' then
  4311. s:=s+'="'+hs+'"';
  4312. end;
  4313. if vo_is_hidden_para in hp.varoptions then
  4314. s:=s+'>';
  4315. end;
  4316. end;
  4317. if not first then
  4318. s:=s+')';
  4319. if (po_varargs in procoptions) then
  4320. s:=s+';VarArgs';
  4321. typename_paras:=s;
  4322. end;
  4323. function tabstractprocdef.is_methodpointer:boolean;
  4324. begin
  4325. result:=false;
  4326. end;
  4327. function tabstractprocdef.is_addressonly:boolean;
  4328. begin
  4329. result:=true;
  4330. end;
  4331. function tabstractprocdef.no_self_node: boolean;
  4332. begin
  4333. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  4334. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  4335. end;
  4336. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef;
  4337. var
  4338. j, nestinglevel: longint;
  4339. pvs, npvs: tparavarsym;
  4340. begin
  4341. nestinglevel:=parast.symtablelevel;
  4342. if newtyp=procdef then
  4343. begin
  4344. if (copytyp<>pc_bareproc) then
  4345. result:=cprocdef.create(nestinglevel,true)
  4346. else
  4347. result:=cprocdef.create(normal_function_level,true);
  4348. tprocdef(result).visibility:=vis_public;
  4349. end
  4350. else
  4351. begin
  4352. result:=cprocvardef.create(nestinglevel);
  4353. end;
  4354. tabstractprocdef(result).returndef:=returndef;
  4355. tabstractprocdef(result).returndefderef:=returndefderef;
  4356. pvs:=nil;
  4357. npvs:=nil;
  4358. for j:=0 to parast.symlist.count-1 do
  4359. begin
  4360. case tsym(parast.symlist[j]).typ of
  4361. paravarsym:
  4362. begin
  4363. pvs:=tparavarsym(parast.symlist[j]);
  4364. { in case of bare proc, don't copy self, vmt or framepointer
  4365. parameters }
  4366. if (copytyp=pc_bareproc) and
  4367. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result,vo_is_funcret]*pvs.varoptions)<>[]) then
  4368. continue;
  4369. npvs:=cparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  4370. pvs.vardef,pvs.varoptions);
  4371. npvs.defaultconstsym:=pvs.defaultconstsym;
  4372. tabstractprocdef(result).parast.insert(npvs);
  4373. end;
  4374. constsym:
  4375. begin
  4376. // ignore, reuse original constym. Should also be duplicated
  4377. // be safe though
  4378. end;
  4379. symconst.typesym:
  4380. begin
  4381. // reuse original, part of generic declaration
  4382. end
  4383. else
  4384. internalerror(201160604);
  4385. end;
  4386. end;
  4387. tabstractprocdef(result).savesize:=savesize;
  4388. if (typ<>procvardef) and
  4389. (newtyp=procvardef) then
  4390. begin
  4391. { procvars can't be (class)constructures/destructors etc }
  4392. if proctypeoption=potype_constructor then
  4393. begin
  4394. tabstractprocdef(result).returndef:=tdef(owner.defowner);
  4395. if not(is_implicit_pointer_object_type(returndef) or
  4396. (returndef.typ<>objectdef)) then
  4397. tabstractprocdef(result).returndef:=cpointerdef.getreusable(tabstractprocdef(result).returndef);
  4398. tabstractprocdef(result).proctypeoption:=potype_function;
  4399. end
  4400. else if is_void(returndef) then
  4401. tabstractprocdef(result).proctypeoption:=potype_procedure
  4402. else
  4403. tabstractprocdef(result).proctypeoption:=potype_function;
  4404. end
  4405. else
  4406. tabstractprocdef(result).proctypeoption:=proctypeoption;
  4407. tabstractprocdef(result).proccalloption:=proccalloption;
  4408. tabstractprocdef(result).procoptions:=procoptions;
  4409. if (copytyp=pc_bareproc) then
  4410. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  4411. if newtyp=procvardef then
  4412. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  4413. if copytyp=pc_address_only then
  4414. include(tabstractprocdef(result).procoptions,po_addressonly);
  4415. tabstractprocdef(result).callerargareasize:=callerargareasize;
  4416. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  4417. tabstractprocdef(result).maxparacount:=maxparacount;
  4418. tabstractprocdef(result).minparacount:=minparacount;
  4419. if po_explicitparaloc in procoptions then
  4420. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  4421. { recalculate parameter info }
  4422. tabstractprocdef(result).has_paraloc_info:=callnoside;
  4423. {$ifdef m68k}
  4424. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  4425. {$endif}
  4426. if (typ=procdef) and
  4427. (newtyp=procvardef) and
  4428. (owner.symtabletype=ObjectSymtable) then
  4429. include(tprocvardef(result).procoptions,po_methodpointer);
  4430. end;
  4431. function tabstractprocdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  4432. begin
  4433. result:=is_addressonly;
  4434. end;
  4435. procedure tabstractprocdef.check_mark_as_nested;
  4436. begin
  4437. { nested procvars require that nested functions use the Delphi-style
  4438. nested procedure calling convention }
  4439. if (parast.symtablelevel>normal_function_level) and
  4440. (m_nested_procvars in current_settings.modeswitches) then
  4441. include(procoptions,po_delphi_nested_cc);
  4442. end;
  4443. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  4444. begin
  4445. if (side in [callerside,callbothsides]) and
  4446. not(has_paraloc_info in [callerside,callbothsides]) then
  4447. begin
  4448. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  4449. if has_paraloc_info in [calleeside,callbothsides] then
  4450. has_paraloc_info:=callbothsides
  4451. else
  4452. has_paraloc_info:=callerside;
  4453. end;
  4454. if (side in [calleeside,callbothsides]) and
  4455. not(has_paraloc_info in [calleeside,callbothsides]) then
  4456. begin
  4457. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  4458. if has_paraloc_info in [callerside,callbothsides] then
  4459. has_paraloc_info:=callbothsides
  4460. else
  4461. has_paraloc_info:=calleeside;
  4462. end;
  4463. end;
  4464. procedure tabstractprocdef.done_paraloc_info(side: tcallercallee);
  4465. var
  4466. i: longint;
  4467. begin
  4468. if (side in [callerside,callbothsides]) and
  4469. (has_paraloc_info in [callerside,callbothsides]) then
  4470. begin
  4471. funcretloc[callerside].done;
  4472. for i:=0 to paras.count-1 do
  4473. tparavarsym(paras[i]).paraloc[callerside].done;
  4474. if has_paraloc_info=callerside then
  4475. has_paraloc_info:=callnoside
  4476. else
  4477. has_paraloc_info:=calleeside;
  4478. end;
  4479. if (side in [calleeside,callbothsides]) and
  4480. (has_paraloc_info in [calleeside,callbothsides]) then
  4481. begin
  4482. funcretloc[calleeside].done;
  4483. for i:=0 to paras.count-1 do
  4484. tparavarsym(paras[i]).paraloc[calleeside].done;
  4485. if has_paraloc_info=calleeside then
  4486. has_paraloc_info:=callnoside
  4487. else
  4488. has_paraloc_info:=callerside;
  4489. end;
  4490. end;
  4491. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  4492. var
  4493. p: tparavarsym;
  4494. ploc: PCGParalocation;
  4495. i: longint;
  4496. begin
  4497. result:=false;
  4498. init_paraloc_info(side);
  4499. for i:=0 to parast.SymList.Count-1 do
  4500. if tsym(parast.SymList[i]).typ=paravarsym then
  4501. begin
  4502. p:=tparavarsym(parast.SymList[i]);
  4503. { check if no parameter is located on the stack }
  4504. if is_open_array(p.vardef) or
  4505. is_array_of_const(p.vardef) then
  4506. begin
  4507. result:=true;
  4508. exit;
  4509. end;
  4510. ploc:=p.paraloc[side].location;
  4511. while assigned(ploc) do
  4512. begin
  4513. if (ploc^.loc=LOC_REFERENCE) then
  4514. begin
  4515. result:=true;
  4516. exit
  4517. end;
  4518. ploc:=ploc^.next;
  4519. end;
  4520. end;
  4521. end;
  4522. function tabstractprocdef.is_pushleftright: boolean;
  4523. begin
  4524. result:=false;
  4525. end;
  4526. function tabstractprocdef.address_type: tdef;
  4527. begin
  4528. result:=voidcodepointertype;
  4529. end;
  4530. procedure tabstractprocdef.declared_far;
  4531. begin
  4532. Message1(parser_w_proc_directive_ignored,'FAR');
  4533. end;
  4534. procedure tabstractprocdef.declared_near;
  4535. begin
  4536. Message1(parser_w_proc_directive_ignored,'NEAR');
  4537. end;
  4538. {***************************************************************************
  4539. TPROCDEF
  4540. ***************************************************************************}
  4541. function tprocdef.GetResultName: PShortString;
  4542. begin
  4543. if not assigned(implprocdefinfo) then
  4544. internalerror(2014010301);
  4545. result:=implprocdefinfo^.resultname;
  4546. end;
  4547. procedure tprocdef.SetResultName(AValue: PShortString);
  4548. begin
  4549. if not assigned(implprocdefinfo) then
  4550. internalerror(2014010302);
  4551. implprocdefinfo^.resultname:=AValue;
  4552. end;
  4553. function tprocdef.GetParentFPInitBlock: tnode;
  4554. begin
  4555. if not assigned(implprocdefinfo) then
  4556. internalerror(2014010303);
  4557. result:=implprocdefinfo^.parentfpinitblock;
  4558. end;
  4559. function tprocdef.GetParentFPStruct: tsym;
  4560. begin
  4561. if not assigned(implprocdefinfo) then
  4562. internalerror(2014010304);
  4563. result:=implprocdefinfo^.parentfpstruct;
  4564. end;
  4565. function tprocdef.GetParentFPStructPtrType: tdef;
  4566. begin
  4567. if not assigned(implprocdefinfo) then
  4568. internalerror(2014010305);
  4569. result:=implprocdefinfo^.parentfpstructptrtype;
  4570. end;
  4571. procedure tprocdef.SetParentFPInitBlock(AValue: tnode);
  4572. begin
  4573. if not assigned(implprocdefinfo) then
  4574. internalerror(2014010306);
  4575. implprocdefinfo^.parentfpinitblock:=AValue;
  4576. end;
  4577. function tprocdef.Getprocendtai: tai;
  4578. begin
  4579. if not assigned(implprocdefinfo) then
  4580. internalerror(2014010307);
  4581. result:=implprocdefinfo^.procendtai;
  4582. end;
  4583. function tprocdef.Getprocstarttai: tai;
  4584. begin
  4585. if not assigned(implprocdefinfo) then
  4586. internalerror(2014010308);
  4587. result:=implprocdefinfo^.procstarttai;
  4588. end;
  4589. procedure tprocdef.Setprocendtai(AValue: tai);
  4590. begin
  4591. if not assigned(implprocdefinfo) then
  4592. internalerror(2014010309);
  4593. implprocdefinfo^.procendtai:=AValue;
  4594. end;
  4595. function tprocdef.Getskpara: pointer;
  4596. begin
  4597. if not assigned(implprocdefinfo) then
  4598. internalerror(2014010310);
  4599. result:=implprocdefinfo^.skpara;
  4600. end;
  4601. procedure tprocdef.Setskpara(AValue: pointer);
  4602. begin
  4603. if not assigned(implprocdefinfo) then
  4604. internalerror(2014010311);
  4605. implprocdefinfo^.skpara:=AValue;
  4606. end;
  4607. function tprocdef.Getforwarddef: boolean;
  4608. begin
  4609. if not assigned(implprocdefinfo) then
  4610. internalerror(2014010312);
  4611. result:=implprocdefinfo^.forwarddef;
  4612. end;
  4613. function tprocdef.Gethasforward: boolean;
  4614. begin
  4615. if not assigned(implprocdefinfo) then
  4616. internalerror(2014010313);
  4617. result:=implprocdefinfo^.hasforward;
  4618. end;
  4619. function tprocdef.Getinterfacedef: boolean;
  4620. begin
  4621. if not assigned(implprocdefinfo) then
  4622. internalerror(2014010314);
  4623. result:=implprocdefinfo^.interfacedef;
  4624. end;
  4625. procedure tprocdef.Setforwarddef(AValue: boolean);
  4626. begin
  4627. if not assigned(implprocdefinfo) then
  4628. internalerror(2014010315);
  4629. implprocdefinfo^.forwarddef:=AValue;
  4630. end;
  4631. procedure tprocdef.Sethasforward(AValue: boolean);
  4632. begin
  4633. if not assigned(implprocdefinfo) then
  4634. internalerror(2014010316);
  4635. implprocdefinfo^.hasforward:=AValue;
  4636. end;
  4637. function tprocdef.GetIsEmpty: boolean;
  4638. begin
  4639. result:=pio_empty in implprocoptions;
  4640. end;
  4641. procedure tprocdef.SetIsEmpty(AValue: boolean);
  4642. begin
  4643. if AValue then
  4644. include(implprocoptions,pio_empty)
  4645. else
  4646. include(implprocoptions,pio_empty);
  4647. end;
  4648. function tprocdef.GetHasInliningInfo: boolean;
  4649. begin
  4650. result:=pio_has_inlininginfo in implprocoptions;
  4651. end;
  4652. procedure tprocdef.SetHasInliningInfo(AValue: boolean);
  4653. begin
  4654. if AValue then
  4655. include(implprocoptions,pio_has_inlininginfo)
  4656. else
  4657. exclude(implprocoptions,pio_has_inlininginfo);
  4658. end;
  4659. procedure tprocdef.Setinterfacedef(AValue: boolean);
  4660. begin
  4661. if not assigned(implprocdefinfo) then
  4662. internalerror(2014010317);
  4663. implprocdefinfo^.interfacedef:=AValue;
  4664. end;
  4665. procedure tprocdef.Setprocstarttai(AValue: tai);
  4666. begin
  4667. if not assigned(implprocdefinfo) then
  4668. internalerror(2014010318);
  4669. implprocdefinfo^.procstarttai:=AValue;
  4670. end;
  4671. procedure tprocdef.SetParentFPStruct(AValue: tsym);
  4672. begin
  4673. if not assigned(implprocdefinfo) then
  4674. internalerror(2014010319);
  4675. implprocdefinfo^.parentfpstruct:=AValue;
  4676. end;
  4677. procedure tprocdef.SetParentFPStructPtrType(AValue: tdef);
  4678. begin
  4679. if not assigned(implprocdefinfo) then
  4680. internalerror(2014010320);
  4681. implprocdefinfo^.parentfpstructptrtype:=AValue;
  4682. end;
  4683. constructor tprocdef.create(level:byte;doregister:boolean);
  4684. begin
  4685. inherited create(procdef,level,doregister);
  4686. implprocdefinfo:=allocmem(sizeof(implprocdefinfo^));
  4687. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  4688. {$ifdef symansistr}
  4689. _mangledname:='';
  4690. {$else symansistr}
  4691. _mangledname:=nil;
  4692. {$endif symansistr}
  4693. fileinfo:=current_filepos;
  4694. extnumber:=$ffff;
  4695. aliasnames:=TCmdStrList.create;
  4696. funcretsym:=nil;
  4697. forwarddef:=true;
  4698. interfacedef:=false;
  4699. hasforward:=false;
  4700. struct := nil;
  4701. import_dll:=nil;
  4702. import_name:=nil;
  4703. import_nr:=0;
  4704. inlininginfo:=nil;
  4705. deprecatedmsg:=nil;
  4706. end;
  4707. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  4708. var
  4709. i,aliasnamescount,sizeleft : longint;
  4710. level : byte;
  4711. buf : array[0..255] of byte;
  4712. begin
  4713. inherited ppuload(procdef,ppufile);
  4714. {$ifdef symansistr}
  4715. if po_has_mangledname in procoptions then
  4716. _mangledname:=ppufile.getansistring
  4717. else
  4718. _mangledname:='';
  4719. {$else symansistr}
  4720. if po_has_mangledname in procoptions then
  4721. _mangledname:=stringdup(ppufile.getstring)
  4722. else
  4723. _mangledname:=nil;
  4724. {$endif symansistr}
  4725. extnumber:=ppufile.getword;
  4726. level:=ppufile.getbyte;
  4727. ppufile.getderef(structderef);
  4728. ppufile.getderef(procsymderef);
  4729. ppufile.getposinfo(fileinfo);
  4730. visibility:=tvisibility(ppufile.getbyte);
  4731. ppufile.getsmallset(symoptions);
  4732. if sp_has_deprecated_msg in symoptions then
  4733. deprecatedmsg:=stringdup(ppufile.getstring)
  4734. else
  4735. deprecatedmsg:=nil;
  4736. synthetickind:=tsynthetickind(ppufile.getbyte);
  4737. { import stuff }
  4738. if po_has_importdll in procoptions then
  4739. import_dll:=stringdup(ppufile.getstring)
  4740. else
  4741. import_dll:=nil;
  4742. if po_has_importname in procoptions then
  4743. import_name:=stringdup(ppufile.getstring)
  4744. else
  4745. import_name:=nil;
  4746. import_nr:=ppufile.getword;
  4747. if (po_msgint in procoptions) then
  4748. messageinf.i:=ppufile.getlongint;
  4749. if (po_msgstr in procoptions) then
  4750. messageinf.str:=stringdup(ppufile.getstring);
  4751. if (po_dispid in procoptions) then
  4752. dispid:=ppufile.getlongint;
  4753. { inline stuff }
  4754. ppufile.getsmallset(implprocoptions);
  4755. if has_inlininginfo then
  4756. begin
  4757. ppufile.getderef(funcretsymderef);
  4758. new(inlininginfo);
  4759. ppufile.getsmallset(inlininginfo^.flags);
  4760. end
  4761. else
  4762. begin
  4763. inlininginfo:=nil;
  4764. funcretsym:=nil;
  4765. end;
  4766. aliasnames:=TCmdStrList.create;
  4767. { count alias names }
  4768. aliasnamescount:=ppufile.getbyte;
  4769. for i:=1 to aliasnamescount do
  4770. aliasnames.insert(ppufile.getstring);
  4771. { load the token stream containing the declaration }
  4772. sizeleft:=ppufile.getlongint;
  4773. if sizeleft>0 then
  4774. begin
  4775. init_genericdecl;
  4776. while sizeleft>0 do
  4777. begin
  4778. if sizeleft>sizeof(buf) then
  4779. i:=sizeof(buf)
  4780. else
  4781. i:=sizeleft;
  4782. ppufile.getdata(buf,i);
  4783. genericdecltokenbuf.write(buf,i);
  4784. dec(sizeleft,i);
  4785. end;
  4786. end;
  4787. ppuload_platform(ppufile);
  4788. { load para symtable }
  4789. parast:=tparasymtable.create(self,level);
  4790. tparasymtable(parast).ppuload(ppufile);
  4791. { load local symtable }
  4792. if has_inlininginfo then
  4793. begin
  4794. localst:=tlocalsymtable.create(self,level);
  4795. tlocalsymtable(localst).ppuload(ppufile);
  4796. end
  4797. else
  4798. localst:=nil;
  4799. { inline stuff }
  4800. if has_inlininginfo then
  4801. inlininginfo^.code:=ppuloadnodetree(ppufile);
  4802. { default values for no persistent data }
  4803. if (cs_link_deffile in current_settings.globalswitches) and
  4804. (tf_need_export in target_info.flags) and
  4805. (po_exports in procoptions) then
  4806. deffile.AddExport(mangledname);
  4807. { Disable po_has_inlining until the derefimpl is done }
  4808. has_inlininginfo:=false;
  4809. end;
  4810. destructor tprocdef.destroy;
  4811. begin
  4812. aliasnames.free;
  4813. aliasnames:=nil;
  4814. if assigned(localst) and
  4815. (localst.symtabletype<>staticsymtable) then
  4816. begin
  4817. {$ifdef MEMDEBUG}
  4818. memproclocalst.start;
  4819. {$endif MEMDEBUG}
  4820. localst.free;
  4821. localst:=nil;
  4822. {$ifdef MEMDEBUG}
  4823. memproclocalst.start;
  4824. {$endif MEMDEBUG}
  4825. end;
  4826. if assigned(inlininginfo) then
  4827. begin
  4828. {$ifdef MEMDEBUG}
  4829. memprocnodetree.start;
  4830. {$endif MEMDEBUG}
  4831. tnode(inlininginfo^.code).free;
  4832. {$ifdef MEMDEBUG}
  4833. memprocnodetree.start;
  4834. {$endif MEMDEBUG}
  4835. dispose(inlininginfo);
  4836. inlininginfo:=nil;
  4837. end;
  4838. freeimplprocdefinfo;
  4839. genericdecltokenbuf.free;
  4840. genericdecltokenbuf:=nil;
  4841. stringdispose(import_dll);
  4842. stringdispose(import_name);
  4843. stringdispose(deprecatedmsg);
  4844. if (po_msgstr in procoptions) then
  4845. stringdispose(messageinf.str);
  4846. {$ifndef symansistr}
  4847. if assigned(_mangledname) then
  4848. begin
  4849. {$ifdef MEMDEBUG}
  4850. memmanglednames.start;
  4851. {$endif MEMDEBUG}
  4852. stringdispose(_mangledname);
  4853. {$ifdef MEMDEBUG}
  4854. memmanglednames.stop;
  4855. {$endif MEMDEBUG}
  4856. end;
  4857. {$endif symansistr}
  4858. inherited destroy;
  4859. end;
  4860. procedure tprocdef.freeimplprocdefinfo;
  4861. begin
  4862. if assigned(implprocdefinfo) then
  4863. begin
  4864. stringdispose(implprocdefinfo^.resultname);
  4865. freemem(implprocdefinfo);
  4866. implprocdefinfo:=nil;
  4867. end;
  4868. end;
  4869. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  4870. var
  4871. oldintfcrc : boolean;
  4872. aliasnamescount,i,sizeleft : longint;
  4873. item : TCmdStrListItem;
  4874. buf : array[0..255] of byte;
  4875. begin
  4876. { released procdef? }
  4877. if not assigned(parast) then
  4878. exit;
  4879. inherited ppuwrite(ppufile);
  4880. {$ifdef symansistr}
  4881. if po_has_mangledname in procoptions then
  4882. ppufile.putansistring(_mangledname);
  4883. {$else symansistr}
  4884. if po_has_mangledname in procoptions then
  4885. ppufile.putstring(_mangledname^);
  4886. {$endif symansistr}
  4887. ppufile.putword(extnumber);
  4888. ppufile.putbyte(parast.symtablelevel);
  4889. ppufile.putderef(structderef);
  4890. ppufile.putderef(procsymderef);
  4891. ppufile.putposinfo(fileinfo);
  4892. ppufile.putbyte(byte(visibility));
  4893. ppufile.putsmallset(symoptions);
  4894. if sp_has_deprecated_msg in symoptions then
  4895. ppufile.putstring(deprecatedmsg^);
  4896. ppufile.putbyte(byte(synthetickind));
  4897. { import }
  4898. if po_has_importdll in procoptions then
  4899. ppufile.putstring(import_dll^);
  4900. if po_has_importname in procoptions then
  4901. ppufile.putstring(import_name^);
  4902. ppufile.putword(import_nr);
  4903. if (po_msgint in procoptions) then
  4904. ppufile.putlongint(messageinf.i);
  4905. if (po_msgstr in procoptions) then
  4906. ppufile.putstring(messageinf.str^);
  4907. if (po_dispid in procoptions) then
  4908. ppufile.putlongint(dispid);
  4909. { inline stuff }
  4910. oldintfcrc:=ppufile.do_crc;
  4911. ppufile.do_crc:=false;
  4912. ppufile.putsmallset(implprocoptions);
  4913. if has_inlininginfo then
  4914. begin
  4915. ppufile.putderef(funcretsymderef);
  4916. ppufile.putsmallset(inlininginfo^.flags);
  4917. end;
  4918. { count alias names }
  4919. aliasnamescount:=0;
  4920. item:=TCmdStrListItem(aliasnames.first);
  4921. while assigned(item) do
  4922. begin
  4923. inc(aliasnamescount);
  4924. item:=TCmdStrListItem(item.next);
  4925. end;
  4926. if aliasnamescount>255 then
  4927. internalerror(200711021);
  4928. ppufile.putbyte(aliasnamescount);
  4929. item:=TCmdStrListItem(aliasnames.first);
  4930. while assigned(item) do
  4931. begin
  4932. ppufile.putstring(item.str);
  4933. item:=TCmdStrListItem(item.next);
  4934. end;
  4935. ppufile.do_crc:=oldintfcrc;
  4936. { generic tokens for the declaration }
  4937. if assigned(genericdecltokenbuf) and (genericdecltokenbuf.size>0) then
  4938. begin
  4939. sizeleft:=genericdecltokenbuf.size;
  4940. genericdecltokenbuf.seek(0);
  4941. ppufile.putlongint(sizeleft);
  4942. while sizeleft>0 do
  4943. begin
  4944. if sizeleft>sizeof(buf) then
  4945. i:=sizeof(buf)
  4946. else
  4947. i:=sizeleft;
  4948. genericdecltokenbuf.read(buf,i);
  4949. ppufile.putdata(buf,i);
  4950. dec(sizeleft,i);
  4951. end;
  4952. end
  4953. else
  4954. ppufile.putlongint(0);
  4955. { write this entry }
  4956. writeentry(ppufile,ibprocdef);
  4957. { Save the para symtable, this is taken from the interface }
  4958. tparasymtable(parast).ppuwrite(ppufile);
  4959. { save localsymtable for inline procedures or when local
  4960. browser info is requested, this has no influence on the crc }
  4961. if has_inlininginfo then
  4962. begin
  4963. oldintfcrc:=ppufile.do_crc;
  4964. ppufile.do_crc:=false;
  4965. tlocalsymtable(localst).ppuwrite(ppufile);
  4966. ppufile.do_crc:=oldintfcrc;
  4967. end;
  4968. { node tree for inlining }
  4969. oldintfcrc:=ppufile.do_crc;
  4970. ppufile.do_crc:=false;
  4971. if has_inlininginfo then
  4972. ppuwritenodetree(ppufile,inlininginfo^.code);
  4973. ppufile.do_crc:=oldintfcrc;
  4974. end;
  4975. function tprocdef.fullprocname(showhidden:boolean):string;
  4976. var
  4977. pno: tprocnameoptions;
  4978. begin
  4979. pno:=[];
  4980. if showhidden then
  4981. include(pno,pno_showhidden);
  4982. result:=customprocname(pno);
  4983. end;
  4984. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  4985. var
  4986. s, rn : ansistring;
  4987. t : ttoken;
  4988. begin
  4989. {$ifdef EXTDEBUG}
  4990. include(pno,pno_showhidden);
  4991. {$endif EXTDEBUG}
  4992. s:='';
  4993. if proctypeoption=potype_operator then
  4994. begin
  4995. for t:=NOTOKEN to last_overloaded do
  4996. if procsym.realname='$'+overloaded_names[t] then
  4997. begin
  4998. s:='operator ';
  4999. if (pno_ownername in pno) and
  5000. assigned(struct) then
  5001. s:=s+struct.RttiName+'.';
  5002. s:=s+arraytokeninfo[t].str+typename_paras(pno);
  5003. break;
  5004. end;
  5005. end
  5006. else
  5007. begin
  5008. if (po_classmethod in procoptions) and
  5009. not(pno_noclassmarker in pno) then
  5010. s:='class ';
  5011. case proctypeoption of
  5012. potype_constructor,
  5013. potype_class_constructor:
  5014. s:=s+'constructor ';
  5015. potype_class_destructor,
  5016. potype_destructor:
  5017. s:=s+'destructor ';
  5018. else
  5019. if (pno_proctypeoption in pno) then
  5020. begin
  5021. if assigned(returndef) and
  5022. not(is_void(returndef)) then
  5023. s:=s+'function '
  5024. else
  5025. s:=s+'procedure ';
  5026. end;
  5027. end;
  5028. if (pno_ownername in pno) and
  5029. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  5030. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  5031. rn:=procsym.realname;
  5032. if (pno_noleadingdollar in pno) and
  5033. (rn[1]='$') then
  5034. delete(rn,1,1);
  5035. s:=s+rn+typename_paras(pno);
  5036. end;
  5037. if not(proctypeoption in [potype_constructor,potype_destructor,
  5038. potype_class_constructor,potype_class_destructor]) and
  5039. assigned(returndef) and
  5040. not(is_void(returndef)) then
  5041. s:=s+':'+returndef.GetTypeName;
  5042. if owner.symtabletype=localsymtable then
  5043. s:=s+' is nested'
  5044. else if po_is_block in procoptions then
  5045. s:=s+' is block';
  5046. s:=s+';';
  5047. { forced calling convention? }
  5048. if (po_hascallingconvention in procoptions) then
  5049. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  5050. if (po_staticmethod in procoptions) and
  5051. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  5052. s:=s+' Static;';
  5053. if pno_mangledname in pno then
  5054. s:=s+' -- mangled name: '+mangledname;
  5055. customprocname:=s;
  5056. end;
  5057. function tprocdef.is_methodpointer:boolean;
  5058. begin
  5059. { don't check assigned(_class), that's also the case for nested
  5060. procedures inside methods }
  5061. result:=(owner.symtabletype=ObjectSymtable)and not no_self_node;
  5062. end;
  5063. function tprocdef.is_addressonly:boolean;
  5064. begin
  5065. result:=assigned(owner) and
  5066. not is_methodpointer and
  5067. (not(m_nested_procvars in current_settings.modeswitches) or
  5068. not is_nested_pd(self));
  5069. end;
  5070. procedure tprocdef.make_external;
  5071. begin
  5072. include(procoptions,po_external);
  5073. forwarddef:=false;
  5074. end;
  5075. procedure tprocdef.init_genericdecl;
  5076. begin
  5077. if assigned(genericdecltokenbuf) then
  5078. internalerror(2015061901);
  5079. genericdecltokenbuf:=tdynamicarray.create(256);
  5080. end;
  5081. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  5082. begin
  5083. case t of
  5084. gs_local :
  5085. GetSymtable:=localst;
  5086. gs_para :
  5087. GetSymtable:=parast;
  5088. else
  5089. GetSymtable:=nil;
  5090. end;
  5091. end;
  5092. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef;
  5093. var
  5094. j : longint;
  5095. begin
  5096. result:=inherited getcopyas(newtyp,copytyp);
  5097. if newtyp=procvardef then
  5098. begin
  5099. { create new paralist }
  5100. tprocvardef(result).calcparas;
  5101. exit;
  5102. end;
  5103. { don't copy mangled name, can be different }
  5104. tprocdef(result).messageinf:=messageinf;
  5105. tprocdef(result).dispid:=dispid;
  5106. if po_msgstr in procoptions then
  5107. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  5108. tprocdef(result).symoptions:=symoptions;
  5109. if assigned(deprecatedmsg) then
  5110. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  5111. { will have to be associated with appropriate procsym }
  5112. tprocdef(result).procsym:=nil;
  5113. { don't create aliases for bare copies, nor copy the funcretsym as
  5114. the function result parameter will be inserted again if necessary
  5115. (e.g. if the calling convention is changed) }
  5116. if copytyp<>pc_bareproc then
  5117. begin
  5118. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  5119. if assigned(funcretsym) then
  5120. begin
  5121. if funcretsym.owner=parast then
  5122. begin
  5123. j:=parast.symlist.indexof(funcretsym);
  5124. if j<0 then
  5125. internalerror(2011040606);
  5126. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  5127. end
  5128. else if funcretsym.owner=localst then
  5129. begin
  5130. { nothing to do, will be inserted for the new procdef while
  5131. parsing its body (by pdecsub.insert_funcret_local) }
  5132. end
  5133. else
  5134. internalerror(2011040605);
  5135. end;
  5136. end;
  5137. { will have to be associated with a new struct }
  5138. tprocdef(result).struct:=nil;
  5139. if assigned(implprocdefinfo) then
  5140. begin
  5141. if assigned(resultname) then
  5142. tprocdef(result).resultname:=stringdup(resultname^);
  5143. end;
  5144. if assigned(import_dll) then
  5145. tprocdef(result).import_dll:=stringdup(import_dll^);
  5146. if assigned(import_name) then
  5147. tprocdef(result).import_name:=stringdup(import_name^);
  5148. tprocdef(result).import_nr:=import_nr;
  5149. tprocdef(result).extnumber:=$ffff;
  5150. tprocdef(result).visibility:=visibility;
  5151. tprocdef(result).synthetickind:=synthetickind;
  5152. { we need a separate implementation for the copied def }
  5153. tprocdef(result).forwarddef:=true;
  5154. tprocdef(result).interfacedef:=true;
  5155. { create new paralist }
  5156. tprocdef(result).calcparas;
  5157. end;
  5158. function tprocdef.getcopy: tstoreddef;
  5159. begin
  5160. result:=getcopyas(procdef,pc_normal);
  5161. end;
  5162. procedure tprocdef.buildderef;
  5163. begin
  5164. inherited buildderef;
  5165. structderef.build(struct);
  5166. { procsym that originaly defined this definition, should be in the
  5167. same symtable }
  5168. procsymderef.build(procsym);
  5169. end;
  5170. procedure tprocdef.buildderefimpl;
  5171. begin
  5172. inherited buildderefimpl;
  5173. { inline tree }
  5174. if has_inlininginfo then
  5175. begin
  5176. { Localst is not available for main/unit init }
  5177. if assigned(localst) then
  5178. begin
  5179. tlocalsymtable(localst).buildderef;
  5180. tlocalsymtable(localst).buildderefimpl;
  5181. end;
  5182. funcretsymderef.build(funcretsym);
  5183. inlininginfo^.code.buildderefimpl;
  5184. end;
  5185. end;
  5186. procedure tprocdef.deref;
  5187. begin
  5188. inherited deref;
  5189. struct:=tabstractrecorddef(structderef.resolve);
  5190. { procsym that originaly defined this definition, should be in the
  5191. same symtable }
  5192. procsym:=tprocsym(procsymderef.resolve);
  5193. end;
  5194. procedure tprocdef.derefimpl;
  5195. begin
  5196. { Enable has_inlininginfo when the inlininginfo
  5197. structure is available. The has_inlininginfo was disabled
  5198. after the load, since the data was invalid }
  5199. if assigned(inlininginfo) then
  5200. has_inlininginfo:=true;
  5201. { Inline }
  5202. if has_inlininginfo then
  5203. begin
  5204. { Locals }
  5205. if assigned(localst) then
  5206. begin
  5207. tlocalsymtable(localst).deref(false);
  5208. tlocalsymtable(localst).derefimpl(false);
  5209. end;
  5210. inlininginfo^.code.derefimpl;
  5211. { funcretsym, this is always located in the localst }
  5212. funcretsym:=tsym(funcretsymderef.resolve);
  5213. end
  5214. else
  5215. begin
  5216. { safety }
  5217. { Not safe! A unit may be reresolved after its interface has been
  5218. parsed but before its implementation has been parsed, and in that
  5219. case the funcretsym is still required!
  5220. funcretsym:=nil; }
  5221. end;
  5222. end;
  5223. function tprocdef.GetTypeName : string;
  5224. begin
  5225. GetTypeName := FullProcName(false);
  5226. end;
  5227. function tprocdef.mangledname : TSymStr;
  5228. begin
  5229. {$ifdef symansistr}
  5230. if _mangledname='' then
  5231. begin
  5232. result:=defaultmangledname;
  5233. _mangledname:=result;
  5234. end
  5235. else
  5236. result:=_mangledname;
  5237. {$else symansistr}
  5238. if not assigned(_mangledname) then
  5239. begin
  5240. result:=defaultmangledname;
  5241. _mangledname:=stringdup(mangledname);
  5242. end
  5243. else
  5244. result:=_mangledname^;
  5245. {$endif symansistr}
  5246. end;
  5247. function tprocdef.defaultmangledname: TSymStr;
  5248. begin
  5249. { we need to use the symtable where the procsym is inserted,
  5250. because that is visible to the world }
  5251. defaultmangledname:=make_mangledname('',procsym.owner,procsym.name);
  5252. defaultmangledname:=defaultmangledname+mangledprocparanames(Length(defaultmangledname))
  5253. end;
  5254. function tprocdef.cplusplusmangledname : TSymStr;
  5255. function getcppparaname(p : tdef) : TSymStr;
  5256. const
  5257. {$ifdef NAMEMANGLING_GCC2}
  5258. ordtype2str : array[tordtype] of string[2] = (
  5259. '',
  5260. 'Uc','Us','Ui','Us','',
  5261. 'Sc','s','i','x','',
  5262. 'b','b','b','b','b',
  5263. 'c','w','x');
  5264. {$else NAMEMANGLING_GCC2}
  5265. ordtype2str : array[tordtype] of string[1] = (
  5266. 'v',
  5267. 'h','t','j','y','',
  5268. 'a','s','i','x','',
  5269. 'b','b','b','b',
  5270. 'b','b','b','b',
  5271. 'c','w','x');
  5272. floattype2str : array[tfloattype] of string[1] = (
  5273. 'f','d','e','e',
  5274. 'd','d','g');
  5275. {$endif NAMEMANGLING_GCC2}
  5276. var
  5277. s : TSymStr;
  5278. begin
  5279. case p.typ of
  5280. orddef:
  5281. s:=ordtype2str[torddef(p).ordtype];
  5282. pointerdef:
  5283. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  5284. {$ifndef NAMEMANGLING_GCC2}
  5285. floatdef:
  5286. s:=floattype2str[tfloatdef(p).floattype];
  5287. {$endif NAMEMANGLING_GCC2}
  5288. else
  5289. internalerror(2103001);
  5290. end;
  5291. getcppparaname:=s;
  5292. end;
  5293. var
  5294. s,s2 : TSymStr;
  5295. hp : TParavarsym;
  5296. i : integer;
  5297. begin
  5298. {$ifdef NAMEMANGLING_GCC2}
  5299. { outdated gcc 2.x name mangling scheme }
  5300. s := procsym.realname;
  5301. if procsym.owner.symtabletype=ObjectSymtable then
  5302. begin
  5303. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  5304. case proctypeoption of
  5305. potype_destructor:
  5306. s:='_$_'+tostr(length(s2))+s2;
  5307. potype_constructor:
  5308. s:='___'+tostr(length(s2))+s2;
  5309. else
  5310. s:='_'+s+'__'+tostr(length(s2))+s2;
  5311. end;
  5312. end
  5313. else s:=s+'__';
  5314. s:=s+'F';
  5315. { concat modifiers }
  5316. { !!!!! }
  5317. { now we handle the parameters }
  5318. if maxparacount>0 then
  5319. begin
  5320. for i:=0 to paras.count-1 do
  5321. begin
  5322. hp:=tparavarsym(paras[i]);
  5323. { no hidden parameters form part of a C++ mangled name:
  5324. a) self is not included
  5325. b) there are no "high" or other hidden parameters
  5326. }
  5327. if vo_is_hidden_para in hp.varoptions then
  5328. continue;
  5329. s2:=getcppparaname(hp.vardef);
  5330. if hp.varspez in [vs_var,vs_out] then
  5331. s2:='R'+s2;
  5332. s:=s+s2;
  5333. end;
  5334. end
  5335. else
  5336. s:=s+'v';
  5337. cplusplusmangledname:=s;
  5338. {$else NAMEMANGLING_GCC2}
  5339. { gcc 3.x and 4.x name mangling scheme }
  5340. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  5341. if procsym.owner.symtabletype=ObjectSymtable then
  5342. begin
  5343. s:='_ZN';
  5344. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  5345. s:=s+tostr(length(s2))+s2;
  5346. case proctypeoption of
  5347. potype_constructor:
  5348. s:=s+'C1';
  5349. potype_destructor:
  5350. s:=s+'D1';
  5351. else
  5352. s:=s+tostr(length(procsym.realname))+procsym.realname;
  5353. end;
  5354. s:=s+'E';
  5355. end
  5356. else
  5357. s:=procsym.realname;
  5358. { now we handle the parameters }
  5359. if maxparacount>0 then
  5360. begin
  5361. for i:=0 to paras.count-1 do
  5362. begin
  5363. hp:=tparavarsym(paras[i]);
  5364. { no hidden parameters form part of a C++ mangled name:
  5365. a) self is not included
  5366. b) there are no "high" or other hidden parameters
  5367. }
  5368. if vo_is_hidden_para in hp.varoptions then
  5369. continue;
  5370. s2:=getcppparaname(hp.vardef);
  5371. if hp.varspez in [vs_var,vs_out] then
  5372. s2:='R'+s2;
  5373. s:=s+s2;
  5374. end;
  5375. end
  5376. else
  5377. s:=s+'v';
  5378. cplusplusmangledname:=s;
  5379. {$endif NAMEMANGLING_GCC2}
  5380. end;
  5381. function tprocdef.objcmangledname : TSymStr;
  5382. var
  5383. manglednamelen: longint;
  5384. iscatmethod : boolean;
  5385. begin
  5386. if not (po_msgstr in procoptions) then
  5387. internalerror(2009030901);
  5388. { we may very well need longer strings to handle these... }
  5389. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  5390. length('+"[ ]"')+length(messageinf.str^);
  5391. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  5392. if (iscatmethod) then
  5393. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  5394. if manglednamelen>255 then
  5395. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  5396. if not(po_classmethod in procoptions) then
  5397. result:='"-['
  5398. else
  5399. result:='"+[';
  5400. { quotes are necessary because the +/- otherwise confuse the assembler
  5401. into expecting a number
  5402. }
  5403. if iscatmethod then
  5404. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  5405. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  5406. if iscatmethod then
  5407. result:=result+')';
  5408. result:=result+' '+messageinf.str^+']"';
  5409. end;
  5410. procedure tprocdef.setmangledname(const s : TSymStr);
  5411. begin
  5412. { This is not allowed anymore, the forward declaration
  5413. already needs to create the correct mangledname, no changes
  5414. afterwards are allowed (PFV) }
  5415. { Exception: interface definitions in mode macpas, since in that }
  5416. { case no reference to the old name can exist yet (JM) }
  5417. {$ifdef symansistr}
  5418. if _mangledname<>'' then
  5419. if ((m_mac in current_settings.modeswitches) and
  5420. (interfacedef)) then
  5421. _mangledname:=''
  5422. else
  5423. internalerror(200411171);
  5424. {$else symansistr}
  5425. if assigned(_mangledname) then
  5426. if ((m_mac in current_settings.modeswitches) and
  5427. (interfacedef)) then
  5428. stringdispose(_mangledname)
  5429. else
  5430. internalerror(200411171);
  5431. {$endif symansistr}
  5432. {$ifdef jvm}
  5433. { this routine can be called for compilerproces. can't set mangled
  5434. name since it must be calculated, but it uses import_name when set
  5435. -> set that one }
  5436. import_name:=stringdup(s);
  5437. include(procoptions,po_has_importname);
  5438. include(procoptions,po_has_mangledname);
  5439. {$else}
  5440. {$ifdef symansistr}
  5441. _mangledname:=s;
  5442. {$else symansistr}
  5443. _mangledname:=stringdup(s);
  5444. {$endif symansistr}
  5445. {$endif jvm}
  5446. include(procoptions,po_has_mangledname);
  5447. end;
  5448. {***************************************************************************
  5449. TPROCVARDEF
  5450. ***************************************************************************}
  5451. constructor tprocvardef.create(level:byte);
  5452. begin
  5453. inherited create(procvardef,level,true);
  5454. end;
  5455. class function tprocvardef.getreusableprocaddr(def: tabstractprocdef): tprocvardef;
  5456. var
  5457. res: PHashSetItem;
  5458. oldsymtablestack: tsymtablestack;
  5459. begin
  5460. if not assigned(current_module) then
  5461. internalerror(2011081301);
  5462. res:=current_module.procaddrdefs.FindOrAdd(@def,sizeof(def));
  5463. if not assigned(res^.Data) then
  5464. begin
  5465. { since these pointerdefs can be reused anywhere in the current
  5466. unit, add them to the global/staticsymtable (or local symtable
  5467. if they're a local def, because otherwise they'll be saved
  5468. to the ppu referencing a local symtable entry that doesn't
  5469. exist in the ppu) }
  5470. oldsymtablestack:=symtablestack;
  5471. { do not simply push/pop current_module.localsymtable, because
  5472. that can have side-effects (e.g., it removes helpers) }
  5473. symtablestack:=nil;
  5474. result:=tprocvardef(def.getcopyas(procvardef,pc_address_only));
  5475. setup_reusable_def(def,result,res,oldsymtablestack);
  5476. { res^.Data may still be nil -> don't overwrite result }
  5477. exit;
  5478. end;
  5479. result:=tprocvardef(res^.Data);
  5480. end;
  5481. class function tprocvardef.getreusableprocaddr_no_free(def: tabstractprocdef): tprocvardef;
  5482. begin
  5483. result:=getreusableprocaddr(def);
  5484. if not result.is_registered then
  5485. include(result.defoptions,df_not_registered_no_free);
  5486. end;
  5487. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  5488. begin
  5489. inherited ppuload(procvardef,ppufile);
  5490. { load para symtable }
  5491. parast:=tparasymtable.create(self,ppufile.getbyte);
  5492. ppuload_platform(ppufile);
  5493. tparasymtable(parast).ppuload(ppufile);
  5494. end;
  5495. function tprocvardef.getcopy : tstoreddef;
  5496. var
  5497. i : tcallercallee;
  5498. j : longint;
  5499. begin
  5500. result:=cprocvardef.create(parast.symtablelevel);
  5501. tprocvardef(result).returndef:=returndef;
  5502. tprocvardef(result).returndefderef:=returndefderef;
  5503. tprocvardef(result).parast:=parast.getcopy;
  5504. tprocvardef(result).savesize:=savesize;
  5505. { create paralist copy }
  5506. calcparas;
  5507. tprocvardef(result).paras:=tparalist.create(false);
  5508. tprocvardef(result).paras.count:=paras.count;
  5509. for j:=0 to paras.count-1 do
  5510. tprocvardef(result).paras[j]:=paras[j];
  5511. tprocvardef(result).proctypeoption:=proctypeoption;
  5512. tprocvardef(result).proccalloption:=proccalloption;
  5513. tprocvardef(result).procoptions:=procoptions;
  5514. tprocvardef(result).callerargareasize:=callerargareasize;
  5515. tprocvardef(result).calleeargareasize:=calleeargareasize;
  5516. tprocvardef(result).maxparacount:=maxparacount;
  5517. tprocvardef(result).minparacount:=minparacount;
  5518. for i:=low(funcretloc) to high(funcretloc) do
  5519. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  5520. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  5521. {$ifdef m68k}
  5522. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  5523. {$endif}
  5524. end;
  5525. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  5526. begin
  5527. inherited ppuwrite(ppufile);
  5528. { Save the para symtable level (necessary to distinguish nested
  5529. procvars) }
  5530. ppufile.putbyte(parast.symtablelevel);
  5531. { Write this entry }
  5532. writeentry(ppufile,ibprocvardef);
  5533. { Save the para symtable, this is taken from the interface }
  5534. tparasymtable(parast).ppuwrite(ppufile);
  5535. end;
  5536. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  5537. begin
  5538. case t of
  5539. gs_para :
  5540. GetSymtable:=parast;
  5541. else
  5542. GetSymtable:=nil;
  5543. end;
  5544. end;
  5545. function tprocvardef.size : asizeint;
  5546. begin
  5547. if ((po_methodpointer in procoptions) or
  5548. is_nested_pd(self)) and
  5549. not(po_addressonly in procoptions) then
  5550. begin
  5551. if is_nested_pd(self) then
  5552. size:=voidcodepointertype.size+parentfpvoidpointertype.size
  5553. else
  5554. size:=voidcodepointertype.size+voidpointertype.size;
  5555. end
  5556. else
  5557. size:=voidcodepointertype.size;
  5558. end;
  5559. function tprocvardef.is_methodpointer:boolean;
  5560. begin
  5561. result:=(po_methodpointer in procoptions);
  5562. end;
  5563. function tprocvardef.is_addressonly:boolean;
  5564. begin
  5565. result:=(not(po_methodpointer in procoptions) and
  5566. not is_nested_pd(self)) or
  5567. (po_addressonly in procoptions);
  5568. end;
  5569. function tprocvardef.getmangledparaname:TSymStr;
  5570. begin
  5571. if not(po_methodpointer in procoptions) then
  5572. if not is_nested_pd(self) then
  5573. result:='procvar'
  5574. else
  5575. { we need the manglednames here, because nestedprocvars can be anonymous, e.g.
  5576. having not a type name or not an unique one, see webtbs/tw27515.pp
  5577. Further, use $_ ... _$ delimiters to avoid ambiguous names, see webtbs/tw27515.pp }
  5578. result:='$_nestedprovar'+mangledprocparanames(0)+'_$'
  5579. else
  5580. result:='procvarofobj'
  5581. end;
  5582. function tprocvardef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef;
  5583. begin
  5584. result:=inherited;
  5585. tabstractprocdef(result).calcparas;
  5586. end;
  5587. function tprocvardef.is_publishable : boolean;
  5588. begin
  5589. is_publishable:=(po_methodpointer in procoptions);
  5590. end;
  5591. function tprocvardef.GetTypeName : string;
  5592. var
  5593. s: string;
  5594. pno : tprocnameoptions;
  5595. begin
  5596. {$ifdef EXTDEBUG}
  5597. pno:=[pno_showhidden];
  5598. {$else EXTDEBUG}
  5599. pno:=[];
  5600. {$endif EXTDEBUG}
  5601. s:='<';
  5602. if po_classmethod in procoptions then
  5603. s := s+'class method type of'
  5604. else
  5605. if po_addressonly in procoptions then
  5606. s := s+'address of'
  5607. else
  5608. s := s+'procedure variable type of';
  5609. if assigned(returndef) and
  5610. (returndef<>voidtype) then
  5611. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  5612. else
  5613. s:=s+' procedure'+typename_paras(pno);
  5614. if po_methodpointer in procoptions then
  5615. s := s+' of object';
  5616. if is_nested_pd(self) then
  5617. s := s+' is nested';
  5618. { calling convention doesn't matter for blocks }
  5619. if po_is_block in procoptions then
  5620. GetTypeName := s+' is block;'
  5621. else
  5622. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  5623. end;
  5624. {***************************************************************************
  5625. TOBJECTDEF
  5626. ***************************************************************************}
  5627. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef;doregister:boolean);
  5628. begin
  5629. inherited create(n,objectdef,doregister);
  5630. fcurrent_dispid:=0;
  5631. objecttype:=ot;
  5632. childof:=nil;
  5633. if objecttype=odt_helper then
  5634. owner.includeoption(sto_has_helper);
  5635. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords,
  5636. current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  5637. { create space for vmt !! }
  5638. vmtentries:=TFPList.Create;
  5639. set_parent(c);
  5640. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  5641. prepareguid;
  5642. { setup implemented interfaces }
  5643. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5644. ImplementedInterfaces:=TFPObjectList.Create(true)
  5645. else
  5646. ImplementedInterfaces:=nil;
  5647. writing_class_record_dbginfo:=false;
  5648. end;
  5649. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  5650. var
  5651. i,
  5652. implintfcount : longint;
  5653. d, getterd : tderef;
  5654. ImplIntf : TImplementedInterface;
  5655. vmtentry : pvmtentry;
  5656. begin
  5657. inherited ppuload(objectdef,ppufile);
  5658. objecttype:=tobjecttyp(ppufile.getbyte);
  5659. objextname:=stringdup(ppufile.getstring);
  5660. { only used for external Objective-C classes/protocols }
  5661. if (objextname^='') then
  5662. stringdispose(objextname);
  5663. symtable:=tObjectSymtable.create(self,objrealname^,0,0,0);
  5664. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  5665. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  5666. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  5667. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  5668. tObjectSymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  5669. ppufile.getderef(vmt_fieldderef);
  5670. ppufile.getderef(childofderef);
  5671. { load guid }
  5672. iidstr:=nil;
  5673. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5674. begin
  5675. new(iidguid);
  5676. ppufile.getguid(iidguid^);
  5677. iidstr:=stringdup(ppufile.getstring);
  5678. end;
  5679. abstractcnt:=ppufile.getlongint;
  5680. if objecttype=odt_helper then
  5681. ppufile.getderef(extendeddefderef);
  5682. vmtentries:=TFPList.Create;
  5683. vmtentries.count:=ppufile.getlongint;
  5684. for i:=0 to vmtentries.count-1 do
  5685. begin
  5686. ppufile.getderef(d);
  5687. new(vmtentry);
  5688. vmtentry^.procdef:=nil;
  5689. vmtentry^.procdefderef:=d;
  5690. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  5691. vmtentries[i]:=vmtentry;
  5692. end;
  5693. { load implemented interfaces }
  5694. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5695. begin
  5696. ImplementedInterfaces:=TFPObjectList.Create(true);
  5697. implintfcount:=ppufile.getlongint;
  5698. for i:=0 to implintfcount-1 do
  5699. begin
  5700. ppufile.getderef(d);
  5701. ppufile.getderef(getterd);
  5702. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  5703. ImplIntf.IOffset:=ppufile.getlongint;
  5704. byte(ImplIntf.IType):=ppufile.getbyte;
  5705. ImplementedInterfaces.Add(ImplIntf);
  5706. end;
  5707. end
  5708. else
  5709. ImplementedInterfaces:=nil;
  5710. if df_copied_def in defoptions then
  5711. begin
  5712. ppufile.getderef(cloneddefderef);
  5713. ppuload_platform(ppufile);
  5714. end
  5715. else
  5716. begin
  5717. ppuload_platform(ppufile);
  5718. tObjectSymtable(symtable).ppuload(ppufile);
  5719. end;
  5720. { handles the predefined class tobject }
  5721. { the last TOBJECT which is loaded gets }
  5722. { it ! }
  5723. if (childof=nil) and
  5724. (objecttype in [odt_class,odt_javaclass]) and
  5725. (objname^='TOBJECT') then
  5726. class_tobject:=self;
  5727. if (childof=nil) and
  5728. (objecttype=odt_interfacecom) then
  5729. if (objname^='IUNKNOWN') then
  5730. interface_iunknown:=self
  5731. else
  5732. if (objname^='IDISPATCH') then
  5733. interface_idispatch:=self;
  5734. if (childof=nil) and
  5735. (objecttype=odt_objcclass) and
  5736. (objname^='PROTOCOL') then
  5737. objc_protocoltype:=self;
  5738. if (objecttype=odt_javaclass) and
  5739. not(oo_is_formal in objectoptions) then
  5740. begin
  5741. if (objname^='JLOBJECT') then
  5742. java_jlobject:=self
  5743. else if (objname^='JLTHROWABLE') then
  5744. java_jlthrowable:=self
  5745. else if (objname^='FPCBASERECORDTYPE') then
  5746. java_fpcbaserecordtype:=self
  5747. else if (objname^='JLSTRING') then
  5748. java_jlstring:=self
  5749. else if (objname^='ANSISTRINGCLASS') then
  5750. java_ansistring:=self
  5751. else if (objname^='SHORTSTRINGCLASS') then
  5752. java_shortstring:=self
  5753. else if (objname^='JLENUM') then
  5754. java_jlenum:=self
  5755. else if (objname^='JUENUMSET') then
  5756. java_juenumset:=self
  5757. else if (objname^='FPCBITSET') then
  5758. java_jubitset:=self
  5759. else if (objname^='FPCBASEPROCVARTYPE') then
  5760. java_procvarbase:=self;
  5761. end;
  5762. writing_class_record_dbginfo:=false;
  5763. end;
  5764. destructor tobjectdef.destroy;
  5765. begin
  5766. if assigned(symtable) then
  5767. begin
  5768. symtable.free;
  5769. symtable:=nil;
  5770. end;
  5771. stringdispose(objextname);
  5772. stringdispose(iidstr);
  5773. if assigned(ImplementedInterfaces) then
  5774. begin
  5775. ImplementedInterfaces.free;
  5776. ImplementedInterfaces:=nil;
  5777. end;
  5778. if assigned(iidguid) then
  5779. begin
  5780. dispose(iidguid);
  5781. iidguid:=nil;
  5782. end;
  5783. if assigned(vmtentries) then
  5784. begin
  5785. resetvmtentries;
  5786. vmtentries.free;
  5787. vmtentries:=nil;
  5788. end;
  5789. if assigned(vmcallstaticinfo) then
  5790. begin
  5791. freemem(vmcallstaticinfo);
  5792. vmcallstaticinfo:=nil;
  5793. end;
  5794. inherited destroy;
  5795. end;
  5796. function tobjectdef.getcopy : tstoreddef;
  5797. var
  5798. i : longint;
  5799. begin
  5800. result:=cobjectdef.create(objecttype,objrealname^,childof,true);
  5801. { the constructor allocates a symtable which we release to avoid memory leaks }
  5802. tobjectdef(result).symtable.free;
  5803. tobjectdef(result).symtable:=symtable.getcopy;
  5804. if assigned(objextname) then
  5805. tobjectdef(result).objextname:=stringdup(objextname^);
  5806. if assigned(import_lib) then
  5807. tobjectdef(result).import_lib:=stringdup(import_lib^);
  5808. tobjectdef(result).objectoptions:=objectoptions;
  5809. include(tobjectdef(result).defoptions,df_copied_def);
  5810. tobjectdef(result).extendeddef:=extendeddef;
  5811. if assigned(tcinitcode) then
  5812. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  5813. tobjectdef(result).vmt_field:=vmt_field;
  5814. if assigned(iidguid) then
  5815. begin
  5816. new(tobjectdef(result).iidguid);
  5817. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  5818. end;
  5819. if assigned(iidstr) then
  5820. tobjectdef(result).iidstr:=stringdup(iidstr^);
  5821. tobjectdef(result).abstractcnt:=abstractcnt;
  5822. if assigned(ImplementedInterfaces) then
  5823. begin
  5824. for i:=0 to ImplementedInterfaces.count-1 do
  5825. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  5826. end;
  5827. if assigned(vmtentries) then
  5828. begin
  5829. tobjectdef(result).vmtentries:=TFPList.Create;
  5830. tobjectdef(result).copyvmtentries(self);
  5831. end;
  5832. end;
  5833. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  5834. var
  5835. i : longint;
  5836. vmtentry : pvmtentry;
  5837. ImplIntf : TImplementedInterface;
  5838. old_do_indirect_crc: boolean;
  5839. begin
  5840. { if class1 in unit A changes, and class2 in unit B inherits from it
  5841. (so unit B uses unit A), then unit B with class2 will be recompiled.
  5842. However, if there is also a class3 in unit C that only depends on
  5843. unit B, then unit C will not be recompiled because nothing changed
  5844. to the interface of unit B. Nevertheless, unit C can indirectly
  5845. depend on unit A via derefs, and these must be updated -> the
  5846. indirect crc keeps track of such changes. }
  5847. old_do_indirect_crc:=ppufile.do_indirect_crc;
  5848. ppufile.do_indirect_crc:=true;
  5849. inherited ppuwrite(ppufile);
  5850. ppufile.putbyte(byte(objecttype));
  5851. if assigned(objextname) then
  5852. ppufile.putstring(objextname^)
  5853. else
  5854. ppufile.putstring('');
  5855. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  5856. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  5857. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  5858. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  5859. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignmin));
  5860. ppufile.putderef(vmt_fieldderef);
  5861. ppufile.putderef(childofderef);
  5862. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5863. begin
  5864. ppufile.putguid(iidguid^);
  5865. ppufile.putstring(iidstr^);
  5866. end;
  5867. ppufile.putlongint(abstractcnt);
  5868. if objecttype=odt_helper then
  5869. ppufile.putderef(extendeddefderef);
  5870. ppufile.putlongint(vmtentries.count);
  5871. for i:=0 to vmtentries.count-1 do
  5872. begin
  5873. vmtentry:=pvmtentry(vmtentries[i]);
  5874. ppufile.putderef(vmtentry^.procdefderef);
  5875. ppufile.putbyte(byte(vmtentry^.visibility));
  5876. end;
  5877. if assigned(ImplementedInterfaces) then
  5878. begin
  5879. ppufile.putlongint(ImplementedInterfaces.Count);
  5880. for i:=0 to ImplementedInterfaces.Count-1 do
  5881. begin
  5882. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  5883. ppufile.putderef(ImplIntf.intfdefderef);
  5884. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  5885. ppufile.putlongint(ImplIntf.Ioffset);
  5886. ppufile.putbyte(byte(ImplIntf.IType));
  5887. end;
  5888. end;
  5889. if df_copied_def in defoptions then
  5890. ppufile.putderef(cloneddefderef);
  5891. writeentry(ppufile,ibobjectdef);
  5892. if not(df_copied_def in defoptions) then
  5893. tObjectSymtable(symtable).ppuwrite(ppufile);
  5894. ppufile.do_indirect_crc:=old_do_indirect_crc;
  5895. end;
  5896. function tobjectdef.GetTypeName:string;
  5897. begin
  5898. { in this case we will go in endless recursion, because then }
  5899. { there is no tsym associated yet with the def. It can occur }
  5900. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  5901. { instead of the actual type name }
  5902. if not assigned(typesym) then
  5903. result:='<Currently Parsed Class>'
  5904. else
  5905. result:=typesymbolprettyname;
  5906. end;
  5907. procedure tobjectdef.buildderef;
  5908. var
  5909. i : longint;
  5910. vmtentry : pvmtentry;
  5911. begin
  5912. inherited buildderef;
  5913. vmt_fieldderef.build(vmt_field);
  5914. childofderef.build(childof);
  5915. if df_copied_def in defoptions then
  5916. cloneddefderef.build(symtable.defowner)
  5917. else
  5918. tstoredsymtable(symtable).buildderef;
  5919. if objecttype=odt_helper then
  5920. extendeddefderef.build(extendeddef);
  5921. for i:=0 to vmtentries.count-1 do
  5922. begin
  5923. vmtentry:=pvmtentry(vmtentries[i]);
  5924. vmtentry^.procdefderef.build(vmtentry^.procdef);
  5925. end;
  5926. if assigned(ImplementedInterfaces) then
  5927. begin
  5928. for i:=0 to ImplementedInterfaces.count-1 do
  5929. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  5930. end;
  5931. end;
  5932. procedure tobjectdef.deref;
  5933. var
  5934. i : longint;
  5935. vmtentry : pvmtentry;
  5936. begin
  5937. inherited deref;
  5938. vmt_field:=tsym(vmt_fieldderef.resolve);
  5939. childof:=tobjectdef(childofderef.resolve);
  5940. if df_copied_def in defoptions then
  5941. begin
  5942. cloneddef:=tobjectdef(cloneddefderef.resolve);
  5943. symtable:=cloneddef.symtable.getcopy;
  5944. end
  5945. else
  5946. tstoredsymtable(symtable).deref(false);
  5947. if objecttype=odt_helper then
  5948. extendeddef:=tdef(extendeddefderef.resolve);
  5949. for i:=0 to vmtentries.count-1 do
  5950. begin
  5951. vmtentry:=pvmtentry(vmtentries[i]);
  5952. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  5953. end;
  5954. if assigned(ImplementedInterfaces) then
  5955. begin
  5956. for i:=0 to ImplementedInterfaces.count-1 do
  5957. TImplementedInterface(ImplementedInterfaces[i]).deref;
  5958. end;
  5959. end;
  5960. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  5961. var
  5962. pd: tprocdef absolute def;
  5963. st: tsymtable;
  5964. psym: tsym;
  5965. nname: TIDString;
  5966. begin
  5967. if (tdef(def).typ<>procdef) then
  5968. exit;
  5969. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  5970. owner = symtable in which objcclassdef is defined
  5971. }
  5972. st:=pd.owner.defowner.owner;
  5973. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  5974. { check for an existing procsym with our special name }
  5975. psym:=tsym(st.find(nname));
  5976. if not assigned(psym) then
  5977. begin
  5978. psym:=cprocsym.create(nname);
  5979. { avoid warning about this symbol being unused }
  5980. psym.IncRefCount;
  5981. { don't check for duplicates:
  5982. a) we checked above
  5983. b) in case we are in the implementation section of a unit, this
  5984. will also check for this symbol in the interface section
  5985. (since you normally cannot have symbols with the same name
  5986. both interface and implementation), and it's possible to
  5987. have class helpers for the same class in the interface and
  5988. in the implementation, and they cannot be merged since only
  5989. the once in the interface must be saved to the ppu/visible
  5990. from other units }
  5991. st.insert(psym,false);
  5992. end
  5993. else if (psym.typ<>procsym) then
  5994. internalerror(2009111501);
  5995. { add ourselves to this special procsym }
  5996. tprocsym(psym).procdeflist.add(def);
  5997. end;
  5998. procedure tobjectdef.derefimpl;
  5999. begin
  6000. inherited derefimpl;
  6001. { the procdefs are not owned by the class helper procsyms, so they
  6002. are not stored/restored either -> re-add them here }
  6003. if (objecttype in [odt_objcclass,odt_objcprotocol]) or
  6004. (oo_is_classhelper in objectoptions) then
  6005. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  6006. end;
  6007. procedure tobjectdef.resetvmtentries;
  6008. var
  6009. i : longint;
  6010. begin
  6011. for i:=0 to vmtentries.Count-1 do
  6012. Dispose(pvmtentry(vmtentries[i]));
  6013. vmtentries.clear;
  6014. end;
  6015. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  6016. var
  6017. i : longint;
  6018. vmtentry : pvmtentry;
  6019. begin
  6020. resetvmtentries;
  6021. vmtentries.count:=objdef.vmtentries.count;
  6022. for i:=0 to objdef.vmtentries.count-1 do
  6023. begin
  6024. new(vmtentry);
  6025. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  6026. vmtentries[i]:=vmtentry;
  6027. end;
  6028. end;
  6029. function tobjectdef.getparentdef:tdef;
  6030. begin
  6031. { TODO: Remove getparentdef hack}
  6032. { With 2 forward declared classes with the child class before the
  6033. parent class the child class is written earlier to the ppu. Leaving it
  6034. possible to have a reference to the parent class for property overriding,
  6035. but the parent class still has the childof not resolved yet (PFV) }
  6036. if childof=nil then
  6037. childof:=tobjectdef(childofderef.resolve);
  6038. result:=childof;
  6039. end;
  6040. procedure tobjectdef.prepareguid;
  6041. begin
  6042. { set up guid }
  6043. if not assigned(iidguid) then
  6044. begin
  6045. new(iidguid);
  6046. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  6047. end;
  6048. { setup iidstring }
  6049. if not assigned(iidstr) then
  6050. iidstr:=stringdup(''); { default is empty string }
  6051. end;
  6052. procedure tobjectdef.set_parent( c : tobjectdef);
  6053. begin
  6054. if assigned(childof) then
  6055. exit;
  6056. childof:=c;
  6057. if not assigned(c) then
  6058. exit;
  6059. { inherit options and status }
  6060. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  6061. { initially has the same number of abstract methods as the parent }
  6062. abstractcnt:=c.abstractcnt;
  6063. { add the data of the anchestor class/object }
  6064. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  6065. begin
  6066. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  6067. { inherit recordalignment }
  6068. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  6069. { if both the parent and this record use C-alignment, also inherit
  6070. the current field alignment }
  6071. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  6072. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  6073. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  6074. { the padding is not inherited for Objective-C classes (maybe not
  6075. for cppclass either?) }
  6076. if objecttype=odt_objcclass then
  6077. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  6078. if (oo_has_vmt in objectoptions) and
  6079. (oo_has_vmt in c.objectoptions) then
  6080. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  6081. { if parent has a vmt field then the offset is the same for the child PM }
  6082. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  6083. begin
  6084. vmt_field:=c.vmt_field;
  6085. include(objectoptions,oo_has_vmt);
  6086. end;
  6087. end;
  6088. end;
  6089. procedure tobjectdef.insertvmt;
  6090. begin
  6091. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  6092. exit;
  6093. if (oo_has_vmt in objectoptions) then
  6094. internalerror(12345)
  6095. else
  6096. begin
  6097. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  6098. tObjectSymtable(symtable).fieldalignment);
  6099. if (tf_requires_proper_alignment in target_info.flags) then
  6100. begin
  6101. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  6102. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  6103. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  6104. end;
  6105. vmt_field:=cfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[],true);
  6106. hidesym(vmt_field);
  6107. tObjectSymtable(symtable).insert(vmt_field);
  6108. tObjectSymtable(symtable).addfield(tfieldvarsym(vmt_field),vis_hidden);
  6109. include(objectoptions,oo_has_vmt);
  6110. end;
  6111. end;
  6112. function tobjectdef.vmt_offset: asizeint;
  6113. begin
  6114. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  6115. result:=0
  6116. else if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  6117. result:=tfieldvarsym(vmt_field).fieldoffset
  6118. else
  6119. result:=tfieldvarsym(vmt_field).fieldoffset div 8;
  6120. end;
  6121. procedure tobjectdef.check_forwards;
  6122. begin
  6123. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  6124. inherited;
  6125. if (oo_is_forward in objectoptions) then
  6126. begin
  6127. { ok, in future, the forward can be resolved }
  6128. Message1(sym_e_class_forward_not_resolved,objrealname^);
  6129. exclude(objectoptions,oo_is_forward);
  6130. end;
  6131. end;
  6132. function tobjectdef.find_destructor: tprocdef;
  6133. var
  6134. objdef: tobjectdef;
  6135. begin
  6136. objdef:=self;
  6137. while assigned(objdef) do
  6138. begin
  6139. result:=objdef.find_procdef_bytype(potype_destructor);
  6140. if assigned(result) then
  6141. exit;
  6142. objdef:=objdef.childof;
  6143. end;
  6144. result:=nil;
  6145. end;
  6146. function tobjectdef.implements_any_interfaces: boolean;
  6147. begin
  6148. result := (ImplementedInterfaces.Count > 0) or
  6149. (assigned(childof) and childof.implements_any_interfaces);
  6150. end;
  6151. function tobjectdef.size : asizeint;
  6152. begin
  6153. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  6154. result:=voidpointertype.size
  6155. else
  6156. result:=tObjectSymtable(symtable).datasize;
  6157. end;
  6158. function tobjectdef.alignment:shortint;
  6159. begin
  6160. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  6161. alignment:=voidpointertype.alignment
  6162. else
  6163. alignment:=tObjectSymtable(symtable).recordalignment;
  6164. end;
  6165. function tobjectdef.vmtmethodoffset(index:longint):longint;
  6166. begin
  6167. { for offset of methods for classes, see rtl/inc/objpash.inc }
  6168. case objecttype of
  6169. odt_class:
  6170. { the +2*sizeof(pint) is size and -size }
  6171. vmtmethodoffset:=index*voidcodepointertype.size+10*voidpointertype.size+2*sizeof(pint);
  6172. odt_helper,
  6173. odt_objcclass,
  6174. odt_objcprotocol:
  6175. vmtmethodoffset:=0;
  6176. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  6177. vmtmethodoffset:=index*voidcodepointertype.size;
  6178. odt_javaclass,
  6179. odt_interfacejava:
  6180. { invalid }
  6181. vmtmethodoffset:=-1;
  6182. else
  6183. { the +2*sizeof(pint) is size and -size }
  6184. {$ifdef WITHDMT}
  6185. vmtmethodoffset:=index*voidcodepointertype.size+2*voidpointertype.size+2*sizeof(pint);
  6186. {$else WITHDMT}
  6187. vmtmethodoffset:=index*voidcodepointertype.size+1*voidpointertype.size+2*sizeof(pint);
  6188. {$endif WITHDMT}
  6189. end;
  6190. end;
  6191. function tobjectdef.vmt_mangledname : TSymStr;
  6192. begin
  6193. if not(oo_has_vmt in objectoptions) then
  6194. Message1(parser_n_object_has_no_vmt,objrealname^);
  6195. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  6196. end;
  6197. function tobjectdef.vmt_def: trecorddef;
  6198. var
  6199. vmttypesym: tsym;
  6200. begin
  6201. vmttypesym:=tsym(get_top_level_symtable.Find('vmtdef$'+mangledparaname));
  6202. if not assigned(vmttypesym) or
  6203. (vmttypesym.typ<>symconst.typesym) or
  6204. (ttypesym(vmttypesym).typedef.typ<>recorddef) then
  6205. internalerror(2015052501);
  6206. result:=trecorddef(ttypesym(vmttypesym).typedef);
  6207. end;
  6208. function tobjectdef.needs_inittable : boolean;
  6209. var
  6210. hp : tobjectdef;
  6211. begin
  6212. case objecttype of
  6213. odt_helper,
  6214. odt_class :
  6215. needs_inittable:=false;
  6216. odt_dispinterface,
  6217. odt_interfacecom:
  6218. needs_inittable:=true;
  6219. odt_interfacecorba:
  6220. begin
  6221. hp:=childof;
  6222. while assigned(hp) do
  6223. begin
  6224. if hp=interface_iunknown then
  6225. begin
  6226. needs_inittable:=true;
  6227. exit;
  6228. end;
  6229. hp:=hp.childof;
  6230. end;
  6231. needs_inittable:=false;
  6232. end;
  6233. odt_object:
  6234. needs_inittable:=
  6235. tObjectSymtable(symtable).needs_init_final or
  6236. (assigned(childof) and
  6237. childof.needs_inittable);
  6238. odt_cppclass,
  6239. odt_objcclass,
  6240. odt_objcprotocol,
  6241. odt_javaclass,
  6242. odt_interfacejava:
  6243. needs_inittable:=false;
  6244. else
  6245. internalerror(200108267);
  6246. end;
  6247. end;
  6248. function tobjectdef.needs_separate_initrtti : boolean;
  6249. begin
  6250. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  6251. end;
  6252. function tobjectdef.rtti_mangledname(rt: trttitype): TSymStr;
  6253. begin
  6254. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  6255. result:=inherited rtti_mangledname(rt)
  6256. else
  6257. begin
  6258. { necessary in case of a dynamic array of nsobject, or
  6259. if an nsobject field appears in a record that needs
  6260. init/finalisation }
  6261. if rt=initrtti then
  6262. begin
  6263. result:=voidpointertype.rtti_mangledname(rt);
  6264. exit;
  6265. end;
  6266. if not(target_info.system in systems_objc_nfabi) then
  6267. begin
  6268. result:=target_asm.labelprefix;
  6269. case objecttype of
  6270. odt_objcclass:
  6271. begin
  6272. case rt of
  6273. objcclassrtti:
  6274. if not(oo_is_classhelper in objectoptions) then
  6275. result:=result+'_OBJC_CLASS_'
  6276. else
  6277. result:=result+'_OBJC_CATEGORY_';
  6278. objcmetartti:
  6279. if not(oo_is_classhelper in objectoptions) then
  6280. result:=result+'_OBJC_METACLASS_'
  6281. else
  6282. internalerror(2009111511);
  6283. else
  6284. internalerror(2009092302);
  6285. end;
  6286. end;
  6287. odt_objcprotocol:
  6288. result:=result+'_OBJC_PROTOCOL_';
  6289. end;
  6290. end
  6291. else
  6292. begin
  6293. case objecttype of
  6294. odt_objcclass:
  6295. begin
  6296. if (oo_is_classhelper in objectoptions) and
  6297. (rt<>objcclassrtti) then
  6298. internalerror(2009111512);
  6299. case rt of
  6300. objcclassrtti:
  6301. if not(oo_is_classhelper in objectoptions) then
  6302. result:='_OBJC_CLASS_$_'
  6303. else
  6304. result:='_OBJC_$_CATEGORY_';
  6305. objcmetartti:
  6306. result:='_OBJC_METACLASS_$_';
  6307. objcclassrortti:
  6308. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  6309. objcmetarortti:
  6310. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  6311. else
  6312. internalerror(2009092303);
  6313. end;
  6314. end;
  6315. odt_objcprotocol:
  6316. begin
  6317. result:=lower(target_asm.labelprefix);
  6318. case rt of
  6319. objcclassrtti:
  6320. result:=result+'_OBJC_PROTOCOL_$_';
  6321. objcmetartti:
  6322. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  6323. else
  6324. internalerror(2009092501);
  6325. end;
  6326. end;
  6327. else
  6328. internalerror(2013113005);
  6329. end;
  6330. end;
  6331. result:=result+objextname^;
  6332. end;
  6333. end;
  6334. function tobjectdef.members_need_inittable : boolean;
  6335. begin
  6336. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  6337. end;
  6338. function tobjectdef.is_publishable : boolean;
  6339. begin
  6340. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  6341. end;
  6342. function tobjectdef.get_next_dispid: longint;
  6343. begin
  6344. inc(fcurrent_dispid);
  6345. result:=fcurrent_dispid;
  6346. end;
  6347. function tobjectdef.search_enumerator_get: tprocdef;
  6348. begin
  6349. result:=inherited;
  6350. if not assigned(result) and assigned(childof) then
  6351. result:=childof.search_enumerator_get;
  6352. end;
  6353. function tobjectdef.search_enumerator_move: tprocdef;
  6354. begin
  6355. result:=inherited;
  6356. if not assigned(result) and assigned(childof) then
  6357. result:=childof.search_enumerator_move;
  6358. end;
  6359. function tobjectdef.search_enumerator_current: tsym;
  6360. begin
  6361. result:=inherited;
  6362. if not assigned(result) and assigned(childof) then
  6363. result:=childof.search_enumerator_current;
  6364. end;
  6365. procedure tobjectdef.register_created_classref_type;
  6366. begin
  6367. if not classref_created_in_current_module then
  6368. begin
  6369. classref_created_in_current_module:=true;
  6370. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  6371. end;
  6372. end;
  6373. procedure tobjectdef.register_created_object_type;
  6374. begin
  6375. if not created_in_current_module then
  6376. begin
  6377. created_in_current_module:=true;
  6378. current_module.wpoinfo.addcreatedobjtype(self);
  6379. end;
  6380. end;
  6381. procedure tobjectdef.register_maybe_created_object_type;
  6382. begin
  6383. { if we know it has been created for sure, no need
  6384. to also record that it maybe can be created in
  6385. this module
  6386. }
  6387. if not (created_in_current_module) and
  6388. not (maybe_created_in_current_module) then
  6389. begin
  6390. maybe_created_in_current_module:=true;
  6391. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  6392. end;
  6393. end;
  6394. procedure tobjectdef.register_vmt_call(index: longint);
  6395. begin
  6396. if (is_object(self) or is_class(self)) then
  6397. current_module.wpoinfo.addcalledvmtentry(self,index);
  6398. end;
  6399. procedure check_and_finish_msg(data: tobject; arg: pointer);
  6400. var
  6401. def: tdef absolute data;
  6402. pd: tprocdef absolute data;
  6403. i,
  6404. paracount: longint;
  6405. begin
  6406. if (def.typ=procdef) then
  6407. begin
  6408. { add all messages also under a dummy name to the symtable in
  6409. which the objcclass/protocol/category is declared, so they can
  6410. be called via id.<name>
  6411. }
  6412. create_class_helper_for_procdef(pd,nil);
  6413. { we have to wait until now to set the mangled name because it
  6414. depends on the (possibly external) class name, which is defined
  6415. at the very end. }
  6416. if not(po_msgstr in pd.procoptions) then
  6417. begin
  6418. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  6419. { recover to avoid internalerror later on }
  6420. include(pd.procoptions,po_msgstr);
  6421. pd.messageinf.str:=stringdup('MissingDeclaration');
  6422. end;
  6423. { Mangled name is already set in case this is a copy of
  6424. another type. }
  6425. if not(po_has_mangledname in pd.procoptions) then
  6426. begin
  6427. { check whether the number of formal parameters is correct,
  6428. and whether they have valid Objective-C types }
  6429. paracount:=0;
  6430. for i:=1 to length(pd.messageinf.str^) do
  6431. if pd.messageinf.str^[i]=':' then
  6432. inc(paracount);
  6433. for i:=0 to pd.paras.count-1 do
  6434. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  6435. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  6436. dec(paracount);
  6437. if (paracount<>0) then
  6438. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  6439. pd.setmangledname(pd.objcmangledname);
  6440. end
  6441. else
  6442. { all checks already done }
  6443. exit;
  6444. if not(oo_is_external in pd.struct.objectoptions) then
  6445. begin
  6446. if (po_varargs in pd.procoptions) then
  6447. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  6448. else
  6449. begin
  6450. { check for "array of const" parameters }
  6451. for i:=0 to pd.parast.symlist.count-1 do
  6452. begin
  6453. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  6454. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  6455. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  6456. end;
  6457. end;
  6458. end;
  6459. end;
  6460. end;
  6461. procedure mark_private_fields_used(data: tobject; arg: pointer);
  6462. var
  6463. sym: tsym absolute data;
  6464. begin
  6465. if (sym.typ=fieldvarsym) and
  6466. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  6467. sym.IncRefCount;
  6468. end;
  6469. procedure tobjectdef.finish_objc_data;
  6470. begin
  6471. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  6472. if (oo_is_external in objectoptions) then
  6473. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  6474. end;
  6475. procedure verify_objc_vardef(data: tobject; arg: pointer);
  6476. var
  6477. sym: tabstractvarsym absolute data;
  6478. res: pboolean absolute arg;
  6479. founderrordef: tdef;
  6480. begin
  6481. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  6482. exit;
  6483. if (sym.typ=paravarsym) and
  6484. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  6485. is_array_of_const(tparavarsym(sym).vardef)) then
  6486. exit;
  6487. if not objcchecktype(sym.vardef,founderrordef) then
  6488. begin
  6489. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6490. res^:=false;
  6491. end;
  6492. end;
  6493. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  6494. var
  6495. def: tdef absolute data;
  6496. res: pboolean absolute arg;
  6497. founderrordef: tdef;
  6498. begin
  6499. if (def.typ<>procdef) then
  6500. exit;
  6501. { check parameter types for validity }
  6502. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  6503. { check the result type for validity }
  6504. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  6505. begin
  6506. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6507. res^:=false;
  6508. end;
  6509. end;
  6510. function tobjectdef.check_objc_types: boolean;
  6511. begin
  6512. { done in separate step from finish_objc_data, because when
  6513. finish_objc_data is called, not all forwarddefs have been resolved
  6514. yet and we need to know all types here }
  6515. result:=true;
  6516. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  6517. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  6518. end;
  6519. procedure do_cpp_import_info(data: tobject; arg: pointer);
  6520. var
  6521. def: tdef absolute data;
  6522. pd: tprocdef absolute data;
  6523. begin
  6524. if (def.typ=procdef) then
  6525. begin
  6526. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  6527. if (oo_is_external in pd.struct.objectoptions) then
  6528. begin
  6529. { copied from psub.read_proc }
  6530. if assigned(tobjectdef(pd.struct).import_lib) then
  6531. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  6532. else
  6533. begin
  6534. { add import name to external list for DLL scanning }
  6535. if tf_has_dllscanner in target_info.flags then
  6536. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  6537. end;
  6538. end;
  6539. end;
  6540. end;
  6541. procedure tobjectdef.finish_cpp_data;
  6542. begin
  6543. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  6544. end;
  6545. {****************************************************************************
  6546. TImplementedInterface
  6547. ****************************************************************************}
  6548. function TImplementedInterface.GetIOffset: longint;
  6549. begin
  6550. if (fIOffset=-1) and
  6551. (IType in [etFieldValue,etFieldValueClass]) then
  6552. result:=tfieldvarsym(ImplementsField).fieldoffset
  6553. else
  6554. result:=fIOffset;
  6555. end;
  6556. constructor TImplementedInterface.create(aintf: tobjectdef);
  6557. begin
  6558. inherited create;
  6559. intfdef:=aintf;
  6560. IOffset:=-1;
  6561. IType:=etStandard;
  6562. NameMappings:=nil;
  6563. procdefs:=nil;
  6564. end;
  6565. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  6566. begin
  6567. inherited create;
  6568. intfdef:=nil;
  6569. intfdefderef:=intfd;
  6570. ImplementsGetterDeref:=getterd;
  6571. IOffset:=-1;
  6572. IType:=etStandard;
  6573. NameMappings:=nil;
  6574. procdefs:=nil;
  6575. end;
  6576. destructor TImplementedInterface.destroy;
  6577. var
  6578. i : longint;
  6579. mappedname : pshortstring;
  6580. begin
  6581. if assigned(NameMappings) then
  6582. begin
  6583. for i:=0 to NameMappings.Count-1 do
  6584. begin
  6585. mappedname:=pshortstring(NameMappings[i]);
  6586. stringdispose(mappedname);
  6587. end;
  6588. NameMappings.free;
  6589. NameMappings:=nil;
  6590. end;
  6591. if assigned(procdefs) then
  6592. begin
  6593. procdefs.free;
  6594. procdefs:=nil;
  6595. end;
  6596. inherited destroy;
  6597. end;
  6598. procedure TImplementedInterface.buildderef;
  6599. begin
  6600. intfdefderef.build(intfdef);
  6601. ImplementsGetterDeref.build(ImplementsGetter);
  6602. end;
  6603. procedure TImplementedInterface.deref;
  6604. begin
  6605. intfdef:=tobjectdef(intfdefderef.resolve);
  6606. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  6607. end;
  6608. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  6609. begin
  6610. if not assigned(NameMappings) then
  6611. NameMappings:=TFPHashList.Create;
  6612. NameMappings.Add(origname,stringdup(newname));
  6613. end;
  6614. function TImplementedInterface.GetMapping(const origname: string):string;
  6615. var
  6616. mappedname : pshortstring;
  6617. begin
  6618. result:='';
  6619. if not assigned(NameMappings) then
  6620. exit;
  6621. mappedname:=PShortstring(NameMappings.Find(origname));
  6622. if assigned(mappedname) then
  6623. result:=mappedname^;
  6624. end;
  6625. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  6626. begin
  6627. if not assigned(procdefs) then
  6628. procdefs:=TFPObjectList.Create(false);
  6629. { duplicate entries must be stored, because multiple }
  6630. { interfaces can declare methods with the same name }
  6631. { and all of these get their own VMT entry }
  6632. procdefs.Add(pd);
  6633. end;
  6634. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  6635. var
  6636. i : longint;
  6637. begin
  6638. result:=false;
  6639. { interfaces being implemented through delegation are not mergable (FK) }
  6640. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  6641. exit;
  6642. weight:=0;
  6643. { empty interface is mergeable }
  6644. if ProcDefs.Count=0 then
  6645. begin
  6646. result:=true;
  6647. exit;
  6648. end;
  6649. { The interface to merge must at least the number of
  6650. procedures of this interface }
  6651. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  6652. exit;
  6653. for i:=0 to ProcDefs.Count-1 do
  6654. begin
  6655. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  6656. exit;
  6657. end;
  6658. weight:=ProcDefs.Count;
  6659. result:=true;
  6660. end;
  6661. function TImplementedInterface.getcopy:TImplementedInterface;
  6662. begin
  6663. Result:=TImplementedInterface.Create(nil);
  6664. { 1) the procdefs list will be freed once for each copy
  6665. 2) since the procdefs list owns its elements, those will also be freed for each copy
  6666. 3) idem for the name mappings
  6667. }
  6668. { warning: this is completely wrong on so many levels...
  6669. Move(pointer(self)^,pointer(result)^,InstanceSize);
  6670. We need to make clean copies of the different fields
  6671. this is not implemented yet, and thus we generate an internal
  6672. error instead PM 2011-06-14 }
  6673. internalerror(2011061401);
  6674. end;
  6675. {****************************************************************************
  6676. TFORWARDDEF
  6677. ****************************************************************************}
  6678. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  6679. begin
  6680. inherited create(forwarddef,true);
  6681. tosymname:=stringdup(s);
  6682. forwardpos:=pos;
  6683. end;
  6684. function tforwarddef.GetTypeName:string;
  6685. begin
  6686. GetTypeName:='unresolved forward to '+tosymname^;
  6687. end;
  6688. destructor tforwarddef.destroy;
  6689. begin
  6690. stringdispose(tosymname);
  6691. inherited destroy;
  6692. end;
  6693. function tforwarddef.getcopy:tstoreddef;
  6694. begin
  6695. result:=cforwarddef.create(tosymname^, forwardpos);
  6696. end;
  6697. {****************************************************************************
  6698. TUNDEFINEDDEF
  6699. ****************************************************************************}
  6700. constructor tundefineddef.create(doregister:boolean);
  6701. begin
  6702. inherited create(undefineddef,doregister);
  6703. end;
  6704. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  6705. begin
  6706. inherited ppuload(undefineddef,ppufile);
  6707. ppuload_platform(ppufile);
  6708. end;
  6709. function tundefineddef.GetTypeName:string;
  6710. begin
  6711. GetTypeName:='<undefined type>';
  6712. end;
  6713. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  6714. begin
  6715. inherited ppuwrite(ppufile);
  6716. writeentry(ppufile,ibundefineddef);
  6717. end;
  6718. {****************************************************************************
  6719. TERRORDEF
  6720. ****************************************************************************}
  6721. constructor terrordef.create;
  6722. begin
  6723. inherited create(errordef,true);
  6724. { prevent consecutive faults }
  6725. savesize:=1;
  6726. end;
  6727. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  6728. begin
  6729. { Can't write errordefs to ppu }
  6730. internalerror(200411063);
  6731. end;
  6732. function terrordef.GetTypeName:string;
  6733. begin
  6734. GetTypeName:='<erroneous type>';
  6735. end;
  6736. function terrordef.getmangledparaname:TSymStr;
  6737. begin
  6738. getmangledparaname:='error';
  6739. end;
  6740. {****************************************************************************
  6741. Definition Helpers
  6742. ****************************************************************************}
  6743. function is_interfacecom(def: tdef): boolean;
  6744. begin
  6745. is_interfacecom:=
  6746. assigned(def) and
  6747. (def.typ=objectdef) and
  6748. (tobjectdef(def).objecttype=odt_interfacecom);
  6749. end;
  6750. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  6751. begin
  6752. is_interfacecom_or_dispinterface:=
  6753. assigned(def) and
  6754. (def.typ=objectdef) and
  6755. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  6756. end;
  6757. function is_any_interface_kind(def: tdef): boolean;
  6758. begin
  6759. result:=
  6760. assigned(def) and
  6761. (def.typ=objectdef) and
  6762. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  6763. is_objccategory(def));
  6764. end;
  6765. function is_interfacecorba(def: tdef): boolean;
  6766. begin
  6767. is_interfacecorba:=
  6768. assigned(def) and
  6769. (def.typ=objectdef) and
  6770. (tobjectdef(def).objecttype=odt_interfacecorba);
  6771. end;
  6772. function is_interface(def: tdef): boolean;
  6773. begin
  6774. is_interface:=
  6775. assigned(def) and
  6776. (def.typ=objectdef) and
  6777. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  6778. end;
  6779. function is_dispinterface(def: tdef): boolean;
  6780. begin
  6781. result:=
  6782. assigned(def) and
  6783. (def.typ=objectdef) and
  6784. (tobjectdef(def).objecttype=odt_dispinterface);
  6785. end;
  6786. function is_class(def: tdef): boolean;
  6787. begin
  6788. is_class:=
  6789. assigned(def) and
  6790. (def.typ=objectdef) and
  6791. (tobjectdef(def).objecttype=odt_class);
  6792. end;
  6793. function is_object(def: tdef): boolean;
  6794. begin
  6795. is_object:=
  6796. assigned(def) and
  6797. (def.typ=objectdef) and
  6798. (tobjectdef(def).objecttype=odt_object);
  6799. end;
  6800. function is_cppclass(def: tdef): boolean;
  6801. begin
  6802. is_cppclass:=
  6803. assigned(def) and
  6804. (def.typ=objectdef) and
  6805. (tobjectdef(def).objecttype=odt_cppclass);
  6806. end;
  6807. function is_objcclass(def: tdef): boolean;
  6808. begin
  6809. is_objcclass:=
  6810. assigned(def) and
  6811. (def.typ=objectdef) and
  6812. (tobjectdef(def).objecttype=odt_objcclass);
  6813. end;
  6814. function is_objectpascal_helper(def: tdef): boolean;
  6815. begin
  6816. result:=
  6817. assigned(def) and
  6818. (def.typ=objectdef) and
  6819. (tobjectdef(def).objecttype=odt_helper);
  6820. end;
  6821. function is_objcclassref(def: tdef): boolean;
  6822. begin
  6823. is_objcclassref:=
  6824. assigned(def) and
  6825. (def.typ=classrefdef) and
  6826. is_objcclass(tclassrefdef(def).pointeddef);
  6827. end;
  6828. function is_objcprotocol(def: tdef): boolean;
  6829. begin
  6830. result:=
  6831. assigned(def) and
  6832. (def.typ=objectdef) and
  6833. (tobjectdef(def).objecttype=odt_objcprotocol);
  6834. end;
  6835. function is_objccategory(def: tdef): boolean;
  6836. begin
  6837. result:=
  6838. assigned(def) and
  6839. (def.typ=objectdef) and
  6840. { if used as a forward type }
  6841. ((tobjectdef(def).objecttype=odt_objccategory) or
  6842. { if used as after it has been resolved }
  6843. ((tobjectdef(def).objecttype=odt_objcclass) and
  6844. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6845. end;
  6846. function is_objc_class_or_protocol(def: tdef): boolean;
  6847. begin
  6848. result:=
  6849. assigned(def) and
  6850. (def.typ=objectdef) and
  6851. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  6852. end;
  6853. function is_objc_protocol_or_category(def: tdef): boolean;
  6854. begin
  6855. result:=
  6856. assigned(def) and
  6857. (def.typ=objectdef) and
  6858. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  6859. ((tobjectdef(def).objecttype = odt_objcclass) and
  6860. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6861. end;
  6862. function is_classhelper(def: tdef): boolean;
  6863. begin
  6864. result:=
  6865. is_objectpascal_helper(def) or
  6866. is_objccategory(def);
  6867. end;
  6868. function is_class_or_interface(def: tdef): boolean;
  6869. begin
  6870. result:=
  6871. assigned(def) and
  6872. (def.typ=objectdef) and
  6873. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  6874. end;
  6875. function is_class_or_interface_or_objc(def: tdef): boolean;
  6876. begin
  6877. result:=
  6878. assigned(def) and
  6879. (def.typ=objectdef) and
  6880. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  6881. end;
  6882. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  6883. begin
  6884. result:=
  6885. assigned(def) and
  6886. (def.typ=objectdef) and
  6887. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6888. end;
  6889. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  6890. begin
  6891. result:=
  6892. assigned(def) and
  6893. (def.typ=objectdef) and
  6894. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6895. end;
  6896. function is_class_or_interface_or_object(def: tdef): boolean;
  6897. begin
  6898. result:=
  6899. assigned(def) and
  6900. (def.typ=objectdef) and
  6901. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  6902. end;
  6903. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  6904. begin
  6905. result:=
  6906. assigned(def) and
  6907. (def.typ=objectdef) and
  6908. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  6909. end;
  6910. function is_implicit_pointer_object_type(def: tdef): boolean;
  6911. begin
  6912. result:=
  6913. assigned(def) and
  6914. (((def.typ=objectdef) and
  6915. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  6916. ((target_info.system in systems_jvm) and
  6917. (def.typ=recorddef)));
  6918. end;
  6919. function is_implicit_array_pointer(def: tdef): boolean;
  6920. begin
  6921. result:=is_dynamic_array(def) or is_dynamicstring(def);
  6922. end;
  6923. function is_class_or_object(def: tdef): boolean;
  6924. begin
  6925. result:=
  6926. assigned(def) and
  6927. (def.typ=objectdef) and
  6928. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  6929. end;
  6930. function is_record(def: tdef): boolean;
  6931. begin
  6932. result:=
  6933. assigned(def) and
  6934. (def.typ=recorddef);
  6935. end;
  6936. function is_javaclass(def: tdef): boolean;
  6937. begin
  6938. result:=
  6939. assigned(def) and
  6940. (def.typ=objectdef) and
  6941. (tobjectdef(def).objecttype=odt_javaclass);
  6942. end;
  6943. function is_javaclassref(def: tdef): boolean;
  6944. begin
  6945. is_javaclassref:=
  6946. assigned(def) and
  6947. (def.typ=classrefdef) and
  6948. is_javaclass(tclassrefdef(def).pointeddef);
  6949. end;
  6950. function is_javainterface(def: tdef): boolean;
  6951. begin
  6952. result:=
  6953. assigned(def) and
  6954. (def.typ=objectdef) and
  6955. (tobjectdef(def).objecttype=odt_interfacejava);
  6956. end;
  6957. function is_java_class_or_interface(def: tdef): boolean;
  6958. begin
  6959. result:=
  6960. assigned(def) and
  6961. (def.typ=objectdef) and
  6962. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  6963. end;
  6964. procedure loadobjctypes;
  6965. begin
  6966. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  6967. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  6968. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  6969. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  6970. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  6971. end;
  6972. procedure maybeloadcocoatypes;
  6973. var
  6974. tsym: ttypesym;
  6975. cocoaunit: string[15];
  6976. begin
  6977. if assigned(objc_fastenumeration) then
  6978. exit;
  6979. if not(target_info.system in [system_arm_darwin,system_i386_iphonesim,system_aarch64_darwin,system_x86_64_iphonesim]) then
  6980. cocoaunit:='COCOAALL'
  6981. else
  6982. cocoaunit:='IPHONEALL';
  6983. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  6984. if assigned(tsym) then
  6985. objc_fastenumeration:=tobjectdef(tsym.typedef)
  6986. else
  6987. objc_fastenumeration:=nil;
  6988. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  6989. if assigned(tsym) then
  6990. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  6991. else
  6992. objc_fastenumerationstate:=nil;
  6993. end;
  6994. function use_vectorfpu(def : tdef) : boolean;
  6995. begin
  6996. {$ifdef x86}
  6997. {$define use_vectorfpuimplemented}
  6998. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  6999. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  7000. {$endif x86}
  7001. {$ifdef arm}
  7002. {$define use_vectorfpuimplemented}
  7003. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  7004. {$endif arm}
  7005. {$ifdef aarch64}
  7006. {$define use_vectorfpuimplemented}
  7007. use_vectorfpu:=true;
  7008. {$endif aarch64}
  7009. {$ifndef use_vectorfpuimplemented}
  7010. use_vectorfpu:=false;
  7011. {$endif}
  7012. end;
  7013. end.