symdef.pas 232 KB

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