symdef.pas 271 KB

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