symdef.pas 294 KB

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