symdef.pas 294 KB

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