symdef.pas 224 KB

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