symdef.pas 284 KB

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