symdef.pas 223 KB

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