symdef.pas 266 KB

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