symdef.pas 229 KB

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