symdef.pas 232 KB

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