symdef.pas 285 KB

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