symdef.pas 276 KB

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