symdef.pas 293 KB

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