symdef.pas 291 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696
  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(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. qlow:=qword(-lowrange);
  3695. { prevent overflow, return 0 to indicate overflow }
  3696. if qhigh+qlow>qword(high(asizeint)-1) then
  3697. result:=0
  3698. else
  3699. result:=qhigh+qlow+1;
  3700. end
  3701. else
  3702. result:=int64(highrange)-lowrange+1;
  3703. end;
  3704. function tarraydef.size : asizeint;
  3705. var
  3706. cachedelecount : asizeuint;
  3707. cachedelesize : asizeint;
  3708. begin
  3709. if ado_IsDynamicArray in arrayoptions then
  3710. begin
  3711. size:=voidpointertype.size;
  3712. exit;
  3713. end;
  3714. { Tarraydef.size may never be called for an open array! }
  3715. if (highrange=-1) and (lowrange=0) then
  3716. internalerror(99080501);
  3717. if not (ado_IsBitPacked in arrayoptions) then
  3718. cachedelesize:=elesize
  3719. else
  3720. cachedelesize := elepackedbitsize;
  3721. cachedelecount:=elecount;
  3722. if (cachedelesize = 0) then
  3723. begin
  3724. size := 0;
  3725. exit;
  3726. end;
  3727. if (cachedelecount = 0) then
  3728. begin
  3729. size := -1;
  3730. exit;
  3731. end;
  3732. { prevent overflow, return -1 to indicate overflow }
  3733. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  3734. if (cachedelecount > asizeuint(high(asizeint))) or
  3735. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  3736. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  3737. accessing the array, see ncgmem (PFV) }
  3738. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  3739. begin
  3740. result:=-1;
  3741. exit;
  3742. end;
  3743. result:=cachedelesize*asizeint(cachedelecount);
  3744. if (ado_IsBitPacked in arrayoptions) then
  3745. { can't just add 7 and divide by 8, because that may overflow }
  3746. result:=result div 8 + ord((result mod 8)<>0);
  3747. {$ifdef cpu16bitaddr}
  3748. if result>65535 then
  3749. begin
  3750. result:=-1;
  3751. exit;
  3752. end;
  3753. {$endif cpu16bitaddr}
  3754. end;
  3755. procedure tarraydef.setelementdef(def:tdef);
  3756. begin
  3757. _elementdef:=def;
  3758. if not(
  3759. (ado_IsDynamicArray in arrayoptions) or
  3760. (ado_IsConvertedPointer in arrayoptions) or
  3761. (ado_IsConstructor in arrayoptions) or
  3762. (highrange<lowrange)
  3763. ) and
  3764. (size=-1) then
  3765. Message(sym_e_segment_too_large);
  3766. end;
  3767. function tarraydef.alignment : shortint;
  3768. begin
  3769. { alignment of dyn. arrays doesn't depend on the element size }
  3770. if (ado_IsDynamicArray in arrayoptions) then
  3771. alignment:=voidpointertype.alignment
  3772. { alignment is the target alignment for the used load size }
  3773. else if (ado_IsBitPacked in arrayoptions) and
  3774. (elementdef.typ in [enumdef,orddef]) then
  3775. alignment:=cgsize_orddef(int_cgsize(packedbitsloadsize(elepackedbitsize))).alignment
  3776. { alignment is the alignment of the elements }
  3777. else
  3778. alignment:=elementdef.alignment
  3779. end;
  3780. function tarraydef.needs_inittable : boolean;
  3781. begin
  3782. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  3783. end;
  3784. function tarraydef.needs_separate_initrtti : boolean;
  3785. begin
  3786. if ado_IsBitPacked in arrayoptions then
  3787. result:=false
  3788. else
  3789. result:=elementdef.needs_separate_initrtti;
  3790. end;
  3791. function tarraydef.GetTypeName : string;
  3792. begin
  3793. if (ado_IsConstString in arrayoptions) then
  3794. result:='Constant String'
  3795. else if (ado_isarrayofconst in arrayoptions) or
  3796. (ado_isConstructor in arrayoptions) then
  3797. begin
  3798. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  3799. GetTypeName:='Array Of Const'
  3800. else
  3801. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  3802. end
  3803. else if (ado_IsDynamicArray in arrayoptions) then
  3804. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  3805. else if ((highrange=-1) and (lowrange=0)) then
  3806. GetTypeName:='{Open} Array Of '+elementdef.typename
  3807. else
  3808. begin
  3809. result := '';
  3810. if (ado_IsBitPacked in arrayoptions) then
  3811. result:='BitPacked ';
  3812. if rangedef.typ=enumdef then
  3813. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  3814. else
  3815. result:=result+'Array['+tostr(lowrange)+'..'+
  3816. tostr(highrange)+'] Of '+elementdef.typename
  3817. end;
  3818. end;
  3819. function tarraydef.getmangledparaname : TSymStr;
  3820. begin
  3821. if ado_isarrayofconst in arrayoptions then
  3822. getmangledparaname:='array_of_const'
  3823. else
  3824. if ((highrange=-1) and (lowrange=0)) then
  3825. getmangledparaname:='array_of_'+elementdef.mangledparaname
  3826. else
  3827. internalerror(200204176);
  3828. end;
  3829. function tarraydef.is_publishable : boolean;
  3830. begin
  3831. Result:=ado_IsDynamicArray in arrayoptions;
  3832. end;
  3833. {***************************************************************************
  3834. tabstractrecorddef
  3835. ***************************************************************************}
  3836. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp;doregister:boolean);
  3837. begin
  3838. inherited create(dt,doregister);
  3839. objname:=stringdup(upper(n));
  3840. objrealname:=stringdup(n);
  3841. objectoptions:=[];
  3842. if assigned(current_module.namespace) then
  3843. begin
  3844. import_lib:=stringdup(current_module.namespace^);
  3845. replace(import_lib^,'.','/');
  3846. end;
  3847. end;
  3848. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3849. begin
  3850. inherited ppuload(dt,ppufile);
  3851. objrealname:=ppufile.getpshortstring;
  3852. objname:=stringdup(upper(objrealname^));
  3853. import_lib:=ppufile.getpshortstring;
  3854. { only used for external C++ classes and Java classes/records }
  3855. if (import_lib^='') then
  3856. stringdispose(import_lib);
  3857. ppufile.getsmallset(objectoptions);
  3858. end;
  3859. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  3860. begin
  3861. inherited ppuwrite(ppufile);
  3862. ppufile.putstring(objrealname^);
  3863. if assigned(import_lib) then
  3864. ppufile.putstring(import_lib^)
  3865. else
  3866. ppufile.putstring('');
  3867. ppufile.putsmallset(objectoptions);
  3868. end;
  3869. destructor tabstractrecorddef.destroy;
  3870. begin
  3871. stringdispose(objname);
  3872. stringdispose(objrealname);
  3873. stringdispose(import_lib);
  3874. tcinitcode.free;
  3875. inherited destroy;
  3876. end;
  3877. procedure tabstractrecorddef.buildderefimpl;
  3878. begin
  3879. inherited buildderefimpl;
  3880. if not (df_copied_def in defoptions) then
  3881. tstoredsymtable(symtable).buildderefimpl;
  3882. end;
  3883. procedure tabstractrecorddef.derefimpl;
  3884. begin
  3885. inherited derefimpl;
  3886. if not (df_copied_def in defoptions) then
  3887. tstoredsymtable(symtable).derefimpl(false);
  3888. end;
  3889. procedure tabstractrecorddef.check_forwards;
  3890. begin
  3891. { the defs of a copied def are defined for the original type only }
  3892. if not(df_copied_def in defoptions) then
  3893. tstoredsymtable(symtable).check_forwards;
  3894. end;
  3895. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  3896. var
  3897. i: longint;
  3898. sym: tsym;
  3899. begin
  3900. for i:=0 to symtable.SymList.Count-1 do
  3901. begin
  3902. sym:=tsym(symtable.SymList[i]);
  3903. if sym.typ=procsym then
  3904. begin
  3905. result:=tprocsym(sym).find_procdef_bytype(pt);
  3906. if assigned(result) then
  3907. exit;
  3908. end;
  3909. end;
  3910. result:=nil;
  3911. end;
  3912. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  3913. begin
  3914. if t=gs_record then
  3915. GetSymtable:=symtable
  3916. else
  3917. GetSymtable:=nil;
  3918. end;
  3919. function tabstractrecorddef.is_packed:boolean;
  3920. begin
  3921. result:=tabstractrecordsymtable(symtable).is_packed;
  3922. end;
  3923. function tabstractrecorddef.RttiName: string;
  3924. function generate_full_paramname(maxlength:longint):string;
  3925. const
  3926. commacount : array[boolean] of longint = (0,1);
  3927. var
  3928. fullparas,
  3929. paramname : ansistring;
  3930. module : tmodule;
  3931. sym : ttypesym;
  3932. i : longint;
  3933. begin
  3934. { we want at least enough space for an ellipsis }
  3935. if maxlength<3 then
  3936. internalerror(2014121203);
  3937. fullparas:='';
  3938. for i:=0 to genericparas.count-1 do
  3939. begin
  3940. sym:=ttypesym(genericparas[i]);
  3941. module:=find_module_from_symtable(sym.owner);
  3942. if not assigned(module) then
  3943. internalerror(2014121202);
  3944. paramname:=module.realmodulename^;
  3945. if sym.typedef.typ in [objectdef,recorddef] then
  3946. paramname:=paramname+'.'+tabstractrecorddef(sym.typedef).rttiname
  3947. else
  3948. paramname:=paramname+'.'+sym.typedef.typename;
  3949. if length(fullparas)+commacount[i>0]+length(paramname)>maxlength then
  3950. begin
  3951. if i>0 then
  3952. fullparas:=fullparas+',...'
  3953. else
  3954. fullparas:=fullparas+'...';
  3955. break;
  3956. end;
  3957. { could we fit an ellipsis after this parameter if it should be too long? }
  3958. if (maxlength-(length(fullparas)+commacount[i>0]+length(paramname))<4) and (i<genericparas.count-1) then
  3959. begin
  3960. { then omit already this parameter }
  3961. if i>0 then
  3962. fullparas:=fullparas+',...'
  3963. else
  3964. fullparas:=fullparas+'...';
  3965. break;
  3966. end;
  3967. if i>0 then
  3968. fullparas:=fullparas+',';
  3969. fullparas:=fullparas+paramname;
  3970. end;
  3971. result:=fullparas;
  3972. end;
  3973. var
  3974. nongeneric,
  3975. basename : string;
  3976. i,
  3977. remlength,
  3978. paramcount,
  3979. crcidx : longint;
  3980. begin
  3981. if rttistring='' then
  3982. begin
  3983. if is_specialization then
  3984. begin
  3985. rttistring:=OwnerHierarchyName;
  3986. { there should be two $ characters, one before the CRC and one before the count }
  3987. crcidx:=-1;
  3988. for i:=length(objrealname^) downto 1 do
  3989. if objrealname^[i]='$' then
  3990. begin
  3991. crcidx:=i;
  3992. break;
  3993. end;
  3994. if crcidx<0 then
  3995. internalerror(2014121201);
  3996. basename:=copy(objrealname^,1,crcidx-1);
  3997. split_generic_name(basename,nongeneric,paramcount);
  3998. rttistring:=rttistring+nongeneric+'<';
  3999. remlength:=255-length(rttistring)-1;
  4000. if remlength<4 then
  4001. rttistring:=rttistring+'>'
  4002. else
  4003. rttistring:=rttistring+generate_full_paramname(remlength)+'>';
  4004. end
  4005. else
  4006. if is_generic then
  4007. begin
  4008. rttistring:=OwnerHierarchyName;
  4009. split_generic_name(objrealname^,nongeneric,paramcount);
  4010. rttistring:=rttistring+nongeneric+'<';
  4011. { we don't want any ',' if there is only one parameter }
  4012. for i:=0 to paramcount-0 do
  4013. rttistring:=rttistring+',';
  4014. rttistring:=rttistring+'>';
  4015. end
  4016. else
  4017. rttistring:=OwnerHierarchyName+objrealname^;
  4018. end;
  4019. result:=rttistring;
  4020. end;
  4021. function tabstractrecorddef.search_enumerator_get: tprocdef;
  4022. var
  4023. sym : tsym;
  4024. i : integer;
  4025. pd : tprocdef;
  4026. hashedid : THashedIDString;
  4027. begin
  4028. result:=nil;
  4029. hashedid.id:='GETENUMERATOR';
  4030. sym:=tsym(symtable.FindWithHash(hashedid));
  4031. if assigned(sym) and (sym.typ=procsym) then
  4032. begin
  4033. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  4034. begin
  4035. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4036. if (pd.proctypeoption = potype_function) and
  4037. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  4038. (pd.visibility >= vis_public) then
  4039. begin
  4040. result:=pd;
  4041. exit;
  4042. end;
  4043. end;
  4044. end;
  4045. end;
  4046. function tabstractrecorddef.search_enumerator_move: tprocdef;
  4047. var
  4048. sym : tsym;
  4049. i : integer;
  4050. pd : tprocdef;
  4051. hashedid : THashedIDString;
  4052. begin
  4053. result:=nil;
  4054. // first search for po_enumerator_movenext method modifier
  4055. // then search for public function MoveNext: Boolean
  4056. for i:=0 to symtable.SymList.Count-1 do
  4057. begin
  4058. sym:=TSym(symtable.SymList[i]);
  4059. if (sym.typ=procsym) then
  4060. begin
  4061. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  4062. if assigned(pd) then
  4063. begin
  4064. result:=pd;
  4065. exit;
  4066. end;
  4067. end;
  4068. end;
  4069. hashedid.id:='MOVENEXT';
  4070. sym:=tsym(symtable.FindWithHash(hashedid));
  4071. if assigned(sym) and (sym.typ=procsym) then
  4072. begin
  4073. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  4074. begin
  4075. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4076. if (pd.proctypeoption = potype_function) and
  4077. is_boolean(pd.returndef) and
  4078. (pd.minparacount = 0) and
  4079. (pd.visibility >= vis_public) then
  4080. begin
  4081. result:=pd;
  4082. exit;
  4083. end;
  4084. end;
  4085. end;
  4086. end;
  4087. function tabstractrecorddef.search_enumerator_current: tsym;
  4088. var
  4089. sym: tsym;
  4090. i: integer;
  4091. hashedid : THashedIDString;
  4092. begin
  4093. result:=nil;
  4094. // first search for ppo_enumerator_current property modifier
  4095. // then search for public property Current
  4096. for i:=0 to symtable.SymList.Count-1 do
  4097. begin
  4098. sym:=TSym(symtable.SymList[i]);
  4099. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  4100. begin
  4101. result:=sym;
  4102. exit;
  4103. end;
  4104. end;
  4105. hashedid.id:='CURRENT';
  4106. sym:=tsym(symtable.FindWithHash(hashedid));
  4107. if assigned(sym) and (sym.typ=propertysym) and
  4108. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  4109. begin
  4110. result:=sym;
  4111. exit;
  4112. end;
  4113. end;
  4114. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  4115. var
  4116. st: tsymtable;
  4117. enclosingdef: tdef;
  4118. begin
  4119. if typ=objectdef then
  4120. result:=tobjectdef(self).objextname^
  4121. else if assigned(typesym) then
  4122. result:=typesym.realname
  4123. { have to generate anonymous nested type in current unit/class/record }
  4124. else
  4125. internalerror(2011032601);
  4126. { in case of specializations, add some extras to prevent name conflicts
  4127. with nested classes }
  4128. if df_specialization in defoptions then
  4129. result:='$'+result+'$specialization$';
  4130. st:=owner;
  4131. while assigned(st) and
  4132. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  4133. begin
  4134. { nested classes are named as "OuterClass$InnerClass" }
  4135. enclosingdef:=tdef(st.defowner);
  4136. if enclosingdef.typ=procdef then
  4137. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  4138. else if enclosingdef.typ=objectdef then
  4139. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  4140. else if assigned(enclosingdef.typesym) then
  4141. result:=enclosingdef.typesym.realname+'$'+result
  4142. else
  4143. internalerror(2011060305);
  4144. st:=enclosingdef.owner;
  4145. end;
  4146. if with_package_name and
  4147. assigned(import_lib) then
  4148. result:=import_lib^+'/'+result;
  4149. end;
  4150. function tabstractrecorddef.contains_float_field: boolean;
  4151. var
  4152. i : longint;
  4153. begin
  4154. result:=true;
  4155. for i:=0 to symtable.symlist.count-1 do
  4156. begin
  4157. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  4158. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  4159. continue;
  4160. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  4161. begin
  4162. if tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).is_fpuregable then
  4163. exit;
  4164. { search recursively }
  4165. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  4166. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_float_field) then
  4167. exit;
  4168. end;
  4169. end;
  4170. result:=false;
  4171. end;
  4172. function tabstractrecorddef.contains_cross_aword_field: boolean;
  4173. var
  4174. i : longint;
  4175. foffset, fsize: aword;
  4176. begin
  4177. result:=true;
  4178. for i:=0 to symtable.symlist.count-1 do
  4179. begin
  4180. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  4181. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  4182. continue;
  4183. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  4184. begin
  4185. if is_packed then
  4186. begin
  4187. foffset:=tfieldvarsym(symtable.symlist[i]).fieldoffset;
  4188. fsize:=tfieldvarsym(symtable.symlist[i]).vardef.packedbitsize;
  4189. end
  4190. else
  4191. begin
  4192. foffset:=tfieldvarsym(symtable.symlist[i]).fieldoffset*8;
  4193. fsize:=tfieldvarsym(symtable.symlist[i]).vardef.size*8;
  4194. end;
  4195. if (fsize>0) and ((foffset div (sizeof(aword)*8)) <> ((foffset+fsize-1) div (sizeof(aword)*8))) then
  4196. exit;
  4197. { search recursively }
  4198. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  4199. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_cross_aword_field) then
  4200. exit;
  4201. end;
  4202. end;
  4203. result:=false;
  4204. end;
  4205. {***************************************************************************
  4206. trecorddef
  4207. ***************************************************************************}
  4208. constructor trecorddef.create(const n:string; p:TSymtable);
  4209. begin
  4210. inherited create(n,recorddef,true);
  4211. symtable:=p;
  4212. { we can own the symtable only if nobody else owns a copy so far }
  4213. if symtable.refcount=1 then
  4214. symtable.defowner:=self;
  4215. isunion:=false;
  4216. cloneddefderef.reset;
  4217. end;
  4218. constructor trecorddef.create_global_internal(n: string; packrecords, recordalignmin: shortint);
  4219. var
  4220. oldsymtablestack: tsymtablestack;
  4221. ts: ttypesym;
  4222. definedname: boolean;
  4223. begin
  4224. { construct name }
  4225. definedname:=n<>'';
  4226. if not definedname then
  4227. n:='$InternalRec'+tostr(current_module.deflist.count);
  4228. oldsymtablestack:=symtablestack;
  4229. { do not simply push/pop current_module.localsymtable, because
  4230. that can have side-effects (e.g., it removes helpers) }
  4231. symtablestack:=nil;
  4232. symtable:=trecordsymtable.create(n,packrecords,recordalignmin);
  4233. symtable.defowner:=self;
  4234. isunion:=false;
  4235. inherited create(n,recorddef,true);
  4236. { if we specified a name, then we'll probably want to look up the
  4237. type again by name too -> create typesym }
  4238. ts:=nil;
  4239. if definedname then
  4240. begin
  4241. ts:=ctypesym.create(n,self,true);
  4242. { avoid hints about unused types (these may only be used for
  4243. typed constant data) }
  4244. ts.increfcount;
  4245. end;
  4246. if assigned(current_module.localsymtable) then
  4247. begin
  4248. current_module.localsymtable.insertdef(self);
  4249. if definedname then
  4250. current_module.localsymtable.insert(ts);
  4251. end
  4252. else
  4253. begin
  4254. current_module.globalsymtable.insertdef(self);
  4255. if definedname then
  4256. current_module.globalsymtable.insert(ts);
  4257. end;
  4258. symtablestack:=oldsymtablestack;
  4259. { don't create RTTI for internal types, these are not exported }
  4260. defstates:=defstates+[ds_rtti_table_written,ds_init_table_written];
  4261. include(defoptions,df_internal);
  4262. end;
  4263. function trecorddef.add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
  4264. var
  4265. sym: tfieldvarsym;
  4266. name: TIDString;
  4267. pname: ^TIDString;
  4268. begin
  4269. if optionalname='' then
  4270. begin
  4271. name:='$f'+tostr(trecordsymtable(symtable).symlist.count);
  4272. pname:=@name
  4273. end
  4274. else
  4275. pname:=@optionalname;
  4276. sym:=cfieldvarsym.create(pname^,vs_value,def,[],true);
  4277. symtable.insert(sym);
  4278. trecordsymtable(symtable).addfield(sym,vis_hidden);
  4279. result:=sym;
  4280. end;
  4281. procedure trecorddef.add_fields_from_deflist(fieldtypes: tfplist);
  4282. var
  4283. i: longint;
  4284. begin
  4285. for i:=0 to fieldtypes.count-1 do
  4286. add_field_by_def('',tdef(fieldtypes[i]));
  4287. end;
  4288. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  4289. procedure readvariantrecdesc(var variantrecdesc : pvariantrecdesc);
  4290. var
  4291. i,j : longint;
  4292. begin
  4293. if ppufile.getbyte=1 then
  4294. begin
  4295. new(variantrecdesc);
  4296. ppufile.getderef(variantrecdesc^.variantselectorderef);
  4297. SetLength(variantrecdesc^.branches,ppufile.getasizeint);
  4298. for i:=0 to high(variantrecdesc^.branches) do
  4299. begin
  4300. SetLength(variantrecdesc^.branches[i].values,ppufile.getasizeint);
  4301. for j:=0 to high(variantrecdesc^.branches[i].values) do
  4302. variantrecdesc^.branches[i].values[j]:=ppufile.getexprint;
  4303. readvariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4304. end;
  4305. end
  4306. else
  4307. variantrecdesc:=nil;
  4308. end;
  4309. begin
  4310. inherited ppuload(recorddef,ppufile);
  4311. if df_copied_def in defoptions then
  4312. begin
  4313. ppufile.getderef(cloneddefderef);
  4314. ppuload_platform(ppufile);
  4315. end
  4316. else
  4317. begin
  4318. symtable:=trecordsymtable.create(objrealname^,0,0);
  4319. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  4320. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  4321. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  4322. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  4323. trecordsymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  4324. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  4325. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  4326. ppufile.getsmallset(trecordsymtable(symtable).managementoperators);
  4327. { position of ppuload_platform call must correspond
  4328. to position of writeentry in ppuwrite method }
  4329. ppuload_platform(ppufile);
  4330. trecordsymtable(symtable).ppuload(ppufile);
  4331. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  4332. but because iso mode supports no units, there is no need to store the variantrecdesc
  4333. in the ppu
  4334. }
  4335. // readvariantrecdesc(variantrecdesc);
  4336. { requires usefieldalignment to be set }
  4337. symtable.defowner:=self;
  4338. end;
  4339. isunion:=false;
  4340. end;
  4341. destructor trecorddef.destroy;
  4342. procedure free_variantrecdesc(var variantrecdesc : pvariantrecdesc);
  4343. var
  4344. i : longint;
  4345. begin
  4346. while assigned(variantrecdesc) do
  4347. begin
  4348. for i:=0 to high(variantrecdesc^.branches) do
  4349. begin
  4350. free_variantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4351. SetLength(variantrecdesc^.branches[i].values,0);
  4352. end;
  4353. SetLength(variantrecdesc^.branches,0);
  4354. dispose(variantrecdesc);
  4355. variantrecdesc:=nil;
  4356. end;
  4357. end;
  4358. begin
  4359. if assigned(variantrecdesc) then
  4360. free_variantrecdesc(variantrecdesc);
  4361. if assigned(symtable) then
  4362. begin
  4363. symtable.free;
  4364. symtable:=nil;
  4365. end;
  4366. inherited destroy;
  4367. end;
  4368. function trecorddef.getcopy : tstoreddef;
  4369. begin
  4370. result:=crecorddef.create(objrealname^,symtable.getcopy);
  4371. trecorddef(result).isunion:=isunion;
  4372. include(trecorddef(result).defoptions,df_copied_def);
  4373. if assigned(tcinitcode) then
  4374. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  4375. if assigned(import_lib) then
  4376. trecorddef(result).import_lib:=stringdup(import_lib^);
  4377. end;
  4378. function trecorddef.needs_inittable : boolean;
  4379. begin
  4380. { each record with managed field or with any management operator needs
  4381. init table }
  4382. needs_inittable:=(trecordsymtable(symtable).managementoperators<>[]) or
  4383. trecordsymtable(symtable).needs_init_final
  4384. end;
  4385. function trecorddef.needs_separate_initrtti : boolean;
  4386. begin
  4387. result:=true;
  4388. end;
  4389. function trecorddef.has_non_trivial_init_child(check_parent:boolean):boolean;
  4390. begin
  4391. result:=trecordsymtable(symtable).has_non_trivial_init;
  4392. end;
  4393. procedure trecorddef.buildderef;
  4394. begin
  4395. inherited buildderef;
  4396. if df_copied_def in defoptions then
  4397. cloneddefderef.build(symtable.defowner)
  4398. else
  4399. tstoredsymtable(symtable).buildderef;
  4400. end;
  4401. procedure trecorddef.deref;
  4402. begin
  4403. inherited deref;
  4404. { now dereference the definitions }
  4405. if df_copied_def in defoptions then
  4406. begin
  4407. cloneddef:=trecorddef(cloneddefderef.resolve);
  4408. symtable:=cloneddef.symtable.getcopy;
  4409. end
  4410. else
  4411. tstoredsymtable(symtable).deref(false);
  4412. { internal types, only load from the system unit }
  4413. if assigned(owner) and
  4414. assigned(owner.name) and
  4415. (owner.name^='SYSTEM') then
  4416. begin
  4417. { TGUID }
  4418. if not assigned(rec_tguid) and
  4419. (upper(typename)='TGUID') then
  4420. rec_tguid:=self
  4421. { JMP_BUF }
  4422. else if not assigned(rec_jmp_buf) and
  4423. (upper(typename)='JMP_BUF') then
  4424. rec_jmp_buf:=self
  4425. else if not assigned(rec_exceptaddr) and
  4426. (upper(typename)='TEXCEPTADDR') then
  4427. rec_exceptaddr:=self;
  4428. end;
  4429. end;
  4430. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  4431. procedure writevariantrecdesc(variantrecdesc : pvariantrecdesc);
  4432. var
  4433. i,j : longint;
  4434. begin
  4435. if assigned(variantrecdesc) then
  4436. begin
  4437. ppufile.putbyte(1);
  4438. ppufile.putderef(variantrecdesc^.variantselectorderef);
  4439. ppufile.putasizeint(length(variantrecdesc^.branches));
  4440. for i:=0 to high(variantrecdesc^.branches) do
  4441. begin
  4442. ppufile.putasizeint(length(variantrecdesc^.branches[i].values));
  4443. for j:=0 to high(variantrecdesc^.branches[i].values) do
  4444. ppufile.putexprint(variantrecdesc^.branches[i].values[j]);
  4445. writevariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4446. end;
  4447. end
  4448. else
  4449. ppufile.putbyte(0);
  4450. end;
  4451. begin
  4452. inherited ppuwrite(ppufile);
  4453. if df_copied_def in defoptions then
  4454. ppufile.putderef(cloneddefderef)
  4455. else
  4456. begin
  4457. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  4458. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  4459. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  4460. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  4461. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignmin));
  4462. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  4463. ppufile.putword(trecordsymtable(symtable).paddingsize);
  4464. ppufile.putsmallset(trecordsymtable(symtable).managementoperators);
  4465. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  4466. but because iso mode supports no units, there is no need to store the variantrecdesc
  4467. in the ppu
  4468. }
  4469. // writevariantrecdesc(variantrecdesc);
  4470. end;
  4471. writeentry(ppufile,ibrecorddef);
  4472. if not(df_copied_def in defoptions) then
  4473. trecordsymtable(symtable).ppuwrite(ppufile);
  4474. end;
  4475. function trecorddef.size:asizeint;
  4476. begin
  4477. result:=trecordsymtable(symtable).datasize;
  4478. end;
  4479. function trecorddef.alignment:shortint;
  4480. begin
  4481. alignment:=trecordsymtable(symtable).recordalignment;
  4482. end;
  4483. function trecorddef.padalignment:shortint;
  4484. begin
  4485. padalignment := trecordsymtable(symtable).padalignment;
  4486. end;
  4487. function trecorddef.GetTypeName : string;
  4488. begin
  4489. GetTypeName:='<record type>'
  4490. end;
  4491. {***************************************************************************
  4492. TABSTRACTPROCDEF
  4493. ***************************************************************************}
  4494. constructor tabstractprocdef.create(dt:tdeftyp;level:byte;doregister:boolean);
  4495. begin
  4496. inherited create(dt,doregister);
  4497. parast:=tparasymtable.create(self,level);
  4498. paras:=nil;
  4499. minparacount:=0;
  4500. maxparacount:=0;
  4501. proctypeoption:=potype_none;
  4502. proccalloption:=pocall_none;
  4503. procoptions:=[];
  4504. returndef:=voidtype;
  4505. returndefderef.reset;
  4506. savesize:=sizeof(pint);
  4507. callerargareasize:=0;
  4508. calleeargareasize:=0;
  4509. has_paraloc_info:=callnoside;
  4510. funcretloc[callerside].init;
  4511. funcretloc[calleeside].init;
  4512. check_mark_as_nested;
  4513. end;
  4514. destructor tabstractprocdef.destroy;
  4515. begin
  4516. if assigned(paras) then
  4517. begin
  4518. {$ifdef MEMDEBUG}
  4519. memprocpara.start;
  4520. {$endif MEMDEBUG}
  4521. paras.free;
  4522. paras:=nil;
  4523. {$ifdef MEMDEBUG}
  4524. memprocpara.stop;
  4525. {$endif MEMDEBUG}
  4526. end;
  4527. if assigned(parast) then
  4528. begin
  4529. {$ifdef MEMDEBUG}
  4530. memprocparast.start;
  4531. {$endif MEMDEBUG}
  4532. parast.free;
  4533. parast:=nil;
  4534. {$ifdef MEMDEBUG}
  4535. memprocparast.stop;
  4536. {$endif MEMDEBUG}
  4537. end;
  4538. funcretloc[callerside].done;
  4539. funcretloc[calleeside].done;
  4540. inherited destroy;
  4541. end;
  4542. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  4543. begin
  4544. if (tsym(p).typ<>paravarsym) then
  4545. exit;
  4546. inc(plongint(arg)^);
  4547. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  4548. begin
  4549. if not assigned(tparavarsym(p).defaultconstsym) then
  4550. inc(minparacount);
  4551. inc(maxparacount);
  4552. end;
  4553. end;
  4554. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  4555. begin
  4556. if (tsym(p).typ<>paravarsym) then
  4557. exit;
  4558. paras.add(p);
  4559. end;
  4560. procedure tabstractprocdef.calcparas;
  4561. var
  4562. paracount : longint;
  4563. begin
  4564. { This can already be assigned when
  4565. we need to reresolve this unit (PFV) }
  4566. if assigned(paras) then
  4567. paras.free;
  4568. paras:=tparalist.create(false);
  4569. paracount:=0;
  4570. minparacount:=0;
  4571. maxparacount:=0;
  4572. parast.SymList.ForEachCall(@count_para,@paracount);
  4573. paras.capacity:=paracount;
  4574. { Insert parameters in table }
  4575. parast.SymList.ForEachCall(@insert_para,nil);
  4576. { Order parameters }
  4577. paras.sortparas;
  4578. end;
  4579. function tabstractprocdef.mangledprocparanames(oldlen : longint) : string;
  4580. var
  4581. crc : dword;
  4582. hp : TParavarsym;
  4583. hs : TSymStr;
  4584. newlen,
  4585. i : integer;
  4586. begin
  4587. result:='';
  4588. hp:=nil;
  4589. { add parameter types }
  4590. for i:=0 to paras.count-1 do
  4591. begin
  4592. hp:=tparavarsym(paras[i]);
  4593. if not(vo_is_hidden_para in hp.varoptions) then
  4594. result:=result+'$'+hp.vardef.mangledparaname;
  4595. end;
  4596. { add resultdef, add $$ as separator to make it unique from a
  4597. parameter separator }
  4598. if not is_void(returndef) then
  4599. result:=result+'$$'+returndef.mangledparaname;
  4600. newlen:=length(result)+oldlen;
  4601. { Replace with CRC if the parameter line is very long }
  4602. if (newlen-oldlen>12) and
  4603. ((newlen>100) or (newlen-oldlen>64)) then
  4604. begin
  4605. crc:=0;
  4606. for i:=0 to paras.count-1 do
  4607. begin
  4608. hp:=tparavarsym(paras[i]);
  4609. if not(vo_is_hidden_para in hp.varoptions) then
  4610. begin
  4611. hs:=hp.vardef.mangledparaname;
  4612. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4613. end;
  4614. end;
  4615. if not is_void(returndef) then
  4616. begin
  4617. { add a little prefix so that x(integer; integer) is different from x(integer):integer }
  4618. hs:='$$'+returndef.mangledparaname;
  4619. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4620. end;
  4621. result:='$crc'+hexstr(crc,8);
  4622. end;
  4623. end;
  4624. procedure tabstractprocdef.buildderef;
  4625. begin
  4626. { released procdef? }
  4627. if not assigned(parast) then
  4628. exit;
  4629. inherited buildderef;
  4630. returndefderef.build(returndef);
  4631. if po_explicitparaloc in procoptions then
  4632. funcretloc[callerside].buildderef;
  4633. { parast }
  4634. tparasymtable(parast).buildderef;
  4635. end;
  4636. procedure tabstractprocdef.deref;
  4637. begin
  4638. inherited deref;
  4639. returndef:=tdef(returndefderef.resolve);
  4640. if po_explicitparaloc in procoptions then
  4641. begin
  4642. funcretloc[callerside].deref;
  4643. has_paraloc_info:=callerside;
  4644. end
  4645. else
  4646. begin
  4647. { deref is called after loading from a ppu, but also after another
  4648. unit has been reloaded/recompiled and all references must be
  4649. re-resolved. Since the funcretloc contains a reference to a tdef,
  4650. reset it so that we won't try to access the stale def }
  4651. funcretloc[callerside].init;
  4652. has_paraloc_info:=callnoside;
  4653. end;
  4654. { parast }
  4655. tparasymtable(parast).deref(false);
  4656. { recalculated parameters }
  4657. calcparas;
  4658. end;
  4659. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  4660. begin
  4661. inherited ppuload(dt,ppufile);
  4662. parast:=nil;
  4663. Paras:=nil;
  4664. minparacount:=0;
  4665. maxparacount:=0;
  4666. ppufile.getderef(returndefderef);
  4667. proctypeoption:=tproctypeoption(ppufile.getbyte);
  4668. proccalloption:=tproccalloption(ppufile.getbyte);
  4669. ppufile.getnormalset(procoptions);
  4670. funcretloc[callerside].init;
  4671. if po_explicitparaloc in procoptions then
  4672. funcretloc[callerside].ppuload(ppufile);
  4673. savesize:=sizeof(pint);
  4674. if (po_explicitparaloc in procoptions) then
  4675. has_paraloc_info:=callerside;
  4676. end;
  4677. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  4678. var
  4679. oldintfcrc : boolean;
  4680. begin
  4681. { released procdef? }
  4682. if not assigned(parast) then
  4683. exit;
  4684. inherited ppuwrite(ppufile);
  4685. ppufile.putderef(returndefderef);
  4686. oldintfcrc:=ppufile.do_interface_crc;
  4687. ppufile.do_interface_crc:=false;
  4688. ppufile.putbyte(ord(proctypeoption));
  4689. ppufile.putbyte(ord(proccalloption));
  4690. ppufile.putnormalset(procoptions);
  4691. ppufile.do_interface_crc:=oldintfcrc;
  4692. if (po_explicitparaloc in procoptions) then
  4693. funcretloc[callerside].ppuwrite(ppufile);
  4694. end;
  4695. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  4696. var
  4697. hs,s : ansistring;
  4698. hp : TParavarsym;
  4699. hpc : tconstsym;
  4700. first : boolean;
  4701. i,j : integer;
  4702. begin
  4703. s:='';
  4704. first:=true;
  4705. for i:=0 to paras.count-1 do
  4706. begin
  4707. hp:=tparavarsym(paras[i]);
  4708. if not(vo_is_hidden_para in hp.varoptions) or
  4709. (pno_showhidden in pno) then
  4710. begin
  4711. if first then
  4712. begin
  4713. s:=s+'(';
  4714. first:=false;
  4715. end
  4716. else
  4717. s:=s+';';
  4718. if vo_is_hidden_para in hp.varoptions then
  4719. s:=s+'<';
  4720. case hp.varspez of
  4721. vs_var :
  4722. s:=s+'var ';
  4723. vs_const :
  4724. s:=s+'const ';
  4725. vs_out :
  4726. s:=s+'out ';
  4727. vs_constref :
  4728. s:=s+'constref ';
  4729. else
  4730. ;
  4731. end;
  4732. if (pno_paranames in pno) then
  4733. s:=s+hp.realname+':';
  4734. if hp.univpara then
  4735. s:=s+'univ ';
  4736. if assigned(hp.vardef.typesym) then
  4737. begin
  4738. hs:=hp.vardef.typesym.realname;
  4739. if hs[1]<>'$' then
  4740. s:=s+hp.vardef.OwnerHierarchyName+hs
  4741. else
  4742. s:=s+hp.vardef.GetTypeName;
  4743. end
  4744. else
  4745. s:=s+hp.vardef.GetTypeName;
  4746. { default value }
  4747. if assigned(hp.defaultconstsym) then
  4748. begin
  4749. hpc:=tconstsym(hp.defaultconstsym);
  4750. hs:='';
  4751. case hpc.consttyp of
  4752. constwstring:
  4753. begin
  4754. if pcompilerwidestring(hpc.value.valueptr)^.len>0 then
  4755. begin
  4756. setlength(hs,pcompilerwidestring(hpc.value.valueptr)^.len);
  4757. for j:=0 to pcompilerwidestring(hpc.value.valueptr)^.len-1 do
  4758. begin
  4759. if (ord(pcompilerwidestring(hpc.value.valueptr)^.data[j])<127) and
  4760. not(byte(pcompilerwidestring(hpc.value.valueptr)^.data[j]) in [0,10,13]) then
  4761. hs[j+1]:=char(pcompilerwidestring(hpc.value.valueptr)^.data[j])
  4762. else
  4763. hs[j+1]:='.';
  4764. end;
  4765. end;
  4766. end;
  4767. conststring,
  4768. constresourcestring :
  4769. begin
  4770. if hpc.value.len>0 then
  4771. begin
  4772. setLength(hs,hpc.value.len);
  4773. { don't write past the end of hs if the constant
  4774. is > 255 chars }
  4775. move(hpc.value.valueptr^,hs[1],length(hs));
  4776. { make sure that constant strings with newline chars
  4777. don't create a linebreak in the assembler code,
  4778. since comments are line-based. Also remove nulls
  4779. because the comments are written as a pchar. }
  4780. ReplaceCase(hs,#0,'.');
  4781. ReplaceCase(hs,#10,'.');
  4782. ReplaceCase(hs,#13,'.');
  4783. end;
  4784. end;
  4785. constreal :
  4786. str(pbestreal(hpc.value.valueptr)^,hs);
  4787. constpointer :
  4788. hs:=tostr(hpc.value.valueordptr);
  4789. constord :
  4790. begin
  4791. if is_boolean(hpc.constdef) then
  4792. begin
  4793. if hpc.value.valueord<>0 then
  4794. hs:='TRUE'
  4795. else
  4796. hs:='FALSE';
  4797. end
  4798. else
  4799. hs:=tostr(hpc.value.valueord);
  4800. end;
  4801. constnil :
  4802. hs:='nil';
  4803. constset :
  4804. hs:='<set>';
  4805. constguid:
  4806. hs:=guid2string(pguid(hpc.value.valueptr)^);
  4807. constnone:
  4808. internalerror(2019050704);
  4809. end;
  4810. if hs<>'' then
  4811. s:=s+'=`'+hs+'`';
  4812. end;
  4813. if vo_is_hidden_para in hp.varoptions then
  4814. s:=s+'>';
  4815. end;
  4816. end;
  4817. if not first then
  4818. s:=s+')';
  4819. if (po_varargs in procoptions) then
  4820. s:=s+';VarArgs';
  4821. typename_paras:=s;
  4822. end;
  4823. function tabstractprocdef.is_methodpointer:boolean;
  4824. begin
  4825. result:=false;
  4826. end;
  4827. function tabstractprocdef.is_addressonly:boolean;
  4828. begin
  4829. result:=true;
  4830. end;
  4831. function tabstractprocdef.no_self_node: boolean;
  4832. begin
  4833. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  4834. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  4835. end;
  4836. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp; const paraprefix: string): tstoreddef;
  4837. var
  4838. j, nestinglevel: longint;
  4839. pvs, npvs: tparavarsym;
  4840. begin
  4841. nestinglevel:=parast.symtablelevel;
  4842. if newtyp=procdef then
  4843. begin
  4844. if (copytyp<>pc_bareproc) then
  4845. result:=cprocdef.create(nestinglevel,true)
  4846. else
  4847. result:=cprocdef.create(normal_function_level,true);
  4848. tprocdef(result).visibility:=vis_public;
  4849. end
  4850. else
  4851. begin
  4852. result:=cprocvardef.create(nestinglevel);
  4853. end;
  4854. tabstractprocdef(result).returndef:=returndef;
  4855. tabstractprocdef(result).returndefderef:=returndefderef;
  4856. pvs:=nil;
  4857. npvs:=nil;
  4858. for j:=0 to parast.symlist.count-1 do
  4859. begin
  4860. case tsym(parast.symlist[j]).typ of
  4861. paravarsym:
  4862. begin
  4863. pvs:=tparavarsym(parast.symlist[j]);
  4864. { in case of bare proc, don't copy self, vmt or framepointer
  4865. parameters }
  4866. if (copytyp in [pc_bareproc,pc_normal_no_hidden]) and
  4867. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result,vo_is_funcret]*pvs.varoptions)<>[]) then
  4868. continue;
  4869. if paraprefix='' then
  4870. npvs:=cparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  4871. pvs.vardef,pvs.varoptions)
  4872. else if not(vo_is_high_para in pvs.varoptions) then
  4873. npvs:=cparavarsym.create(paraprefix+pvs.realname,pvs.paranr,pvs.varspez,
  4874. pvs.vardef,pvs.varoptions)
  4875. else
  4876. npvs:=cparavarsym.create('$high'+paraprefix+copy(pvs.name,5,length(pvs.name)),pvs.paranr,pvs.varspez,
  4877. pvs.vardef,pvs.varoptions);
  4878. npvs.defaultconstsym:=pvs.defaultconstsym;
  4879. tabstractprocdef(result).parast.insert(npvs);
  4880. end;
  4881. constsym:
  4882. begin
  4883. // ignore, reuse original constym. Should also be duplicated
  4884. // be safe though
  4885. end;
  4886. symconst.typesym:
  4887. begin
  4888. // reuse original, part of generic declaration
  4889. end
  4890. else
  4891. internalerror(201160604);
  4892. end;
  4893. end;
  4894. tabstractprocdef(result).savesize:=savesize;
  4895. if (typ<>procvardef) and
  4896. (newtyp=procvardef) then
  4897. begin
  4898. { procvars can't be (class)constructures/destructors etc }
  4899. if proctypeoption=potype_constructor then
  4900. begin
  4901. tabstractprocdef(result).returndef:=tdef(owner.defowner);
  4902. if not(is_implicit_pointer_object_type(returndef) or
  4903. (returndef.typ<>objectdef)) then
  4904. tabstractprocdef(result).returndef:=cpointerdef.getreusable(tabstractprocdef(result).returndef);
  4905. tabstractprocdef(result).proctypeoption:=potype_function;
  4906. end
  4907. else if is_void(returndef) then
  4908. tabstractprocdef(result).proctypeoption:=potype_procedure
  4909. else
  4910. tabstractprocdef(result).proctypeoption:=potype_function;
  4911. end
  4912. else
  4913. tabstractprocdef(result).proctypeoption:=proctypeoption;
  4914. tabstractprocdef(result).proccalloption:=proccalloption;
  4915. tabstractprocdef(result).procoptions:=procoptions;
  4916. if (copytyp=pc_bareproc) then
  4917. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  4918. if newtyp=procvardef then
  4919. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  4920. if copytyp=pc_address_only then
  4921. include(tabstractprocdef(result).procoptions,po_addressonly);
  4922. tabstractprocdef(result).callerargareasize:=callerargareasize;
  4923. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  4924. tabstractprocdef(result).maxparacount:=maxparacount;
  4925. tabstractprocdef(result).minparacount:=minparacount;
  4926. if po_explicitparaloc in procoptions then
  4927. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  4928. { recalculate parameter info }
  4929. tabstractprocdef(result).has_paraloc_info:=callnoside;
  4930. {$ifdef m68k}
  4931. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  4932. {$endif}
  4933. if (typ=procdef) and
  4934. (newtyp=procvardef) and
  4935. (owner.symtabletype=ObjectSymtable) then
  4936. include(tprocvardef(result).procoptions,po_methodpointer);
  4937. end;
  4938. function tabstractprocdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  4939. begin
  4940. result:=is_addressonly;
  4941. end;
  4942. procedure tabstractprocdef.check_mark_as_nested;
  4943. begin
  4944. { nested procvars require that nested functions use the Delphi-style
  4945. nested procedure calling convention }
  4946. if (parast.symtablelevel>normal_function_level) and
  4947. (m_nested_procvars in current_settings.modeswitches) then
  4948. include(procoptions,po_delphi_nested_cc);
  4949. end;
  4950. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  4951. begin
  4952. if (side in [callerside,callbothsides]) and
  4953. not(has_paraloc_info in [callerside,callbothsides]) then
  4954. begin
  4955. if not is_c_variadic(self) then
  4956. callerargareasize:=paramanager.create_paraloc_info(self,callerside)
  4957. else
  4958. callerargareasize:=paramanager.create_varargs_paraloc_info(self,callerside,nil);
  4959. if has_paraloc_info in [calleeside,callbothsides] then
  4960. has_paraloc_info:=callbothsides
  4961. else
  4962. has_paraloc_info:=callerside;
  4963. end;
  4964. if (side in [calleeside,callbothsides]) and
  4965. not(has_paraloc_info in [calleeside,callbothsides]) then
  4966. begin
  4967. if not is_c_variadic(self) then
  4968. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside)
  4969. else
  4970. callerargareasize:=paramanager.create_varargs_paraloc_info(self,calleeside,nil);
  4971. if has_paraloc_info in [callerside,callbothsides] then
  4972. has_paraloc_info:=callbothsides
  4973. else
  4974. has_paraloc_info:=calleeside;
  4975. end;
  4976. end;
  4977. procedure tabstractprocdef.done_paraloc_info(side: tcallercallee);
  4978. var
  4979. i: longint;
  4980. begin
  4981. if (side in [callerside,callbothsides]) and
  4982. (has_paraloc_info in [callerside,callbothsides]) then
  4983. begin
  4984. funcretloc[callerside].done;
  4985. for i:=0 to paras.count-1 do
  4986. tparavarsym(paras[i]).paraloc[callerside].done;
  4987. if has_paraloc_info=callerside then
  4988. has_paraloc_info:=callnoside
  4989. else
  4990. has_paraloc_info:=calleeside;
  4991. end;
  4992. if (side in [calleeside,callbothsides]) and
  4993. (has_paraloc_info in [calleeside,callbothsides]) then
  4994. begin
  4995. funcretloc[calleeside].done;
  4996. for i:=0 to paras.count-1 do
  4997. tparavarsym(paras[i]).paraloc[calleeside].done;
  4998. if has_paraloc_info=calleeside then
  4999. has_paraloc_info:=callnoside
  5000. else
  5001. has_paraloc_info:=callerside;
  5002. end;
  5003. end;
  5004. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  5005. var
  5006. p: tparavarsym;
  5007. ploc: PCGParalocation;
  5008. i: longint;
  5009. begin
  5010. result:=false;
  5011. init_paraloc_info(side);
  5012. for i:=0 to parast.SymList.Count-1 do
  5013. if tsym(parast.SymList[i]).typ=paravarsym then
  5014. begin
  5015. p:=tparavarsym(parast.SymList[i]);
  5016. { check if no parameter is located on the stack }
  5017. if (is_open_array(p.vardef) or
  5018. is_array_of_const(p.vardef)) and (p.varspez=vs_value) then
  5019. begin
  5020. result:=true;
  5021. exit;
  5022. end;
  5023. ploc:=p.paraloc[side].location;
  5024. while assigned(ploc) do
  5025. begin
  5026. if (ploc^.loc=LOC_REFERENCE) then
  5027. begin
  5028. result:=true;
  5029. exit
  5030. end;
  5031. ploc:=ploc^.next;
  5032. end;
  5033. end;
  5034. end;
  5035. function tabstractprocdef.is_pushleftright: boolean;
  5036. begin
  5037. result:=false;
  5038. end;
  5039. function tabstractprocdef.address_type: tdef;
  5040. begin
  5041. result:=voidcodepointertype;
  5042. end;
  5043. function tabstractprocdef.ofs_address_type:tdef;
  5044. begin
  5045. result:=address_type;
  5046. end;
  5047. procedure tabstractprocdef.declared_far;
  5048. begin
  5049. Message1(parser_w_proc_directive_ignored,'FAR');
  5050. end;
  5051. procedure tabstractprocdef.declared_near;
  5052. begin
  5053. Message1(parser_w_proc_directive_ignored,'NEAR');
  5054. end;
  5055. {***************************************************************************
  5056. TPROCDEF
  5057. ***************************************************************************}
  5058. function tprocdef.store_localst: boolean;
  5059. begin
  5060. result:=has_inlininginfo or (df_generic in defoptions);
  5061. end;
  5062. function tprocdef.GetResultName: PShortString;
  5063. begin
  5064. if not assigned(implprocdefinfo) then
  5065. internalerror(2014010301);
  5066. result:=implprocdefinfo^.resultname;
  5067. end;
  5068. procedure tprocdef.SetResultName(AValue: PShortString);
  5069. begin
  5070. if not assigned(implprocdefinfo) then
  5071. internalerror(2014010302);
  5072. implprocdefinfo^.resultname:=AValue;
  5073. end;
  5074. function tprocdef.GetParentFPInitBlock: tnode;
  5075. begin
  5076. if not assigned(implprocdefinfo) then
  5077. internalerror(2014010303);
  5078. result:=implprocdefinfo^.parentfpinitblock;
  5079. end;
  5080. function tprocdef.GetParentFPStruct: tsym;
  5081. begin
  5082. if not assigned(implprocdefinfo) then
  5083. internalerror(2014010304);
  5084. result:=implprocdefinfo^.parentfpstruct;
  5085. end;
  5086. function tprocdef.GetParentFPStructPtrType: tdef;
  5087. begin
  5088. if not assigned(implprocdefinfo) then
  5089. internalerror(2014010305);
  5090. result:=implprocdefinfo^.parentfpstructptrtype;
  5091. end;
  5092. procedure tprocdef.SetParentFPInitBlock(AValue: tnode);
  5093. begin
  5094. if not assigned(implprocdefinfo) then
  5095. internalerror(2014010306);
  5096. implprocdefinfo^.parentfpinitblock:=AValue;
  5097. end;
  5098. function tprocdef.Getprocendtai: tai;
  5099. begin
  5100. if not assigned(implprocdefinfo) then
  5101. internalerror(2014010307);
  5102. result:=implprocdefinfo^.procendtai;
  5103. end;
  5104. function tprocdef.Getprocstarttai: tai;
  5105. begin
  5106. if not assigned(implprocdefinfo) then
  5107. internalerror(2014010308);
  5108. result:=implprocdefinfo^.procstarttai;
  5109. end;
  5110. procedure tprocdef.Setprocendtai(AValue: tai);
  5111. begin
  5112. if not assigned(implprocdefinfo) then
  5113. internalerror(2014010309);
  5114. implprocdefinfo^.procendtai:=AValue;
  5115. end;
  5116. function tprocdef.Getskpara: pointer;
  5117. begin
  5118. if not assigned(implprocdefinfo) then
  5119. internalerror(2014010310);
  5120. result:=implprocdefinfo^.skpara;
  5121. end;
  5122. procedure tprocdef.Setskpara(AValue: pointer);
  5123. begin
  5124. if not assigned(implprocdefinfo) then
  5125. internalerror(2014010311);
  5126. implprocdefinfo^.skpara:=AValue;
  5127. end;
  5128. function tprocdef.Getpersonality: tprocdef;
  5129. begin
  5130. if not assigned(implprocdefinfo) then
  5131. internalerror(2016121701);
  5132. result:=implprocdefinfo^.personality;
  5133. end;
  5134. procedure tprocdef.Setpersonality(AValue: tprocdef);
  5135. begin
  5136. if not assigned(implprocdefinfo) then
  5137. internalerror(2016121702);
  5138. implprocdefinfo^.personality:=AValue;
  5139. end;
  5140. function tprocdef.Getforwarddef: boolean;
  5141. begin
  5142. if not assigned(implprocdefinfo) then
  5143. internalerror(2014010312);
  5144. result:=implprocdefinfo^.forwarddef;
  5145. end;
  5146. function tprocdef.Gethasforward: boolean;
  5147. begin
  5148. if not assigned(implprocdefinfo) then
  5149. internalerror(2014010313);
  5150. result:=implprocdefinfo^.hasforward;
  5151. end;
  5152. function tprocdef.Getinterfacedef: boolean;
  5153. begin
  5154. if not assigned(implprocdefinfo) then
  5155. internalerror(2014010314);
  5156. result:=implprocdefinfo^.interfacedef;
  5157. end;
  5158. procedure tprocdef.Setforwarddef(AValue: boolean);
  5159. begin
  5160. if not assigned(implprocdefinfo) then
  5161. internalerror(2014010315);
  5162. implprocdefinfo^.forwarddef:=AValue;
  5163. end;
  5164. procedure tprocdef.Sethasforward(AValue: boolean);
  5165. begin
  5166. if not assigned(implprocdefinfo) then
  5167. internalerror(2014010316);
  5168. implprocdefinfo^.hasforward:=AValue;
  5169. end;
  5170. function tprocdef.GetIsEmpty: boolean;
  5171. begin
  5172. result:=pio_empty in implprocoptions;
  5173. end;
  5174. procedure tprocdef.SetIsEmpty(AValue: boolean);
  5175. begin
  5176. if AValue then
  5177. include(implprocoptions,pio_empty)
  5178. else
  5179. include(implprocoptions,pio_empty);
  5180. end;
  5181. function tprocdef.GetHasInliningInfo: boolean;
  5182. begin
  5183. result:=pio_has_inlininginfo in implprocoptions;
  5184. end;
  5185. procedure tprocdef.SetHasInliningInfo(AValue: boolean);
  5186. begin
  5187. if AValue then
  5188. include(implprocoptions,pio_has_inlininginfo)
  5189. else
  5190. exclude(implprocoptions,pio_has_inlininginfo);
  5191. end;
  5192. procedure tprocdef.Setinterfacedef(AValue: boolean);
  5193. begin
  5194. if not assigned(implprocdefinfo) then
  5195. internalerror(2014010317);
  5196. implprocdefinfo^.interfacedef:=AValue;
  5197. end;
  5198. procedure tprocdef.Setprocstarttai(AValue: tai);
  5199. begin
  5200. if not assigned(implprocdefinfo) then
  5201. internalerror(2014010318);
  5202. implprocdefinfo^.procstarttai:=AValue;
  5203. end;
  5204. procedure tprocdef.SetParentFPStruct(AValue: tsym);
  5205. begin
  5206. if not assigned(implprocdefinfo) then
  5207. internalerror(2014010319);
  5208. implprocdefinfo^.parentfpstruct:=AValue;
  5209. end;
  5210. procedure tprocdef.SetParentFPStructPtrType(AValue: tdef);
  5211. begin
  5212. if not assigned(implprocdefinfo) then
  5213. internalerror(2014010320);
  5214. implprocdefinfo^.parentfpstructptrtype:=AValue;
  5215. end;
  5216. constructor tprocdef.create(level:byte;doregister:boolean);
  5217. begin
  5218. inherited create(procdef,level,doregister);
  5219. implprocdefinfo:=allocmem(sizeof(implprocdefinfo^));
  5220. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  5221. {$ifdef symansistr}
  5222. _mangledname:='';
  5223. {$else symansistr}
  5224. _mangledname:=nil;
  5225. {$endif symansistr}
  5226. fileinfo:=current_filepos;
  5227. extnumber:=$ffff;
  5228. aliasnames:=TCmdStrList.create;
  5229. funcretsym:=nil;
  5230. funcretsymderef.reset;
  5231. procsymderef.reset;
  5232. forwarddef:=true;
  5233. interfacedef:=false;
  5234. hasforward:=false;
  5235. struct := nil;
  5236. structderef.reset;
  5237. import_dll:=nil;
  5238. import_name:=nil;
  5239. import_nr:=0;
  5240. inlininginfo:=nil;
  5241. deprecatedmsg:=nil;
  5242. end;
  5243. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  5244. var
  5245. i,aliasnamescount,sizeleft : longint;
  5246. level : byte;
  5247. buf : array[0..255] of byte;
  5248. begin
  5249. inherited ppuload(procdef,ppufile);
  5250. {$ifdef symansistr}
  5251. if po_has_mangledname in procoptions then
  5252. _mangledname:=ppufile.getansistring
  5253. else
  5254. _mangledname:='';
  5255. {$else symansistr}
  5256. if po_has_mangledname in procoptions then
  5257. _mangledname:=ppufile.getpshortstring
  5258. else
  5259. _mangledname:=nil;
  5260. {$endif symansistr}
  5261. extnumber:=ppufile.getword;
  5262. level:=ppufile.getbyte;
  5263. ppufile.getderef(structderef);
  5264. ppufile.getderef(procsymderef);
  5265. ppufile.getposinfo(fileinfo);
  5266. visibility:=tvisibility(ppufile.getbyte);
  5267. ppufile.getsmallset(symoptions);
  5268. if sp_has_deprecated_msg in symoptions then
  5269. deprecatedmsg:=ppufile.getpshortstring
  5270. else
  5271. deprecatedmsg:=nil;
  5272. { import stuff }
  5273. if po_has_importdll in procoptions then
  5274. import_dll:=ppufile.getpshortstring
  5275. else
  5276. import_dll:=nil;
  5277. if po_has_importname in procoptions then
  5278. import_name:=ppufile.getpshortstring
  5279. else
  5280. import_name:=nil;
  5281. import_nr:=ppufile.getword;
  5282. if (po_msgint in procoptions) then
  5283. messageinf.i:=ppufile.getlongint;
  5284. if (po_msgstr in procoptions) then
  5285. messageinf.str:=ppufile.getpshortstring;
  5286. if (po_dispid in procoptions) then
  5287. dispid:=ppufile.getlongint;
  5288. { inline stuff }
  5289. ppufile.getsmallset(implprocoptions);
  5290. if has_inlininginfo then
  5291. begin
  5292. ppufile.getderef(funcretsymderef);
  5293. new(inlininginfo);
  5294. ppufile.getsmallset(inlininginfo^.flags);
  5295. end
  5296. else
  5297. begin
  5298. inlininginfo:=nil;
  5299. funcretsym:=nil;
  5300. end;
  5301. aliasnames:=TCmdStrList.create;
  5302. { count alias names }
  5303. aliasnamescount:=ppufile.getbyte;
  5304. for i:=1 to aliasnamescount do
  5305. aliasnames.insert(ppufile.getstring);
  5306. { load the token stream containing the declaration }
  5307. sizeleft:=ppufile.getlongint;
  5308. if sizeleft>0 then
  5309. begin
  5310. init_genericdecl;
  5311. while sizeleft>0 do
  5312. begin
  5313. if sizeleft>sizeof(buf) then
  5314. i:=sizeof(buf)
  5315. else
  5316. i:=sizeleft;
  5317. ppufile.getdata(buf,i);
  5318. genericdecltokenbuf.write(buf,i);
  5319. dec(sizeleft,i);
  5320. end;
  5321. end;
  5322. ppuload_platform(ppufile);
  5323. { load para symtable }
  5324. parast:=tparasymtable.create(self,level);
  5325. tparasymtable(parast).ppuload(ppufile);
  5326. { load local symtable }
  5327. if store_localst then
  5328. begin
  5329. localst:=tlocalsymtable.create(self,level);
  5330. tlocalsymtable(localst).ppuload(ppufile);
  5331. end
  5332. else
  5333. localst:=nil;
  5334. { inline stuff }
  5335. if has_inlininginfo then
  5336. inlininginfo^.code:=ppuloadnodetree(ppufile);
  5337. { default values for no persistent data }
  5338. if (cs_link_deffile in current_settings.globalswitches) and
  5339. (tf_need_export in target_info.flags) and
  5340. (po_exports in procoptions) then
  5341. deffile.AddExport(mangledname);
  5342. { Disable po_has_inlining until the derefimpl is done }
  5343. has_inlininginfo:=false;
  5344. end;
  5345. destructor tprocdef.destroy;
  5346. begin
  5347. aliasnames.free;
  5348. aliasnames:=nil;
  5349. if assigned(localst) and
  5350. (localst.symtabletype<>staticsymtable) then
  5351. begin
  5352. {$ifdef MEMDEBUG}
  5353. memproclocalst.start;
  5354. {$endif MEMDEBUG}
  5355. localst.free;
  5356. localst:=nil;
  5357. {$ifdef MEMDEBUG}
  5358. memproclocalst.start;
  5359. {$endif MEMDEBUG}
  5360. end;
  5361. if assigned(inlininginfo) then
  5362. begin
  5363. {$ifdef MEMDEBUG}
  5364. memprocnodetree.start;
  5365. {$endif MEMDEBUG}
  5366. tnode(inlininginfo^.code).free;
  5367. {$ifdef MEMDEBUG}
  5368. memprocnodetree.start;
  5369. {$endif MEMDEBUG}
  5370. dispose(inlininginfo);
  5371. inlininginfo:=nil;
  5372. end;
  5373. freeimplprocdefinfo;
  5374. genericdecltokenbuf.free;
  5375. genericdecltokenbuf:=nil;
  5376. stringdispose(import_dll);
  5377. stringdispose(import_name);
  5378. stringdispose(deprecatedmsg);
  5379. if (po_msgstr in procoptions) then
  5380. stringdispose(messageinf.str);
  5381. {$ifndef symansistr}
  5382. if assigned(_mangledname) then
  5383. begin
  5384. {$ifdef MEMDEBUG}
  5385. memmanglednames.start;
  5386. {$endif MEMDEBUG}
  5387. stringdispose(_mangledname);
  5388. {$ifdef MEMDEBUG}
  5389. memmanglednames.stop;
  5390. {$endif MEMDEBUG}
  5391. end;
  5392. {$endif symansistr}
  5393. inherited destroy;
  5394. end;
  5395. procedure tprocdef.freeimplprocdefinfo;
  5396. begin
  5397. if assigned(implprocdefinfo) then
  5398. begin
  5399. stringdispose(implprocdefinfo^.resultname);
  5400. freemem(implprocdefinfo);
  5401. implprocdefinfo:=nil;
  5402. end;
  5403. end;
  5404. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  5405. var
  5406. oldintfcrc : boolean;
  5407. aliasnamescount,i,sizeleft : longint;
  5408. item : TCmdStrListItem;
  5409. buf : array[0..255] of byte;
  5410. begin
  5411. { released procdef? }
  5412. if not assigned(parast) then
  5413. exit;
  5414. inherited ppuwrite(ppufile);
  5415. {$ifdef symansistr}
  5416. if po_has_mangledname in procoptions then
  5417. ppufile.putansistring(_mangledname);
  5418. {$else symansistr}
  5419. if po_has_mangledname in procoptions then
  5420. ppufile.putstring(_mangledname^);
  5421. {$endif symansistr}
  5422. ppufile.putword(extnumber);
  5423. ppufile.putbyte(parast.symtablelevel);
  5424. ppufile.putderef(structderef);
  5425. ppufile.putderef(procsymderef);
  5426. ppufile.putposinfo(fileinfo);
  5427. ppufile.putbyte(byte(visibility));
  5428. ppufile.putsmallset(symoptions);
  5429. if sp_has_deprecated_msg in symoptions then
  5430. ppufile.putstring(deprecatedmsg^);
  5431. { import }
  5432. if po_has_importdll in procoptions then
  5433. ppufile.putstring(import_dll^);
  5434. if po_has_importname in procoptions then
  5435. ppufile.putstring(import_name^);
  5436. ppufile.putword(import_nr);
  5437. if (po_msgint in procoptions) then
  5438. ppufile.putlongint(messageinf.i);
  5439. if (po_msgstr in procoptions) then
  5440. ppufile.putstring(messageinf.str^);
  5441. if (po_dispid in procoptions) then
  5442. ppufile.putlongint(dispid);
  5443. { inline stuff }
  5444. oldintfcrc:=ppufile.do_crc;
  5445. ppufile.do_crc:=false;
  5446. ppufile.putsmallset(implprocoptions);
  5447. if has_inlininginfo then
  5448. begin
  5449. ppufile.putderef(funcretsymderef);
  5450. ppufile.putsmallset(inlininginfo^.flags);
  5451. end;
  5452. { count alias names }
  5453. aliasnamescount:=0;
  5454. item:=TCmdStrListItem(aliasnames.first);
  5455. while assigned(item) do
  5456. begin
  5457. inc(aliasnamescount);
  5458. item:=TCmdStrListItem(item.next);
  5459. end;
  5460. if aliasnamescount>255 then
  5461. internalerror(200711021);
  5462. ppufile.putbyte(aliasnamescount);
  5463. item:=TCmdStrListItem(aliasnames.first);
  5464. while assigned(item) do
  5465. begin
  5466. ppufile.putstring(item.str);
  5467. item:=TCmdStrListItem(item.next);
  5468. end;
  5469. ppufile.do_crc:=oldintfcrc;
  5470. { generic tokens for the declaration }
  5471. if assigned(genericdecltokenbuf) and (genericdecltokenbuf.size>0) then
  5472. begin
  5473. sizeleft:=genericdecltokenbuf.size;
  5474. genericdecltokenbuf.seek(0);
  5475. ppufile.putlongint(sizeleft);
  5476. while sizeleft>0 do
  5477. begin
  5478. if sizeleft>sizeof(buf) then
  5479. i:=sizeof(buf)
  5480. else
  5481. i:=sizeleft;
  5482. genericdecltokenbuf.read(buf,i);
  5483. ppufile.putdata(buf,i);
  5484. dec(sizeleft,i);
  5485. end;
  5486. end
  5487. else
  5488. ppufile.putlongint(0);
  5489. { write this entry }
  5490. writeentry(ppufile,ibprocdef);
  5491. { Save the para symtable, this is taken from the interface }
  5492. tparasymtable(parast).ppuwrite(ppufile);
  5493. { save localsymtable for inline procedures or when local
  5494. browser info is requested, this has no influence on the crc }
  5495. if store_localst and not ppufile.crc_only then
  5496. begin
  5497. oldintfcrc:=ppufile.do_crc;
  5498. ppufile.do_crc:=false;
  5499. tlocalsymtable(localst).ppuwrite(ppufile);
  5500. ppufile.do_crc:=oldintfcrc;
  5501. end;
  5502. { node tree for inlining }
  5503. oldintfcrc:=ppufile.do_crc;
  5504. ppufile.do_crc:=false;
  5505. if has_inlininginfo then
  5506. ppuwritenodetree(ppufile,inlininginfo^.code);
  5507. ppufile.do_crc:=oldintfcrc;
  5508. end;
  5509. function tprocdef.fullprocname(showhidden:boolean):string;
  5510. var
  5511. pno: tprocnameoptions;
  5512. begin
  5513. pno:=[];
  5514. if showhidden then
  5515. include(pno,pno_showhidden);
  5516. result:=customprocname(pno);
  5517. end;
  5518. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  5519. var
  5520. s, rn : ansistring;
  5521. t : ttoken;
  5522. syssym : tsyssym;
  5523. begin
  5524. {$ifdef EXTDEBUG}
  5525. include(pno,pno_showhidden);
  5526. {$endif EXTDEBUG}
  5527. s:='';
  5528. if proctypeoption=potype_operator then
  5529. begin
  5530. for t:=NOTOKEN to last_overloaded do
  5531. if procsym.realname='$'+overloaded_names[t] then
  5532. begin
  5533. s:='operator ';
  5534. if (pno_ownername in pno) and
  5535. assigned(struct) then
  5536. s:=s+struct.RttiName+'.';
  5537. s:=s+arraytokeninfo[t].str;
  5538. if not (pno_noparams in pno) then
  5539. s:=s+typename_paras(pno);
  5540. break;
  5541. end;
  5542. end
  5543. else
  5544. begin
  5545. if (po_classmethod in procoptions) and
  5546. not(pno_noclassmarker in pno) then
  5547. s:='class ';
  5548. case proctypeoption of
  5549. potype_constructor,
  5550. potype_class_constructor:
  5551. s:=s+'constructor ';
  5552. potype_class_destructor,
  5553. potype_destructor:
  5554. s:=s+'destructor ';
  5555. else
  5556. if (pno_proctypeoption in pno) then
  5557. begin
  5558. if assigned(returndef) and
  5559. not(is_void(returndef)) then
  5560. s:=s+'function '
  5561. else
  5562. s:=s+'procedure ';
  5563. end;
  5564. end;
  5565. if (pno_ownername in pno) and
  5566. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  5567. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  5568. if (po_compilerproc in procoptions) and (extnumber<>$ffff) then
  5569. begin
  5570. syssym:=tsyssym.find_by_number(extnumber);
  5571. if not assigned(syssym) then
  5572. internalerror(2016060305);
  5573. rn:=syssym.realname;
  5574. end
  5575. else
  5576. rn:=procsym.realname;
  5577. if (pno_noleadingdollar in pno) and
  5578. (rn[1]='$') then
  5579. delete(rn,1,1);
  5580. s:=s+rn;
  5581. if not (pno_noparams in pno) then
  5582. s:=s+typename_paras(pno);
  5583. end;
  5584. if not(proctypeoption in [potype_constructor,potype_destructor,
  5585. potype_class_constructor,potype_class_destructor]) and
  5586. assigned(returndef) and
  5587. not(is_void(returndef)) then
  5588. s:=s+':'+returndef.GetTypeName;
  5589. if assigned(owner) and (owner.symtabletype=localsymtable) then
  5590. s:=s+' is nested'
  5591. else if po_is_block in procoptions then
  5592. s:=s+' is block';
  5593. s:=s+';';
  5594. if po_far in procoptions then
  5595. s:=s+' far;';
  5596. { forced calling convention? }
  5597. if (po_hascallingconvention in procoptions) then
  5598. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  5599. if (po_staticmethod in procoptions) and
  5600. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  5601. s:=s+' Static;';
  5602. if pno_mangledname in pno then
  5603. s:=s+' -- mangled name: '+mangledname;
  5604. customprocname:=s;
  5605. end;
  5606. function tprocdef.is_methodpointer:boolean;
  5607. begin
  5608. { don't check assigned(_class), that's also the case for nested
  5609. procedures inside methods }
  5610. result:=(owner.symtabletype=ObjectSymtable)and not no_self_node;
  5611. end;
  5612. function tprocdef.is_addressonly:boolean;
  5613. begin
  5614. result:=assigned(owner) and
  5615. not is_methodpointer and
  5616. (not(m_nested_procvars in current_settings.modeswitches) or
  5617. not is_nested_pd(self));
  5618. end;
  5619. procedure tprocdef.make_external;
  5620. begin
  5621. include(procoptions,po_external);
  5622. forwarddef:=false;
  5623. end;
  5624. procedure tprocdef.init_genericdecl;
  5625. begin
  5626. if assigned(genericdecltokenbuf) then
  5627. internalerror(2015061901);
  5628. genericdecltokenbuf:=tdynamicarray.create(256);
  5629. end;
  5630. function tprocdef.getfuncretsyminfo(out ressym: tsym; out resdef: tdef): boolean;
  5631. begin
  5632. result:=false;
  5633. if proctypeoption=potype_constructor then
  5634. begin
  5635. result:=true;
  5636. ressym:=tsym(parast.Find('self'));
  5637. resdef:=tabstractnormalvarsym(ressym).vardef;
  5638. { and TP-style constructors return a pointer to self }
  5639. if is_object(resdef) then
  5640. resdef:=cpointerdef.getreusable(resdef);
  5641. end
  5642. else if not is_void(returndef) then
  5643. begin
  5644. result:=true;
  5645. ressym:=funcretsym;
  5646. resdef:=tabstractnormalvarsym(ressym).vardef;
  5647. end;
  5648. end;
  5649. function tprocdef.has_alias_name(const s: TSymStr): boolean;
  5650. var
  5651. item : TCmdStrListItem;
  5652. begin
  5653. result:=true;
  5654. if mangledname=s then
  5655. exit;
  5656. item:=TCmdStrListItem(aliasnames.first);
  5657. while assigned(item) do
  5658. begin
  5659. if item.str=s then
  5660. exit;
  5661. item:=TCmdStrListItem(item.next);
  5662. end;
  5663. result:=false;
  5664. end;
  5665. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  5666. begin
  5667. case t of
  5668. gs_local :
  5669. GetSymtable:=localst;
  5670. gs_para :
  5671. GetSymtable:=parast;
  5672. else
  5673. GetSymtable:=nil;
  5674. end;
  5675. end;
  5676. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string): tstoreddef;
  5677. var
  5678. j : longint;
  5679. begin
  5680. result:=inherited;
  5681. if newtyp=procvardef then
  5682. begin
  5683. { create new paralist }
  5684. tprocvardef(result).calcparas;
  5685. exit;
  5686. end;
  5687. { don't copy mangled name, can be different }
  5688. tprocdef(result).messageinf:=messageinf;
  5689. tprocdef(result).dispid:=dispid;
  5690. if po_msgstr in procoptions then
  5691. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  5692. tprocdef(result).symoptions:=symoptions;
  5693. if assigned(deprecatedmsg) then
  5694. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  5695. { will have to be associated with appropriate procsym }
  5696. tprocdef(result).procsym:=nil;
  5697. { don't create aliases for bare copies, nor copy the funcretsym as
  5698. the function result parameter will be inserted again if necessary
  5699. (e.g. if the calling convention is changed) }
  5700. if not(copytyp in [pc_bareproc,pc_normal_no_hidden]) then
  5701. begin
  5702. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  5703. if assigned(funcretsym) then
  5704. begin
  5705. if funcretsym.owner=parast then
  5706. begin
  5707. j:=parast.symlist.indexof(funcretsym);
  5708. if j<0 then
  5709. internalerror(2011040606);
  5710. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  5711. end
  5712. else if funcretsym.owner=localst then
  5713. begin
  5714. { nothing to do, will be inserted for the new procdef while
  5715. parsing its body (by pdecsub.insert_funcret_local) }
  5716. end
  5717. else
  5718. internalerror(2011040605);
  5719. end;
  5720. end;
  5721. { will have to be associated with a new struct }
  5722. tprocdef(result).struct:=nil;
  5723. if assigned(implprocdefinfo) then
  5724. begin
  5725. if assigned(resultname) then
  5726. tprocdef(result).resultname:=stringdup(resultname^);
  5727. tprocdef(result).synthetickind:=synthetickind;
  5728. end;
  5729. if assigned(import_dll) then
  5730. tprocdef(result).import_dll:=stringdup(import_dll^);
  5731. if assigned(import_name) then
  5732. tprocdef(result).import_name:=stringdup(import_name^);
  5733. tprocdef(result).import_nr:=import_nr;
  5734. tprocdef(result).extnumber:=$ffff;
  5735. tprocdef(result).visibility:=visibility;
  5736. { we need a separate implementation for the copied def }
  5737. tprocdef(result).forwarddef:=true;
  5738. tprocdef(result).interfacedef:=true;
  5739. { create new paralist }
  5740. tprocdef(result).calcparas;
  5741. end;
  5742. function tprocdef.getcopy: tstoreddef;
  5743. begin
  5744. result:=getcopyas(procdef,pc_normal,'');
  5745. end;
  5746. procedure tprocdef.buildderef;
  5747. begin
  5748. inherited buildderef;
  5749. structderef.build(struct);
  5750. { procsym that originaly defined this definition, should be in the
  5751. same symtable }
  5752. procsymderef.build(procsym);
  5753. end;
  5754. procedure tprocdef.buildderefimpl;
  5755. begin
  5756. inherited buildderefimpl;
  5757. { Localst is not available for main/unit init }
  5758. if store_localst and assigned(localst) then
  5759. begin
  5760. tlocalsymtable(localst).buildderef;
  5761. tlocalsymtable(localst).buildderefimpl;
  5762. end;
  5763. { inline tree }
  5764. if has_inlininginfo then
  5765. begin
  5766. funcretsymderef.build(funcretsym);
  5767. inlininginfo^.code.buildderefimpl;
  5768. end;
  5769. end;
  5770. procedure tprocdef.deref;
  5771. begin
  5772. inherited deref;
  5773. struct:=tabstractrecorddef(structderef.resolve);
  5774. { procsym that originaly defined this definition, should be in the
  5775. same symtable }
  5776. procsym:=tprocsym(procsymderef.resolve);
  5777. end;
  5778. procedure tprocdef.derefimpl;
  5779. begin
  5780. { Enable has_inlininginfo when the inlininginfo
  5781. structure is available. The has_inlininginfo was disabled
  5782. after the load, since the data was invalid }
  5783. if assigned(inlininginfo) then
  5784. has_inlininginfo:=true;
  5785. { Locals }
  5786. if store_localst and assigned(localst) then
  5787. begin
  5788. tlocalsymtable(localst).deref(false);
  5789. tlocalsymtable(localst).derefimpl(false);
  5790. end;
  5791. { Inline }
  5792. if has_inlininginfo then
  5793. begin
  5794. inlininginfo^.code.derefimpl;
  5795. { funcretsym, this is always located in the localst }
  5796. funcretsym:=tsym(funcretsymderef.resolve);
  5797. end
  5798. else
  5799. begin
  5800. { safety }
  5801. { Not safe! A unit may be reresolved after its interface has been
  5802. parsed but before its implementation has been parsed, and in that
  5803. case the funcretsym is still required!
  5804. funcretsym:=nil; }
  5805. end;
  5806. end;
  5807. function tprocdef.GetTypeName : string;
  5808. begin
  5809. GetTypeName := FullProcName(false);
  5810. end;
  5811. function tprocdef.mangledname : TSymStr;
  5812. begin
  5813. {$ifdef symansistr}
  5814. if _mangledname='' then
  5815. begin
  5816. result:=defaultmangledname;
  5817. _mangledname:=result;
  5818. end
  5819. else
  5820. result:=_mangledname;
  5821. {$else symansistr}
  5822. if not assigned(_mangledname) then
  5823. begin
  5824. result:=defaultmangledname;
  5825. _mangledname:=stringdup(mangledname);
  5826. end
  5827. else
  5828. result:=_mangledname^;
  5829. {$endif symansistr}
  5830. end;
  5831. function tprocdef.defaultmangledname: TSymStr;
  5832. begin
  5833. { we need to use the symtable where the procsym is inserted,
  5834. because that is visible to the world }
  5835. defaultmangledname:=make_mangledname('',procsym.owner,procsym.name);
  5836. defaultmangledname:=defaultmangledname+mangledprocparanames(Length(defaultmangledname))
  5837. end;
  5838. function tprocdef.cplusplusmangledname : TSymStr;
  5839. function getcppparaname(p : tdef) : TSymStr;
  5840. const
  5841. {$ifdef NAMEMANGLING_GCC2}
  5842. ordtype2str : array[tordtype] of string[2] = (
  5843. '',
  5844. 'Uc','Us','Ui','Us','',
  5845. 'Sc','s','i','x','',
  5846. 'b','b','b','b','b','b',
  5847. 'c','w','x');
  5848. {$else NAMEMANGLING_GCC2}
  5849. ordtype2str : array[tordtype] of string[1] = (
  5850. 'v',
  5851. 'h','t','j','y','',
  5852. 'a','s','i','x','',
  5853. 'b','b','b','b','b',
  5854. 'b','b','b','b',
  5855. 'c','w','x','C');
  5856. floattype2str : array[tfloattype] of string[1] = (
  5857. 'f','d','e','e',
  5858. 'd','d','g');
  5859. {$endif NAMEMANGLING_GCC2}
  5860. var
  5861. s : TSymStr;
  5862. begin
  5863. case p.typ of
  5864. orddef:
  5865. begin
  5866. s:=ordtype2str[torddef(p).ordtype];
  5867. if s='C' then
  5868. s:=ordtype2str[range_to_basetype(torddef(p).low,torddef(p).high)];
  5869. end;
  5870. pointerdef:
  5871. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  5872. {$ifndef NAMEMANGLING_GCC2}
  5873. floatdef:
  5874. s:=floattype2str[tfloatdef(p).floattype];
  5875. {$endif NAMEMANGLING_GCC2}
  5876. else
  5877. internalerror(2103001);
  5878. end;
  5879. getcppparaname:=s;
  5880. end;
  5881. var
  5882. s,s2 : TSymStr;
  5883. hp : TParavarsym;
  5884. i : integer;
  5885. begin
  5886. {$ifdef NAMEMANGLING_GCC2}
  5887. { outdated gcc 2.x name mangling scheme }
  5888. s := procsym.realname;
  5889. if procsym.owner.symtabletype=ObjectSymtable then
  5890. begin
  5891. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  5892. case proctypeoption of
  5893. potype_destructor:
  5894. s:='_$_'+tostr(length(s2))+s2;
  5895. potype_constructor:
  5896. s:='___'+tostr(length(s2))+s2;
  5897. else
  5898. s:='_'+s+'__'+tostr(length(s2))+s2;
  5899. end;
  5900. end
  5901. else s:=s+'__';
  5902. s:=s+'F';
  5903. { concat modifiers }
  5904. { !!!!! }
  5905. { now we handle the parameters }
  5906. if maxparacount>0 then
  5907. begin
  5908. for i:=0 to paras.count-1 do
  5909. begin
  5910. hp:=tparavarsym(paras[i]);
  5911. { no hidden parameters form part of a C++ mangled name:
  5912. a) self is not included
  5913. b) there are no "high" or other hidden parameters
  5914. }
  5915. if vo_is_hidden_para in hp.varoptions then
  5916. continue;
  5917. s2:=getcppparaname(hp.vardef);
  5918. if hp.varspez in [vs_var,vs_out] then
  5919. s2:='R'+s2;
  5920. s:=s+s2;
  5921. end;
  5922. end
  5923. else
  5924. s:=s+'v';
  5925. cplusplusmangledname:=s;
  5926. {$else NAMEMANGLING_GCC2}
  5927. { gcc 3.x and 4.x name mangling scheme }
  5928. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  5929. if procsym.owner.symtabletype=ObjectSymtable then
  5930. begin
  5931. s:='_ZN';
  5932. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  5933. s:=s+tostr(length(s2))+s2;
  5934. case proctypeoption of
  5935. potype_constructor:
  5936. s:=s+'C1';
  5937. potype_destructor:
  5938. s:=s+'D1';
  5939. else
  5940. s:=s+tostr(length(procsym.realname))+procsym.realname;
  5941. end;
  5942. s:=s+'E';
  5943. end
  5944. else
  5945. s:=procsym.realname;
  5946. { now we handle the parameters }
  5947. if maxparacount>0 then
  5948. begin
  5949. for i:=0 to paras.count-1 do
  5950. begin
  5951. hp:=tparavarsym(paras[i]);
  5952. { no hidden parameters form part of a C++ mangled name:
  5953. a) self is not included
  5954. b) there are no "high" or other hidden parameters
  5955. }
  5956. if vo_is_hidden_para in hp.varoptions then
  5957. continue;
  5958. s2:=getcppparaname(hp.vardef);
  5959. if hp.varspez in [vs_var,vs_out] then
  5960. s2:='R'+s2;
  5961. s:=s+s2;
  5962. end;
  5963. end
  5964. else
  5965. s:=s+'v';
  5966. cplusplusmangledname:=s;
  5967. {$endif NAMEMANGLING_GCC2}
  5968. end;
  5969. function tprocdef.objcmangledname : TSymStr;
  5970. var
  5971. manglednamelen: longint;
  5972. iscatmethod : boolean;
  5973. begin
  5974. if not (po_msgstr in procoptions) then
  5975. internalerror(2009030901);
  5976. { we may very well need longer strings to handle these... }
  5977. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  5978. length('+"[ ]"')+length(messageinf.str^);
  5979. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  5980. if (iscatmethod) then
  5981. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  5982. if manglednamelen>255 then
  5983. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  5984. if not(po_classmethod in procoptions) then
  5985. result:='"-['
  5986. else
  5987. result:='"+[';
  5988. { quotes are necessary because the +/- otherwise confuse the assembler
  5989. into expecting a number
  5990. }
  5991. if iscatmethod then
  5992. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  5993. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  5994. if iscatmethod then
  5995. result:=result+')';
  5996. result:=result+' '+messageinf.str^+']"';
  5997. end;
  5998. procedure tprocdef.setmangledname(const s : TSymStr);
  5999. begin
  6000. { This is not allowed anymore, the forward declaration
  6001. already needs to create the correct mangledname, no changes
  6002. afterwards are allowed (PFV) }
  6003. { Exception: interface definitions in mode macpas, since in that }
  6004. { case no reference to the old name can exist yet (JM) }
  6005. {$ifdef symansistr}
  6006. if _mangledname<>'' then
  6007. if ((m_mac in current_settings.modeswitches) and
  6008. (interfacedef)) then
  6009. _mangledname:=''
  6010. else
  6011. internalerror(200411171);
  6012. {$else symansistr}
  6013. if assigned(_mangledname) then
  6014. if ((m_mac in current_settings.modeswitches) and
  6015. (interfacedef)) then
  6016. stringdispose(_mangledname)
  6017. else
  6018. internalerror(200411171);
  6019. {$endif symansistr}
  6020. {$ifdef jvm}
  6021. { this routine can be called for compilerproces. can't set mangled
  6022. name since it must be calculated, but it uses import_name when set
  6023. -> set that one }
  6024. import_name:=stringdup(s);
  6025. include(procoptions,po_has_importname);
  6026. include(procoptions,po_has_mangledname);
  6027. {$else}
  6028. {$ifdef symansistr}
  6029. _mangledname:=s;
  6030. {$else symansistr}
  6031. _mangledname:=stringdup(s);
  6032. {$endif symansistr}
  6033. {$endif jvm}
  6034. include(procoptions,po_has_mangledname);
  6035. end;
  6036. function tprocdef.needsglobalasmsym: boolean;
  6037. begin
  6038. result:=
  6039. (cs_profile in current_settings.moduleswitches) or
  6040. { smart linking using a library requires to promote
  6041. all non-nested procedures to AB_GLOBAL
  6042. otherwise you get undefined symbol error at linking
  6043. for msdos target with -CX option for instance }
  6044. (create_smartlink_library and not is_nested_pd(self)) or
  6045. (po_global in procoptions);
  6046. end;
  6047. procedure tprocdef.setcompilerprocname;
  6048. begin
  6049. procsym.realname:='$'+lower(procsym.name);
  6050. end;
  6051. {***************************************************************************
  6052. TPROCVARDEF
  6053. ***************************************************************************}
  6054. constructor tprocvardef.create(level:byte);
  6055. begin
  6056. inherited create(procvardef,level,true);
  6057. end;
  6058. class function tprocvardef.getreusableprocaddr(def: tabstractprocdef): tprocvardef;
  6059. var
  6060. res: PHashSetItem;
  6061. oldsymtablestack: tsymtablestack;
  6062. begin
  6063. if not assigned(current_module) then
  6064. internalerror(2011081301);
  6065. res:=current_module.procaddrdefs.FindOrAdd(@def,sizeof(def));
  6066. if not assigned(res^.Data) then
  6067. begin
  6068. { since these pointerdefs can be reused anywhere in the current
  6069. unit, add them to the global/staticsymtable (or local symtable
  6070. if they're a local def, because otherwise they'll be saved
  6071. to the ppu referencing a local symtable entry that doesn't
  6072. exist in the ppu) }
  6073. oldsymtablestack:=symtablestack;
  6074. { do not simply push/pop current_module.localsymtable, because
  6075. that can have side-effects (e.g., it removes helpers) }
  6076. symtablestack:=nil;
  6077. result:=tprocvardef(def.getcopyas(procvardef,pc_address_only,''));
  6078. setup_reusable_def(def,result,res,oldsymtablestack);
  6079. { res^.Data may still be nil -> don't overwrite result }
  6080. exit;
  6081. end;
  6082. result:=tprocvardef(res^.Data);
  6083. end;
  6084. class function tprocvardef.getreusableprocaddr_no_free(def: tabstractprocdef): tprocvardef;
  6085. begin
  6086. result:=getreusableprocaddr(def);
  6087. if not result.is_registered then
  6088. include(result.defoptions,df_not_registered_no_free);
  6089. end;
  6090. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  6091. begin
  6092. inherited ppuload(procvardef,ppufile);
  6093. { load para symtable }
  6094. parast:=tparasymtable.create(self,ppufile.getbyte);
  6095. ppuload_platform(ppufile);
  6096. tparasymtable(parast).ppuload(ppufile);
  6097. end;
  6098. function tprocvardef.getcopy : tstoreddef;
  6099. var
  6100. i : tcallercallee;
  6101. j : longint;
  6102. begin
  6103. result:=cprocvardef.create(parast.symtablelevel);
  6104. tprocvardef(result).returndef:=returndef;
  6105. tprocvardef(result).returndefderef:=returndefderef;
  6106. tprocvardef(result).parast:=parast.getcopy;
  6107. tprocvardef(result).savesize:=savesize;
  6108. { create paralist copy }
  6109. calcparas;
  6110. tprocvardef(result).paras:=tparalist.create(false);
  6111. tprocvardef(result).paras.count:=paras.count;
  6112. for j:=0 to paras.count-1 do
  6113. tprocvardef(result).paras[j]:=paras[j];
  6114. tprocvardef(result).proctypeoption:=proctypeoption;
  6115. tprocvardef(result).proccalloption:=proccalloption;
  6116. tprocvardef(result).procoptions:=procoptions;
  6117. tprocvardef(result).callerargareasize:=callerargareasize;
  6118. tprocvardef(result).calleeargareasize:=calleeargareasize;
  6119. tprocvardef(result).maxparacount:=maxparacount;
  6120. tprocvardef(result).minparacount:=minparacount;
  6121. for i:=low(funcretloc) to high(funcretloc) do
  6122. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  6123. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  6124. {$ifdef m68k}
  6125. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  6126. {$endif}
  6127. end;
  6128. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  6129. begin
  6130. inherited ppuwrite(ppufile);
  6131. { Save the para symtable level (necessary to distinguish nested
  6132. procvars) }
  6133. ppufile.putbyte(parast.symtablelevel);
  6134. { Write this entry }
  6135. writeentry(ppufile,ibprocvardef);
  6136. { Save the para symtable, this is taken from the interface }
  6137. tparasymtable(parast).ppuwrite(ppufile);
  6138. end;
  6139. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  6140. begin
  6141. case t of
  6142. gs_para :
  6143. GetSymtable:=parast;
  6144. else
  6145. GetSymtable:=nil;
  6146. end;
  6147. end;
  6148. function tprocvardef.size : asizeint;
  6149. begin
  6150. { we return false for is_addressonly for a block (because it's not a
  6151. simple pointer to a function), but they are handled as implicit
  6152. pointers to a datastructure that contains everything ->
  6153. voidpointertype.size instead of voidcodepointertype.size }
  6154. if po_is_block in procoptions then
  6155. size:=voidpointertype.size
  6156. else if not is_addressonly then
  6157. begin
  6158. if is_nested_pd(self) then
  6159. size:=voidcodepointertype.size+parentfpvoidpointertype.size
  6160. else
  6161. size:=voidcodepointertype.size+voidpointertype.size;
  6162. end
  6163. else
  6164. size:=voidcodepointertype.size;
  6165. end;
  6166. function tprocvardef.is_methodpointer:boolean;
  6167. begin
  6168. result:=(po_methodpointer in procoptions);
  6169. end;
  6170. function tprocvardef.is_addressonly:boolean;
  6171. begin
  6172. result:=((not(po_methodpointer in procoptions) or (po_staticmethod in procoptions)) and
  6173. not(po_is_block in procoptions) and
  6174. not is_nested_pd(self)) or
  6175. (po_addressonly in procoptions);
  6176. end;
  6177. function tprocvardef.getmangledparaname:TSymStr;
  6178. begin
  6179. if not(po_methodpointer in procoptions) then
  6180. if not is_nested_pd(self) then
  6181. result:='procvar'
  6182. else
  6183. { we need the manglednames here, because nestedprocvars can be anonymous, e.g.
  6184. having not a type name or not an unique one, see webtbs/tw27515.pp
  6185. Further, use $_ ... _$ delimiters to avoid ambiguous names, see webtbs/tw27515.pp }
  6186. result:='$_nestedprovar'+mangledprocparanames(0)+'_$'
  6187. else
  6188. result:='procvarofobj'
  6189. end;
  6190. function tprocvardef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string): tstoreddef;
  6191. begin
  6192. result:=inherited;
  6193. tabstractprocdef(result).calcparas;
  6194. end;
  6195. function tprocvardef.is_publishable : boolean;
  6196. begin
  6197. is_publishable:=(po_methodpointer in procoptions);
  6198. end;
  6199. function tprocvardef.GetTypeName : string;
  6200. var
  6201. s: string;
  6202. pno : tprocnameoptions;
  6203. begin
  6204. {$ifdef EXTDEBUG}
  6205. pno:=[pno_showhidden];
  6206. {$else EXTDEBUG}
  6207. pno:=[];
  6208. {$endif EXTDEBUG}
  6209. s:='<';
  6210. if po_is_block in procoptions then
  6211. s := s+'reference to'
  6212. else if po_classmethod in procoptions then
  6213. s := s+'class method type of'
  6214. else
  6215. if po_addressonly in procoptions then
  6216. s := s+'address of'
  6217. else
  6218. s := s+'procedure variable type of';
  6219. if assigned(returndef) and
  6220. (returndef<>voidtype) then
  6221. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  6222. else
  6223. s:=s+' procedure'+typename_paras(pno);
  6224. if po_methodpointer in procoptions then
  6225. s := s+' of object';
  6226. if is_nested_pd(self) then
  6227. s := s+' is nested';
  6228. if po_far in procoptions then
  6229. s := s+';far';
  6230. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  6231. end;
  6232. {***************************************************************************
  6233. TOBJECTDEF
  6234. ***************************************************************************}
  6235. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef;doregister:boolean);
  6236. begin
  6237. inherited create(n,objectdef,doregister);
  6238. fcurrent_dispid:=0;
  6239. objecttype:=ot;
  6240. childof:=nil;
  6241. childofderef.reset;
  6242. vmt_fieldderef.reset;
  6243. extendeddefderef.reset;
  6244. cloneddefderef.reset;
  6245. if objecttype=odt_helper then
  6246. owner.includeoption(sto_has_helper);
  6247. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords,
  6248. current_settings.alignment.recordalignmin);
  6249. { create space for vmt !! }
  6250. vmtentries:=TFPList.Create;
  6251. set_parent(c);
  6252. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  6253. prepareguid;
  6254. { setup implemented interfaces }
  6255. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  6256. ImplementedInterfaces:=TFPObjectList.Create(true)
  6257. else
  6258. ImplementedInterfaces:=nil;
  6259. writing_class_record_dbginfo:=false;
  6260. end;
  6261. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  6262. var
  6263. i,
  6264. implintfcount : longint;
  6265. d, getterd : tderef;
  6266. ImplIntf : TImplementedInterface;
  6267. vmtentry : pvmtentry;
  6268. begin
  6269. inherited ppuload(objectdef,ppufile);
  6270. objecttype:=tobjecttyp(ppufile.getbyte);
  6271. helpertype:=thelpertype(ppufile.getbyte);
  6272. objextname:=ppufile.getpshortstring;
  6273. { only used for external Objective-C classes/protocols }
  6274. if (objextname^='') then
  6275. stringdispose(objextname);
  6276. symtable:=tObjectSymtable.create(self,objrealname^,0,0);
  6277. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  6278. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  6279. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  6280. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  6281. tObjectSymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  6282. ppufile.getderef(vmt_fieldderef);
  6283. ppufile.getderef(childofderef);
  6284. { load guid }
  6285. iidstr:=nil;
  6286. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  6287. begin
  6288. new(iidguid);
  6289. ppufile.getguid(iidguid^);
  6290. iidstr:=ppufile.getpshortstring;
  6291. end;
  6292. abstractcnt:=ppufile.getlongint;
  6293. if objecttype=odt_helper then
  6294. ppufile.getderef(extendeddefderef);
  6295. vmtentries:=TFPList.Create;
  6296. vmtentries.count:=ppufile.getlongint;
  6297. for i:=0 to vmtentries.count-1 do
  6298. begin
  6299. ppufile.getderef(d);
  6300. new(vmtentry);
  6301. vmtentry^.procdef:=nil;
  6302. vmtentry^.procdefderef:=d;
  6303. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  6304. vmtentries[i]:=vmtentry;
  6305. end;
  6306. { load implemented interfaces }
  6307. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  6308. begin
  6309. ImplementedInterfaces:=TFPObjectList.Create(true);
  6310. implintfcount:=ppufile.getlongint;
  6311. for i:=0 to implintfcount-1 do
  6312. begin
  6313. ppufile.getderef(d);
  6314. ppufile.getderef(getterd);
  6315. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  6316. ImplIntf.IOffset:=ppufile.getlongint;
  6317. byte(ImplIntf.IType):=ppufile.getbyte;
  6318. ImplementedInterfaces.Add(ImplIntf);
  6319. end;
  6320. end
  6321. else
  6322. ImplementedInterfaces:=nil;
  6323. if df_copied_def in defoptions then
  6324. begin
  6325. ppufile.getderef(cloneddefderef);
  6326. ppuload_platform(ppufile);
  6327. end
  6328. else
  6329. begin
  6330. ppuload_platform(ppufile);
  6331. tObjectSymtable(symtable).ppuload(ppufile);
  6332. end;
  6333. { handles the predefined class tobject }
  6334. { the last TOBJECT which is loaded gets }
  6335. { it ! }
  6336. if (childof=nil) and
  6337. (objecttype in [odt_class,odt_javaclass]) and
  6338. (objname^='TOBJECT') then
  6339. class_tobject:=self;
  6340. if (childof=nil) and
  6341. (objecttype=odt_interfacecom) then
  6342. if (objname^='IUNKNOWN') then
  6343. interface_iunknown:=self
  6344. else
  6345. if (objname^='IDISPATCH') then
  6346. interface_idispatch:=self;
  6347. if (childof=nil) and
  6348. (objecttype=odt_objcclass) and
  6349. (objname^='PROTOCOL') then
  6350. objc_protocoltype:=self;
  6351. if (objecttype=odt_javaclass) and
  6352. not(oo_is_formal in objectoptions) then
  6353. begin
  6354. if (objname^='JLOBJECT') then
  6355. java_jlobject:=self
  6356. else if (objname^='JLTHROWABLE') then
  6357. java_jlthrowable:=self
  6358. else if (objname^='FPCBASERECORDTYPE') then
  6359. java_fpcbaserecordtype:=self
  6360. else if (objname^='JLSTRING') then
  6361. java_jlstring:=self
  6362. else if (objname^='ANSISTRINGCLASS') then
  6363. java_ansistring:=self
  6364. else if (objname^='SHORTSTRINGCLASS') then
  6365. java_shortstring:=self
  6366. else if (objname^='JLENUM') then
  6367. java_jlenum:=self
  6368. else if (objname^='JUENUMSET') then
  6369. java_juenumset:=self
  6370. else if (objname^='FPCBITSET') then
  6371. java_jubitset:=self
  6372. else if (objname^='FPCBASEPROCVARTYPE') then
  6373. java_procvarbase:=self;
  6374. end;
  6375. writing_class_record_dbginfo:=false;
  6376. end;
  6377. destructor tobjectdef.destroy;
  6378. begin
  6379. if assigned(symtable) then
  6380. begin
  6381. symtable.free;
  6382. symtable:=nil;
  6383. end;
  6384. stringdispose(objextname);
  6385. stringdispose(iidstr);
  6386. if assigned(ImplementedInterfaces) then
  6387. begin
  6388. ImplementedInterfaces.free;
  6389. ImplementedInterfaces:=nil;
  6390. end;
  6391. if assigned(iidguid) then
  6392. begin
  6393. dispose(iidguid);
  6394. iidguid:=nil;
  6395. end;
  6396. if assigned(vmtentries) then
  6397. begin
  6398. resetvmtentries;
  6399. vmtentries.free;
  6400. vmtentries:=nil;
  6401. end;
  6402. if assigned(vmcallstaticinfo) then
  6403. begin
  6404. freemem(vmcallstaticinfo);
  6405. vmcallstaticinfo:=nil;
  6406. end;
  6407. inherited destroy;
  6408. end;
  6409. function tobjectdef.getcopy : tstoreddef;
  6410. var
  6411. i : longint;
  6412. begin
  6413. result:=cobjectdef.create(objecttype,objrealname^,childof,true);
  6414. { the constructor allocates a symtable which we release to avoid memory leaks }
  6415. tobjectdef(result).symtable.free;
  6416. tobjectdef(result).symtable:=symtable.getcopy;
  6417. if assigned(objextname) then
  6418. tobjectdef(result).objextname:=stringdup(objextname^);
  6419. if assigned(import_lib) then
  6420. tobjectdef(result).import_lib:=stringdup(import_lib^);
  6421. tobjectdef(result).objectoptions:=objectoptions;
  6422. include(tobjectdef(result).defoptions,df_copied_def);
  6423. tobjectdef(result).extendeddef:=extendeddef;
  6424. if assigned(tcinitcode) then
  6425. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  6426. tobjectdef(result).vmt_field:=vmt_field;
  6427. if assigned(iidguid) then
  6428. begin
  6429. new(tobjectdef(result).iidguid);
  6430. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  6431. end;
  6432. if assigned(iidstr) then
  6433. tobjectdef(result).iidstr:=stringdup(iidstr^);
  6434. tobjectdef(result).abstractcnt:=abstractcnt;
  6435. if assigned(ImplementedInterfaces) then
  6436. begin
  6437. for i:=0 to ImplementedInterfaces.count-1 do
  6438. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  6439. end;
  6440. if assigned(vmtentries) then
  6441. begin
  6442. tobjectdef(result).vmtentries:=TFPList.Create;
  6443. tobjectdef(result).copyvmtentries(self);
  6444. end;
  6445. end;
  6446. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  6447. var
  6448. i : longint;
  6449. vmtentry : pvmtentry;
  6450. ImplIntf : TImplementedInterface;
  6451. old_do_indirect_crc: boolean;
  6452. begin
  6453. { if class1 in unit A changes, and class2 in unit B inherits from it
  6454. (so unit B uses unit A), then unit B with class2 will be recompiled.
  6455. However, if there is also a class3 in unit C that only depends on
  6456. unit B, then unit C will not be recompiled because nothing changed
  6457. to the interface of unit B. Nevertheless, unit C can indirectly
  6458. depend on unit A via derefs, and these must be updated -> the
  6459. indirect crc keeps track of such changes. }
  6460. old_do_indirect_crc:=ppufile.do_indirect_crc;
  6461. ppufile.do_indirect_crc:=true;
  6462. inherited ppuwrite(ppufile);
  6463. ppufile.putbyte(byte(objecttype));
  6464. ppufile.putbyte(byte(helpertype));
  6465. if assigned(objextname) then
  6466. ppufile.putstring(objextname^)
  6467. else
  6468. ppufile.putstring('');
  6469. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  6470. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  6471. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  6472. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  6473. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignmin));
  6474. ppufile.putderef(vmt_fieldderef);
  6475. ppufile.putderef(childofderef);
  6476. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  6477. begin
  6478. ppufile.putguid(iidguid^);
  6479. ppufile.putstring(iidstr^);
  6480. end;
  6481. ppufile.putlongint(abstractcnt);
  6482. if objecttype=odt_helper then
  6483. ppufile.putderef(extendeddefderef);
  6484. ppufile.putlongint(vmtentries.count);
  6485. for i:=0 to vmtentries.count-1 do
  6486. begin
  6487. vmtentry:=pvmtentry(vmtentries[i]);
  6488. ppufile.putderef(vmtentry^.procdefderef);
  6489. ppufile.putbyte(byte(vmtentry^.visibility));
  6490. end;
  6491. if assigned(ImplementedInterfaces) then
  6492. begin
  6493. ppufile.putlongint(ImplementedInterfaces.Count);
  6494. for i:=0 to ImplementedInterfaces.Count-1 do
  6495. begin
  6496. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  6497. ppufile.putderef(ImplIntf.intfdefderef);
  6498. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  6499. ppufile.putlongint(ImplIntf.Ioffset);
  6500. ppufile.putbyte(byte(ImplIntf.IType));
  6501. end;
  6502. end;
  6503. if df_copied_def in defoptions then
  6504. ppufile.putderef(cloneddefderef);
  6505. writeentry(ppufile,ibobjectdef);
  6506. if not(df_copied_def in defoptions) then
  6507. tObjectSymtable(symtable).ppuwrite(ppufile);
  6508. ppufile.do_indirect_crc:=old_do_indirect_crc;
  6509. end;
  6510. function tobjectdef.GetTypeName:string;
  6511. begin
  6512. { in this case we will go in endless recursion, because then }
  6513. { there is no tsym associated yet with the def. It can occur }
  6514. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  6515. { instead of the actual type name }
  6516. if not assigned(typesym) then
  6517. result:='<Currently Parsed Class>'
  6518. else
  6519. result:=typesymbolprettyname;
  6520. end;
  6521. procedure tobjectdef.buildderef;
  6522. var
  6523. i : longint;
  6524. vmtentry : pvmtentry;
  6525. begin
  6526. inherited buildderef;
  6527. vmt_fieldderef.build(vmt_field);
  6528. childofderef.build(childof);
  6529. if df_copied_def in defoptions then
  6530. cloneddefderef.build(symtable.defowner)
  6531. else
  6532. tstoredsymtable(symtable).buildderef;
  6533. if objecttype=odt_helper then
  6534. extendeddefderef.build(extendeddef);
  6535. for i:=0 to vmtentries.count-1 do
  6536. begin
  6537. vmtentry:=pvmtentry(vmtentries[i]);
  6538. vmtentry^.procdefderef.build(vmtentry^.procdef);
  6539. end;
  6540. if assigned(ImplementedInterfaces) then
  6541. begin
  6542. for i:=0 to ImplementedInterfaces.count-1 do
  6543. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  6544. end;
  6545. end;
  6546. procedure tobjectdef.deref;
  6547. var
  6548. i : longint;
  6549. vmtentry : pvmtentry;
  6550. begin
  6551. inherited deref;
  6552. vmt_field:=tsym(vmt_fieldderef.resolve);
  6553. childof:=tobjectdef(childofderef.resolve);
  6554. if df_copied_def in defoptions then
  6555. begin
  6556. cloneddef:=tobjectdef(cloneddefderef.resolve);
  6557. symtable:=cloneddef.symtable.getcopy;
  6558. end
  6559. else
  6560. tstoredsymtable(symtable).deref(false);
  6561. if objecttype=odt_helper then
  6562. extendeddef:=tdef(extendeddefderef.resolve);
  6563. for i:=0 to vmtentries.count-1 do
  6564. begin
  6565. vmtentry:=pvmtentry(vmtentries[i]);
  6566. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  6567. end;
  6568. if assigned(ImplementedInterfaces) then
  6569. begin
  6570. for i:=0 to ImplementedInterfaces.count-1 do
  6571. TImplementedInterface(ImplementedInterfaces[i]).deref;
  6572. end;
  6573. end;
  6574. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  6575. var
  6576. pd: tprocdef absolute def;
  6577. st: tsymtable;
  6578. psym: tsym;
  6579. nname: TIDString;
  6580. begin
  6581. if (tdef(def).typ<>procdef) then
  6582. exit;
  6583. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  6584. owner = symtable in which objcclassdef is defined
  6585. }
  6586. st:=pd.owner.defowner.owner;
  6587. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  6588. { check for an existing procsym with our special name }
  6589. psym:=tsym(st.find(nname));
  6590. if not assigned(psym) then
  6591. begin
  6592. psym:=cprocsym.create(nname);
  6593. { avoid warning about this symbol being unused }
  6594. psym.IncRefCount;
  6595. { don't check for duplicates:
  6596. a) we checked above
  6597. b) in case we are in the implementation section of a unit, this
  6598. will also check for this symbol in the interface section
  6599. (since you normally cannot have symbols with the same name
  6600. both interface and implementation), and it's possible to
  6601. have class helpers for the same class in the interface and
  6602. in the implementation, and they cannot be merged since only
  6603. the once in the interface must be saved to the ppu/visible
  6604. from other units }
  6605. st.insert(psym,false);
  6606. end
  6607. else if (psym.typ<>procsym) then
  6608. internalerror(2009111501);
  6609. { add ourselves to this special procsym }
  6610. tprocsym(psym).procdeflist.add(def);
  6611. end;
  6612. procedure tobjectdef.derefimpl;
  6613. begin
  6614. inherited derefimpl;
  6615. { the procdefs are not owned by the class helper procsyms, so they
  6616. are not stored/restored either -> re-add them here }
  6617. if (objecttype in [odt_objcclass,odt_objcprotocol]) or
  6618. (oo_is_classhelper in objectoptions) then
  6619. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  6620. end;
  6621. procedure tobjectdef.resetvmtentries;
  6622. var
  6623. i : longint;
  6624. begin
  6625. for i:=0 to vmtentries.Count-1 do
  6626. Dispose(pvmtentry(vmtentries[i]));
  6627. vmtentries.clear;
  6628. end;
  6629. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  6630. var
  6631. i : longint;
  6632. vmtentry : pvmtentry;
  6633. begin
  6634. resetvmtentries;
  6635. vmtentries.count:=objdef.vmtentries.count;
  6636. for i:=0 to objdef.vmtentries.count-1 do
  6637. begin
  6638. new(vmtentry);
  6639. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  6640. vmtentries[i]:=vmtentry;
  6641. end;
  6642. end;
  6643. function tobjectdef.getparentdef:tdef;
  6644. begin
  6645. { TODO: Remove getparentdef hack}
  6646. { With 2 forward declared classes with the child class before the
  6647. parent class the child class is written earlier to the ppu. Leaving it
  6648. possible to have a reference to the parent class for property overriding,
  6649. but the parent class still has the childof not resolved yet (PFV) }
  6650. if childof=nil then
  6651. childof:=tobjectdef(childofderef.resolve);
  6652. result:=childof;
  6653. end;
  6654. procedure tobjectdef.prepareguid;
  6655. begin
  6656. { set up guid }
  6657. if not assigned(iidguid) then
  6658. begin
  6659. new(iidguid);
  6660. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  6661. end;
  6662. { setup iidstring }
  6663. if not assigned(iidstr) then
  6664. iidstr:=stringdup(''); { default is empty string }
  6665. end;
  6666. procedure tobjectdef.set_parent( c : tobjectdef);
  6667. begin
  6668. if assigned(childof) then
  6669. exit;
  6670. childof:=c;
  6671. if not assigned(c) then
  6672. exit;
  6673. { inherit options and status }
  6674. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  6675. { initially has the same number of abstract methods as the parent }
  6676. abstractcnt:=c.abstractcnt;
  6677. { add the data of the anchestor class/object }
  6678. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  6679. begin
  6680. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  6681. { inherit recordalignment }
  6682. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  6683. { if both the parent and this record use C-alignment, also inherit
  6684. the current field alignment }
  6685. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  6686. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  6687. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  6688. { the padding is not inherited for Objective-C classes (maybe not
  6689. for cppclass either?) }
  6690. if objecttype=odt_objcclass then
  6691. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  6692. if (oo_has_vmt in objectoptions) and
  6693. (oo_has_vmt in c.objectoptions) then
  6694. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  6695. { if parent has a vmt field then the offset is the same for the child PM }
  6696. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  6697. begin
  6698. vmt_field:=c.vmt_field;
  6699. include(objectoptions,oo_has_vmt);
  6700. end;
  6701. end;
  6702. end;
  6703. procedure tobjectdef.insertvmt;
  6704. begin
  6705. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  6706. exit;
  6707. if (oo_has_vmt in objectoptions) then
  6708. internalerror(12345)
  6709. else
  6710. begin
  6711. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  6712. tObjectSymtable(symtable).fieldalignment);
  6713. if (tf_requires_proper_alignment in target_info.flags) then
  6714. begin
  6715. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  6716. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  6717. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  6718. end;
  6719. vmt_field:=cfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[],true);
  6720. hidesym(vmt_field);
  6721. tObjectSymtable(symtable).insert(vmt_field);
  6722. tObjectSymtable(symtable).addfield(tfieldvarsym(vmt_field),vis_hidden);
  6723. include(objectoptions,oo_has_vmt);
  6724. end;
  6725. end;
  6726. function tobjectdef.vmt_offset: asizeint;
  6727. begin
  6728. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  6729. result:=0
  6730. else if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  6731. result:=tfieldvarsym(vmt_field).fieldoffset
  6732. else
  6733. result:=tfieldvarsym(vmt_field).fieldoffset div 8;
  6734. end;
  6735. procedure tobjectdef.check_forwards;
  6736. begin
  6737. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  6738. inherited;
  6739. if (oo_is_forward in objectoptions) then
  6740. begin
  6741. { ok, in future, the forward can be resolved }
  6742. Message1(sym_e_class_forward_not_resolved,objrealname^);
  6743. exclude(objectoptions,oo_is_forward);
  6744. end;
  6745. end;
  6746. function tobjectdef.find_destructor: tprocdef;
  6747. var
  6748. objdef: tobjectdef;
  6749. begin
  6750. objdef:=self;
  6751. while assigned(objdef) do
  6752. begin
  6753. result:=objdef.find_procdef_bytype(potype_destructor);
  6754. if assigned(result) then
  6755. exit;
  6756. objdef:=objdef.childof;
  6757. end;
  6758. result:=nil;
  6759. end;
  6760. function tobjectdef.implements_any_interfaces: boolean;
  6761. begin
  6762. result := (ImplementedInterfaces.Count > 0) or
  6763. (assigned(childof) and childof.implements_any_interfaces);
  6764. end;
  6765. function tobjectdef.size : asizeint;
  6766. begin
  6767. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  6768. result:=voidpointertype.size
  6769. else
  6770. result:=tObjectSymtable(symtable).datasize;
  6771. end;
  6772. function tobjectdef.alignment:shortint;
  6773. begin
  6774. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  6775. alignment:=voidpointertype.alignment
  6776. else
  6777. alignment:=tObjectSymtable(symtable).recordalignment;
  6778. end;
  6779. function tobjectdef.vmtmethodoffset(index:longint):longint;
  6780. begin
  6781. { for offset of methods for classes, see rtl/inc/objpash.inc }
  6782. case objecttype of
  6783. odt_class:
  6784. { the +2*sizeof(pint) is size and -size }
  6785. vmtmethodoffset:=index*voidcodepointertype.size+10*voidpointertype.size+2*sizeof(pint);
  6786. odt_helper,
  6787. odt_objcclass,
  6788. odt_objcprotocol:
  6789. vmtmethodoffset:=0;
  6790. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  6791. vmtmethodoffset:=index*voidcodepointertype.size;
  6792. odt_javaclass,
  6793. odt_interfacejava:
  6794. { invalid }
  6795. vmtmethodoffset:=-1;
  6796. else
  6797. { the +2*sizeof(pint) is size and -size }
  6798. {$ifdef WITHDMT}
  6799. vmtmethodoffset:=index*voidcodepointertype.size+2*voidpointertype.size+2*sizeof(pint);
  6800. {$else WITHDMT}
  6801. vmtmethodoffset:=index*voidcodepointertype.size+1*voidpointertype.size+2*sizeof(pint);
  6802. {$endif WITHDMT}
  6803. end;
  6804. end;
  6805. function tobjectdef.vmt_mangledname : TSymStr;
  6806. begin
  6807. if not(oo_has_vmt in objectoptions) then
  6808. Message1(parser_n_object_has_no_vmt,objrealname^);
  6809. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  6810. end;
  6811. function tobjectdef.vmt_def: trecorddef;
  6812. var
  6813. vmttypesym: tsym;
  6814. begin
  6815. if not(typesym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  6816. vmttypesym:=tsym(typesym.owner.Find('vmtdef$'+mangledparaname))
  6817. else
  6818. vmttypesym:=tsym(tobjectsymtable(typesym.owner).get_unit_symtable.Find('vmtdef$'+mangledparaname));
  6819. if not assigned(vmttypesym) or
  6820. (vmttypesym.typ<>symconst.typesym) or
  6821. (ttypesym(vmttypesym).typedef.typ<>recorddef) then
  6822. internalerror(2015052501);
  6823. result:=trecorddef(ttypesym(vmttypesym).typedef);
  6824. end;
  6825. function tobjectdef.needs_inittable : boolean;
  6826. var
  6827. hp : tobjectdef;
  6828. begin
  6829. case objecttype of
  6830. odt_helper,
  6831. odt_class :
  6832. needs_inittable:=false;
  6833. odt_dispinterface,
  6834. odt_interfacecom:
  6835. needs_inittable:=true;
  6836. odt_interfacecorba:
  6837. begin
  6838. hp:=childof;
  6839. while assigned(hp) do
  6840. begin
  6841. if hp=interface_iunknown then
  6842. begin
  6843. needs_inittable:=true;
  6844. exit;
  6845. end;
  6846. hp:=hp.childof;
  6847. end;
  6848. needs_inittable:=false;
  6849. end;
  6850. odt_object:
  6851. needs_inittable:=
  6852. tObjectSymtable(symtable).needs_init_final or
  6853. (assigned(childof) and
  6854. childof.needs_inittable);
  6855. odt_cppclass,
  6856. odt_objcclass,
  6857. odt_objcprotocol,
  6858. odt_javaclass,
  6859. odt_interfacejava:
  6860. needs_inittable:=false;
  6861. else
  6862. internalerror(200108267);
  6863. end;
  6864. end;
  6865. function tobjectdef.needs_separate_initrtti : boolean;
  6866. begin
  6867. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  6868. end;
  6869. function tobjectdef.has_non_trivial_init_child(check_parent:boolean):boolean;
  6870. begin
  6871. if objecttype in [odt_class,odt_object] then
  6872. begin
  6873. result:=tobjectsymtable(symtable).has_non_trivial_init or
  6874. (check_parent and assigned(childof) and childof.has_non_trivial_init_child(true));
  6875. end
  6876. else
  6877. result:=false;
  6878. end;
  6879. function tobjectdef.rtti_mangledname(rt: trttitype): TSymStr;
  6880. begin
  6881. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  6882. result:=inherited rtti_mangledname(rt)
  6883. else
  6884. begin
  6885. { necessary in case of a dynamic array of nsobject, or
  6886. if an nsobject field appears in a record that needs
  6887. init/finalisation }
  6888. if rt=initrtti then
  6889. begin
  6890. result:=voidpointertype.rtti_mangledname(rt);
  6891. exit;
  6892. end;
  6893. if not(target_info.system in systems_objc_nfabi) then
  6894. begin
  6895. result:=target_asm.labelprefix;
  6896. case objecttype of
  6897. odt_objcclass:
  6898. begin
  6899. case rt of
  6900. objcclassrtti:
  6901. if not(oo_is_classhelper in objectoptions) then
  6902. result:=result+'_OBJC_CLASS_'
  6903. else
  6904. result:=result+'_OBJC_CATEGORY_';
  6905. objcmetartti:
  6906. if not(oo_is_classhelper in objectoptions) then
  6907. result:=result+'_OBJC_METACLASS_'
  6908. else
  6909. internalerror(2009111511);
  6910. else
  6911. internalerror(2009092302);
  6912. end;
  6913. end;
  6914. odt_objcprotocol:
  6915. result:=result+'_OBJC_PROTOCOL_';
  6916. else
  6917. ;
  6918. end;
  6919. end
  6920. else
  6921. begin
  6922. case objecttype of
  6923. odt_objcclass:
  6924. begin
  6925. if (oo_is_classhelper in objectoptions) and
  6926. (rt<>objcclassrtti) then
  6927. internalerror(2009111512);
  6928. case rt of
  6929. objcclassrtti:
  6930. if not(oo_is_classhelper in objectoptions) then
  6931. result:='_OBJC_CLASS_$_'
  6932. else
  6933. result:='_OBJC_$_CATEGORY_';
  6934. objcmetartti:
  6935. result:='_OBJC_METACLASS_$_';
  6936. objcclassrortti:
  6937. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  6938. objcmetarortti:
  6939. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  6940. else
  6941. internalerror(2009092303);
  6942. end;
  6943. end;
  6944. odt_objcprotocol:
  6945. begin
  6946. result:=lower(target_asm.labelprefix);
  6947. case rt of
  6948. objcclassrtti:
  6949. result:=result+'_OBJC_PROTOCOL_$_';
  6950. objcmetartti:
  6951. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  6952. else
  6953. internalerror(2009092501);
  6954. end;
  6955. end;
  6956. else
  6957. internalerror(2013113005);
  6958. end;
  6959. end;
  6960. result:=result+objextname^;
  6961. end;
  6962. end;
  6963. function tobjectdef.members_need_inittable : boolean;
  6964. begin
  6965. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  6966. end;
  6967. function tobjectdef.is_publishable : boolean;
  6968. begin
  6969. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  6970. end;
  6971. function tobjectdef.get_next_dispid: longint;
  6972. begin
  6973. inc(fcurrent_dispid);
  6974. result:=fcurrent_dispid;
  6975. end;
  6976. function tobjectdef.search_enumerator_get: tprocdef;
  6977. begin
  6978. result:=inherited;
  6979. if not assigned(result) and assigned(childof) then
  6980. result:=childof.search_enumerator_get;
  6981. end;
  6982. function tobjectdef.search_enumerator_move: tprocdef;
  6983. begin
  6984. result:=inherited;
  6985. if not assigned(result) and assigned(childof) then
  6986. result:=childof.search_enumerator_move;
  6987. end;
  6988. function tobjectdef.search_enumerator_current: tsym;
  6989. begin
  6990. result:=inherited;
  6991. if not assigned(result) and assigned(childof) then
  6992. result:=childof.search_enumerator_current;
  6993. end;
  6994. procedure tobjectdef.register_created_classref_type;
  6995. begin
  6996. if not classref_created_in_current_module then
  6997. begin
  6998. classref_created_in_current_module:=true;
  6999. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  7000. end;
  7001. end;
  7002. procedure tobjectdef.register_created_object_type;
  7003. begin
  7004. if not created_in_current_module then
  7005. begin
  7006. created_in_current_module:=true;
  7007. current_module.wpoinfo.addcreatedobjtype(self);
  7008. end;
  7009. end;
  7010. procedure tobjectdef.register_maybe_created_object_type;
  7011. begin
  7012. { if we know it has been created for sure, no need
  7013. to also record that it maybe can be created in
  7014. this module
  7015. }
  7016. if not (created_in_current_module) and
  7017. not (maybe_created_in_current_module) then
  7018. begin
  7019. maybe_created_in_current_module:=true;
  7020. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  7021. end;
  7022. end;
  7023. procedure tobjectdef.register_vmt_call(index: longint);
  7024. begin
  7025. if (is_object(self) or is_class(self)) then
  7026. current_module.wpoinfo.addcalledvmtentry(self,index);
  7027. end;
  7028. procedure check_and_finish_msg(data: tobject; arg: pointer);
  7029. var
  7030. def: tdef absolute data;
  7031. pd: tprocdef absolute data;
  7032. i,
  7033. paracount: longint;
  7034. begin
  7035. if (def.typ=procdef) then
  7036. begin
  7037. { add all messages also under a dummy name to the symtable in
  7038. which the objcclass/protocol/category is declared, so they can
  7039. be called via id.<name>
  7040. }
  7041. create_class_helper_for_procdef(pd,nil);
  7042. { we have to wait until now to set the mangled name because it
  7043. depends on the (possibly external) class name, which is defined
  7044. at the very end. }
  7045. if not(po_msgstr in pd.procoptions) then
  7046. begin
  7047. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  7048. { recover to avoid internalerror later on }
  7049. include(pd.procoptions,po_msgstr);
  7050. pd.messageinf.str:=stringdup('MissingDeclaration');
  7051. end;
  7052. { Mangled name is already set in case this is a copy of
  7053. another type. }
  7054. if not(po_has_mangledname in pd.procoptions) then
  7055. begin
  7056. { check whether the number of formal parameters is correct,
  7057. and whether they have valid Objective-C types }
  7058. paracount:=0;
  7059. for i:=1 to length(pd.messageinf.str^) do
  7060. if pd.messageinf.str^[i]=':' then
  7061. inc(paracount);
  7062. for i:=0 to pd.paras.count-1 do
  7063. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  7064. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  7065. dec(paracount);
  7066. if (paracount<>0) then
  7067. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  7068. pd.setmangledname(pd.objcmangledname);
  7069. end
  7070. else
  7071. { all checks already done }
  7072. exit;
  7073. if not(oo_is_external in pd.struct.objectoptions) then
  7074. begin
  7075. if (po_varargs in pd.procoptions) then
  7076. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  7077. else
  7078. begin
  7079. { check for "array of const" parameters }
  7080. for i:=0 to pd.parast.symlist.count-1 do
  7081. begin
  7082. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  7083. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  7084. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  7085. end;
  7086. end;
  7087. end;
  7088. end;
  7089. end;
  7090. procedure mark_private_fields_used(data: tobject; arg: pointer);
  7091. var
  7092. sym: tsym absolute data;
  7093. begin
  7094. if (sym.typ=fieldvarsym) and
  7095. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  7096. sym.IncRefCount;
  7097. end;
  7098. procedure tobjectdef.finish_objc_data;
  7099. begin
  7100. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  7101. if (oo_is_external in objectoptions) then
  7102. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  7103. end;
  7104. procedure verify_objc_vardef(data: tobject; arg: pointer);
  7105. var
  7106. sym: tabstractvarsym absolute data;
  7107. res: pboolean absolute arg;
  7108. founderrordef: tdef;
  7109. begin
  7110. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  7111. exit;
  7112. if (sym.typ=paravarsym) and
  7113. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  7114. is_array_of_const(tparavarsym(sym).vardef)) then
  7115. exit;
  7116. if not objcchecktype(sym.vardef,founderrordef) then
  7117. begin
  7118. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  7119. res^:=false;
  7120. end;
  7121. end;
  7122. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  7123. var
  7124. def: tdef absolute data;
  7125. res: pboolean absolute arg;
  7126. founderrordef: tdef;
  7127. begin
  7128. if (def.typ<>procdef) then
  7129. exit;
  7130. { check parameter types for validity }
  7131. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  7132. { check the result type for validity }
  7133. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  7134. begin
  7135. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  7136. res^:=false;
  7137. end;
  7138. end;
  7139. function tobjectdef.check_objc_types: boolean;
  7140. begin
  7141. { done in separate step from finish_objc_data, because when
  7142. finish_objc_data is called, not all forwarddefs have been resolved
  7143. yet and we need to know all types here }
  7144. result:=true;
  7145. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  7146. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  7147. end;
  7148. procedure do_cpp_import_info(data: tobject; arg: pointer);
  7149. var
  7150. def: tdef absolute data;
  7151. pd: tprocdef absolute data;
  7152. begin
  7153. if (def.typ=procdef) then
  7154. begin
  7155. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  7156. if (oo_is_external in pd.struct.objectoptions) then
  7157. begin
  7158. { copied from psub.read_proc }
  7159. if assigned(tobjectdef(pd.struct).import_lib) then
  7160. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  7161. else
  7162. begin
  7163. { add import name to external list for DLL scanning }
  7164. if tf_has_dllscanner in target_info.flags then
  7165. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  7166. end;
  7167. end;
  7168. end;
  7169. end;
  7170. procedure tobjectdef.finish_cpp_data;
  7171. begin
  7172. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  7173. end;
  7174. {****************************************************************************
  7175. TImplementedInterface
  7176. ****************************************************************************}
  7177. function TImplementedInterface.GetIOffset: longint;
  7178. begin
  7179. if (fIOffset=-1) and
  7180. (IType in [etFieldValue,etFieldValueClass]) then
  7181. result:=tfieldvarsym(ImplementsField).fieldoffset
  7182. else
  7183. result:=fIOffset;
  7184. end;
  7185. constructor TImplementedInterface.create(aintf: tobjectdef);
  7186. begin
  7187. inherited create;
  7188. intfdef:=aintf;
  7189. intfdefderef.reset;
  7190. IOffset:=-1;
  7191. IType:=etStandard;
  7192. NameMappings:=nil;
  7193. procdefs:=nil;
  7194. end;
  7195. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  7196. begin
  7197. inherited create;
  7198. intfdef:=nil;
  7199. intfdefderef:=intfd;
  7200. ImplementsGetterDeref:=getterd;
  7201. IOffset:=-1;
  7202. IType:=etStandard;
  7203. NameMappings:=nil;
  7204. procdefs:=nil;
  7205. end;
  7206. destructor TImplementedInterface.destroy;
  7207. var
  7208. i : longint;
  7209. mappedname : pshortstring;
  7210. begin
  7211. if assigned(NameMappings) then
  7212. begin
  7213. for i:=0 to NameMappings.Count-1 do
  7214. begin
  7215. mappedname:=pshortstring(NameMappings[i]);
  7216. stringdispose(mappedname);
  7217. end;
  7218. NameMappings.free;
  7219. NameMappings:=nil;
  7220. end;
  7221. if assigned(procdefs) then
  7222. begin
  7223. procdefs.free;
  7224. procdefs:=nil;
  7225. end;
  7226. inherited destroy;
  7227. end;
  7228. procedure TImplementedInterface.buildderef;
  7229. begin
  7230. intfdefderef.build(intfdef);
  7231. ImplementsGetterDeref.build(ImplementsGetter);
  7232. end;
  7233. procedure TImplementedInterface.deref;
  7234. begin
  7235. intfdef:=tobjectdef(intfdefderef.resolve);
  7236. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  7237. end;
  7238. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  7239. begin
  7240. if not assigned(NameMappings) then
  7241. NameMappings:=TFPHashList.Create;
  7242. NameMappings.Add(origname,stringdup(newname));
  7243. end;
  7244. function TImplementedInterface.GetMapping(const origname: string):string;
  7245. var
  7246. mappedname : pshortstring;
  7247. begin
  7248. result:='';
  7249. if not assigned(NameMappings) then
  7250. exit;
  7251. mappedname:=PShortstring(NameMappings.Find(origname));
  7252. if assigned(mappedname) then
  7253. result:=mappedname^;
  7254. end;
  7255. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  7256. begin
  7257. if not assigned(procdefs) then
  7258. procdefs:=TFPObjectList.Create(false);
  7259. { duplicate entries must be stored, because multiple }
  7260. { interfaces can declare methods with the same name }
  7261. { and all of these get their own VMT entry }
  7262. procdefs.Add(pd);
  7263. end;
  7264. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  7265. var
  7266. i : longint;
  7267. begin
  7268. result:=false;
  7269. { interfaces being implemented through delegation are not mergable (FK) }
  7270. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  7271. exit;
  7272. weight:=0;
  7273. { empty interface is mergeable }
  7274. if ProcDefs.Count=0 then
  7275. begin
  7276. result:=true;
  7277. exit;
  7278. end;
  7279. { The interface to merge must at least the number of
  7280. procedures of this interface }
  7281. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  7282. exit;
  7283. for i:=0 to ProcDefs.Count-1 do
  7284. begin
  7285. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  7286. exit;
  7287. end;
  7288. weight:=ProcDefs.Count;
  7289. result:=true;
  7290. end;
  7291. function TImplementedInterface.getcopy:TImplementedInterface;
  7292. begin
  7293. Result:=TImplementedInterface.Create(nil);
  7294. { 1) the procdefs list will be freed once for each copy
  7295. 2) since the procdefs list owns its elements, those will also be freed for each copy
  7296. 3) idem for the name mappings
  7297. }
  7298. { warning: this is completely wrong on so many levels...
  7299. Move(pointer(self)^,pointer(result)^,InstanceSize);
  7300. We need to make clean copies of the different fields
  7301. this is not implemented yet, and thus we generate an internal
  7302. error instead PM 2011-06-14 }
  7303. internalerror(2011061401);
  7304. end;
  7305. {****************************************************************************
  7306. TFORWARDDEF
  7307. ****************************************************************************}
  7308. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  7309. begin
  7310. inherited create(forwarddef,true);
  7311. tosymname:=stringdup(s);
  7312. forwardpos:=pos;
  7313. end;
  7314. function tforwarddef.GetTypeName:string;
  7315. begin
  7316. GetTypeName:='unresolved forward to '+tosymname^;
  7317. end;
  7318. destructor tforwarddef.destroy;
  7319. begin
  7320. stringdispose(tosymname);
  7321. inherited destroy;
  7322. end;
  7323. function tforwarddef.getcopy:tstoreddef;
  7324. begin
  7325. result:=cforwarddef.create(tosymname^, forwardpos);
  7326. end;
  7327. {****************************************************************************
  7328. TUNDEFINEDDEF
  7329. ****************************************************************************}
  7330. constructor tundefineddef.create(doregister:boolean);
  7331. begin
  7332. inherited create(undefineddef,doregister);
  7333. end;
  7334. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  7335. begin
  7336. inherited ppuload(undefineddef,ppufile);
  7337. ppuload_platform(ppufile);
  7338. end;
  7339. function tundefineddef.GetTypeName:string;
  7340. begin
  7341. GetTypeName:='<undefined type>';
  7342. end;
  7343. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  7344. begin
  7345. inherited ppuwrite(ppufile);
  7346. writeentry(ppufile,ibundefineddef);
  7347. end;
  7348. {****************************************************************************
  7349. TERRORDEF
  7350. ****************************************************************************}
  7351. constructor terrordef.create;
  7352. begin
  7353. inherited create(errordef,true);
  7354. { prevent consecutive faults }
  7355. savesize:=1;
  7356. end;
  7357. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  7358. begin
  7359. { Can't write errordefs to ppu }
  7360. internalerror(200411063);
  7361. end;
  7362. function terrordef.GetTypeName:string;
  7363. begin
  7364. GetTypeName:='<erroneous type>';
  7365. end;
  7366. function terrordef.getmangledparaname:TSymStr;
  7367. begin
  7368. getmangledparaname:='error';
  7369. end;
  7370. {****************************************************************************
  7371. Definition Helpers
  7372. ****************************************************************************}
  7373. function is_interfacecom(def: tdef): boolean;
  7374. begin
  7375. is_interfacecom:=
  7376. assigned(def) and
  7377. (def.typ=objectdef) and
  7378. (tobjectdef(def).objecttype=odt_interfacecom);
  7379. end;
  7380. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  7381. begin
  7382. is_interfacecom_or_dispinterface:=
  7383. assigned(def) and
  7384. (def.typ=objectdef) and
  7385. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  7386. end;
  7387. function is_any_interface_kind(def: tdef): boolean;
  7388. begin
  7389. result:=
  7390. assigned(def) and
  7391. (def.typ=objectdef) and
  7392. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  7393. is_objccategory(def));
  7394. end;
  7395. function is_interfacecorba(def: tdef): boolean;
  7396. begin
  7397. is_interfacecorba:=
  7398. assigned(def) and
  7399. (def.typ=objectdef) and
  7400. (tobjectdef(def).objecttype=odt_interfacecorba);
  7401. end;
  7402. function is_interface(def: tdef): boolean;
  7403. begin
  7404. is_interface:=
  7405. assigned(def) and
  7406. (def.typ=objectdef) and
  7407. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  7408. end;
  7409. function is_dispinterface(def: tdef): boolean;
  7410. begin
  7411. result:=
  7412. assigned(def) and
  7413. (def.typ=objectdef) and
  7414. (tobjectdef(def).objecttype=odt_dispinterface);
  7415. end;
  7416. function is_class(def: tdef): boolean;
  7417. begin
  7418. is_class:=
  7419. assigned(def) and
  7420. (def.typ=objectdef) and
  7421. (tobjectdef(def).objecttype=odt_class);
  7422. end;
  7423. function is_object(def: tdef): boolean;
  7424. begin
  7425. is_object:=
  7426. assigned(def) and
  7427. (def.typ=objectdef) and
  7428. (tobjectdef(def).objecttype=odt_object);
  7429. end;
  7430. function is_cppclass(def: tdef): boolean;
  7431. begin
  7432. is_cppclass:=
  7433. assigned(def) and
  7434. (def.typ=objectdef) and
  7435. (tobjectdef(def).objecttype=odt_cppclass);
  7436. end;
  7437. function is_objcclass(def: tdef): boolean;
  7438. begin
  7439. is_objcclass:=
  7440. assigned(def) and
  7441. (def.typ=objectdef) and
  7442. (tobjectdef(def).objecttype=odt_objcclass);
  7443. end;
  7444. function is_objectpascal_helper(def: tdef): boolean;
  7445. begin
  7446. result:=
  7447. assigned(def) and
  7448. (def.typ=objectdef) and
  7449. (tobjectdef(def).objecttype=odt_helper);
  7450. end;
  7451. function is_objcclassref(def: tdef): boolean;
  7452. begin
  7453. is_objcclassref:=
  7454. assigned(def) and
  7455. (def.typ=classrefdef) and
  7456. is_objcclass(tclassrefdef(def).pointeddef);
  7457. end;
  7458. function is_objcprotocol(def: tdef): boolean;
  7459. begin
  7460. result:=
  7461. assigned(def) and
  7462. (def.typ=objectdef) and
  7463. (tobjectdef(def).objecttype=odt_objcprotocol);
  7464. end;
  7465. function is_objccategory(def: tdef): boolean;
  7466. begin
  7467. result:=
  7468. assigned(def) and
  7469. (def.typ=objectdef) and
  7470. { if used as a forward type }
  7471. ((tobjectdef(def).objecttype=odt_objccategory) or
  7472. { if used as after it has been resolved }
  7473. ((tobjectdef(def).objecttype=odt_objcclass) and
  7474. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  7475. end;
  7476. function is_objc_class_or_protocol(def: tdef): boolean;
  7477. begin
  7478. result:=
  7479. assigned(def) and
  7480. (def.typ=objectdef) and
  7481. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  7482. end;
  7483. function is_objc_protocol_or_category(def: tdef): boolean;
  7484. begin
  7485. result:=
  7486. assigned(def) and
  7487. (def.typ=objectdef) and
  7488. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  7489. ((tobjectdef(def).objecttype = odt_objcclass) and
  7490. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  7491. end;
  7492. function is_classhelper(def: tdef): boolean;
  7493. begin
  7494. result:=
  7495. is_objectpascal_helper(def) or
  7496. is_objccategory(def);
  7497. end;
  7498. function is_class_or_interface(def: tdef): boolean;
  7499. begin
  7500. result:=
  7501. assigned(def) and
  7502. (def.typ=objectdef) and
  7503. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  7504. end;
  7505. function is_class_or_interface_or_objc(def: tdef): boolean;
  7506. begin
  7507. result:=
  7508. assigned(def) and
  7509. (def.typ=objectdef) and
  7510. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  7511. end;
  7512. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  7513. begin
  7514. result:=
  7515. assigned(def) and
  7516. (def.typ=objectdef) and
  7517. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  7518. end;
  7519. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  7520. begin
  7521. result:=
  7522. assigned(def) and
  7523. (def.typ=objectdef) and
  7524. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  7525. end;
  7526. function is_class_or_interface_or_object(def: tdef): boolean;
  7527. begin
  7528. result:=
  7529. assigned(def) and
  7530. (def.typ=objectdef) and
  7531. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  7532. end;
  7533. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  7534. begin
  7535. result:=
  7536. assigned(def) and
  7537. (def.typ=objectdef) and
  7538. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  7539. end;
  7540. function is_implicit_pointer_object_type(def: tdef): boolean;
  7541. begin
  7542. result:=
  7543. assigned(def) and
  7544. (((def.typ=objectdef) and
  7545. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  7546. ((target_info.system in systems_jvm) and
  7547. (def.typ=recorddef)));
  7548. end;
  7549. function is_implicit_array_pointer(def: tdef): boolean;
  7550. begin
  7551. result:=is_dynamic_array(def) or is_dynamicstring(def);
  7552. end;
  7553. function is_class_or_object(def: tdef): boolean;
  7554. begin
  7555. result:=
  7556. assigned(def) and
  7557. (def.typ=objectdef) and
  7558. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  7559. end;
  7560. function is_record(def: tdef): boolean;
  7561. begin
  7562. result:=
  7563. assigned(def) and
  7564. (def.typ=recorddef);
  7565. end;
  7566. function is_javaclass(def: tdef): boolean;
  7567. begin
  7568. result:=
  7569. assigned(def) and
  7570. (def.typ=objectdef) and
  7571. (tobjectdef(def).objecttype=odt_javaclass);
  7572. end;
  7573. function is_javaclassref(def: tdef): boolean;
  7574. begin
  7575. is_javaclassref:=
  7576. assigned(def) and
  7577. (def.typ=classrefdef) and
  7578. is_javaclass(tclassrefdef(def).pointeddef);
  7579. end;
  7580. function is_javainterface(def: tdef): boolean;
  7581. begin
  7582. result:=
  7583. assigned(def) and
  7584. (def.typ=objectdef) and
  7585. (tobjectdef(def).objecttype=odt_interfacejava);
  7586. end;
  7587. function is_java_class_or_interface(def: tdef): boolean;
  7588. begin
  7589. result:=
  7590. assigned(def) and
  7591. (def.typ=objectdef) and
  7592. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  7593. end;
  7594. procedure loadobjctypes;
  7595. begin
  7596. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  7597. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  7598. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  7599. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  7600. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  7601. end;
  7602. procedure maybeloadcocoatypes;
  7603. var
  7604. tsym: ttypesym;
  7605. cocoaunit: string[15];
  7606. begin
  7607. if assigned(objc_fastenumeration) then
  7608. exit;
  7609. if not(target_info.system in [system_arm_darwin,system_i386_iphonesim,system_aarch64_darwin,system_x86_64_iphonesim]) then
  7610. cocoaunit:='COCOAALL'
  7611. else
  7612. cocoaunit:='IPHONEALL';
  7613. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  7614. if assigned(tsym) then
  7615. objc_fastenumeration:=tobjectdef(tsym.typedef)
  7616. else
  7617. objc_fastenumeration:=nil;
  7618. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  7619. if assigned(tsym) then
  7620. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  7621. else
  7622. objc_fastenumerationstate:=nil;
  7623. end;
  7624. function use_vectorfpu(def : tdef) : boolean;
  7625. begin
  7626. {$ifdef x86}
  7627. {$define use_vectorfpuimplemented}
  7628. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  7629. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  7630. {$endif x86}
  7631. {$ifdef arm}
  7632. {$define use_vectorfpuimplemented}
  7633. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  7634. {$endif arm}
  7635. {$ifdef aarch64}
  7636. {$define use_vectorfpuimplemented}
  7637. use_vectorfpu:=true;
  7638. {$endif aarch64}
  7639. {$ifndef use_vectorfpuimplemented}
  7640. use_vectorfpu:=false;
  7641. {$endif}
  7642. end;
  7643. end.