symdef.pas 232 KB

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