symdef.pas 225 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914
  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. end;
  196. trecorddef = class(tabstractrecorddef)
  197. public
  198. isunion : boolean;
  199. constructor create(const n:string; p:TSymtable);
  200. constructor ppuload(ppufile:tcompilerppufile);
  201. destructor destroy;override;
  202. function getcopy : tstoreddef;override;
  203. procedure ppuwrite(ppufile:tcompilerppufile);override;
  204. procedure buildderef;override;
  205. procedure deref;override;
  206. function size:asizeint;override;
  207. function alignment : shortint;override;
  208. function padalignment: shortint;
  209. function GetTypeName:string;override;
  210. { debug }
  211. function needs_inittable : boolean;override;
  212. function needs_separate_initrtti:boolean;override;
  213. { jvm }
  214. function is_related(d : tdef) : boolean;override;
  215. end;
  216. tobjectdef = class;
  217. { TImplementedInterface }
  218. TImplementedInterface = class
  219. private
  220. fIOffset : longint;
  221. function GetIOffset: longint;
  222. public
  223. IntfDef : tobjectdef;
  224. IntfDefDeref : tderef;
  225. IType : tinterfaceentrytype;
  226. VtblImplIntf : TImplementedInterface;
  227. NameMappings : TFPHashList;
  228. ProcDefs : TFPObjectList;
  229. ImplementsGetter : tsym;
  230. ImplementsGetterDeref : tderef;
  231. ImplementsField : tsym;
  232. constructor create(aintf: tobjectdef);
  233. constructor create_deref(intfd,getterd:tderef);
  234. destructor destroy; override;
  235. function getcopy:TImplementedInterface;
  236. procedure buildderef;
  237. procedure deref;
  238. procedure AddMapping(const origname, newname: string);
  239. function GetMapping(const origname: string):string;
  240. procedure AddImplProc(pd:tprocdef);
  241. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  242. property IOffset: longint read GetIOffset write fIOffset;
  243. end;
  244. { tvmtentry }
  245. tvmtentry = record
  246. procdef : tprocdef;
  247. procdefderef : tderef;
  248. visibility : tvisibility;
  249. end;
  250. pvmtentry = ^tvmtentry;
  251. { tobjectdef }
  252. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  253. pmvcallstaticinfo = ^tmvcallstaticinfo;
  254. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  255. tobjectdef = class(tabstractrecorddef)
  256. private
  257. fcurrent_dispid: longint;
  258. public
  259. dwarf_struct_lab : tasmsymbol;
  260. childof : tobjectdef;
  261. childofderef : tderef;
  262. { for Object Pascal helpers }
  263. extendeddef : tdef;
  264. extendeddefderef: tderef;
  265. { for Objective-C: protocols and classes can have the same name there }
  266. objextname : pshortstring;
  267. { to be able to have a variable vmt position }
  268. { and no vmt field for objects without virtuals }
  269. vmtentries : TFPList;
  270. vmcallstaticinfo : pmvcallstaticinfo;
  271. vmt_offset : longint;
  272. iidguid : pguid;
  273. iidstr : pshortstring;
  274. { store implemented interfaces defs and name mappings }
  275. ImplementedInterfaces : TFPObjectList;
  276. { number of abstract methods (used by JVM target to determine whether
  277. or not the class should be marked as abstract: must be done if 1 or
  278. more abstract methods) }
  279. abstractcnt : longint;
  280. writing_class_record_dbginfo,
  281. { a class of this type has been created in this module }
  282. created_in_current_module,
  283. { a loadvmtnode for this class has been created in this
  284. module, so if a classrefdef variable of this or a parent
  285. class is used somewhere to instantiate a class, then this
  286. class may be instantiated
  287. }
  288. maybe_created_in_current_module,
  289. { a "class of" this particular class has been created in
  290. this module
  291. }
  292. classref_created_in_current_module : boolean;
  293. objecttype : tobjecttyp;
  294. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef);
  295. constructor ppuload(ppufile:tcompilerppufile);
  296. destructor destroy;override;
  297. function getcopy : tstoreddef;override;
  298. procedure ppuwrite(ppufile:tcompilerppufile);override;
  299. function GetTypeName:string;override;
  300. procedure buildderef;override;
  301. procedure deref;override;
  302. procedure buildderefimpl;override;
  303. procedure derefimpl;override;
  304. procedure resetvmtentries;
  305. procedure copyvmtentries(objdef:tobjectdef);
  306. function getparentdef:tdef;override;
  307. function size : asizeint;override;
  308. function alignment:shortint;override;
  309. function vmtmethodoffset(index:longint):longint;
  310. function members_need_inittable : boolean;
  311. function find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  312. { this should be called when this class implements an interface }
  313. procedure prepareguid;
  314. function is_publishable : boolean;override;
  315. function is_related(d : tdef) : boolean;override;
  316. function needs_inittable : boolean;override;
  317. function needs_separate_initrtti : boolean;override;
  318. function rtti_mangledname(rt:trttitype):string;override;
  319. function vmt_mangledname : TSymStr;
  320. procedure check_forwards; override;
  321. procedure insertvmt;
  322. procedure set_parent(c : tobjectdef);
  323. function find_destructor: tprocdef;
  324. function implements_any_interfaces: boolean;
  325. { dispinterface support }
  326. function get_next_dispid: longint;
  327. { enumerator support }
  328. function search_enumerator_get: tprocdef; override;
  329. function search_enumerator_move: tprocdef; override;
  330. function search_enumerator_current: tsym; override;
  331. { WPO }
  332. procedure register_created_object_type;override;
  333. procedure register_maybe_created_object_type;
  334. procedure register_created_classref_type;
  335. procedure register_vmt_call(index:longint);
  336. { ObjC }
  337. procedure finish_objc_data;
  338. function check_objc_types: boolean;
  339. { C++ }
  340. procedure finish_cpp_data;
  341. end;
  342. tclassrefdef = class(tabstractpointerdef)
  343. constructor create(def:tdef);
  344. constructor ppuload(ppufile:tcompilerppufile);
  345. procedure ppuwrite(ppufile:tcompilerppufile);override;
  346. function getcopy:tstoreddef;override;
  347. function GetTypeName:string;override;
  348. function is_publishable : boolean;override;
  349. function rtti_mangledname(rt:trttitype):string;override;
  350. procedure register_created_object_type;override;
  351. end;
  352. tarraydef = class(tstoreddef)
  353. lowrange,
  354. highrange : asizeint;
  355. rangedef : tdef;
  356. rangedefderef : tderef;
  357. arrayoptions : tarraydefoptions;
  358. symtable : TSymtable;
  359. protected
  360. _elementdef : tdef;
  361. _elementdefderef : tderef;
  362. procedure setelementdef(def:tdef);
  363. public
  364. function elesize : asizeint;
  365. function elepackedbitsize : asizeint;
  366. function elecount : asizeuint;
  367. constructor create_from_pointer(def:tdef);
  368. constructor create(l,h:asizeint;def:tdef);
  369. constructor ppuload(ppufile:tcompilerppufile);
  370. destructor destroy; override;
  371. function getcopy : tstoreddef;override;
  372. procedure ppuwrite(ppufile:tcompilerppufile);override;
  373. function GetTypeName:string;override;
  374. function getmangledparaname : TSymStr;override;
  375. procedure buildderef;override;
  376. procedure deref;override;
  377. function size : asizeint;override;
  378. function alignment : shortint;override;
  379. { returns the label of the range check string }
  380. function needs_inittable : boolean;override;
  381. function needs_separate_initrtti : boolean;override;
  382. property elementdef : tdef read _elementdef write setelementdef;
  383. function is_publishable : boolean;override;
  384. end;
  385. torddef = class(tstoreddef)
  386. low,high : TConstExprInt;
  387. ordtype : tordtype;
  388. constructor create(t : tordtype;v,b : TConstExprInt);
  389. constructor ppuload(ppufile:tcompilerppufile);
  390. function getcopy : tstoreddef;override;
  391. procedure ppuwrite(ppufile:tcompilerppufile);override;
  392. function is_publishable : boolean;override;
  393. function GetTypeName:string;override;
  394. function alignment:shortint;override;
  395. procedure setsize;
  396. function packedbitsize: asizeint; override;
  397. function getvardef : longint;override;
  398. end;
  399. tfloatdef = class(tstoreddef)
  400. floattype : tfloattype;
  401. constructor create(t : tfloattype);
  402. constructor ppuload(ppufile:tcompilerppufile);
  403. function getcopy : tstoreddef;override;
  404. procedure ppuwrite(ppufile:tcompilerppufile);override;
  405. function GetTypeName:string;override;
  406. function is_publishable : boolean;override;
  407. function alignment:shortint;override;
  408. function structalignment: shortint;override;
  409. procedure setsize;
  410. function getvardef:longint;override;
  411. end;
  412. { tabstractprocdef }
  413. tprocnameoption = (pno_showhidden, pno_proctypeoption, pno_paranames,
  414. pno_ownername, pno_noclassmarker, pno_noleadingdollar);
  415. tprocnameoptions = set of tprocnameoption;
  416. tproccopytyp = (pc_normal,
  417. { always creates a top-level function, removes all
  418. special parameters (self, vmt, parentfp, ...) }
  419. pc_bareproc
  420. );
  421. tabstractprocdef = class(tstoreddef)
  422. { saves a definition to the return type }
  423. returndef : tdef;
  424. returndefderef : tderef;
  425. parast : TSymtable;
  426. paras : tparalist;
  427. proctypeoption : tproctypeoption;
  428. proccalloption : tproccalloption;
  429. procoptions : tprocoptions;
  430. callerargareasize,
  431. calleeargareasize: pint;
  432. {$ifdef m68k}
  433. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  434. {$endif}
  435. funcretloc : array[tcallercallee] of TCGPara;
  436. has_paraloc_info : tcallercallee; { paraloc info is available }
  437. { number of user visible parameters }
  438. maxparacount,
  439. minparacount : byte;
  440. constructor create(dt:tdeftyp;level:byte);
  441. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  442. destructor destroy;override;
  443. procedure ppuwrite(ppufile:tcompilerppufile);override;
  444. procedure buildderef;override;
  445. procedure deref;override;
  446. procedure calcparas;
  447. function typename_paras(pno: tprocnameoptions): ansistring;
  448. function is_methodpointer:boolean;virtual;
  449. function is_addressonly:boolean;virtual;
  450. function no_self_node:boolean;
  451. { get either a copy as a procdef or procvardef }
  452. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef; virtual;
  453. procedure check_mark_as_nested;
  454. procedure init_paraloc_info(side: tcallercallee);
  455. function stack_tainting_parameter(side: tcallercallee): boolean;
  456. private
  457. procedure count_para(p:TObject;arg:pointer);
  458. procedure insert_para(p:TObject;arg:pointer);
  459. end;
  460. tprocvardef = class(tabstractprocdef)
  461. {$ifdef jvm}
  462. { class representing this procvar on the Java side }
  463. classdef : tobjectdef;
  464. classdefderef : tderef;
  465. {$endif}
  466. constructor create(level:byte);
  467. constructor ppuload(ppufile:tcompilerppufile);
  468. function getcopy : tstoreddef;override;
  469. procedure ppuwrite(ppufile:tcompilerppufile);override;
  470. {$ifdef jvm}
  471. procedure buildderef;override;
  472. procedure deref;override;
  473. {$endif}
  474. function GetSymtable(t:tGetSymtable):TSymtable;override;
  475. function size : asizeint;override;
  476. function GetTypeName:string;override;
  477. function is_publishable : boolean;override;
  478. function is_methodpointer:boolean;override;
  479. function is_addressonly:boolean;override;
  480. function getmangledparaname:TSymStr;override;
  481. end;
  482. tmessageinf = record
  483. case integer of
  484. 0 : (str : pshortstring);
  485. 1 : (i : longint);
  486. end;
  487. tinlininginfo = record
  488. { node tree }
  489. code : tnode;
  490. flags : tprocinfoflags;
  491. end;
  492. pinlininginfo = ^tinlininginfo;
  493. { kinds of synthetic procdefs that can be generated }
  494. tsynthetickind = (
  495. tsk_none,
  496. tsk_anon_inherited, // anonymous inherited call
  497. tsk_jvm_clone, // Java-style clone method
  498. tsk_record_deepcopy, // deepcopy for records field by field
  499. tsk_record_initialize, // initialize for records field by field (explicit rather than via rtti)
  500. tsk_empty, // an empty routine
  501. tsk_tcinit, // initialisation of typed constants
  502. tsk_callthrough, // call through to another routine with the same parameters/return type (its def is stored in the skpara field)
  503. tsk_callthrough_nonabstract,// call through to another routine if the current class not abstract (otherwise do the same as tsk_empty)
  504. tsk_jvm_enum_values, // Java "values" class method of JLEnum descendants
  505. tsk_jvm_enum_valueof, // Java "valueOf" class method of JLEnum descendants
  506. tsk_jvm_enum_classconstr, // Java class constructor for JLEnum descendants
  507. tsk_jvm_enum_jumps_constr, // Java constructor for JLEnum descendants for enums with jumps
  508. tsk_jvm_enum_fpcordinal, // Java FPCOrdinal function that returns the enum's ordinal value from an FPC POV
  509. tsk_jvm_enum_fpcvalueof, // Java FPCValueOf function that returns the enum instance corresponding to an ordinal from an FPC POV
  510. tsk_jvm_enum_long2set, // Java fpcLongToEnumSet function that returns an enumset corresponding to a bit pattern in a jlong
  511. tsk_jvm_enum_bitset2set, // Java fpcBitSetToEnumSet function that returns an enumset corresponding to a BitSet
  512. tsk_jvm_enum_set2Set, // Java fpcEnumSetToEnumSet function that returns an enumset corresponding to another enumset (different enum kind)
  513. tsk_jvm_procvar_invoke, // Java invoke method that calls a wrapped procvar
  514. tsk_jvm_procvar_intconstr, // Java procvar class constructor that accepts an interface instance for easy Java interoperation
  515. tsk_jvm_virtual_clmethod, // Java wrapper for virtual class method
  516. tsk_field_getter, // getter for a field (callthrough property is passed in skpara)
  517. tsk_field_setter // Setter for a field (callthrough property is passed in skpara)
  518. );
  519. {$ifdef oldregvars}
  520. { register variables }
  521. pregvarinfo = ^tregvarinfo;
  522. tregvarinfo = record
  523. regvars : array[1..maxvarregs] of tsym;
  524. regvars_para : array[1..maxvarregs] of boolean;
  525. regvars_refs : array[1..maxvarregs] of longint;
  526. fpuregvars : array[1..maxfpuvarregs] of tsym;
  527. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  528. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  529. end;
  530. {$endif oldregvars}
  531. { tprocdef }
  532. tprocdef = class(tabstractprocdef)
  533. private
  534. {$ifdef symansistr}
  535. _mangledname : ansistring;
  536. {$else symansistr}
  537. _mangledname : pshortstring;
  538. {$endif}
  539. public
  540. messageinf : tmessageinf;
  541. dispid : longint;
  542. {$ifndef EXTDEBUG}
  543. { where is this function defined and what were the symbol
  544. flags, needed here because there
  545. is only one symbol for all overloaded functions
  546. EXTDEBUG has fileinfo in tdef (PFV) }
  547. fileinfo : tfileposinfo;
  548. {$endif}
  549. symoptions : tsymoptions;
  550. deprecatedmsg : pshortstring;
  551. { symbol owning this definition }
  552. procsym : tsym;
  553. procsymderef : tderef;
  554. { alias names }
  555. aliasnames : TCmdStrList;
  556. { symtables }
  557. localst : TSymtable;
  558. funcretsym : tsym;
  559. funcretsymderef : tderef;
  560. struct : tabstractrecorddef;
  561. structderef : tderef;
  562. {$if defined(powerpc) or defined(m68k)}
  563. { library symbol for AmigaOS/MorphOS }
  564. libsym : tsym;
  565. libsymderef : tderef;
  566. {$endif powerpc or m68k}
  567. { name of the result variable to insert in the localsymtable }
  568. resultname : pshortstring;
  569. { import info }
  570. import_dll,
  571. import_name : pshortstring;
  572. { info for inlining the subroutine, if this pointer is nil,
  573. the procedure can't be inlined }
  574. inlininginfo : pinlininginfo;
  575. {$ifdef jvm}
  576. { generated assembler code; used by JVM backend so it can afterwards
  577. easily write out all methods grouped per class }
  578. exprasmlist : TAsmList;
  579. {$endif jvm}
  580. { temporary reference to structure containing copies of all local
  581. variables and parameters accessed by nested routines; reference to
  582. this structure is passed as "parent frame pointer" on targets that
  583. lack this concept (at least JVM and LLVM); no need to save to/
  584. restore from ppu, since nested routines are always in the same
  585. unit (no need to save to ppu) }
  586. parentfpstruct: tsym;
  587. { pointer to parentfpstruct's type (not yet valid during parsing, so
  588. cannot be used for $parentfp parameter) (no need to save to ppu) }
  589. parentfpstructptrtype: tdef;
  590. { code to copy the parameters accessed from nested routines into the
  591. parentfpstruct (no need to save to ppu) }
  592. parentfpinitblock: tnode;
  593. {$ifdef oldregvars}
  594. regvarinfo: pregvarinfo;
  595. {$endif oldregvars}
  596. { First/last assembler symbol/instruction in aasmoutput list.
  597. Note: initialised after compiling the code for the procdef, but
  598. not saved to/restored from ppu. Used when inserting debug info }
  599. procstarttai,
  600. procendtai : tai;
  601. import_nr : word;
  602. extnumber : word;
  603. {$ifdef i386}
  604. fpu_used : byte;
  605. {$endif i386}
  606. {$ifdef mips}
  607. { needed for stabs debugging }
  608. total_local_size : longint;
  609. {$endif mips}
  610. visibility : tvisibility;
  611. { set to a value different from tsk_none in case this procdef is for
  612. a routine that has to be internally generated by the compiler }
  613. synthetickind : tsynthetickind;
  614. { optional parameter for the synthetic routine generation logic }
  615. skpara: pointer;
  616. { true, if the procedure is only declared
  617. (forward procedure) }
  618. forwarddef,
  619. { true if the procedure is declared in the interface }
  620. interfacedef : boolean;
  621. { true if the procedure has a forward declaration }
  622. hasforward : boolean;
  623. constructor create(level:byte);
  624. constructor ppuload(ppufile:tcompilerppufile);
  625. destructor destroy;override;
  626. procedure ppuwrite(ppufile:tcompilerppufile);override;
  627. procedure buildderef;override;
  628. procedure buildderefimpl;override;
  629. procedure deref;override;
  630. procedure derefimpl;override;
  631. function GetSymtable(t:tGetSymtable):TSymtable;override;
  632. { warnings:
  633. * the symtablestack top has to be the symtable to which the copy
  634. should be added
  635. * getcopy does not create a finished/ready-to-use procdef; it
  636. needs to be finalised afterwards by calling
  637. symcreat.finish_copied_procdef() afterwards
  638. }
  639. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef; override;
  640. function getcopy: tstoreddef; override;
  641. function GetTypeName : string;override;
  642. function mangledname : TSymStr;
  643. procedure setmangledname(const s : TSymStr);
  644. function fullprocname(showhidden:boolean):string;
  645. function customprocname(pno: tprocnameoptions):ansistring;
  646. function defaultmangledname: TSymStr;
  647. function cplusplusmangledname : TSymStr;
  648. function objcmangledname : TSymStr;
  649. {$ifdef jvm}
  650. function jvmmangledbasename(signature: boolean): TSymStr;
  651. {$endif}
  652. function is_methodpointer:boolean;override;
  653. function is_addressonly:boolean;override;
  654. procedure make_external;
  655. end;
  656. { single linked list of overloaded procs }
  657. pprocdeflist = ^tprocdeflist;
  658. tprocdeflist = record
  659. def : tprocdef;
  660. defderef : tderef;
  661. next : pprocdeflist;
  662. end;
  663. tstringdef = class(tstoreddef)
  664. encoding : tstringencoding;
  665. stringtype : tstringtype;
  666. len : asizeint;
  667. constructor createshort(l : byte);
  668. constructor loadshort(ppufile:tcompilerppufile);
  669. constructor createlong(l : asizeint);
  670. constructor loadlong(ppufile:tcompilerppufile);
  671. constructor createansi(aencoding:tstringencoding);
  672. constructor loadansi(ppufile:tcompilerppufile);
  673. constructor createwide;
  674. constructor loadwide(ppufile:tcompilerppufile);
  675. constructor createunicode;
  676. constructor loadunicode(ppufile:tcompilerppufile);
  677. function getcopy : tstoreddef;override;
  678. function stringtypname:string;
  679. procedure ppuwrite(ppufile:tcompilerppufile);override;
  680. function GetTypeName:string;override;
  681. function getmangledparaname:TSymStr;override;
  682. function is_publishable : boolean;override;
  683. function alignment : shortint;override;
  684. function needs_inittable : boolean;override;
  685. function getvardef:longint;override;
  686. function is_related(d : tdef) : boolean;override;
  687. end;
  688. { tenumdef }
  689. tenumdef = class(tstoreddef)
  690. minval,
  691. maxval : asizeint;
  692. basedef : tenumdef;
  693. basedefderef : tderef;
  694. symtable : TSymtable;
  695. {$ifdef jvm}
  696. { class representing this enum on the Java side }
  697. classdef : tobjectdef;
  698. classdefderef : tderef;
  699. {$endif}
  700. has_jumps : boolean;
  701. constructor create;
  702. constructor create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  703. constructor ppuload(ppufile:tcompilerppufile);
  704. destructor destroy;override;
  705. function getcopy : tstoreddef;override;
  706. procedure ppuwrite(ppufile:tcompilerppufile);override;
  707. procedure buildderef;override;
  708. procedure deref;override;
  709. function GetTypeName:string;override;
  710. function is_publishable : boolean;override;
  711. procedure calcsavesize;
  712. function packedbitsize: asizeint; override;
  713. procedure setmax(_max:asizeint);
  714. procedure setmin(_min:asizeint);
  715. function min:asizeint;
  716. function max:asizeint;
  717. function getfirstsym:tsym;
  718. function int2enumsym(l: asizeint): tsym;
  719. { returns basedef if assigned, otherwise self }
  720. function getbasedef: tenumdef;
  721. end;
  722. tsetdef = class(tstoreddef)
  723. elementdef : tdef;
  724. elementdefderef : tderef;
  725. setbase,
  726. setmax : aword;
  727. constructor create(def:tdef;low, high : asizeint);
  728. constructor ppuload(ppufile:tcompilerppufile);
  729. function getcopy : tstoreddef;override;
  730. procedure ppuwrite(ppufile:tcompilerppufile);override;
  731. procedure buildderef;override;
  732. procedure deref;override;
  733. function GetTypeName:string;override;
  734. function is_publishable : boolean;override;
  735. end;
  736. tdefawaresymtablestack = class(TSymtablestack)
  737. private
  738. procedure addhelpers(st: TSymtable);
  739. procedure removehelpers(st: TSymtable);
  740. public
  741. procedure push(st: TSymtable); override;
  742. procedure pop(st: TSymtable); override;
  743. end;
  744. var
  745. current_structdef: tabstractrecorddef; { used for private functions check !! }
  746. current_genericdef: tstoreddef; { used to reject declaration of generic class inside generic class }
  747. current_specializedef: tstoreddef; { used to implement usage of generic class in itself }
  748. { default types }
  749. generrordef, { error in definition }
  750. voidpointertype, { pointer for Void-pointeddef }
  751. charpointertype, { pointer for Char-pointeddef }
  752. widecharpointertype, { pointer for WideChar-pointeddef }
  753. voidfarpointertype,
  754. cundefinedtype,
  755. cformaltype, { unique formal definition }
  756. ctypedformaltype, { unique typed formal definition }
  757. voidtype, { Void (procedure) }
  758. cansichartype, { Char }
  759. cwidechartype, { WideChar }
  760. pasbool8type, { boolean type }
  761. pasbool16type,
  762. pasbool32type,
  763. pasbool64type,
  764. bool8type,
  765. bool16type,
  766. bool32type,
  767. bool64type, { implement me }
  768. u8inttype, { 8-Bit unsigned integer }
  769. s8inttype, { 8-Bit signed integer }
  770. u16inttype, { 16-Bit unsigned integer }
  771. s16inttype, { 16-Bit signed integer }
  772. u32inttype, { 32-Bit unsigned integer }
  773. s32inttype, { 32-Bit signed integer }
  774. u64inttype, { 64-bit unsigned integer }
  775. s64inttype, { 64-bit signed integer }
  776. s32floattype, { 32 bit floating point number }
  777. s64floattype, { 64 bit floating point number }
  778. s80floattype, { 80 bit floating point number }
  779. sc80floattype, { 80 bit floating point number but stored like in C }
  780. s64currencytype, { pointer to a currency type }
  781. cshortstringtype, { pointer to type of short string const }
  782. clongstringtype, { pointer to type of long string const }
  783. cansistringtype, { pointer to type of ansi string const }
  784. cwidestringtype, { pointer to type of wide string const }
  785. cunicodestringtype,
  786. openshortstringtype, { pointer to type of an open shortstring,
  787. needed for readln() }
  788. openchararraytype, { pointer to type of an open array of char,
  789. needed for readln() }
  790. cfiletype, { get the same definition for all file }
  791. { used for stabs }
  792. methodpointertype, { typecasting of methodpointers to extract self }
  793. hresultdef,
  794. { we use only one variant def for every variant class }
  795. cvarianttype,
  796. colevarianttype,
  797. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  798. sinttype,
  799. uinttype,
  800. { integer types corresponding to OS_SINT/OS_INT }
  801. ossinttype,
  802. osuinttype,
  803. { unsigned and signed ord type with the same size as a pointer }
  804. ptruinttype,
  805. ptrsinttype,
  806. { several types to simulate more or less C++ objects for GDB }
  807. vmttype,
  808. vmtarraytype,
  809. pvmttype : tdef; { type of classrefs, used for stabs }
  810. { pointer to the anchestor of all classes }
  811. class_tobject : tobjectdef;
  812. { pointer to the ancestor of all COM interfaces }
  813. interface_iunknown : tobjectdef;
  814. { pointer to the ancestor of all dispinterfaces }
  815. interface_idispatch : tobjectdef;
  816. { pointer to the TGUID type
  817. of all interfaces }
  818. rec_tguid : trecorddef;
  819. { pointer to jump buffer }
  820. rec_jmp_buf : trecorddef;
  821. { Objective-C base types }
  822. objc_metaclasstype,
  823. objc_superclasstype,
  824. objc_idtype,
  825. objc_seltype : tpointerdef;
  826. objc_objecttype : trecorddef;
  827. { base type of @protocol(protocolname) Objective-C statements }
  828. objc_protocoltype : tobjectdef;
  829. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  830. objc_fastenumeration : tobjectdef;
  831. objc_fastenumerationstate : trecorddef;
  832. { Java base types }
  833. { java.lang.Object }
  834. java_jlobject : tobjectdef;
  835. { java.lang.Throwable }
  836. java_jlthrowable : tobjectdef;
  837. { FPC base type for records }
  838. java_fpcbaserecordtype : tobjectdef;
  839. { java.lang.String }
  840. java_jlstring : tobjectdef;
  841. { java.lang.Enum }
  842. java_jlenum : tobjectdef;
  843. { java.util.EnumSet }
  844. java_juenumset : tobjectdef;
  845. { java.util.BitSet }
  846. java_jubitset : tobjectdef;
  847. { FPC java implementation of ansistrings }
  848. java_ansistring : tobjectdef;
  849. { FPC java implementation of shortstrings }
  850. java_shortstring : tobjectdef;
  851. { FPC java procvar base class }
  852. java_procvarbase : tobjectdef;
  853. const
  854. {$ifdef i386}
  855. pbestrealtype : ^tdef = @s80floattype;
  856. {$endif}
  857. {$ifdef x86_64}
  858. pbestrealtype : ^tdef = @s80floattype;
  859. {$endif}
  860. {$ifdef m68k}
  861. pbestrealtype : ^tdef = @s64floattype;
  862. {$endif}
  863. {$ifdef alpha}
  864. pbestrealtype : ^tdef = @s64floattype;
  865. {$endif}
  866. {$ifdef powerpc}
  867. pbestrealtype : ^tdef = @s64floattype;
  868. {$endif}
  869. {$ifdef POWERPC64}
  870. pbestrealtype : ^tdef = @s64floattype;
  871. {$endif}
  872. {$ifdef ia64}
  873. pbestrealtype : ^tdef = @s64floattype;
  874. {$endif}
  875. {$ifdef SPARC}
  876. pbestrealtype : ^tdef = @s64floattype;
  877. {$endif SPARC}
  878. {$ifdef vis}
  879. pbestrealtype : ^tdef = @s64floattype;
  880. {$endif vis}
  881. {$ifdef ARM}
  882. pbestrealtype : ^tdef = @s64floattype;
  883. {$endif ARM}
  884. {$ifdef MIPS}
  885. pbestrealtype : ^tdef = @s64floattype;
  886. {$endif MIPS}
  887. {$ifdef AVR}
  888. pbestrealtype : ^tdef = @s64floattype;
  889. {$endif AVR}
  890. {$ifdef JVM}
  891. pbestrealtype : ^tdef = @s64floattype;
  892. {$endif JVM}
  893. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  894. function make_dllmangledname(const dllname,importname:TSymStr;
  895. import_nr : word; pco : tproccalloption):TSymStr;
  896. { should be in the types unit, but the types unit uses the node stuff :( }
  897. function is_interfacecom(def: tdef): boolean;
  898. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  899. function is_any_interface_kind(def: tdef): boolean;
  900. function is_interfacecorba(def: tdef): boolean;
  901. function is_interface(def: tdef): boolean;
  902. function is_dispinterface(def: tdef): boolean;
  903. function is_object(def: tdef): boolean;
  904. function is_class(def: tdef): boolean;
  905. function is_cppclass(def: tdef): boolean;
  906. function is_objectpascal_helper(def: tdef): boolean;
  907. function is_objcclass(def: tdef): boolean;
  908. function is_objcclassref(def: tdef): boolean;
  909. function is_objcprotocol(def: tdef): boolean;
  910. function is_objccategory(def: tdef): boolean;
  911. function is_objc_class_or_protocol(def: tdef): boolean;
  912. function is_objc_protocol_or_category(def: tdef): boolean;
  913. function is_classhelper(def: tdef): boolean;
  914. function is_class_or_interface(def: tdef): boolean;
  915. function is_class_or_interface_or_objc(def: tdef): boolean;
  916. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  917. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  918. function is_class_or_interface_or_object(def: tdef): boolean;
  919. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  920. function is_implicit_pointer_object_type(def: tdef): boolean;
  921. function is_class_or_object(def: tdef): boolean;
  922. function is_record(def: tdef): boolean;
  923. function is_javaclass(def: tdef): boolean;
  924. function is_javaclassref(def: tdef): boolean;
  925. function is_javainterface(def: tdef): boolean;
  926. function is_java_class_or_interface(def: tdef): boolean;
  927. procedure loadobjctypes;
  928. procedure maybeloadcocoatypes;
  929. function use_vectorfpu(def : tdef) : boolean;
  930. { returns a pointerdef for def, reusing an existing one in case it exists
  931. in the current module }
  932. function getpointerdef(def: tdef): tpointerdef;
  933. { returns an arraydef for an array containing a single array of def, resuing
  934. an existing one in case it exists in the current module }
  935. function getsingletonarraydef(def: tdef): tarraydef;
  936. function getarraydef(def: tdef; elecount: asizeint): tarraydef;
  937. function getansistringcodepage:tstringencoding; inline;
  938. function getansistringdef:tstringdef;
  939. function getparaencoding(def:tdef):tstringencoding; inline;
  940. implementation
  941. uses
  942. SysUtils,
  943. cutils,
  944. { global }
  945. verbose,
  946. { target }
  947. systems,paramgr,
  948. { symtable }
  949. symsym,symtable,defutil,objcdef,
  950. {$ifdef jvm}
  951. jvmdef,
  952. {$endif}
  953. { module }
  954. fmodule,
  955. { other }
  956. gendef,
  957. fpccrc
  958. ;
  959. {****************************************************************************
  960. Helpers
  961. ****************************************************************************}
  962. function getansistringcodepage:tstringencoding; inline;
  963. begin
  964. if cs_explicit_codepage in current_settings.moduleswitches then
  965. result:=current_settings.sourcecodepage
  966. else
  967. result:=0;
  968. end;
  969. function getansistringdef:tstringdef;
  970. var
  971. symtable:tsymtable;
  972. begin
  973. { if codepage is explicitly defined in this mudule we need to return
  974. a replacement for ansistring def }
  975. if cs_explicit_codepage in current_settings.moduleswitches then
  976. begin
  977. if not assigned(current_module) then
  978. internalerror(2011101301);
  979. { codepage can be redeclared only once per unit so we don't need a list of
  980. redefined ansistring but only one pointer }
  981. if not assigned(current_module.ansistrdef) then
  982. begin
  983. { if we did not create it yet we need to do this now }
  984. if current_module.is_unit then
  985. symtable:=current_module.globalsymtable
  986. else
  987. symtable:=current_module.localsymtable;
  988. symtablestack.push(symtable);
  989. current_module.ansistrdef:=tstringdef.createansi(current_settings.sourcecodepage);
  990. symtablestack.pop(symtable);
  991. end;
  992. result:=tstringdef(current_module.ansistrdef);
  993. end
  994. else
  995. result:=tstringdef(cansistringtype);
  996. end;
  997. function getparaencoding(def:tdef):tstringencoding; inline;
  998. begin
  999. { don't pass CP_NONE encoding to internal functions
  1000. they expect 0 encoding instead }
  1001. result:=tstringdef(def).encoding;
  1002. if result=CP_NONE then
  1003. result:=0
  1004. end;
  1005. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  1006. var
  1007. s,hs,
  1008. prefix : TSymStr;
  1009. oldlen,
  1010. newlen,
  1011. i : longint;
  1012. crc : dword;
  1013. hp : tparavarsym;
  1014. begin
  1015. prefix:='';
  1016. if not assigned(st) then
  1017. internalerror(200204212);
  1018. { sub procedures }
  1019. while (st.symtabletype=localsymtable) do
  1020. begin
  1021. if st.defowner.typ<>procdef then
  1022. internalerror(200204173);
  1023. { Add the full mangledname of procedure to prevent
  1024. conflicts with 2 overloads having both a nested procedure
  1025. with the same name, see tb0314 (PFV) }
  1026. s:=tprocdef(st.defowner).procsym.name;
  1027. oldlen:=length(s);
  1028. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  1029. begin
  1030. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  1031. if not(vo_is_hidden_para in hp.varoptions) then
  1032. s:=s+'$'+hp.vardef.mangledparaname;
  1033. end;
  1034. if not is_void(tprocdef(st.defowner).returndef) then
  1035. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  1036. newlen:=length(s);
  1037. { Replace with CRC if the parameter line is very long }
  1038. if (newlen-oldlen>12) and
  1039. ((newlen+length(prefix)>100) or (newlen-oldlen>32)) then
  1040. begin
  1041. crc:=0;
  1042. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  1043. begin
  1044. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  1045. if not(vo_is_hidden_para in hp.varoptions) then
  1046. begin
  1047. hs:=hp.vardef.mangledparaname;
  1048. crc:=UpdateCrc32(crc,hs[1],length(hs));
  1049. end;
  1050. end;
  1051. hs:=hp.vardef.mangledparaname;
  1052. crc:=UpdateCrc32(crc,hs[1],length(hs));
  1053. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  1054. end;
  1055. if prefix<>'' then
  1056. prefix:=s+'_'+prefix
  1057. else
  1058. prefix:=s;
  1059. if length(prefix)>100 then
  1060. begin
  1061. crc:=0;
  1062. crc:=UpdateCrc32(crc,prefix[1],length(prefix));
  1063. prefix:='$CRC'+hexstr(crc,8);
  1064. end;
  1065. st:=st.defowner.owner;
  1066. end;
  1067. { object/classes symtable, nested type definitions in classes require the while loop }
  1068. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  1069. begin
  1070. if not (st.defowner.typ in [objectdef,recorddef]) then
  1071. internalerror(200204174);
  1072. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  1073. st:=st.defowner.owner;
  1074. end;
  1075. { symtable must now be static or global }
  1076. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  1077. internalerror(200204175);
  1078. { The mangled name is made out of at most 4 parts:
  1079. 1) Optional typeprefix given as first parameter
  1080. with '_$' appended if not empty
  1081. 2) Unit name or 'P$'+program name (never empty)
  1082. 3) optional prefix variable that contains a unique
  1083. name for the local symbol table (prepended with '$_$'
  1084. if not empty)
  1085. 4) suffix as given as third parameter,
  1086. also optional (i.e. can be empty)
  1087. prepended by '_$$_' if not empty }
  1088. result:='';
  1089. if typeprefix<>'' then
  1090. result:=result+typeprefix+'_$';
  1091. { Add P$ for program, which can have the same name as
  1092. a unit }
  1093. if (TSymtable(main_module.localsymtable)=st) and
  1094. (not main_module.is_unit) then
  1095. result:=result+'P$'+st.name^
  1096. else
  1097. result:=result+st.name^;
  1098. if prefix<>'' then
  1099. result:=result+'$_$'+prefix;
  1100. if suffix<>'' then
  1101. result:=result+'_$$_'+suffix;
  1102. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  1103. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  1104. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  1105. { those in the debug map, leading to troubles with dsymutil). So always }
  1106. { add an underscore on darwin. }
  1107. if (target_info.system in systems_darwin) then
  1108. result := '_' + result;
  1109. end;
  1110. function make_dllmangledname(const dllname,importname:TSymStr;import_nr : word; pco : tproccalloption):TSymStr;
  1111. var
  1112. crc : cardinal;
  1113. i : longint;
  1114. use_crc : boolean;
  1115. dllprefix : TSymStr;
  1116. begin
  1117. if (target_info.system in (systems_all_windows + systems_nativent +
  1118. [system_i386_emx, system_i386_os2]))
  1119. and (dllname <> '') then
  1120. begin
  1121. dllprefix:=lower(ExtractFileName(dllname));
  1122. { Remove .dll suffix if present }
  1123. if copy(dllprefix,length(dllprefix)-3,length(dllprefix))='.dll' then
  1124. dllprefix:=copy(dllprefix,1,length(dllprefix)-4);
  1125. use_crc:=false;
  1126. for i:=1 to length(dllprefix) do
  1127. if not (dllprefix[i] in ['a'..'z','A'..'Z','_','0'..'9']) then
  1128. begin
  1129. use_crc:=true;
  1130. break;
  1131. end;
  1132. if use_crc then
  1133. begin
  1134. crc:=0;
  1135. crc:=UpdateCrc32(crc,dllprefix[1],length(dllprefix));
  1136. dllprefix:='_$dll$crc$'+hexstr(crc,8)+'$';
  1137. end
  1138. else
  1139. dllprefix:='_$dll$'+dllprefix+'$';
  1140. if importname<>'' then
  1141. result:=dllprefix+importname
  1142. else
  1143. result:=dllprefix+'_index_'+tostr(import_nr);
  1144. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  1145. { This allows to import VC++ mangled names from DLLs. }
  1146. { Do not perform replacement, if external symbol is not imported from DLL. }
  1147. if (dllname<>'') then
  1148. begin
  1149. Replace(result,'?','__q$$');
  1150. {$ifdef arm}
  1151. { @ symbol is not allowed in ARM assembler only }
  1152. Replace(result,'@','__a$$');
  1153. {$endif arm}
  1154. end;
  1155. end
  1156. else
  1157. begin
  1158. if importname<>'' then
  1159. begin
  1160. if not(pco in [pocall_cdecl,pocall_cppdecl]) then
  1161. result:=importname
  1162. else
  1163. result:=target_info.Cprefix+importname;
  1164. end
  1165. else
  1166. result:='_index_'+tostr(import_nr);
  1167. end;
  1168. end;
  1169. {****************************************************************************
  1170. TDEFAWARESYMTABLESTACK
  1171. (symtablestack descendant that does some special actions on
  1172. the pushed/popped symtables)
  1173. ****************************************************************************}
  1174. procedure tdefawaresymtablestack.addhelpers(st: TSymtable);
  1175. var
  1176. i: integer;
  1177. s: string;
  1178. list: TFPObjectList;
  1179. def: tdef;
  1180. begin
  1181. { search the symtable from first to last; the helper to use will be the
  1182. last one in the list }
  1183. for i:=0 to st.symlist.count-1 do
  1184. begin
  1185. if not (st.symlist[i] is ttypesym) then
  1186. continue;
  1187. def:=ttypesym(st.SymList[i]).typedef;
  1188. if is_objectpascal_helper(def) and
  1189. (tobjectdef(def).extendeddef.typ in [recorddef,objectdef]) then
  1190. begin
  1191. s:=make_mangledname('',tabstractrecorddef(tobjectdef(def).extendeddef).symtable,'');
  1192. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1193. if not assigned(list) then
  1194. begin
  1195. list:=TFPObjectList.Create(false);
  1196. current_module.extendeddefs.Add(s,list);
  1197. end;
  1198. list.Add(def);
  1199. end
  1200. else
  1201. { add nested helpers as well }
  1202. if def.typ in [recorddef,objectdef] then
  1203. addhelpers(tabstractrecorddef(def).symtable);
  1204. end;
  1205. end;
  1206. procedure tdefawaresymtablestack.removehelpers(st: TSymtable);
  1207. var
  1208. i, j: integer;
  1209. tmpst: TSymtable;
  1210. list: TFPObjectList;
  1211. begin
  1212. for i:=current_module.extendeddefs.count-1 downto 0 do
  1213. begin
  1214. list:=TFPObjectList(current_module.extendeddefs[i]);
  1215. for j:=list.count-1 downto 0 do
  1216. begin
  1217. if not (list[j] is tobjectdef) then
  1218. Internalerror(2011031501);
  1219. tmpst:=tobjectdef(list[j]).owner;
  1220. repeat
  1221. if tmpst=st then
  1222. begin
  1223. list.delete(j);
  1224. break;
  1225. end
  1226. else
  1227. begin
  1228. if assigned(tmpst.defowner) then
  1229. tmpst:=tmpst.defowner.owner
  1230. else
  1231. tmpst:=nil;
  1232. end;
  1233. until not assigned(tmpst) or (tmpst.symtabletype in [globalsymtable,staticsymtable]);
  1234. end;
  1235. if list.count=0 then
  1236. current_module.extendeddefs.delete(i);
  1237. end;
  1238. end;
  1239. procedure tdefawaresymtablestack.push(st: TSymtable);
  1240. begin
  1241. { nested helpers will be added as well }
  1242. if (st.symtabletype in [globalsymtable,staticsymtable]) and
  1243. (sto_has_helper in st.tableoptions) then
  1244. addhelpers(st);
  1245. inherited push(st);
  1246. end;
  1247. procedure tdefawaresymtablestack.pop(st: TSymtable);
  1248. begin
  1249. inherited pop(st);
  1250. { nested helpers will be removed as well }
  1251. if (st.symtabletype in [globalsymtable,staticsymtable]) and
  1252. (sto_has_helper in st.tableoptions) then
  1253. removehelpers(st);
  1254. end;
  1255. {****************************************************************************
  1256. TDEF (base class for definitions)
  1257. ****************************************************************************}
  1258. procedure tstoreddef.fillgenericparas(symtable: tsymtable);
  1259. var
  1260. sym : tsym;
  1261. i : longint;
  1262. begin
  1263. if not assigned(symtable) then
  1264. internalerror(2012091201);
  1265. if [df_generic,df_specialization]*defoptions=[] then
  1266. exit;
  1267. for i:=0 to symtable.symlist.count-1 do
  1268. begin
  1269. sym:=tsym(symtable.symlist[i]);
  1270. if sp_generic_para in sym.symoptions then
  1271. genericparas.Add(sym.name,sym);
  1272. end;
  1273. end;
  1274. constructor tstoreddef.create(dt:tdeftyp);
  1275. var
  1276. insertstack : psymtablestackitem;
  1277. begin
  1278. inherited create(dt);
  1279. savesize := 0;
  1280. {$ifdef EXTDEBUG}
  1281. fileinfo := current_filepos;
  1282. {$endif}
  1283. generictokenbuf:=nil;
  1284. genericdef:=nil;
  1285. genericparas:=tfphashobjectlist.create(false);
  1286. { Don't register forwarddefs, they are disposed at the
  1287. end of an type block }
  1288. if (dt=forwarddef) then
  1289. exit;
  1290. { Register in current_module }
  1291. if assigned(current_module) then
  1292. begin
  1293. current_module.deflist.Add(self);
  1294. DefId:=current_module.deflist.Count-1;
  1295. end;
  1296. { Register in symtable stack }
  1297. if assigned(symtablestack) then
  1298. begin
  1299. insertstack:=symtablestack.stack;
  1300. while assigned(insertstack) and
  1301. (insertstack^.symtable.symtabletype=withsymtable) do
  1302. insertstack:=insertstack^.next;
  1303. if not assigned(insertstack) then
  1304. internalerror(200602044);
  1305. insertstack^.symtable.insertdef(self);
  1306. end;
  1307. end;
  1308. destructor tstoreddef.destroy;
  1309. begin
  1310. { Direct calls are not allowed, use symtable.deletedef() }
  1311. if assigned(owner) then
  1312. internalerror(200612311);
  1313. if assigned(generictokenbuf) then
  1314. begin
  1315. generictokenbuf.free;
  1316. generictokenbuf:=nil;
  1317. end;
  1318. genericparas.free;
  1319. inherited destroy;
  1320. end;
  1321. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1322. var
  1323. sizeleft,i : longint;
  1324. buf : array[0..255] of byte;
  1325. begin
  1326. inherited create(dt);
  1327. genericparas:=tfphashobjectlist.create(false);
  1328. DefId:=ppufile.getlongint;
  1329. current_module.deflist[DefId]:=self;
  1330. {$ifdef EXTDEBUG}
  1331. fillchar(fileinfo,sizeof(fileinfo),0);
  1332. {$endif}
  1333. { load }
  1334. ppufile.getderef(typesymderef);
  1335. ppufile.getsmallset(defoptions);
  1336. ppufile.getsmallset(defstates);
  1337. if df_generic in defoptions then
  1338. begin
  1339. sizeleft:=ppufile.getlongint;
  1340. initgeneric;
  1341. while sizeleft>0 do
  1342. begin
  1343. if sizeleft>sizeof(buf) then
  1344. i:=sizeof(buf)
  1345. else
  1346. i:=sizeleft;
  1347. ppufile.getdata(buf,i);
  1348. generictokenbuf.write(buf,i);
  1349. dec(sizeleft,i);
  1350. end;
  1351. end;
  1352. if df_specialization in defoptions then
  1353. ppufile.getderef(genericdefderef);
  1354. end;
  1355. function tstoreddef.needs_separate_initrtti:boolean;
  1356. begin
  1357. result:=false;
  1358. end;
  1359. function Tstoreddef.rtti_mangledname(rt:trttitype):string;
  1360. var
  1361. prefix : string[4];
  1362. begin
  1363. if (rt=fullrtti) or (not needs_separate_initrtti) then
  1364. begin
  1365. prefix:='RTTI';
  1366. include(defstates,ds_rtti_table_used);
  1367. end
  1368. else
  1369. begin
  1370. prefix:='INIT';
  1371. include(defstates,ds_init_table_used);
  1372. end;
  1373. if assigned(typesym) and
  1374. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  1375. result:=make_mangledname(prefix,owner,typesym.name)
  1376. else
  1377. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  1378. end;
  1379. function tstoreddef.OwnerHierarchyName: string;
  1380. var
  1381. tmp: tdef;
  1382. begin
  1383. tmp:=self;
  1384. result:='';
  1385. repeat
  1386. { can be not assigned in case of a forwarddef }
  1387. if assigned(tmp.owner) and
  1388. (tmp.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1389. tmp:=tdef(tmp.owner.defowner)
  1390. else
  1391. break;
  1392. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  1393. until tmp=nil;
  1394. end;
  1395. function tstoreddef.in_currentunit: boolean;
  1396. var
  1397. st: tsymtable;
  1398. begin
  1399. st:=owner;
  1400. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  1401. st:=st.defowner.owner;
  1402. result:=st.iscurrentunit;
  1403. end;
  1404. function tstoreddef.getcopy : tstoreddef;
  1405. begin
  1406. Message(sym_e_cant_create_unique_type);
  1407. getcopy:=terrordef.create;
  1408. end;
  1409. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  1410. var
  1411. sizeleft,i : longint;
  1412. buf : array[0..255] of byte;
  1413. oldintfcrc : boolean;
  1414. begin
  1415. ppufile.putlongint(DefId);
  1416. ppufile.putderef(typesymderef);
  1417. ppufile.putsmallset(defoptions);
  1418. oldintfcrc:=ppufile.do_crc;
  1419. ppufile.do_crc:=false;
  1420. ppufile.putsmallset(defstates);
  1421. if df_generic in defoptions then
  1422. begin
  1423. if assigned(generictokenbuf) then
  1424. begin
  1425. sizeleft:=generictokenbuf.size;
  1426. generictokenbuf.seek(0);
  1427. end
  1428. else
  1429. sizeleft:=0;
  1430. ppufile.putlongint(sizeleft);
  1431. while sizeleft>0 do
  1432. begin
  1433. if sizeleft>sizeof(buf) then
  1434. i:=sizeof(buf)
  1435. else
  1436. i:=sizeleft;
  1437. generictokenbuf.read(buf,i);
  1438. ppufile.putdata(buf,i);
  1439. dec(sizeleft,i);
  1440. end;
  1441. end;
  1442. ppufile.do_crc:=oldintfcrc;
  1443. if df_specialization in defoptions then
  1444. ppufile.putderef(genericdefderef);
  1445. end;
  1446. procedure tstoreddef.buildderef;
  1447. begin
  1448. typesymderef.build(typesym);
  1449. genericdefderef.build(genericdef);
  1450. end;
  1451. procedure tstoreddef.buildderefimpl;
  1452. begin
  1453. end;
  1454. procedure tstoreddef.deref;
  1455. begin
  1456. typesym:=ttypesym(typesymderef.resolve);
  1457. if df_specialization in defoptions then
  1458. genericdef:=tstoreddef(genericdefderef.resolve);
  1459. end;
  1460. procedure tstoreddef.derefimpl;
  1461. begin
  1462. end;
  1463. function tstoreddef.size : asizeint;
  1464. begin
  1465. size:=savesize;
  1466. end;
  1467. function tstoreddef.getvardef:longint;
  1468. begin
  1469. result:=varUndefined;
  1470. end;
  1471. function tstoreddef.alignment : shortint;
  1472. begin
  1473. { natural alignment by default }
  1474. alignment:=size_2_align(savesize);
  1475. { can happen if savesize = 0, e.g. for voiddef or
  1476. an empty record
  1477. }
  1478. if (alignment=0) then
  1479. alignment:=1;
  1480. end;
  1481. { returns true, if the definition can be published }
  1482. function tstoreddef.is_publishable : boolean;
  1483. begin
  1484. is_publishable:=false;
  1485. end;
  1486. { needs an init table }
  1487. function tstoreddef.needs_inittable : boolean;
  1488. begin
  1489. needs_inittable:=false;
  1490. end;
  1491. function tstoreddef.is_intregable : boolean;
  1492. var
  1493. recsize,temp: longint;
  1494. begin
  1495. is_intregable:=false;
  1496. case typ of
  1497. orddef,
  1498. pointerdef,
  1499. enumdef,
  1500. classrefdef:
  1501. is_intregable:=true;
  1502. procvardef :
  1503. is_intregable:=tprocvardef(self).is_addressonly or (po_methodpointer in tprocvardef(self).procoptions);
  1504. objectdef:
  1505. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  1506. setdef:
  1507. is_intregable:=is_smallset(self);
  1508. recorddef:
  1509. begin
  1510. recsize:=size;
  1511. is_intregable:=
  1512. ispowerof2(recsize,temp) and
  1513. (recsize <= sizeof(asizeint))
  1514. and not needs_inittable;
  1515. end;
  1516. end;
  1517. end;
  1518. function tstoreddef.is_fpuregable : boolean;
  1519. begin
  1520. {$ifdef x86}
  1521. result:=use_vectorfpu(self);
  1522. {$else x86}
  1523. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1524. {$endif x86}
  1525. end;
  1526. procedure tstoreddef.initgeneric;
  1527. begin
  1528. if assigned(generictokenbuf) then
  1529. internalerror(200512131);
  1530. generictokenbuf:=tdynamicarray.create(256);
  1531. end;
  1532. function tstoreddef.is_generic: boolean;
  1533. begin
  1534. result:=genericparas.count>0;
  1535. end;
  1536. {****************************************************************************
  1537. Tstringdef
  1538. ****************************************************************************}
  1539. constructor tstringdef.createshort(l : byte);
  1540. begin
  1541. inherited create(stringdef);
  1542. stringtype:=st_shortstring;
  1543. encoding:=0;
  1544. len:=l;
  1545. savesize:=len+1;
  1546. end;
  1547. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1548. begin
  1549. inherited ppuload(stringdef,ppufile);
  1550. stringtype:=st_shortstring;
  1551. encoding:=0;
  1552. len:=ppufile.getbyte;
  1553. savesize:=len+1;
  1554. end;
  1555. constructor tstringdef.createlong(l : asizeint);
  1556. begin
  1557. inherited create(stringdef);
  1558. stringtype:=st_longstring;
  1559. encoding:=0;
  1560. len:=l;
  1561. savesize:=sizeof(pint);
  1562. end;
  1563. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1564. begin
  1565. inherited ppuload(stringdef,ppufile);
  1566. stringtype:=st_longstring;
  1567. encoding:=0;
  1568. len:=ppufile.getasizeint;
  1569. savesize:=sizeof(pint);
  1570. end;
  1571. constructor tstringdef.createansi(aencoding:tstringencoding);
  1572. begin
  1573. inherited create(stringdef);
  1574. stringtype:=st_ansistring;
  1575. encoding:=aencoding;
  1576. len:=-1;
  1577. savesize:=sizeof(pint);
  1578. end;
  1579. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1580. begin
  1581. inherited ppuload(stringdef,ppufile);
  1582. stringtype:=st_ansistring;
  1583. len:=ppufile.getaint;
  1584. encoding:=ppufile.getword;
  1585. savesize:=sizeof(pint);
  1586. end;
  1587. constructor tstringdef.createwide;
  1588. begin
  1589. inherited create(stringdef);
  1590. stringtype:=st_widestring;
  1591. if target_info.endian=endian_little then
  1592. encoding:=CP_UTF16LE
  1593. else
  1594. encoding:=CP_UTF16BE;
  1595. len:=-1;
  1596. savesize:=sizeof(pint);
  1597. end;
  1598. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1599. begin
  1600. inherited ppuload(stringdef,ppufile);
  1601. stringtype:=st_widestring;
  1602. if target_info.endian=endian_little then
  1603. encoding:=CP_UTF16LE
  1604. else
  1605. encoding:=CP_UTF16BE;
  1606. len:=ppufile.getaint;
  1607. savesize:=sizeof(pint);
  1608. end;
  1609. constructor tstringdef.createunicode;
  1610. begin
  1611. inherited create(stringdef);
  1612. stringtype:=st_unicodestring;
  1613. if target_info.endian=endian_little then
  1614. encoding:=CP_UTF16LE
  1615. else
  1616. encoding:=CP_UTF16BE;
  1617. len:=-1;
  1618. savesize:=sizeof(pint);
  1619. end;
  1620. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  1621. begin
  1622. inherited ppuload(stringdef,ppufile);
  1623. stringtype:=st_unicodestring;
  1624. len:=ppufile.getaint;
  1625. encoding:=ppufile.getword;
  1626. savesize:=sizeof(pint);
  1627. end;
  1628. function tstringdef.getcopy : tstoreddef;
  1629. begin
  1630. result:=tstringdef.create(typ);
  1631. result.typ:=stringdef;
  1632. tstringdef(result).stringtype:=stringtype;
  1633. tstringdef(result).encoding:=encoding;
  1634. tstringdef(result).len:=len;
  1635. tstringdef(result).savesize:=savesize;
  1636. end;
  1637. function tstringdef.stringtypname:string;
  1638. const
  1639. typname:array[tstringtype] of string[10]=(
  1640. 'shortstr','longstr','ansistr','widestr','unicodestr'
  1641. );
  1642. begin
  1643. stringtypname:=typname[stringtype];
  1644. end;
  1645. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1646. begin
  1647. inherited ppuwrite(ppufile);
  1648. if stringtype=st_shortstring then
  1649. begin
  1650. {$ifdef extdebug}
  1651. if len > 255 then internalerror(12122002);
  1652. {$endif}
  1653. ppufile.putbyte(byte(len))
  1654. end
  1655. else
  1656. ppufile.putaint(len);
  1657. if stringtype in [st_ansistring,st_unicodestring] then
  1658. ppufile.putword(encoding);
  1659. case stringtype of
  1660. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1661. st_longstring : ppufile.writeentry(iblongstringdef);
  1662. st_ansistring : ppufile.writeentry(ibansistringdef);
  1663. st_widestring : ppufile.writeentry(ibwidestringdef);
  1664. st_unicodestring : ppufile.writeentry(ibunicodestringdef);
  1665. end;
  1666. end;
  1667. function tstringdef.needs_inittable : boolean;
  1668. begin
  1669. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1670. end;
  1671. function tstringdef.GetTypeName : string;
  1672. const
  1673. names : array[tstringtype] of string[15] = (
  1674. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  1675. begin
  1676. GetTypeName:=names[stringtype];
  1677. end;
  1678. function tstringdef.getvardef : longint;
  1679. const
  1680. vardef : array[tstringtype] of longint = (
  1681. varUndefined,varUndefined,varString,varOleStr,varUString);
  1682. begin
  1683. result:=vardef[stringtype];
  1684. end;
  1685. function tstringdef.is_related(d: tdef): boolean;
  1686. begin
  1687. result:=
  1688. (target_info.system in systems_jvm) and
  1689. (((stringtype in [st_unicodestring,st_widestring]) and
  1690. ((d=java_jlobject) or
  1691. (d=java_jlstring))) or
  1692. ((stringtype=st_ansistring) and
  1693. ((d=java_jlobject) or
  1694. (d=java_ansistring))));
  1695. end;
  1696. function tstringdef.alignment : shortint;
  1697. begin
  1698. case stringtype of
  1699. st_unicodestring,
  1700. st_widestring,
  1701. st_ansistring:
  1702. alignment:=size_2_align(savesize);
  1703. st_longstring,
  1704. st_shortstring:
  1705. { char to string accesses byte 0 and 1 with one word access }
  1706. if (tf_requires_proper_alignment in target_info.flags) or
  1707. { macpas needs an alignment of 2 (MetroWerks compatible) }
  1708. (m_mac in current_settings.modeswitches) then
  1709. alignment:=size_2_align(2)
  1710. else
  1711. alignment:=size_2_align(1);
  1712. else
  1713. internalerror(200412301);
  1714. end;
  1715. end;
  1716. function tstringdef.getmangledparaname : TSymStr;
  1717. begin
  1718. getmangledparaname:='STRING';
  1719. end;
  1720. function tstringdef.is_publishable : boolean;
  1721. begin
  1722. is_publishable:=true;
  1723. end;
  1724. {****************************************************************************
  1725. TENUMDEF
  1726. ****************************************************************************}
  1727. constructor tenumdef.create;
  1728. begin
  1729. inherited create(enumdef);
  1730. minval:=0;
  1731. maxval:=0;
  1732. calcsavesize;
  1733. has_jumps:=false;
  1734. basedef:=nil;
  1735. symtable:=tenumsymtable.create(self);
  1736. end;
  1737. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  1738. begin
  1739. inherited create(enumdef);
  1740. minval:=_min;
  1741. maxval:=_max;
  1742. basedef:=_basedef;
  1743. calcsavesize;
  1744. has_jumps:=false;
  1745. symtable:=basedef.symtable.getcopy;
  1746. include(defoptions, df_copied_def);
  1747. end;
  1748. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1749. begin
  1750. inherited ppuload(enumdef,ppufile);
  1751. minval:=ppufile.getaint;
  1752. maxval:=ppufile.getaint;
  1753. savesize:=ppufile.getaint;
  1754. has_jumps:=false;
  1755. {$ifdef jvm}
  1756. ppufile.getderef(classdefderef);
  1757. {$endif}
  1758. if df_copied_def in defoptions then
  1759. begin
  1760. symtable:=nil;
  1761. ppufile.getderef(basedefderef);
  1762. end
  1763. else
  1764. begin
  1765. // create with nil defowner first to prevent values changes on insert
  1766. symtable:=tenumsymtable.create(nil);
  1767. tenumsymtable(symtable).ppuload(ppufile);
  1768. symtable.defowner:=self;
  1769. end;
  1770. end;
  1771. destructor tenumdef.destroy;
  1772. begin
  1773. symtable.free;
  1774. symtable:=nil;
  1775. inherited destroy;
  1776. end;
  1777. function tenumdef.getcopy : tstoreddef;
  1778. begin
  1779. if assigned(basedef) then
  1780. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1781. else
  1782. begin
  1783. result:=tenumdef.create;
  1784. tenumdef(result).minval:=minval;
  1785. tenumdef(result).maxval:=maxval;
  1786. tenumdef(result).symtable.free;
  1787. tenumdef(result).symtable:=symtable.getcopy;
  1788. tenumdef(result).basedef:=self;
  1789. end;
  1790. {$ifdef jvm}
  1791. tenumdef(result).classdef:=classdef;
  1792. {$endif}
  1793. tenumdef(result).has_jumps:=has_jumps;
  1794. tenumdef(result).basedefderef:=basedefderef;
  1795. include(tenumdef(result).defoptions,df_copied_def);
  1796. end;
  1797. procedure tenumdef.calcsavesize;
  1798. begin
  1799. {$IFNDEF cpu64bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  1800. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1801. savesize:=8
  1802. {$IFDEF not cpu64bitaddr} {$pop} {$ENDIF}
  1803. else
  1804. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  1805. savesize:=4
  1806. else
  1807. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1808. savesize:=2
  1809. else
  1810. savesize:=1;
  1811. end;
  1812. function tenumdef.packedbitsize: asizeint;
  1813. var
  1814. sizeval: tconstexprint;
  1815. power: longint;
  1816. begin
  1817. result := 0;
  1818. if (minval >= 0) and
  1819. (maxval <= 1) then
  1820. result := 1
  1821. else
  1822. begin
  1823. if (minval>=0) then
  1824. sizeval:=maxval
  1825. else
  1826. { don't count 0 twice }
  1827. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  1828. { 256 must become 512 etc. }
  1829. nextpowerof2(sizeval+1,power);
  1830. result := power;
  1831. end;
  1832. end;
  1833. procedure tenumdef.setmax(_max:asizeint);
  1834. begin
  1835. maxval:=_max;
  1836. calcsavesize;
  1837. end;
  1838. procedure tenumdef.setmin(_min:asizeint);
  1839. begin
  1840. minval:=_min;
  1841. calcsavesize;
  1842. end;
  1843. function tenumdef.min:asizeint;
  1844. begin
  1845. min:=minval;
  1846. end;
  1847. function tenumdef.max:asizeint;
  1848. begin
  1849. max:=maxval;
  1850. end;
  1851. function tenumdef.getfirstsym: tsym;
  1852. var
  1853. i:integer;
  1854. begin
  1855. for i := 0 to symtable.SymList.Count - 1 do
  1856. begin
  1857. result:=tsym(symtable.SymList[i]);
  1858. if tenumsym(result).value=minval then
  1859. exit;
  1860. end;
  1861. result:=nil;
  1862. end;
  1863. function tenumdef.int2enumsym(l: asizeint): tsym;
  1864. var
  1865. i: longint;
  1866. sym: tsym;
  1867. bdef: tenumdef;
  1868. begin
  1869. result:=nil;
  1870. if (l<minval) or
  1871. (l>maxval) then
  1872. exit;
  1873. bdef:=getbasedef;
  1874. for i:=0 to bdef.symtable.symlist.count-1 do
  1875. begin
  1876. sym:=tsym(bdef.symtable.symlist[i]);
  1877. if (sym.typ=enumsym) and
  1878. (tenumsym(sym).value=l) then
  1879. begin
  1880. result:=sym;
  1881. exit;
  1882. end;
  1883. end;
  1884. end;
  1885. function tenumdef.getbasedef: tenumdef;
  1886. begin
  1887. if not assigned(basedef) then
  1888. result:=self
  1889. else
  1890. result:=basedef;
  1891. end;
  1892. procedure tenumdef.buildderef;
  1893. begin
  1894. inherited buildderef;
  1895. if df_copied_def in defoptions then
  1896. basedefderef.build(basedef)
  1897. else
  1898. tenumsymtable(symtable).buildderef;
  1899. {$ifdef jvm}
  1900. classdefderef.build(classdef);
  1901. {$endif}
  1902. end;
  1903. procedure tenumdef.deref;
  1904. begin
  1905. inherited deref;
  1906. if df_copied_def in defoptions then
  1907. begin
  1908. basedef:=tenumdef(basedefderef.resolve);
  1909. symtable:=basedef.symtable.getcopy;
  1910. end
  1911. else
  1912. tenumsymtable(symtable).deref;
  1913. {$ifdef jvm}
  1914. classdef:=tobjectdef(classdefderef.resolve);
  1915. {$endif}
  1916. end;
  1917. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1918. begin
  1919. inherited ppuwrite(ppufile);
  1920. ppufile.putaint(min);
  1921. ppufile.putaint(max);
  1922. ppufile.putaint(savesize);
  1923. {$ifdef jvm}
  1924. ppufile.putderef(classdefderef);
  1925. {$endif}
  1926. if df_copied_def in defoptions then
  1927. ppufile.putderef(basedefderef);
  1928. ppufile.writeentry(ibenumdef);
  1929. if not (df_copied_def in defoptions) then
  1930. tenumsymtable(symtable).ppuwrite(ppufile);
  1931. end;
  1932. function tenumdef.is_publishable : boolean;
  1933. begin
  1934. is_publishable:=true;
  1935. end;
  1936. function tenumdef.GetTypeName : string;
  1937. begin
  1938. GetTypeName:='<enumeration type>';
  1939. end;
  1940. {****************************************************************************
  1941. TORDDEF
  1942. ****************************************************************************}
  1943. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  1944. begin
  1945. inherited create(orddef);
  1946. low:=v;
  1947. high:=b;
  1948. ordtype:=t;
  1949. setsize;
  1950. end;
  1951. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1952. begin
  1953. inherited ppuload(orddef,ppufile);
  1954. ordtype:=tordtype(ppufile.getbyte);
  1955. low:=ppufile.getexprint;
  1956. high:=ppufile.getexprint;
  1957. setsize;
  1958. end;
  1959. function torddef.getcopy : tstoreddef;
  1960. begin
  1961. result:=torddef.create(ordtype,low,high);
  1962. result.typ:=orddef;
  1963. torddef(result).low:=low;
  1964. torddef(result).high:=high;
  1965. torddef(result).ordtype:=ordtype;
  1966. torddef(result).savesize:=savesize;
  1967. end;
  1968. function torddef.alignment:shortint;
  1969. begin
  1970. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) and
  1971. (ordtype in [s64bit,u64bit]) then
  1972. result := 4
  1973. else
  1974. result := inherited alignment;
  1975. end;
  1976. procedure torddef.setsize;
  1977. const
  1978. sizetbl : array[tordtype] of longint = (
  1979. 0,
  1980. 1,2,4,8,
  1981. 1,2,4,8,
  1982. 1,2,4,8,
  1983. 1,2,4,8,
  1984. 1,2,8
  1985. );
  1986. begin
  1987. savesize:=sizetbl[ordtype];
  1988. end;
  1989. function torddef.packedbitsize: asizeint;
  1990. var
  1991. sizeval: tconstexprint;
  1992. power: longint;
  1993. begin
  1994. result := 0;
  1995. if ordtype = uvoid then
  1996. exit;
  1997. {$ifndef cpu64bitalu}
  1998. if (ordtype in [s64bit,u64bit]) then
  1999. {$else not cpu64bitalu}
  2000. if (ordtype = u64bit) or
  2001. ((ordtype = s64bit) and
  2002. ((low <= (system.low(int64) div 2)) or
  2003. (high > (system.high(int64) div 2)))) then
  2004. {$endif cpu64bitalu}
  2005. result := 64
  2006. else if (low >= 0) and
  2007. (high <= 1) then
  2008. result := 1
  2009. else
  2010. begin
  2011. if (low>=0) then
  2012. sizeval:=high
  2013. else
  2014. { don't count 0 twice }
  2015. sizeval:=(cutils.max(-low,high)*2)-1;
  2016. { 256 must become 512 etc. }
  2017. nextpowerof2(sizeval+1,power);
  2018. result := power;
  2019. end;
  2020. end;
  2021. function torddef.getvardef : longint;
  2022. const
  2023. basetype2vardef : array[tordtype] of longint = (
  2024. varUndefined,
  2025. varbyte,varword,varlongword,varqword,
  2026. varshortint,varsmallint,varinteger,varint64,
  2027. varboolean,varboolean,varboolean,varboolean,
  2028. varboolean,varboolean,varUndefined,varUndefined,
  2029. varUndefined,varUndefined,varCurrency);
  2030. begin
  2031. result:=basetype2vardef[ordtype];
  2032. end;
  2033. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  2034. begin
  2035. inherited ppuwrite(ppufile);
  2036. ppufile.putbyte(byte(ordtype));
  2037. ppufile.putexprint(low);
  2038. ppufile.putexprint(high);
  2039. ppufile.writeentry(iborddef);
  2040. end;
  2041. function torddef.is_publishable : boolean;
  2042. begin
  2043. is_publishable:=(ordtype<>uvoid);
  2044. end;
  2045. function torddef.GetTypeName : string;
  2046. const
  2047. names : array[tordtype] of string[20] = (
  2048. 'untyped',
  2049. 'Byte','Word','DWord','QWord',
  2050. 'ShortInt','SmallInt','LongInt','Int64',
  2051. 'Boolean','Boolean16','Boolean32','Boolean64',
  2052. 'ByteBool','WordBool','LongBool','QWordBool',
  2053. 'Char','WideChar','Currency');
  2054. begin
  2055. GetTypeName:=names[ordtype];
  2056. end;
  2057. {****************************************************************************
  2058. TFLOATDEF
  2059. ****************************************************************************}
  2060. constructor tfloatdef.create(t : tfloattype);
  2061. begin
  2062. inherited create(floatdef);
  2063. floattype:=t;
  2064. setsize;
  2065. end;
  2066. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  2067. begin
  2068. inherited ppuload(floatdef,ppufile);
  2069. floattype:=tfloattype(ppufile.getbyte);
  2070. setsize;
  2071. end;
  2072. function tfloatdef.getcopy : tstoreddef;
  2073. begin
  2074. result:=tfloatdef.create(floattype);
  2075. result.typ:=floatdef;
  2076. tfloatdef(result).savesize:=savesize;
  2077. end;
  2078. function tfloatdef.alignment:shortint;
  2079. begin
  2080. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) then
  2081. case floattype of
  2082. sc80real,
  2083. s80real: result:=16;
  2084. s64real,
  2085. s64currency,
  2086. s64comp : result:=4;
  2087. else
  2088. result := inherited alignment;
  2089. end
  2090. else
  2091. result := inherited alignment;
  2092. end;
  2093. function tfloatdef.structalignment: shortint;
  2094. begin
  2095. { aix is really annoying: the recommended scalar alignment for both
  2096. int64 and double is 64 bits, but in structs int64 has to be aligned
  2097. to 8 bytes and double to 4 bytes }
  2098. if (target_info.system in systems_aix) and
  2099. (floattype=s64real) then
  2100. result:=4
  2101. else
  2102. result:=alignment;
  2103. end;
  2104. procedure tfloatdef.setsize;
  2105. begin
  2106. case floattype of
  2107. s32real : savesize:=4;
  2108. s80real : savesize:=10;
  2109. sc80real:
  2110. if target_info.system in [system_i386_darwin,system_i386_iphonesim,system_x86_64_darwin,
  2111. system_x86_64_linux,system_x86_64_freebsd,
  2112. system_x86_64_openbsd,system_x86_64_netbsd,
  2113. system_x86_64_solaris,system_x86_64_embedded] then
  2114. savesize:=16
  2115. else
  2116. savesize:=12;
  2117. s64real,
  2118. s64currency,
  2119. s64comp : savesize:=8;
  2120. else
  2121. savesize:=0;
  2122. end;
  2123. end;
  2124. function tfloatdef.getvardef : longint;
  2125. const
  2126. floattype2vardef : array[tfloattype] of longint = (
  2127. varSingle,varDouble,varUndefined,varUndefined,
  2128. varUndefined,varCurrency,varUndefined);
  2129. begin
  2130. if (upper(typename)='TDATETIME') and
  2131. assigned(owner) and
  2132. assigned(owner.name) and
  2133. (owner.name^='SYSTEM') then
  2134. result:=varDate
  2135. else
  2136. result:=floattype2vardef[floattype];
  2137. end;
  2138. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  2139. begin
  2140. inherited ppuwrite(ppufile);
  2141. ppufile.putbyte(byte(floattype));
  2142. ppufile.writeentry(ibfloatdef);
  2143. end;
  2144. function tfloatdef.is_publishable : boolean;
  2145. begin
  2146. is_publishable:=true;
  2147. end;
  2148. function tfloatdef.GetTypeName : string;
  2149. const
  2150. names : array[tfloattype] of string[20] = (
  2151. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  2152. begin
  2153. GetTypeName:=names[floattype];
  2154. end;
  2155. {****************************************************************************
  2156. TFILEDEF
  2157. ****************************************************************************}
  2158. constructor tfiledef.createtext;
  2159. begin
  2160. inherited create(filedef);
  2161. filetyp:=ft_text;
  2162. typedfiledef:=nil;
  2163. setsize;
  2164. end;
  2165. constructor tfiledef.createuntyped;
  2166. begin
  2167. inherited create(filedef);
  2168. filetyp:=ft_untyped;
  2169. typedfiledef:=nil;
  2170. setsize;
  2171. end;
  2172. constructor tfiledef.createtyped(def:tdef);
  2173. begin
  2174. inherited create(filedef);
  2175. filetyp:=ft_typed;
  2176. typedfiledef:=def;
  2177. setsize;
  2178. end;
  2179. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  2180. begin
  2181. inherited ppuload(filedef,ppufile);
  2182. filetyp:=tfiletyp(ppufile.getbyte);
  2183. if filetyp=ft_typed then
  2184. ppufile.getderef(typedfiledefderef)
  2185. else
  2186. typedfiledef:=nil;
  2187. setsize;
  2188. end;
  2189. function tfiledef.getcopy : tstoreddef;
  2190. begin
  2191. case filetyp of
  2192. ft_typed:
  2193. result:=tfiledef.createtyped(typedfiledef);
  2194. ft_untyped:
  2195. result:=tfiledef.createuntyped;
  2196. ft_text:
  2197. result:=tfiledef.createtext;
  2198. else
  2199. internalerror(2004121201);
  2200. end;
  2201. end;
  2202. procedure tfiledef.buildderef;
  2203. begin
  2204. inherited buildderef;
  2205. if filetyp=ft_typed then
  2206. typedfiledefderef.build(typedfiledef);
  2207. end;
  2208. procedure tfiledef.deref;
  2209. begin
  2210. inherited deref;
  2211. if filetyp=ft_typed then
  2212. typedfiledef:=tdef(typedfiledefderef.resolve);
  2213. end;
  2214. procedure tfiledef.setsize;
  2215. begin
  2216. {$ifdef cpu64bitaddr}
  2217. case filetyp of
  2218. ft_text :
  2219. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  2220. savesize:=640
  2221. else
  2222. savesize:=632;
  2223. ft_typed,
  2224. ft_untyped :
  2225. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  2226. savesize:=376
  2227. else
  2228. savesize:=368;
  2229. end;
  2230. {$endif cpu64bitaddr}
  2231. {$ifdef cpu32bitaddr}
  2232. case filetyp of
  2233. ft_text :
  2234. savesize:=596; { keep this dividable by 4 for proper alignment of arrays of text, see tw0754 e.g. on arm }
  2235. ft_typed,
  2236. ft_untyped :
  2237. savesize:=332;
  2238. end;
  2239. {$endif cpu32bitaddr}
  2240. {$ifdef cpu16bitaddr}
  2241. case filetyp of
  2242. ft_text :
  2243. savesize:=96;
  2244. ft_typed,
  2245. ft_untyped :
  2246. savesize:=76;
  2247. end;
  2248. {$endif cpu16bitaddr}
  2249. end;
  2250. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  2251. begin
  2252. inherited ppuwrite(ppufile);
  2253. ppufile.putbyte(byte(filetyp));
  2254. if filetyp=ft_typed then
  2255. ppufile.putderef(typedfiledefderef);
  2256. ppufile.writeentry(ibfiledef);
  2257. end;
  2258. function tfiledef.GetTypeName : string;
  2259. begin
  2260. case filetyp of
  2261. ft_untyped:
  2262. GetTypeName:='File';
  2263. ft_typed:
  2264. GetTypeName:='File Of '+typedfiledef.typename;
  2265. ft_text:
  2266. GetTypeName:='Text'
  2267. end;
  2268. end;
  2269. function tfiledef.getmangledparaname : TSymStr;
  2270. begin
  2271. case filetyp of
  2272. ft_untyped:
  2273. getmangledparaname:='FILE';
  2274. ft_typed:
  2275. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  2276. ft_text:
  2277. getmangledparaname:='TEXT'
  2278. end;
  2279. end;
  2280. {****************************************************************************
  2281. TVARIANTDEF
  2282. ****************************************************************************}
  2283. constructor tvariantdef.create(v : tvarianttype);
  2284. begin
  2285. inherited create(variantdef);
  2286. varianttype:=v;
  2287. setsize;
  2288. end;
  2289. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  2290. begin
  2291. inherited ppuload(variantdef,ppufile);
  2292. varianttype:=tvarianttype(ppufile.getbyte);
  2293. setsize;
  2294. end;
  2295. function tvariantdef.getcopy : tstoreddef;
  2296. begin
  2297. result:=tvariantdef.create(varianttype);
  2298. end;
  2299. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  2300. begin
  2301. inherited ppuwrite(ppufile);
  2302. ppufile.putbyte(byte(varianttype));
  2303. ppufile.writeentry(ibvariantdef);
  2304. end;
  2305. function tvariantdef.getvardef : longint;
  2306. begin
  2307. Result:=varVariant;
  2308. end;
  2309. procedure tvariantdef.setsize;
  2310. begin
  2311. {$ifdef cpu64bitaddr}
  2312. savesize:=24;
  2313. {$else cpu64bitaddr}
  2314. savesize:=16;
  2315. {$endif cpu64bitaddr}
  2316. end;
  2317. function tvariantdef.GetTypeName : string;
  2318. begin
  2319. case varianttype of
  2320. vt_normalvariant:
  2321. GetTypeName:='Variant';
  2322. vt_olevariant:
  2323. GetTypeName:='OleVariant';
  2324. end;
  2325. end;
  2326. function tvariantdef.needs_inittable : boolean;
  2327. begin
  2328. needs_inittable:=true;
  2329. end;
  2330. function tvariantdef.is_publishable : boolean;
  2331. begin
  2332. is_publishable:=true;
  2333. end;
  2334. {****************************************************************************
  2335. TABSTRACtpointerdef
  2336. ****************************************************************************}
  2337. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  2338. begin
  2339. inherited create(dt);
  2340. pointeddef:=def;
  2341. savesize:=sizeof(pint);
  2342. end;
  2343. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2344. begin
  2345. inherited ppuload(dt,ppufile);
  2346. ppufile.getderef(pointeddefderef);
  2347. savesize:=sizeof(pint);
  2348. end;
  2349. procedure tabstractpointerdef.buildderef;
  2350. begin
  2351. inherited buildderef;
  2352. pointeddefderef.build(pointeddef);
  2353. end;
  2354. procedure tabstractpointerdef.deref;
  2355. begin
  2356. inherited deref;
  2357. pointeddef:=tdef(pointeddefderef.resolve);
  2358. end;
  2359. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2360. begin
  2361. inherited ppuwrite(ppufile);
  2362. ppufile.putderef(pointeddefderef);
  2363. end;
  2364. {****************************************************************************
  2365. tpointerdef
  2366. ****************************************************************************}
  2367. constructor tpointerdef.create(def:tdef);
  2368. begin
  2369. inherited create(pointerdef,def);
  2370. is_far:=false;
  2371. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  2372. end;
  2373. constructor tpointerdef.createfar(def:tdef);
  2374. begin
  2375. inherited create(pointerdef,def);
  2376. is_far:=true;
  2377. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  2378. end;
  2379. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  2380. begin
  2381. inherited ppuload(pointerdef,ppufile);
  2382. is_far:=(ppufile.getbyte<>0);
  2383. has_pointer_math:=(ppufile.getbyte<>0);
  2384. end;
  2385. function tpointerdef.getcopy : tstoreddef;
  2386. begin
  2387. { don't use direct pointeddef if it is a forwarddef because in other case
  2388. one of them will be destroyed on forward type resolve and the second will
  2389. point to garbage }
  2390. if pointeddef.typ=forwarddef then
  2391. result:=tpointerdef.create(tforwarddef(pointeddef).getcopy)
  2392. else
  2393. result:=tpointerdef.create(pointeddef);
  2394. tpointerdef(result).is_far:=is_far;
  2395. tpointerdef(result).has_pointer_math:=has_pointer_math;
  2396. tpointerdef(result).savesize:=savesize;
  2397. end;
  2398. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2399. begin
  2400. inherited ppuwrite(ppufile);
  2401. ppufile.putbyte(byte(is_far));
  2402. ppufile.putbyte(byte(has_pointer_math));
  2403. ppufile.writeentry(ibpointerdef);
  2404. end;
  2405. function tpointerdef.GetTypeName : string;
  2406. begin
  2407. if is_far then
  2408. GetTypeName:='^'+pointeddef.typename+';far'
  2409. else
  2410. GetTypeName:='^'+pointeddef.typename;
  2411. end;
  2412. {****************************************************************************
  2413. TCLASSREFDEF
  2414. ****************************************************************************}
  2415. constructor tclassrefdef.create(def:tdef);
  2416. begin
  2417. inherited create(classrefdef,def);
  2418. end;
  2419. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  2420. begin
  2421. inherited ppuload(classrefdef,ppufile);
  2422. end;
  2423. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  2424. begin
  2425. inherited ppuwrite(ppufile);
  2426. ppufile.writeentry(ibclassrefdef);
  2427. end;
  2428. function tclassrefdef.getcopy:tstoreddef;
  2429. begin
  2430. if pointeddef.typ=forwarddef then
  2431. result:=tclassrefdef.create(tforwarddef(pointeddef).getcopy)
  2432. else
  2433. result:=tclassrefdef.create(pointeddef);
  2434. tclassrefdef(result).savesize:=savesize;
  2435. end;
  2436. function tclassrefdef.GetTypeName : string;
  2437. begin
  2438. GetTypeName:='Class Of '+pointeddef.typename;
  2439. end;
  2440. function tclassrefdef.is_publishable : boolean;
  2441. begin
  2442. result:=true;
  2443. end;
  2444. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  2445. begin
  2446. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  2447. result:=inherited rtti_mangledname(rt)
  2448. else
  2449. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  2450. end;
  2451. procedure tclassrefdef.register_created_object_type;
  2452. begin
  2453. tobjectdef(pointeddef).register_created_classref_type;
  2454. end;
  2455. {***************************************************************************
  2456. TSETDEF
  2457. ***************************************************************************}
  2458. constructor tsetdef.create(def:tdef;low, high : asizeint);
  2459. var
  2460. setallocbits: aint;
  2461. packedsavesize: aint;
  2462. begin
  2463. inherited create(setdef);
  2464. elementdef:=def;
  2465. setmax:=high;
  2466. if (current_settings.setalloc=0) then
  2467. begin
  2468. setbase:=0;
  2469. if (high<32) then
  2470. savesize:=Sizeof(longint)
  2471. else if (high<256) then
  2472. savesize:=32
  2473. else
  2474. savesize:=(high+7) div 8
  2475. end
  2476. else
  2477. begin
  2478. setallocbits:=current_settings.setalloc*8;
  2479. setbase:=low and not(setallocbits-1);
  2480. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  2481. savesize:=packedsavesize;
  2482. if savesize=3 then
  2483. savesize:=4;
  2484. end;
  2485. end;
  2486. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  2487. begin
  2488. inherited ppuload(setdef,ppufile);
  2489. ppufile.getderef(elementdefderef);
  2490. savesize:=ppufile.getaint;
  2491. setbase:=ppufile.getaint;
  2492. setmax:=ppufile.getaint;
  2493. end;
  2494. function tsetdef.getcopy : tstoreddef;
  2495. begin
  2496. result:=tsetdef.create(elementdef,setbase,setmax);
  2497. { the copy might have been created with a different setalloc setting }
  2498. tsetdef(result).savesize:=savesize;
  2499. end;
  2500. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  2501. begin
  2502. inherited ppuwrite(ppufile);
  2503. ppufile.putderef(elementdefderef);
  2504. ppufile.putaint(savesize);
  2505. ppufile.putaint(setbase);
  2506. ppufile.putaint(setmax);
  2507. ppufile.writeentry(ibsetdef);
  2508. end;
  2509. procedure tsetdef.buildderef;
  2510. begin
  2511. inherited buildderef;
  2512. elementdefderef.build(elementdef);
  2513. end;
  2514. procedure tsetdef.deref;
  2515. begin
  2516. inherited deref;
  2517. elementdef:=tdef(elementdefderef.resolve);
  2518. end;
  2519. function tsetdef.is_publishable : boolean;
  2520. begin
  2521. is_publishable:=savesize in [1,2,4];
  2522. end;
  2523. function tsetdef.GetTypeName : string;
  2524. begin
  2525. if assigned(elementdef) then
  2526. GetTypeName:='Set Of '+elementdef.typename
  2527. else
  2528. GetTypeName:='Empty Set';
  2529. end;
  2530. {***************************************************************************
  2531. TFORMALDEF
  2532. ***************************************************************************}
  2533. constructor tformaldef.create(Atyped:boolean);
  2534. begin
  2535. inherited create(formaldef);
  2536. typed:=Atyped;
  2537. savesize:=0;
  2538. end;
  2539. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  2540. begin
  2541. inherited ppuload(formaldef,ppufile);
  2542. typed:=boolean(ppufile.getbyte);
  2543. savesize:=0;
  2544. end;
  2545. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  2546. begin
  2547. inherited ppuwrite(ppufile);
  2548. ppufile.putbyte(byte(typed));
  2549. ppufile.writeentry(ibformaldef);
  2550. end;
  2551. function tformaldef.GetTypeName : string;
  2552. begin
  2553. if typed then
  2554. GetTypeName:='<Typed formal type>'
  2555. else
  2556. GetTypeName:='<Formal type>';
  2557. end;
  2558. {***************************************************************************
  2559. TARRAYDEF
  2560. ***************************************************************************}
  2561. constructor tarraydef.create(l,h:asizeint;def:tdef);
  2562. begin
  2563. inherited create(arraydef);
  2564. lowrange:=l;
  2565. highrange:=h;
  2566. rangedef:=def;
  2567. _elementdef:=nil;
  2568. arrayoptions:=[];
  2569. symtable:=tarraysymtable.create(self);
  2570. end;
  2571. destructor tarraydef.destroy;
  2572. begin
  2573. symtable.free;
  2574. symtable:=nil;
  2575. inherited;
  2576. end;
  2577. constructor tarraydef.create_from_pointer(def:tdef);
  2578. begin
  2579. { use -1 so that the elecount will not overflow }
  2580. self.create(0,high(asizeint)-1,ptrsinttype);
  2581. arrayoptions:=[ado_IsConvertedPointer];
  2582. setelementdef(def);
  2583. end;
  2584. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  2585. begin
  2586. inherited ppuload(arraydef,ppufile);
  2587. { the addresses are calculated later }
  2588. ppufile.getderef(_elementdefderef);
  2589. ppufile.getderef(rangedefderef);
  2590. lowrange:=ppufile.getaint;
  2591. highrange:=ppufile.getaint;
  2592. ppufile.getsmallset(arrayoptions);
  2593. symtable:=tarraysymtable.create(self);
  2594. tarraysymtable(symtable).ppuload(ppufile)
  2595. end;
  2596. function tarraydef.getcopy : tstoreddef;
  2597. begin
  2598. result:=tarraydef.create(lowrange,highrange,rangedef);
  2599. tarraydef(result).arrayoptions:=arrayoptions;
  2600. tarraydef(result)._elementdef:=_elementdef;
  2601. end;
  2602. procedure tarraydef.buildderef;
  2603. begin
  2604. inherited buildderef;
  2605. tarraysymtable(symtable).buildderef;
  2606. _elementdefderef.build(_elementdef);
  2607. rangedefderef.build(rangedef);
  2608. end;
  2609. procedure tarraydef.deref;
  2610. begin
  2611. inherited deref;
  2612. tarraysymtable(symtable).deref;
  2613. _elementdef:=tdef(_elementdefderef.resolve);
  2614. rangedef:=tdef(rangedefderef.resolve);
  2615. fillgenericparas(symtable);
  2616. end;
  2617. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  2618. begin
  2619. inherited ppuwrite(ppufile);
  2620. ppufile.putderef(_elementdefderef);
  2621. ppufile.putderef(rangedefderef);
  2622. ppufile.putaint(lowrange);
  2623. ppufile.putaint(highrange);
  2624. ppufile.putsmallset(arrayoptions);
  2625. ppufile.writeentry(ibarraydef);
  2626. tarraysymtable(symtable).ppuwrite(ppufile);
  2627. end;
  2628. function tarraydef.elesize : asizeint;
  2629. begin
  2630. if (ado_IsBitPacked in arrayoptions) then
  2631. internalerror(2006080101);
  2632. if assigned(_elementdef) then
  2633. result:=_elementdef.size
  2634. else
  2635. result:=0;
  2636. end;
  2637. function tarraydef.elepackedbitsize : asizeint;
  2638. begin
  2639. if not(ado_IsBitPacked in arrayoptions) then
  2640. internalerror(2006080102);
  2641. if assigned(_elementdef) then
  2642. result:=_elementdef.packedbitsize
  2643. else
  2644. result:=0;
  2645. end;
  2646. function tarraydef.elecount : asizeuint;
  2647. var
  2648. qhigh,qlow : qword;
  2649. begin
  2650. if ado_IsDynamicArray in arrayoptions then
  2651. begin
  2652. result:=0;
  2653. exit;
  2654. end;
  2655. if (highrange>0) and (lowrange<0) then
  2656. begin
  2657. qhigh:=highrange;
  2658. qlow:=qword(-lowrange);
  2659. { prevent overflow, return 0 to indicate overflow }
  2660. if qhigh+qlow>qword(high(asizeint)-1) then
  2661. result:=0
  2662. else
  2663. result:=qhigh+qlow+1;
  2664. end
  2665. else
  2666. result:=int64(highrange)-lowrange+1;
  2667. end;
  2668. function tarraydef.size : asizeint;
  2669. var
  2670. cachedelecount : asizeuint;
  2671. cachedelesize : asizeint;
  2672. begin
  2673. if ado_IsDynamicArray in arrayoptions then
  2674. begin
  2675. size:=sizeof(pint);
  2676. exit;
  2677. end;
  2678. { Tarraydef.size may never be called for an open array! }
  2679. if highrange<lowrange then
  2680. internalerror(99080501);
  2681. if not (ado_IsBitPacked in arrayoptions) then
  2682. cachedelesize:=elesize
  2683. else
  2684. cachedelesize := elepackedbitsize;
  2685. cachedelecount:=elecount;
  2686. if (cachedelesize = 0) then
  2687. begin
  2688. size := 0;
  2689. exit;
  2690. end;
  2691. if (cachedelecount = 0) then
  2692. begin
  2693. size := -1;
  2694. exit;
  2695. end;
  2696. { prevent overflow, return -1 to indicate overflow }
  2697. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2698. if (cachedelecount > asizeuint(high(asizeint))) or
  2699. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  2700. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  2701. accessing the array, see ncgmem (PFV) }
  2702. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  2703. begin
  2704. result:=-1;
  2705. exit;
  2706. end;
  2707. result:=cachedelesize*asizeint(cachedelecount);
  2708. if (ado_IsBitPacked in arrayoptions) then
  2709. { can't just add 7 and divide by 8, because that may overflow }
  2710. result:=result div 8 + ord((result mod 8)<>0);
  2711. end;
  2712. procedure tarraydef.setelementdef(def:tdef);
  2713. begin
  2714. _elementdef:=def;
  2715. if not(
  2716. (ado_IsDynamicArray in arrayoptions) or
  2717. (ado_IsConvertedPointer in arrayoptions) or
  2718. (highrange<lowrange)
  2719. ) and
  2720. (size=-1) then
  2721. Message(sym_e_segment_too_large);
  2722. end;
  2723. function tarraydef.alignment : shortint;
  2724. begin
  2725. { alignment of dyn. arrays doesn't depend on the element size }
  2726. if (ado_IsDynamicArray in arrayoptions) then
  2727. alignment:=size_2_align(sizeof(pint))
  2728. { alignment is the alignment of the elements }
  2729. else if (elementdef.typ in [arraydef,recorddef,orddef,enumdef,floatdef]) or
  2730. ((elementdef.typ=objectdef) and
  2731. is_object(elementdef)) then
  2732. alignment:=elementdef.alignment
  2733. { alignment is the size of the elements }
  2734. else if not (ado_IsBitPacked in arrayoptions) then
  2735. alignment:=size_2_align(elesize)
  2736. else
  2737. alignment:=packedbitsloadsize(elepackedbitsize);
  2738. end;
  2739. function tarraydef.needs_inittable : boolean;
  2740. begin
  2741. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2742. end;
  2743. function tarraydef.needs_separate_initrtti : boolean;
  2744. begin
  2745. if ado_IsBitPacked in arrayoptions then
  2746. result:=false
  2747. else
  2748. result:=elementdef.needs_separate_initrtti;
  2749. end;
  2750. function tarraydef.GetTypeName : string;
  2751. begin
  2752. if (ado_IsConstString in arrayoptions) then
  2753. result:='Constant String'
  2754. else if (ado_isarrayofconst in arrayoptions) or
  2755. (ado_isConstructor in arrayoptions) then
  2756. begin
  2757. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2758. GetTypeName:='Array Of Const'
  2759. else
  2760. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  2761. end
  2762. else if (ado_IsDynamicArray in arrayoptions) then
  2763. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  2764. else if ((highrange=-1) and (lowrange=0)) then
  2765. GetTypeName:='{Open} Array Of '+elementdef.typename
  2766. else
  2767. begin
  2768. result := '';
  2769. if (ado_IsBitPacked in arrayoptions) then
  2770. result:='BitPacked ';
  2771. if rangedef.typ=enumdef then
  2772. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2773. else
  2774. result:=result+'Array['+tostr(lowrange)+'..'+
  2775. tostr(highrange)+'] Of '+elementdef.typename
  2776. end;
  2777. end;
  2778. function tarraydef.getmangledparaname : TSymStr;
  2779. begin
  2780. if ado_isarrayofconst in arrayoptions then
  2781. getmangledparaname:='array_of_const'
  2782. else
  2783. if ((highrange=-1) and (lowrange=0)) then
  2784. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2785. else
  2786. internalerror(200204176);
  2787. end;
  2788. function tarraydef.is_publishable : boolean;
  2789. begin
  2790. Result:=ado_IsDynamicArray in arrayoptions;
  2791. end;
  2792. {***************************************************************************
  2793. tabstractrecorddef
  2794. ***************************************************************************}
  2795. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp);
  2796. begin
  2797. inherited create(dt);
  2798. objname:=stringdup(upper(n));
  2799. objrealname:=stringdup(n);
  2800. objectoptions:=[];
  2801. if assigned(current_module.namespace) then
  2802. begin
  2803. import_lib:=stringdup(current_module.namespace^);
  2804. replace(import_lib^,'.','/');
  2805. end;
  2806. end;
  2807. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2808. begin
  2809. inherited ppuload(dt,ppufile);
  2810. objrealname:=stringdup(ppufile.getstring);
  2811. objname:=stringdup(upper(objrealname^));
  2812. import_lib:=stringdup(ppufile.getstring);
  2813. { only used for external C++ classes and Java classes/records }
  2814. if (import_lib^='') then
  2815. stringdispose(import_lib);
  2816. ppufile.getsmallset(objectoptions);
  2817. end;
  2818. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  2819. begin
  2820. inherited ppuwrite(ppufile);
  2821. ppufile.putstring(objrealname^);
  2822. if assigned(import_lib) then
  2823. ppufile.putstring(import_lib^)
  2824. else
  2825. ppufile.putstring('');
  2826. ppufile.putsmallset(objectoptions);
  2827. end;
  2828. destructor tabstractrecorddef.destroy;
  2829. begin
  2830. stringdispose(objname);
  2831. stringdispose(objrealname);
  2832. stringdispose(import_lib);
  2833. tcinitcode.free;
  2834. inherited destroy;
  2835. end;
  2836. procedure tabstractrecorddef.check_forwards;
  2837. begin
  2838. tstoredsymtable(symtable).check_forwards;
  2839. end;
  2840. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  2841. var
  2842. i: longint;
  2843. sym: tsym;
  2844. begin
  2845. for i:=0 to symtable.SymList.Count-1 do
  2846. begin
  2847. sym:=tsym(symtable.SymList[i]);
  2848. if sym.typ=procsym then
  2849. begin
  2850. result:=tprocsym(sym).find_procdef_bytype(pt);
  2851. if assigned(result) then
  2852. exit;
  2853. end;
  2854. end;
  2855. result:=nil;
  2856. end;
  2857. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2858. begin
  2859. if t=gs_record then
  2860. GetSymtable:=symtable
  2861. else
  2862. GetSymtable:=nil;
  2863. end;
  2864. function tabstractrecorddef.is_packed:boolean;
  2865. begin
  2866. result:=tabstractrecordsymtable(symtable).is_packed;
  2867. end;
  2868. function tabstractrecorddef.RttiName: string;
  2869. begin
  2870. Result:=OwnerHierarchyName+objrealname^;
  2871. end;
  2872. function tabstractrecorddef.search_enumerator_get: tprocdef;
  2873. var
  2874. sym : tsym;
  2875. i : integer;
  2876. pd : tprocdef;
  2877. hashedid : THashedIDString;
  2878. begin
  2879. result:=nil;
  2880. hashedid.id:='GETENUMERATOR';
  2881. sym:=tsym(symtable.FindWithHash(hashedid));
  2882. if assigned(sym) and (sym.typ=procsym) then
  2883. begin
  2884. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  2885. begin
  2886. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  2887. if (pd.proctypeoption = potype_function) and
  2888. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  2889. (pd.visibility >= vis_public) then
  2890. begin
  2891. result:=pd;
  2892. exit;
  2893. end;
  2894. end;
  2895. end;
  2896. end;
  2897. function tabstractrecorddef.search_enumerator_move: tprocdef;
  2898. var
  2899. sym : tsym;
  2900. i : integer;
  2901. pd : tprocdef;
  2902. hashedid : THashedIDString;
  2903. begin
  2904. result:=nil;
  2905. // first search for po_enumerator_movenext method modifier
  2906. // then search for public function MoveNext: Boolean
  2907. for i:=0 to symtable.SymList.Count-1 do
  2908. begin
  2909. sym:=TSym(symtable.SymList[i]);
  2910. if (sym.typ=procsym) then
  2911. begin
  2912. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  2913. if assigned(pd) then
  2914. begin
  2915. result:=pd;
  2916. exit;
  2917. end;
  2918. end;
  2919. end;
  2920. hashedid.id:='MOVENEXT';
  2921. sym:=tsym(symtable.FindWithHash(hashedid));
  2922. if assigned(sym) and (sym.typ=procsym) then
  2923. begin
  2924. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  2925. begin
  2926. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  2927. if (pd.proctypeoption = potype_function) and
  2928. is_boolean(pd.returndef) and
  2929. (pd.minparacount = 0) and
  2930. (pd.visibility >= vis_public) then
  2931. begin
  2932. result:=pd;
  2933. exit;
  2934. end;
  2935. end;
  2936. end;
  2937. end;
  2938. function tabstractrecorddef.search_enumerator_current: tsym;
  2939. var
  2940. sym: tsym;
  2941. i: integer;
  2942. hashedid : THashedIDString;
  2943. begin
  2944. result:=nil;
  2945. // first search for ppo_enumerator_current property modifier
  2946. // then search for public property Current
  2947. for i:=0 to symtable.SymList.Count-1 do
  2948. begin
  2949. sym:=TSym(symtable.SymList[i]);
  2950. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  2951. begin
  2952. result:=sym;
  2953. exit;
  2954. end;
  2955. end;
  2956. hashedid.id:='CURRENT';
  2957. sym:=tsym(symtable.FindWithHash(hashedid));
  2958. if assigned(sym) and (sym.typ=propertysym) and
  2959. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  2960. begin
  2961. result:=sym;
  2962. exit;
  2963. end;
  2964. end;
  2965. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  2966. var
  2967. st: tsymtable;
  2968. enclosingdef: tdef;
  2969. begin
  2970. if typ=objectdef then
  2971. result:=tobjectdef(self).objextname^
  2972. else if assigned(typesym) then
  2973. result:=typesym.realname
  2974. { have to generate anonymous nested type in current unit/class/record }
  2975. else
  2976. internalerror(2011032601);
  2977. { in case of specializations, add some extras to prevent name conflicts
  2978. with nested classes }
  2979. if df_specialization in defoptions then
  2980. result:='$'+result+'$specialization$';
  2981. st:=owner;
  2982. while assigned(st) and
  2983. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  2984. begin
  2985. { nested classes are named as "OuterClass$InnerClass" }
  2986. enclosingdef:=tdef(st.defowner);
  2987. if enclosingdef.typ=procdef then
  2988. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  2989. else if enclosingdef.typ=objectdef then
  2990. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  2991. else if assigned(enclosingdef.typesym) then
  2992. result:=enclosingdef.typesym.realname+'$'+result
  2993. else
  2994. internalerror(2011060305);
  2995. st:=enclosingdef.owner;
  2996. end;
  2997. if with_package_name and
  2998. assigned(import_lib) then
  2999. result:=import_lib^+'/'+result;
  3000. end;
  3001. {***************************************************************************
  3002. trecorddef
  3003. ***************************************************************************}
  3004. constructor trecorddef.create(const n:string; p:TSymtable);
  3005. begin
  3006. inherited create(n,recorddef);
  3007. symtable:=p;
  3008. { we can own the symtable only if nobody else owns a copy so far }
  3009. if symtable.refcount=1 then
  3010. symtable.defowner:=self;
  3011. isunion:=false;
  3012. end;
  3013. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  3014. begin
  3015. inherited ppuload(recorddef,ppufile);
  3016. if df_copied_def in defoptions then
  3017. ppufile.getderef(cloneddefderef)
  3018. else
  3019. begin
  3020. symtable:=trecordsymtable.create(objrealname^,0);
  3021. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  3022. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  3023. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  3024. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  3025. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  3026. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  3027. trecordsymtable(symtable).ppuload(ppufile);
  3028. { requires usefieldalignment to be set }
  3029. symtable.defowner:=self;
  3030. end;
  3031. isunion:=false;
  3032. end;
  3033. destructor trecorddef.destroy;
  3034. begin
  3035. if assigned(symtable) then
  3036. begin
  3037. symtable.free;
  3038. symtable:=nil;
  3039. end;
  3040. inherited destroy;
  3041. end;
  3042. function trecorddef.getcopy : tstoreddef;
  3043. begin
  3044. result:=trecorddef.create(objrealname^,symtable.getcopy);
  3045. trecorddef(result).isunion:=isunion;
  3046. include(trecorddef(result).defoptions,df_copied_def);
  3047. if assigned(tcinitcode) then
  3048. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  3049. if assigned(import_lib) then
  3050. trecorddef(result).import_lib:=stringdup(import_lib^);
  3051. end;
  3052. function trecorddef.needs_inittable : boolean;
  3053. begin
  3054. needs_inittable:=trecordsymtable(symtable).needs_init_final
  3055. end;
  3056. function trecorddef.needs_separate_initrtti : boolean;
  3057. begin
  3058. result:=true;
  3059. end;
  3060. function trecorddef.is_related(d: tdef): boolean;
  3061. begin
  3062. { records are implemented via classes in the JVM target, and are
  3063. all descendents of the java_fpcbaserecordtype class }
  3064. is_related:=false;
  3065. if (target_info.system in systems_jvm) then
  3066. begin
  3067. if d.typ=objectdef then
  3068. begin
  3069. d:=find_real_class_definition(tobjectdef(d),false);
  3070. if (d=java_jlobject) or
  3071. (d=java_fpcbaserecordtype) then
  3072. is_related:=true
  3073. end;
  3074. end;
  3075. end;
  3076. procedure trecorddef.buildderef;
  3077. begin
  3078. inherited buildderef;
  3079. if df_copied_def in defoptions then
  3080. cloneddefderef.build(symtable.defowner)
  3081. else
  3082. tstoredsymtable(symtable).buildderef;
  3083. end;
  3084. procedure trecorddef.deref;
  3085. begin
  3086. inherited deref;
  3087. { now dereference the definitions }
  3088. if df_copied_def in defoptions then
  3089. begin
  3090. cloneddef:=trecorddef(cloneddefderef.resolve);
  3091. symtable:=cloneddef.symtable.getcopy;
  3092. end
  3093. else
  3094. tstoredsymtable(symtable).deref;
  3095. fillgenericparas(symtable);
  3096. { assign TGUID? load only from system unit }
  3097. if not(assigned(rec_tguid)) and
  3098. (upper(typename)='TGUID') and
  3099. assigned(owner) and
  3100. assigned(owner.name) and
  3101. (owner.name^='SYSTEM') then
  3102. rec_tguid:=self;
  3103. { assign JMP_BUF? load only from system unit }
  3104. if not(assigned(rec_jmp_buf)) and
  3105. (upper(typename)='JMP_BUF') and
  3106. assigned(owner) and
  3107. assigned(owner.name) and
  3108. (owner.name^='SYSTEM') then
  3109. rec_jmp_buf:=self;
  3110. end;
  3111. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  3112. begin
  3113. inherited ppuwrite(ppufile);
  3114. if df_copied_def in defoptions then
  3115. ppufile.putderef(cloneddefderef)
  3116. else
  3117. begin
  3118. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  3119. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  3120. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  3121. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  3122. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  3123. ppufile.putword(trecordsymtable(symtable).paddingsize);
  3124. end;
  3125. ppufile.writeentry(ibrecorddef);
  3126. if not(df_copied_def in defoptions) then
  3127. trecordsymtable(symtable).ppuwrite(ppufile);
  3128. end;
  3129. function trecorddef.size:asizeint;
  3130. begin
  3131. result:=trecordsymtable(symtable).datasize;
  3132. end;
  3133. function trecorddef.alignment:shortint;
  3134. begin
  3135. alignment:=trecordsymtable(symtable).recordalignment;
  3136. end;
  3137. function trecorddef.padalignment:shortint;
  3138. begin
  3139. padalignment := trecordsymtable(symtable).padalignment;
  3140. end;
  3141. function trecorddef.GetTypeName : string;
  3142. begin
  3143. GetTypeName:='<record type>'
  3144. end;
  3145. {***************************************************************************
  3146. TABSTRACTPROCDEF
  3147. ***************************************************************************}
  3148. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  3149. begin
  3150. inherited create(dt);
  3151. parast:=tparasymtable.create(self,level);
  3152. paras:=nil;
  3153. minparacount:=0;
  3154. maxparacount:=0;
  3155. proctypeoption:=potype_none;
  3156. proccalloption:=pocall_none;
  3157. procoptions:=[];
  3158. returndef:=voidtype;
  3159. savesize:=sizeof(pint);
  3160. callerargareasize:=0;
  3161. calleeargareasize:=0;
  3162. has_paraloc_info:=callnoside;
  3163. funcretloc[callerside].init;
  3164. funcretloc[calleeside].init;
  3165. check_mark_as_nested;
  3166. end;
  3167. destructor tabstractprocdef.destroy;
  3168. begin
  3169. if assigned(paras) then
  3170. begin
  3171. {$ifdef MEMDEBUG}
  3172. memprocpara.start;
  3173. {$endif MEMDEBUG}
  3174. paras.free;
  3175. paras:=nil;
  3176. {$ifdef MEMDEBUG}
  3177. memprocpara.stop;
  3178. {$endif MEMDEBUG}
  3179. end;
  3180. if assigned(parast) then
  3181. begin
  3182. {$ifdef MEMDEBUG}
  3183. memprocparast.start;
  3184. {$endif MEMDEBUG}
  3185. parast.free;
  3186. parast:=nil;
  3187. {$ifdef MEMDEBUG}
  3188. memprocparast.stop;
  3189. {$endif MEMDEBUG}
  3190. end;
  3191. funcretloc[callerside].done;
  3192. funcretloc[calleeside].done;
  3193. inherited destroy;
  3194. end;
  3195. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  3196. begin
  3197. if (tsym(p).typ<>paravarsym) then
  3198. exit;
  3199. inc(plongint(arg)^);
  3200. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  3201. begin
  3202. if not assigned(tparavarsym(p).defaultconstsym) then
  3203. inc(minparacount);
  3204. inc(maxparacount);
  3205. end;
  3206. end;
  3207. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  3208. begin
  3209. if (tsym(p).typ<>paravarsym) then
  3210. exit;
  3211. paras.add(p);
  3212. end;
  3213. procedure tabstractprocdef.calcparas;
  3214. var
  3215. paracount : longint;
  3216. begin
  3217. { This can already be assigned when
  3218. we need to reresolve this unit (PFV) }
  3219. if assigned(paras) then
  3220. paras.free;
  3221. paras:=tparalist.create(false);
  3222. paracount:=0;
  3223. minparacount:=0;
  3224. maxparacount:=0;
  3225. parast.SymList.ForEachCall(@count_para,@paracount);
  3226. paras.capacity:=paracount;
  3227. { Insert parameters in table }
  3228. parast.SymList.ForEachCall(@insert_para,nil);
  3229. { Order parameters }
  3230. paras.sortparas;
  3231. end;
  3232. procedure tabstractprocdef.buildderef;
  3233. begin
  3234. { released procdef? }
  3235. if not assigned(parast) then
  3236. exit;
  3237. inherited buildderef;
  3238. returndefderef.build(returndef);
  3239. if po_explicitparaloc in procoptions then
  3240. funcretloc[callerside].buildderef;
  3241. { parast }
  3242. tparasymtable(parast).buildderef;
  3243. end;
  3244. procedure tabstractprocdef.deref;
  3245. begin
  3246. inherited deref;
  3247. returndef:=tdef(returndefderef.resolve);
  3248. if po_explicitparaloc in procoptions then
  3249. begin
  3250. funcretloc[callerside].deref;
  3251. has_paraloc_info:=callerside;
  3252. end
  3253. else
  3254. begin
  3255. { deref is called after loading from a ppu, but also after another
  3256. unit has been reloaded/recompiled and all references must be
  3257. re-resolved. Since the funcretloc contains a reference to a tdef,
  3258. reset it so that we won't try to access the stale def }
  3259. funcretloc[callerside].init;
  3260. has_paraloc_info:=callnoside;
  3261. end;
  3262. { parast }
  3263. tparasymtable(parast).deref;
  3264. { recalculated parameters }
  3265. calcparas;
  3266. fillgenericparas(parast);
  3267. end;
  3268. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3269. begin
  3270. inherited ppuload(dt,ppufile);
  3271. parast:=nil;
  3272. Paras:=nil;
  3273. minparacount:=0;
  3274. maxparacount:=0;
  3275. ppufile.getderef(returndefderef);
  3276. { TODO: remove fpu_used loading}
  3277. ppufile.getbyte;
  3278. proctypeoption:=tproctypeoption(ppufile.getbyte);
  3279. proccalloption:=tproccalloption(ppufile.getbyte);
  3280. ppufile.getnormalset(procoptions);
  3281. funcretloc[callerside].init;
  3282. if po_explicitparaloc in procoptions then
  3283. funcretloc[callerside].ppuload(ppufile);
  3284. savesize:=sizeof(pint);
  3285. if (po_explicitparaloc in procoptions) then
  3286. has_paraloc_info:=callerside;
  3287. end;
  3288. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  3289. var
  3290. oldintfcrc : boolean;
  3291. begin
  3292. { released procdef? }
  3293. if not assigned(parast) then
  3294. exit;
  3295. inherited ppuwrite(ppufile);
  3296. ppufile.putderef(returndefderef);
  3297. oldintfcrc:=ppufile.do_interface_crc;
  3298. ppufile.do_interface_crc:=false;
  3299. ppufile.putbyte(0);
  3300. ppufile.putbyte(ord(proctypeoption));
  3301. ppufile.putbyte(ord(proccalloption));
  3302. ppufile.putnormalset(procoptions);
  3303. ppufile.do_interface_crc:=oldintfcrc;
  3304. if (po_explicitparaloc in procoptions) then
  3305. funcretloc[callerside].ppuwrite(ppufile);
  3306. end;
  3307. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  3308. var
  3309. hs,s : ansistring;
  3310. hp : TParavarsym;
  3311. hpc : tconstsym;
  3312. first : boolean;
  3313. i : integer;
  3314. begin
  3315. s:='';
  3316. first:=true;
  3317. for i:=0 to paras.count-1 do
  3318. begin
  3319. hp:=tparavarsym(paras[i]);
  3320. if not(vo_is_hidden_para in hp.varoptions) or
  3321. (pno_showhidden in pno) then
  3322. begin
  3323. if first then
  3324. begin
  3325. s:=s+'(';
  3326. first:=false;
  3327. end
  3328. else
  3329. s:=s+';';
  3330. if vo_is_hidden_para in hp.varoptions then
  3331. s:=s+'<';
  3332. case hp.varspez of
  3333. vs_var :
  3334. s:=s+'var ';
  3335. vs_const :
  3336. s:=s+'const ';
  3337. vs_out :
  3338. s:=s+'out ';
  3339. vs_constref :
  3340. s:=s+'constref ';
  3341. end;
  3342. if (pno_paranames in pno) then
  3343. s:=s+hp.realname+':';
  3344. if hp.univpara then
  3345. s:=s+'univ ';
  3346. if assigned(hp.vardef.typesym) then
  3347. begin
  3348. hs:=hp.vardef.typesym.realname;
  3349. if hs[1]<>'$' then
  3350. s:=s+hp.vardef.OwnerHierarchyName+hs
  3351. else
  3352. s:=s+hp.vardef.GetTypeName;
  3353. end
  3354. else
  3355. s:=s+hp.vardef.GetTypeName;
  3356. { default value }
  3357. if assigned(hp.defaultconstsym) then
  3358. begin
  3359. hpc:=tconstsym(hp.defaultconstsym);
  3360. hs:='';
  3361. case hpc.consttyp of
  3362. conststring,
  3363. constresourcestring :
  3364. begin
  3365. If hpc.value.len>0 then
  3366. begin
  3367. setLength(hs,hpc.value.len);
  3368. { don't write past the end of hs if the constant
  3369. is > 255 chars }
  3370. move(hpc.value.valueptr^,hs[1],length(hs));
  3371. { make sure that constant strings with newline chars
  3372. don't create a linebreak in the assembler code,
  3373. since comments are line-based. Also remove nulls
  3374. because the comments are written as a pchar. }
  3375. ReplaceCase(hs,#0,'.');
  3376. ReplaceCase(hs,#10,'.');
  3377. ReplaceCase(hs,#13,'.');
  3378. end;
  3379. end;
  3380. constreal :
  3381. str(pbestreal(hpc.value.valueptr)^,hs);
  3382. constpointer :
  3383. hs:=tostr(hpc.value.valueordptr);
  3384. constord :
  3385. begin
  3386. if is_boolean(hpc.constdef) then
  3387. begin
  3388. if hpc.value.valueord<>0 then
  3389. hs:='TRUE'
  3390. else
  3391. hs:='FALSE';
  3392. end
  3393. else
  3394. hs:=tostr(hpc.value.valueord);
  3395. end;
  3396. constnil :
  3397. hs:='nil';
  3398. constset :
  3399. hs:='<set>';
  3400. end;
  3401. if hs<>'' then
  3402. s:=s+'="'+hs+'"';
  3403. end;
  3404. if vo_is_hidden_para in hp.varoptions then
  3405. s:=s+'>';
  3406. end;
  3407. end;
  3408. if not first then
  3409. s:=s+')';
  3410. if (po_varargs in procoptions) then
  3411. s:=s+';VarArgs';
  3412. typename_paras:=s;
  3413. end;
  3414. function tabstractprocdef.is_methodpointer:boolean;
  3415. begin
  3416. result:=false;
  3417. end;
  3418. function tabstractprocdef.is_addressonly:boolean;
  3419. begin
  3420. result:=true;
  3421. end;
  3422. function tabstractprocdef.no_self_node: boolean;
  3423. begin
  3424. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  3425. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  3426. end;
  3427. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef;
  3428. var
  3429. j, nestinglevel: longint;
  3430. pvs, npvs: tparavarsym;
  3431. csym, ncsym: tconstsym;
  3432. begin
  3433. nestinglevel:=parast.symtablelevel;
  3434. if newtyp=procdef then
  3435. begin
  3436. if (copytyp<>pc_bareproc) then
  3437. result:=tprocdef.create(nestinglevel)
  3438. else
  3439. result:=tprocdef.create(normal_function_level);
  3440. tprocdef(result).visibility:=vis_public;
  3441. end
  3442. else
  3443. begin
  3444. result:=tprocvardef.create(nestinglevel);
  3445. end;
  3446. tabstractprocdef(result).returndef:=returndef;
  3447. tabstractprocdef(result).returndefderef:=returndefderef;
  3448. pvs:=nil;
  3449. npvs:=nil;
  3450. for j:=0 to parast.symlist.count-1 do
  3451. begin
  3452. case tsym(parast.symlist[j]).typ of
  3453. paravarsym:
  3454. begin
  3455. pvs:=tparavarsym(parast.symlist[j]);
  3456. { in case of bare proc, don't copy self, vmt or framepointer
  3457. parameters }
  3458. if (copytyp=pc_bareproc) and
  3459. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result]*pvs.varoptions)<>[]) then
  3460. continue;
  3461. npvs:=tparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  3462. pvs.vardef,pvs.varoptions);
  3463. npvs.defaultconstsym:=pvs.defaultconstsym;
  3464. tabstractprocdef(result).parast.insert(npvs);
  3465. end;
  3466. constsym:
  3467. begin
  3468. // ignore, reuse original constym. Should also be duplicated
  3469. // be safe though
  3470. end;
  3471. symconst.typesym:
  3472. begin
  3473. // reuse original, part of generic declaration
  3474. end
  3475. else
  3476. internalerror(201160604);
  3477. end;
  3478. end;
  3479. tabstractprocdef(result).savesize:=savesize;
  3480. tabstractprocdef(result).proctypeoption:=proctypeoption;
  3481. tabstractprocdef(result).proccalloption:=proccalloption;
  3482. tabstractprocdef(result).procoptions:=procoptions;
  3483. if (copytyp=pc_bareproc) then
  3484. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  3485. if newtyp=procvardef then
  3486. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  3487. tabstractprocdef(result).callerargareasize:=callerargareasize;
  3488. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  3489. tabstractprocdef(result).maxparacount:=maxparacount;
  3490. tabstractprocdef(result).minparacount:=minparacount;
  3491. if po_explicitparaloc in procoptions then
  3492. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  3493. { recalculate parameter info }
  3494. tabstractprocdef(result).has_paraloc_info:=callnoside;
  3495. {$ifdef m68k}
  3496. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  3497. {$endif}
  3498. if (typ=procdef) and
  3499. (newtyp=procvardef) and
  3500. (owner.symtabletype=ObjectSymtable) then
  3501. include(tprocvardef(result).procoptions,po_methodpointer);
  3502. end;
  3503. procedure tabstractprocdef.check_mark_as_nested;
  3504. begin
  3505. { nested procvars require that nested functions use the Delphi-style
  3506. nested procedure calling convention }
  3507. if (parast.symtablelevel>normal_function_level) and
  3508. (m_nested_procvars in current_settings.modeswitches) then
  3509. include(procoptions,po_delphi_nested_cc);
  3510. end;
  3511. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  3512. begin
  3513. if (side in [callerside,callbothsides]) and
  3514. not(has_paraloc_info in [callerside,callbothsides]) then
  3515. begin
  3516. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  3517. if has_paraloc_info in [calleeside,callbothsides] then
  3518. has_paraloc_info:=callbothsides
  3519. else
  3520. has_paraloc_info:=callerside;
  3521. end;
  3522. if (side in [calleeside,callbothsides]) and
  3523. not(has_paraloc_info in [calleeside,callbothsides]) then
  3524. begin
  3525. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  3526. if has_paraloc_info in [callerside,callbothsides] then
  3527. has_paraloc_info:=callbothsides
  3528. else
  3529. has_paraloc_info:=calleeside;
  3530. end;
  3531. end;
  3532. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  3533. var
  3534. p: tparavarsym;
  3535. ploc: PCGParalocation;
  3536. i: longint;
  3537. begin
  3538. result:=false;
  3539. init_paraloc_info(side);
  3540. for i:=0 to parast.SymList.Count-1 do
  3541. if tsym(parast.SymList[i]).typ=paravarsym then
  3542. begin
  3543. p:=tparavarsym(parast.SymList[i]);
  3544. { check if no parameter is located on the stack }
  3545. if is_open_array(p.vardef) or
  3546. is_array_of_const(p.vardef) then
  3547. begin
  3548. result:=true;
  3549. exit;
  3550. end;
  3551. ploc:=p.paraloc[side].location;
  3552. while assigned(ploc) do
  3553. begin
  3554. if (ploc^.loc=LOC_REFERENCE) then
  3555. begin
  3556. result:=true;
  3557. exit
  3558. end;
  3559. ploc:=ploc^.next;
  3560. end;
  3561. end;
  3562. end;
  3563. {***************************************************************************
  3564. TPROCDEF
  3565. ***************************************************************************}
  3566. constructor tprocdef.create(level:byte);
  3567. begin
  3568. inherited create(procdef,level);
  3569. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  3570. {$ifdef symansistr}
  3571. _mangledname:='';
  3572. {$else symansistr}
  3573. _mangledname:=nil;
  3574. {$endif symansistr}
  3575. fileinfo:=current_filepos;
  3576. extnumber:=$ffff;
  3577. aliasnames:=TCmdStrList.create;
  3578. funcretsym:=nil;
  3579. forwarddef:=true;
  3580. interfacedef:=false;
  3581. hasforward:=false;
  3582. struct := nil;
  3583. import_dll:=nil;
  3584. import_name:=nil;
  3585. import_nr:=0;
  3586. inlininginfo:=nil;
  3587. deprecatedmsg:=nil;
  3588. {$ifdef i386}
  3589. fpu_used:=maxfpuregs;
  3590. {$endif i386}
  3591. end;
  3592. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  3593. var
  3594. i,aliasnamescount : longint;
  3595. level : byte;
  3596. begin
  3597. inherited ppuload(procdef,ppufile);
  3598. {$ifdef symansistr}
  3599. if po_has_mangledname in procoptions then
  3600. _mangledname:=ppufile.getansistring
  3601. else
  3602. _mangledname:='';
  3603. {$else symansistr}
  3604. if po_has_mangledname in procoptions then
  3605. _mangledname:=stringdup(ppufile.getstring)
  3606. else
  3607. _mangledname:=nil;
  3608. {$endif symansistr}
  3609. extnumber:=ppufile.getword;
  3610. level:=ppufile.getbyte;
  3611. ppufile.getderef(structderef);
  3612. ppufile.getderef(procsymderef);
  3613. ppufile.getposinfo(fileinfo);
  3614. visibility:=tvisibility(ppufile.getbyte);
  3615. ppufile.getsmallset(symoptions);
  3616. if sp_has_deprecated_msg in symoptions then
  3617. deprecatedmsg:=stringdup(ppufile.getstring)
  3618. else
  3619. deprecatedmsg:=nil;
  3620. {$ifdef powerpc}
  3621. { library symbol for AmigaOS/MorphOS }
  3622. ppufile.getderef(libsymderef);
  3623. {$endif powerpc}
  3624. { import stuff }
  3625. if po_has_importdll in procoptions then
  3626. import_dll:=stringdup(ppufile.getstring)
  3627. else
  3628. import_dll:=nil;
  3629. if po_has_importname in procoptions then
  3630. import_name:=stringdup(ppufile.getstring)
  3631. else
  3632. import_name:=nil;
  3633. import_nr:=ppufile.getword;
  3634. if (po_msgint in procoptions) then
  3635. messageinf.i:=ppufile.getlongint;
  3636. if (po_msgstr in procoptions) then
  3637. messageinf.str:=stringdup(ppufile.getstring);
  3638. if (po_dispid in procoptions) then
  3639. dispid:=ppufile.getlongint;
  3640. { inline stuff }
  3641. if (po_has_inlininginfo in procoptions) then
  3642. begin
  3643. ppufile.getderef(funcretsymderef);
  3644. new(inlininginfo);
  3645. ppufile.getsmallset(inlininginfo^.flags);
  3646. end
  3647. else
  3648. begin
  3649. inlininginfo:=nil;
  3650. funcretsym:=nil;
  3651. end;
  3652. aliasnames:=TCmdStrList.create;
  3653. { count alias names }
  3654. aliasnamescount:=ppufile.getbyte;
  3655. for i:=1 to aliasnamescount do
  3656. aliasnames.insert(ppufile.getstring);
  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. {$ifdef powerpc}
  3763. { library symbol for AmigaOS/MorphOS }
  3764. ppufile.putderef(libsymderef);
  3765. {$endif powerpc}
  3766. { import }
  3767. if po_has_importdll in procoptions then
  3768. ppufile.putstring(import_dll^);
  3769. if po_has_importname in procoptions then
  3770. ppufile.putstring(import_name^);
  3771. ppufile.putword(import_nr);
  3772. if (po_msgint in procoptions) then
  3773. ppufile.putlongint(messageinf.i);
  3774. if (po_msgstr in procoptions) then
  3775. ppufile.putstring(messageinf.str^);
  3776. if (po_dispid in procoptions) then
  3777. ppufile.putlongint(dispid);
  3778. { inline stuff }
  3779. oldintfcrc:=ppufile.do_crc;
  3780. ppufile.do_crc:=false;
  3781. if (po_has_inlininginfo in procoptions) then
  3782. begin
  3783. ppufile.putderef(funcretsymderef);
  3784. ppufile.putsmallset(inlininginfo^.flags);
  3785. end;
  3786. { count alias names }
  3787. aliasnamescount:=0;
  3788. item:=TCmdStrListItem(aliasnames.first);
  3789. while assigned(item) do
  3790. begin
  3791. inc(aliasnamescount);
  3792. item:=TCmdStrListItem(item.next);
  3793. end;
  3794. if aliasnamescount>255 then
  3795. internalerror(200711021);
  3796. ppufile.putbyte(aliasnamescount);
  3797. item:=TCmdStrListItem(aliasnames.first);
  3798. while assigned(item) do
  3799. begin
  3800. ppufile.putstring(item.str);
  3801. item:=TCmdStrListItem(item.next);
  3802. end;
  3803. ppufile.do_crc:=oldintfcrc;
  3804. { write this entry }
  3805. ppufile.writeentry(ibprocdef);
  3806. { Save the para symtable, this is taken from the interface }
  3807. tparasymtable(parast).ppuwrite(ppufile);
  3808. { save localsymtable for inline procedures or when local
  3809. browser info is requested, this has no influence on the crc }
  3810. if (po_has_inlininginfo in procoptions) then
  3811. begin
  3812. oldintfcrc:=ppufile.do_crc;
  3813. ppufile.do_crc:=false;
  3814. tlocalsymtable(localst).ppuwrite(ppufile);
  3815. ppufile.do_crc:=oldintfcrc;
  3816. end;
  3817. { node tree for inlining }
  3818. oldintfcrc:=ppufile.do_crc;
  3819. ppufile.do_crc:=false;
  3820. if (po_has_inlininginfo in procoptions) then
  3821. ppuwritenodetree(ppufile,inlininginfo^.code);
  3822. ppufile.do_crc:=oldintfcrc;
  3823. end;
  3824. function tprocdef.fullprocname(showhidden:boolean):string;
  3825. var
  3826. pno: tprocnameoptions;
  3827. begin
  3828. pno:=[];
  3829. if showhidden then
  3830. include(pno,pno_showhidden);
  3831. result:=customprocname(pno);
  3832. end;
  3833. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  3834. var
  3835. s, rn : ansistring;
  3836. t : ttoken;
  3837. begin
  3838. {$ifdef EXTDEBUG}
  3839. include(pno,pno_showhidden);
  3840. {$endif EXTDEBUG}
  3841. s:='';
  3842. if proctypeoption=potype_operator then
  3843. begin
  3844. for t:=NOTOKEN to last_overloaded do
  3845. if procsym.realname='$'+overloaded_names[t] then
  3846. begin
  3847. s:='operator ';
  3848. if (pno_ownername in pno) and
  3849. assigned(struct) then
  3850. s:=s+struct.RttiName+'.';
  3851. s:=s+arraytokeninfo[t].str+typename_paras(pno);
  3852. break;
  3853. end;
  3854. end
  3855. else
  3856. begin
  3857. if (po_classmethod in procoptions) and
  3858. not(pno_noclassmarker in pno) then
  3859. s:='class ';
  3860. case proctypeoption of
  3861. potype_constructor,
  3862. potype_class_constructor:
  3863. s:=s+'constructor ';
  3864. potype_class_destructor,
  3865. potype_destructor:
  3866. s:=s+'destructor ';
  3867. else
  3868. if (pno_proctypeoption in pno) and
  3869. assigned(returndef) and
  3870. not(is_void(returndef)) then
  3871. s:=s+'function '
  3872. else
  3873. s:=s+'procedure ';
  3874. end;
  3875. if (pno_ownername in pno) and
  3876. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  3877. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  3878. rn:=procsym.realname;
  3879. if (pno_noleadingdollar in pno) and
  3880. (rn[1]='$') then
  3881. delete(rn,1,1);
  3882. s:=s+rn+typename_paras(pno);
  3883. end;
  3884. if not(proctypeoption in [potype_constructor,potype_destructor,
  3885. potype_class_constructor,potype_class_destructor]) and
  3886. assigned(returndef) and
  3887. not(is_void(returndef)) then
  3888. s:=s+':'+returndef.GetTypeName;
  3889. if owner.symtabletype=localsymtable then
  3890. s:=s+' is nested';
  3891. s:=s+';';
  3892. { forced calling convention? }
  3893. if (po_hascallingconvention in procoptions) then
  3894. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  3895. if (po_staticmethod in procoptions) and
  3896. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  3897. s:=s+' Static;';
  3898. customprocname:=s;
  3899. end;
  3900. function tprocdef.is_methodpointer:boolean;
  3901. begin
  3902. { don't check assigned(_class), that's also the case for nested
  3903. procedures inside methods }
  3904. result:=owner.symtabletype=ObjectSymtable;
  3905. end;
  3906. function tprocdef.is_addressonly:boolean;
  3907. begin
  3908. result:=assigned(owner) and
  3909. (owner.symtabletype<>ObjectSymtable) and
  3910. (not(m_nested_procvars in current_settings.modeswitches) or
  3911. not is_nested_pd(self));
  3912. end;
  3913. procedure tprocdef.make_external;
  3914. begin
  3915. include(procoptions,po_external);
  3916. forwarddef:=false;
  3917. end;
  3918. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  3919. begin
  3920. case t of
  3921. gs_local :
  3922. GetSymtable:=localst;
  3923. gs_para :
  3924. GetSymtable:=parast;
  3925. else
  3926. GetSymtable:=nil;
  3927. end;
  3928. end;
  3929. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef;
  3930. var
  3931. i : tcallercallee;
  3932. j : longint;
  3933. pvs : tparavarsym;
  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.