symdef.pas 264 KB

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