symdef.pas 270 KB

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