symdef.pas 291 KB

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