symdef.pas 224 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911
  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. begin
  3432. nestinglevel:=parast.symtablelevel;
  3433. if newtyp=procdef then
  3434. begin
  3435. if (copytyp<>pc_bareproc) then
  3436. result:=tprocdef.create(nestinglevel)
  3437. else
  3438. result:=tprocdef.create(normal_function_level);
  3439. tprocdef(result).visibility:=vis_public;
  3440. end
  3441. else
  3442. begin
  3443. result:=tprocvardef.create(nestinglevel);
  3444. end;
  3445. tabstractprocdef(result).returndef:=returndef;
  3446. tabstractprocdef(result).returndefderef:=returndefderef;
  3447. pvs:=nil;
  3448. npvs:=nil;
  3449. for j:=0 to parast.symlist.count-1 do
  3450. begin
  3451. case tsym(parast.symlist[j]).typ of
  3452. paravarsym:
  3453. begin
  3454. pvs:=tparavarsym(parast.symlist[j]);
  3455. { in case of bare proc, don't copy self, vmt or framepointer
  3456. parameters }
  3457. if (copytyp=pc_bareproc) and
  3458. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result]*pvs.varoptions)<>[]) then
  3459. continue;
  3460. npvs:=tparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  3461. pvs.vardef,pvs.varoptions);
  3462. npvs.defaultconstsym:=pvs.defaultconstsym;
  3463. tabstractprocdef(result).parast.insert(npvs);
  3464. end;
  3465. constsym:
  3466. begin
  3467. // ignore, reuse original constym. Should also be duplicated
  3468. // be safe though
  3469. end;
  3470. symconst.typesym:
  3471. begin
  3472. // reuse original, part of generic declaration
  3473. end
  3474. else
  3475. internalerror(201160604);
  3476. end;
  3477. end;
  3478. tabstractprocdef(result).savesize:=savesize;
  3479. tabstractprocdef(result).proctypeoption:=proctypeoption;
  3480. tabstractprocdef(result).proccalloption:=proccalloption;
  3481. tabstractprocdef(result).procoptions:=procoptions;
  3482. if (copytyp=pc_bareproc) then
  3483. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  3484. if newtyp=procvardef then
  3485. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  3486. tabstractprocdef(result).callerargareasize:=callerargareasize;
  3487. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  3488. tabstractprocdef(result).maxparacount:=maxparacount;
  3489. tabstractprocdef(result).minparacount:=minparacount;
  3490. if po_explicitparaloc in procoptions then
  3491. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  3492. { recalculate parameter info }
  3493. tabstractprocdef(result).has_paraloc_info:=callnoside;
  3494. {$ifdef m68k}
  3495. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  3496. {$endif}
  3497. if (typ=procdef) and
  3498. (newtyp=procvardef) and
  3499. (owner.symtabletype=ObjectSymtable) then
  3500. include(tprocvardef(result).procoptions,po_methodpointer);
  3501. end;
  3502. procedure tabstractprocdef.check_mark_as_nested;
  3503. begin
  3504. { nested procvars require that nested functions use the Delphi-style
  3505. nested procedure calling convention }
  3506. if (parast.symtablelevel>normal_function_level) and
  3507. (m_nested_procvars in current_settings.modeswitches) then
  3508. include(procoptions,po_delphi_nested_cc);
  3509. end;
  3510. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  3511. begin
  3512. if (side in [callerside,callbothsides]) and
  3513. not(has_paraloc_info in [callerside,callbothsides]) then
  3514. begin
  3515. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  3516. if has_paraloc_info in [calleeside,callbothsides] then
  3517. has_paraloc_info:=callbothsides
  3518. else
  3519. has_paraloc_info:=callerside;
  3520. end;
  3521. if (side in [calleeside,callbothsides]) and
  3522. not(has_paraloc_info in [calleeside,callbothsides]) then
  3523. begin
  3524. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  3525. if has_paraloc_info in [callerside,callbothsides] then
  3526. has_paraloc_info:=callbothsides
  3527. else
  3528. has_paraloc_info:=calleeside;
  3529. end;
  3530. end;
  3531. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  3532. var
  3533. p: tparavarsym;
  3534. ploc: PCGParalocation;
  3535. i: longint;
  3536. begin
  3537. result:=false;
  3538. init_paraloc_info(side);
  3539. for i:=0 to parast.SymList.Count-1 do
  3540. if tsym(parast.SymList[i]).typ=paravarsym then
  3541. begin
  3542. p:=tparavarsym(parast.SymList[i]);
  3543. { check if no parameter is located on the stack }
  3544. if is_open_array(p.vardef) or
  3545. is_array_of_const(p.vardef) then
  3546. begin
  3547. result:=true;
  3548. exit;
  3549. end;
  3550. ploc:=p.paraloc[side].location;
  3551. while assigned(ploc) do
  3552. begin
  3553. if (ploc^.loc=LOC_REFERENCE) then
  3554. begin
  3555. result:=true;
  3556. exit
  3557. end;
  3558. ploc:=ploc^.next;
  3559. end;
  3560. end;
  3561. end;
  3562. {***************************************************************************
  3563. TPROCDEF
  3564. ***************************************************************************}
  3565. constructor tprocdef.create(level:byte);
  3566. begin
  3567. inherited create(procdef,level);
  3568. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  3569. {$ifdef symansistr}
  3570. _mangledname:='';
  3571. {$else symansistr}
  3572. _mangledname:=nil;
  3573. {$endif symansistr}
  3574. fileinfo:=current_filepos;
  3575. extnumber:=$ffff;
  3576. aliasnames:=TCmdStrList.create;
  3577. funcretsym:=nil;
  3578. forwarddef:=true;
  3579. interfacedef:=false;
  3580. hasforward:=false;
  3581. struct := nil;
  3582. import_dll:=nil;
  3583. import_name:=nil;
  3584. import_nr:=0;
  3585. inlininginfo:=nil;
  3586. deprecatedmsg:=nil;
  3587. {$ifdef i386}
  3588. fpu_used:=maxfpuregs;
  3589. {$endif i386}
  3590. end;
  3591. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  3592. var
  3593. i,aliasnamescount : longint;
  3594. level : byte;
  3595. begin
  3596. inherited ppuload(procdef,ppufile);
  3597. {$ifdef symansistr}
  3598. if po_has_mangledname in procoptions then
  3599. _mangledname:=ppufile.getansistring
  3600. else
  3601. _mangledname:='';
  3602. {$else symansistr}
  3603. if po_has_mangledname in procoptions then
  3604. _mangledname:=stringdup(ppufile.getstring)
  3605. else
  3606. _mangledname:=nil;
  3607. {$endif symansistr}
  3608. extnumber:=ppufile.getword;
  3609. level:=ppufile.getbyte;
  3610. ppufile.getderef(structderef);
  3611. ppufile.getderef(procsymderef);
  3612. ppufile.getposinfo(fileinfo);
  3613. visibility:=tvisibility(ppufile.getbyte);
  3614. ppufile.getsmallset(symoptions);
  3615. if sp_has_deprecated_msg in symoptions then
  3616. deprecatedmsg:=stringdup(ppufile.getstring)
  3617. else
  3618. deprecatedmsg:=nil;
  3619. {$ifdef powerpc}
  3620. { library symbol for AmigaOS/MorphOS }
  3621. ppufile.getderef(libsymderef);
  3622. {$endif powerpc}
  3623. { import stuff }
  3624. if po_has_importdll in procoptions then
  3625. import_dll:=stringdup(ppufile.getstring)
  3626. else
  3627. import_dll:=nil;
  3628. if po_has_importname in procoptions then
  3629. import_name:=stringdup(ppufile.getstring)
  3630. else
  3631. import_name:=nil;
  3632. import_nr:=ppufile.getword;
  3633. if (po_msgint in procoptions) then
  3634. messageinf.i:=ppufile.getlongint;
  3635. if (po_msgstr in procoptions) then
  3636. messageinf.str:=stringdup(ppufile.getstring);
  3637. if (po_dispid in procoptions) then
  3638. dispid:=ppufile.getlongint;
  3639. { inline stuff }
  3640. if (po_has_inlininginfo in procoptions) then
  3641. begin
  3642. ppufile.getderef(funcretsymderef);
  3643. new(inlininginfo);
  3644. ppufile.getsmallset(inlininginfo^.flags);
  3645. end
  3646. else
  3647. begin
  3648. inlininginfo:=nil;
  3649. funcretsym:=nil;
  3650. end;
  3651. aliasnames:=TCmdStrList.create;
  3652. { count alias names }
  3653. aliasnamescount:=ppufile.getbyte;
  3654. for i:=1 to aliasnamescount do
  3655. aliasnames.insert(ppufile.getstring);
  3656. { load para symtable }
  3657. parast:=tparasymtable.create(self,level);
  3658. tparasymtable(parast).ppuload(ppufile);
  3659. { load local symtable }
  3660. if (po_has_inlininginfo in procoptions) then
  3661. begin
  3662. localst:=tlocalsymtable.create(self,level);
  3663. tlocalsymtable(localst).ppuload(ppufile);
  3664. end
  3665. else
  3666. localst:=nil;
  3667. { inline stuff }
  3668. if (po_has_inlininginfo in procoptions) then
  3669. inlininginfo^.code:=ppuloadnodetree(ppufile);
  3670. { default values for no persistent data }
  3671. if (cs_link_deffile in current_settings.globalswitches) and
  3672. (tf_need_export in target_info.flags) and
  3673. (po_exports in procoptions) then
  3674. deffile.AddExport(mangledname);
  3675. forwarddef:=false;
  3676. interfacedef:=false;
  3677. hasforward:=false;
  3678. { Disable po_has_inlining until the derefimpl is done }
  3679. exclude(procoptions,po_has_inlininginfo);
  3680. {$ifdef i386}
  3681. fpu_used:=maxfpuregs;
  3682. {$endif i386}
  3683. end;
  3684. destructor tprocdef.destroy;
  3685. begin
  3686. aliasnames.free;
  3687. aliasnames:=nil;
  3688. if assigned(localst) and
  3689. (localst.symtabletype<>staticsymtable) then
  3690. begin
  3691. {$ifdef MEMDEBUG}
  3692. memproclocalst.start;
  3693. {$endif MEMDEBUG}
  3694. localst.free;
  3695. localst:=nil;
  3696. {$ifdef MEMDEBUG}
  3697. memproclocalst.start;
  3698. {$endif MEMDEBUG}
  3699. end;
  3700. if assigned(inlininginfo) then
  3701. begin
  3702. {$ifdef MEMDEBUG}
  3703. memprocnodetree.start;
  3704. {$endif MEMDEBUG}
  3705. tnode(inlininginfo^.code).free;
  3706. {$ifdef MEMDEBUG}
  3707. memprocnodetree.start;
  3708. {$endif MEMDEBUG}
  3709. dispose(inlininginfo);
  3710. inlininginfo:=nil;
  3711. end;
  3712. {$ifdef jvm}
  3713. exprasmlist.free;
  3714. {$endif}
  3715. stringdispose(resultname);
  3716. stringdispose(import_dll);
  3717. stringdispose(import_name);
  3718. stringdispose(deprecatedmsg);
  3719. if (po_msgstr in procoptions) then
  3720. stringdispose(messageinf.str);
  3721. {$ifndef symansistr}
  3722. if assigned(_mangledname) then
  3723. begin
  3724. {$ifdef MEMDEBUG}
  3725. memmanglednames.start;
  3726. {$endif MEMDEBUG}
  3727. stringdispose(_mangledname);
  3728. {$ifdef MEMDEBUG}
  3729. memmanglednames.stop;
  3730. {$endif MEMDEBUG}
  3731. end;
  3732. {$endif symansistr}
  3733. inherited destroy;
  3734. end;
  3735. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  3736. var
  3737. oldintfcrc : boolean;
  3738. aliasnamescount : longint;
  3739. item : TCmdStrListItem;
  3740. begin
  3741. { released procdef? }
  3742. if not assigned(parast) then
  3743. exit;
  3744. inherited ppuwrite(ppufile);
  3745. {$ifdef symansistr}
  3746. if po_has_mangledname in procoptions then
  3747. ppufile.putansistring(_mangledname);
  3748. {$else symansistr}
  3749. if po_has_mangledname in procoptions then
  3750. ppufile.putstring(_mangledname^);
  3751. {$endif symansistr}
  3752. ppufile.putword(extnumber);
  3753. ppufile.putbyte(parast.symtablelevel);
  3754. ppufile.putderef(structderef);
  3755. ppufile.putderef(procsymderef);
  3756. ppufile.putposinfo(fileinfo);
  3757. ppufile.putbyte(byte(visibility));
  3758. ppufile.putsmallset(symoptions);
  3759. if sp_has_deprecated_msg in symoptions then
  3760. ppufile.putstring(deprecatedmsg^);
  3761. {$ifdef powerpc}
  3762. { library symbol for AmigaOS/MorphOS }
  3763. ppufile.putderef(libsymderef);
  3764. {$endif powerpc}
  3765. { import }
  3766. if po_has_importdll in procoptions then
  3767. ppufile.putstring(import_dll^);
  3768. if po_has_importname in procoptions then
  3769. ppufile.putstring(import_name^);
  3770. ppufile.putword(import_nr);
  3771. if (po_msgint in procoptions) then
  3772. ppufile.putlongint(messageinf.i);
  3773. if (po_msgstr in procoptions) then
  3774. ppufile.putstring(messageinf.str^);
  3775. if (po_dispid in procoptions) then
  3776. ppufile.putlongint(dispid);
  3777. { inline stuff }
  3778. oldintfcrc:=ppufile.do_crc;
  3779. ppufile.do_crc:=false;
  3780. if (po_has_inlininginfo in procoptions) then
  3781. begin
  3782. ppufile.putderef(funcretsymderef);
  3783. ppufile.putsmallset(inlininginfo^.flags);
  3784. end;
  3785. { count alias names }
  3786. aliasnamescount:=0;
  3787. item:=TCmdStrListItem(aliasnames.first);
  3788. while assigned(item) do
  3789. begin
  3790. inc(aliasnamescount);
  3791. item:=TCmdStrListItem(item.next);
  3792. end;
  3793. if aliasnamescount>255 then
  3794. internalerror(200711021);
  3795. ppufile.putbyte(aliasnamescount);
  3796. item:=TCmdStrListItem(aliasnames.first);
  3797. while assigned(item) do
  3798. begin
  3799. ppufile.putstring(item.str);
  3800. item:=TCmdStrListItem(item.next);
  3801. end;
  3802. ppufile.do_crc:=oldintfcrc;
  3803. { write this entry }
  3804. ppufile.writeentry(ibprocdef);
  3805. { Save the para symtable, this is taken from the interface }
  3806. tparasymtable(parast).ppuwrite(ppufile);
  3807. { save localsymtable for inline procedures or when local
  3808. browser info is requested, this has no influence on the crc }
  3809. if (po_has_inlininginfo in procoptions) then
  3810. begin
  3811. oldintfcrc:=ppufile.do_crc;
  3812. ppufile.do_crc:=false;
  3813. tlocalsymtable(localst).ppuwrite(ppufile);
  3814. ppufile.do_crc:=oldintfcrc;
  3815. end;
  3816. { node tree for inlining }
  3817. oldintfcrc:=ppufile.do_crc;
  3818. ppufile.do_crc:=false;
  3819. if (po_has_inlininginfo in procoptions) then
  3820. ppuwritenodetree(ppufile,inlininginfo^.code);
  3821. ppufile.do_crc:=oldintfcrc;
  3822. end;
  3823. function tprocdef.fullprocname(showhidden:boolean):string;
  3824. var
  3825. pno: tprocnameoptions;
  3826. begin
  3827. pno:=[];
  3828. if showhidden then
  3829. include(pno,pno_showhidden);
  3830. result:=customprocname(pno);
  3831. end;
  3832. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  3833. var
  3834. s, rn : ansistring;
  3835. t : ttoken;
  3836. begin
  3837. {$ifdef EXTDEBUG}
  3838. include(pno,pno_showhidden);
  3839. {$endif EXTDEBUG}
  3840. s:='';
  3841. if proctypeoption=potype_operator then
  3842. begin
  3843. for t:=NOTOKEN to last_overloaded do
  3844. if procsym.realname='$'+overloaded_names[t] then
  3845. begin
  3846. s:='operator ';
  3847. if (pno_ownername in pno) and
  3848. assigned(struct) then
  3849. s:=s+struct.RttiName+'.';
  3850. s:=s+arraytokeninfo[t].str+typename_paras(pno);
  3851. break;
  3852. end;
  3853. end
  3854. else
  3855. begin
  3856. if (po_classmethod in procoptions) and
  3857. not(pno_noclassmarker in pno) then
  3858. s:='class ';
  3859. case proctypeoption of
  3860. potype_constructor,
  3861. potype_class_constructor:
  3862. s:=s+'constructor ';
  3863. potype_class_destructor,
  3864. potype_destructor:
  3865. s:=s+'destructor ';
  3866. else
  3867. if (pno_proctypeoption in pno) and
  3868. assigned(returndef) and
  3869. not(is_void(returndef)) then
  3870. s:=s+'function '
  3871. else
  3872. s:=s+'procedure ';
  3873. end;
  3874. if (pno_ownername in pno) and
  3875. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  3876. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  3877. rn:=procsym.realname;
  3878. if (pno_noleadingdollar in pno) and
  3879. (rn[1]='$') then
  3880. delete(rn,1,1);
  3881. s:=s+rn+typename_paras(pno);
  3882. end;
  3883. if not(proctypeoption in [potype_constructor,potype_destructor,
  3884. potype_class_constructor,potype_class_destructor]) and
  3885. assigned(returndef) and
  3886. not(is_void(returndef)) then
  3887. s:=s+':'+returndef.GetTypeName;
  3888. if owner.symtabletype=localsymtable then
  3889. s:=s+' is nested';
  3890. s:=s+';';
  3891. { forced calling convention? }
  3892. if (po_hascallingconvention in procoptions) then
  3893. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  3894. if (po_staticmethod in procoptions) and
  3895. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  3896. s:=s+' Static;';
  3897. customprocname:=s;
  3898. end;
  3899. function tprocdef.is_methodpointer:boolean;
  3900. begin
  3901. { don't check assigned(_class), that's also the case for nested
  3902. procedures inside methods }
  3903. result:=owner.symtabletype=ObjectSymtable;
  3904. end;
  3905. function tprocdef.is_addressonly:boolean;
  3906. begin
  3907. result:=assigned(owner) and
  3908. (owner.symtabletype<>ObjectSymtable) and
  3909. (not(m_nested_procvars in current_settings.modeswitches) or
  3910. not is_nested_pd(self));
  3911. end;
  3912. procedure tprocdef.make_external;
  3913. begin
  3914. include(procoptions,po_external);
  3915. forwarddef:=false;
  3916. end;
  3917. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  3918. begin
  3919. case t of
  3920. gs_local :
  3921. GetSymtable:=localst;
  3922. gs_para :
  3923. GetSymtable:=parast;
  3924. else
  3925. GetSymtable:=nil;
  3926. end;
  3927. end;
  3928. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef;
  3929. var
  3930. j : longint;
  3931. begin
  3932. result:=inherited getcopyas(newtyp,copytyp);
  3933. if newtyp=procvardef then
  3934. exit;
  3935. { don't copy mangled name, can be different }
  3936. tprocdef(result).messageinf:=messageinf;
  3937. tprocdef(result).dispid:=dispid;
  3938. if po_msgstr in procoptions then
  3939. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  3940. tprocdef(result).symoptions:=symoptions;
  3941. if assigned(deprecatedmsg) then
  3942. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  3943. { will have to be associated with appropriate procsym }
  3944. tprocdef(result).procsym:=nil;
  3945. if copytyp<>pc_bareproc then
  3946. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  3947. if assigned(funcretsym) then
  3948. begin
  3949. if funcretsym.owner=parast then
  3950. begin
  3951. j:=parast.symlist.indexof(funcretsym);
  3952. if j<0 then
  3953. internalerror(2011040606);
  3954. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  3955. end
  3956. else if funcretsym.owner=localst then
  3957. begin
  3958. { nothing to do, will be inserted for the new procdef while
  3959. parsing its body (by pdecsub.insert_funcret_local) }
  3960. end
  3961. else
  3962. internalerror(2011040605);
  3963. end;
  3964. { will have to be associated with a new struct }
  3965. tprocdef(result).struct:=nil;
  3966. {$if defined(powerpc) or defined(m68k)}
  3967. tprocdef(result).libsym:=libsym;
  3968. {$endif powerpc or m68k}
  3969. if assigned(resultname) then
  3970. tprocdef(result).resultname:=stringdup(resultname^);
  3971. if assigned(import_dll) then
  3972. tprocdef(result).import_dll:=stringdup(import_dll^);
  3973. if assigned(import_name) then
  3974. tprocdef(result).import_name:=stringdup(import_name^);
  3975. tprocdef(result).import_nr:=import_nr;
  3976. tprocdef(result).extnumber:=$ffff;
  3977. {$ifdef i386}
  3978. tprocdef(result).fpu_used:=fpu_used;
  3979. {$endif i386}
  3980. tprocdef(result).visibility:=visibility;
  3981. tprocdef(result).synthetickind:=synthetickind;
  3982. { we need a separate implementation for the copied def }
  3983. tprocdef(result).forwarddef:=true;
  3984. tprocdef(result).interfacedef:=true;
  3985. { create new paralist }
  3986. tprocdef(result).calcparas;
  3987. end;
  3988. function tprocdef.getcopy: tstoreddef;
  3989. begin
  3990. result:=getcopyas(procdef,pc_normal);
  3991. end;
  3992. procedure tprocdef.buildderef;
  3993. begin
  3994. inherited buildderef;
  3995. structderef.build(struct);
  3996. { procsym that originaly defined this definition, should be in the
  3997. same symtable }
  3998. procsymderef.build(procsym);
  3999. {$ifdef powerpc}
  4000. { library symbol for AmigaOS/MorphOS }
  4001. libsymderef.build(libsym);
  4002. {$endif powerpc}
  4003. end;
  4004. procedure tprocdef.buildderefimpl;
  4005. begin
  4006. inherited buildderefimpl;
  4007. { Localst is not available for main/unit init }
  4008. if assigned(localst) then
  4009. begin
  4010. tlocalsymtable(localst).buildderef;
  4011. tlocalsymtable(localst).buildderefimpl;
  4012. end;
  4013. { inline tree }
  4014. if (po_has_inlininginfo in procoptions) then
  4015. begin
  4016. funcretsymderef.build(funcretsym);
  4017. inlininginfo^.code.buildderefimpl;
  4018. end;
  4019. end;
  4020. procedure tprocdef.deref;
  4021. begin
  4022. inherited deref;
  4023. struct:=tabstractrecorddef(structderef.resolve);
  4024. { procsym that originaly defined this definition, should be in the
  4025. same symtable }
  4026. procsym:=tprocsym(procsymderef.resolve);
  4027. {$ifdef powerpc}
  4028. { library symbol for AmigaOS/MorphOS }
  4029. libsym:=tsym(libsymderef.resolve);
  4030. {$endif powerpc}
  4031. end;
  4032. procedure tprocdef.derefimpl;
  4033. begin
  4034. { Enable has_inlininginfo when the inlininginfo
  4035. structure is available. The has_inlininginfo was disabled
  4036. after the load, since the data was invalid }
  4037. if assigned(inlininginfo) then
  4038. include(procoptions,po_has_inlininginfo);
  4039. { Locals }
  4040. if assigned(localst) then
  4041. begin
  4042. tlocalsymtable(localst).deref;
  4043. tlocalsymtable(localst).derefimpl;
  4044. end;
  4045. { Inline }
  4046. if (po_has_inlininginfo in procoptions) then
  4047. begin
  4048. inlininginfo^.code.derefimpl;
  4049. { funcretsym, this is always located in the localst }
  4050. funcretsym:=tsym(funcretsymderef.resolve);
  4051. end
  4052. else
  4053. begin
  4054. { safety }
  4055. { Not safe! A unit may be reresolved after its interface has been
  4056. parsed but before its implementation has been parsed, and in that
  4057. case the funcretsym is still required!
  4058. funcretsym:=nil; }
  4059. end;
  4060. end;
  4061. function tprocdef.GetTypeName : string;
  4062. begin
  4063. GetTypeName := FullProcName(false);
  4064. end;
  4065. function tprocdef.mangledname : TSymStr;
  4066. begin
  4067. {$ifdef symansistr}
  4068. if _mangledname<>'' then
  4069. {$else symansistr}
  4070. if assigned(_mangledname) then
  4071. {$endif symansistr}
  4072. begin
  4073. {$ifdef compress}
  4074. {$error add support for ansistrings in case of symansistr}
  4075. mangledname:=minilzw_decode(_mangledname^);
  4076. {$else}
  4077. {$ifdef symansistr}
  4078. mangledname:=_mangledname;
  4079. {$else symansistr}
  4080. mangledname:=_mangledname^;
  4081. {$endif symansistr}
  4082. {$endif}
  4083. exit;
  4084. end;
  4085. {$ifndef jvm}
  4086. mangledname:=defaultmangledname;
  4087. {$else not jvm}
  4088. mangledname:=jvmmangledbasename(false);
  4089. if (po_has_importdll in procoptions) then
  4090. begin
  4091. { import_dll comes from "external 'import_dll_name' name 'external_name'" }
  4092. if assigned(import_dll) then
  4093. mangledname:=import_dll^+'/'+mangledname
  4094. else
  4095. internalerror(2010122607);
  4096. end
  4097. else
  4098. jvmaddtypeownerprefix(owner,mangledname);
  4099. {$endif not jvm}
  4100. {$ifdef compress}
  4101. {$error add support for ansistrings in case of symansistr}
  4102. _mangledname:=stringdup(minilzw_encode(mangledname));
  4103. {$else}
  4104. {$ifdef symansistr}
  4105. _mangledname:=mangledname;
  4106. {$else symansistr}
  4107. _mangledname:=stringdup(mangledname);
  4108. {$endif symansistr}
  4109. {$endif}
  4110. end;
  4111. function tprocdef.defaultmangledname: TSymStr;
  4112. var
  4113. hp : TParavarsym;
  4114. hs : TSymStr;
  4115. crc : dword;
  4116. newlen,
  4117. oldlen,
  4118. i : integer;
  4119. begin
  4120. { we need to use the symtable where the procsym is inserted,
  4121. because that is visible to the world }
  4122. defaultmangledname:=make_mangledname('',procsym.owner,procsym.name);
  4123. oldlen:=length(defaultmangledname);
  4124. { add parameter types }
  4125. for i:=0 to paras.count-1 do
  4126. begin
  4127. hp:=tparavarsym(paras[i]);
  4128. if not(vo_is_hidden_para in hp.varoptions) then
  4129. defaultmangledname:=defaultmangledname+'$'+hp.vardef.mangledparaname;
  4130. end;
  4131. { add resultdef, add $$ as separator to make it unique from a
  4132. parameter separator }
  4133. if not is_void(returndef) then
  4134. defaultmangledname:=defaultmangledname+'$$'+returndef.mangledparaname;
  4135. newlen:=length(defaultmangledname);
  4136. { Replace with CRC if the parameter line is very long }
  4137. if (newlen-oldlen>12) and
  4138. ((newlen>100) or (newlen-oldlen>64)) then
  4139. begin
  4140. crc:=0;
  4141. for i:=0 to paras.count-1 do
  4142. begin
  4143. hp:=tparavarsym(paras[i]);
  4144. if not(vo_is_hidden_para in hp.varoptions) then
  4145. begin
  4146. hs:=hp.vardef.mangledparaname;
  4147. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4148. end;
  4149. end;
  4150. hs:=hp.vardef.mangledparaname;
  4151. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4152. defaultmangledname:=Copy(defaultmangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  4153. end;
  4154. end;
  4155. function tprocdef.cplusplusmangledname : TSymStr;
  4156. function getcppparaname(p : tdef) : TSymStr;
  4157. const
  4158. {$ifdef NAMEMANGLING_GCC2}
  4159. ordtype2str : array[tordtype] of string[2] = (
  4160. '',
  4161. 'Uc','Us','Ui','Us',
  4162. 'Sc','s','i','x',
  4163. 'b','b','b','b','b',
  4164. 'c','w','x');
  4165. {$else NAMEMANGLING_GCC2}
  4166. ordtype2str : array[tordtype] of string[1] = (
  4167. 'v',
  4168. 'h','t','j','y',
  4169. 'a','s','i','x',
  4170. 'b','b','b','b',
  4171. 'b','b','b','b',
  4172. 'c','w','x');
  4173. floattype2str : array[tfloattype] of string[1] = (
  4174. 'f','d','e','e',
  4175. 'd','d','g');
  4176. {$endif NAMEMANGLING_GCC2}
  4177. var
  4178. s : TSymStr;
  4179. begin
  4180. case p.typ of
  4181. orddef:
  4182. s:=ordtype2str[torddef(p).ordtype];
  4183. pointerdef:
  4184. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  4185. {$ifndef NAMEMANGLING_GCC2}
  4186. floatdef:
  4187. s:=floattype2str[tfloatdef(p).floattype];
  4188. {$endif NAMEMANGLING_GCC2}
  4189. else
  4190. internalerror(2103001);
  4191. end;
  4192. getcppparaname:=s;
  4193. end;
  4194. var
  4195. s,s2 : TSymStr;
  4196. hp : TParavarsym;
  4197. i : integer;
  4198. begin
  4199. {$ifdef NAMEMANGLING_GCC2}
  4200. { outdated gcc 2.x name mangling scheme }
  4201. s := procsym.realname;
  4202. if procsym.owner.symtabletype=ObjectSymtable then
  4203. begin
  4204. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  4205. case proctypeoption of
  4206. potype_destructor:
  4207. s:='_$_'+tostr(length(s2))+s2;
  4208. potype_constructor:
  4209. s:='___'+tostr(length(s2))+s2;
  4210. else
  4211. s:='_'+s+'__'+tostr(length(s2))+s2;
  4212. end;
  4213. end
  4214. else s:=s+'__';
  4215. s:=s+'F';
  4216. { concat modifiers }
  4217. { !!!!! }
  4218. { now we handle the parameters }
  4219. if maxparacount>0 then
  4220. begin
  4221. for i:=0 to paras.count-1 do
  4222. begin
  4223. hp:=tparavarsym(paras[i]);
  4224. { no hidden parameters form part of a C++ mangled name:
  4225. a) self is not included
  4226. b) there are no "high" or other hidden parameters
  4227. }
  4228. if vo_is_hidden_para in hp.varoptions then
  4229. continue;
  4230. s2:=getcppparaname(hp.vardef);
  4231. if hp.varspez in [vs_var,vs_out] then
  4232. s2:='R'+s2;
  4233. s:=s+s2;
  4234. end;
  4235. end
  4236. else
  4237. s:=s+'v';
  4238. cplusplusmangledname:=s;
  4239. {$else NAMEMANGLING_GCC2}
  4240. { gcc 3.x and 4.x name mangling scheme }
  4241. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  4242. if procsym.owner.symtabletype=ObjectSymtable then
  4243. begin
  4244. s:='_ZN';
  4245. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  4246. s:=s+tostr(length(s2))+s2;
  4247. case proctypeoption of
  4248. potype_constructor:
  4249. s:=s+'C1';
  4250. potype_destructor:
  4251. s:=s+'D1';
  4252. else
  4253. s:=s+tostr(length(procsym.realname))+procsym.realname;
  4254. end;
  4255. s:=s+'E';
  4256. end
  4257. else
  4258. s:=procsym.realname;
  4259. { now we handle the parameters }
  4260. if maxparacount>0 then
  4261. begin
  4262. for i:=0 to paras.count-1 do
  4263. begin
  4264. hp:=tparavarsym(paras[i]);
  4265. { no hidden parameters form part of a C++ mangled name:
  4266. a) self is not included
  4267. b) there are no "high" or other hidden parameters
  4268. }
  4269. if vo_is_hidden_para in hp.varoptions then
  4270. continue;
  4271. s2:=getcppparaname(hp.vardef);
  4272. if hp.varspez in [vs_var,vs_out] then
  4273. s2:='R'+s2;
  4274. s:=s+s2;
  4275. end;
  4276. end
  4277. else
  4278. s:=s+'v';
  4279. cplusplusmangledname:=s;
  4280. {$endif NAMEMANGLING_GCC2}
  4281. end;
  4282. function tprocdef.objcmangledname : TSymStr;
  4283. var
  4284. manglednamelen: longint;
  4285. iscatmethod : boolean;
  4286. begin
  4287. if not (po_msgstr in procoptions) then
  4288. internalerror(2009030901);
  4289. { we may very well need longer strings to handle these... }
  4290. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  4291. length('+"[ ]"')+length(messageinf.str^);
  4292. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  4293. if (iscatmethod) then
  4294. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  4295. if manglednamelen>255 then
  4296. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  4297. if not(po_classmethod in procoptions) then
  4298. result:='"-['
  4299. else
  4300. result:='"+[';
  4301. { quotes are necessary because the +/- otherwise confuse the assembler
  4302. into expecting a number
  4303. }
  4304. if iscatmethod then
  4305. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  4306. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  4307. if iscatmethod then
  4308. result:=result+')';
  4309. result:=result+' '+messageinf.str^+']"';
  4310. end;
  4311. {$ifdef jvm}
  4312. function tprocdef.jvmmangledbasename(signature: boolean): TSymStr;
  4313. var
  4314. vs: tparavarsym;
  4315. i: longint;
  4316. founderror: tdef;
  4317. tmpresult: TSymStr;
  4318. container: tsymtable;
  4319. begin
  4320. { format:
  4321. * method definition (in Jasmin):
  4322. (private|protected|public) [static] method(parametertypes)returntype
  4323. * method invocation
  4324. package/class/method(parametertypes)returntype
  4325. -> store common part: method(parametertypes)returntype and
  4326. adorn as required when using it.
  4327. }
  4328. if not signature then
  4329. begin
  4330. { method name }
  4331. { special names for constructors and class constructors }
  4332. if proctypeoption=potype_constructor then
  4333. tmpresult:='<init>'
  4334. else if proctypeoption in [potype_class_constructor,potype_unitinit] then
  4335. tmpresult:='<clinit>'
  4336. else if po_has_importname in procoptions then
  4337. begin
  4338. if assigned(import_name) then
  4339. tmpresult:=import_name^
  4340. else
  4341. internalerror(2010122608);
  4342. end
  4343. else
  4344. begin
  4345. tmpresult:=procsym.realname;
  4346. if tmpresult[1]='$' then
  4347. tmpresult:=copy(tmpresult,2,length(tmpresult)-1);
  4348. { nested functions }
  4349. container:=owner;
  4350. while container.symtabletype=localsymtable do
  4351. begin
  4352. tmpresult:='$'+tprocdef(owner.defowner).procsym.realname+'$'+tostr(tprocdef(owner.defowner).procsym.symid)+'$'+tmpresult;
  4353. container:=container.defowner.owner;
  4354. end;
  4355. end;
  4356. end
  4357. else
  4358. tmpresult:='';
  4359. { parameter types }
  4360. tmpresult:=tmpresult+'(';
  4361. { not the case for the main program (not required for defaultmangledname
  4362. because setmangledname() is called for the main program; in case of
  4363. the JVM, this only sets the importname, however) }
  4364. if assigned(paras) then
  4365. begin
  4366. init_paraloc_info(callerside);
  4367. for i:=0 to paras.count-1 do
  4368. begin
  4369. vs:=tparavarsym(paras[i]);
  4370. { function result is not part of the mangled name }
  4371. if vo_is_funcret in vs.varoptions then
  4372. continue;
  4373. { self pointer neither, except for class methods (the JVM only
  4374. supports static class methods natively, so the self pointer
  4375. here is a regular parameter as far as the JVM is concerned }
  4376. if not(po_classmethod in procoptions) and
  4377. (vo_is_self in vs.varoptions) then
  4378. continue;
  4379. { passing by reference is emulated by passing an array of one
  4380. element containing the value; for types that aren't pointers
  4381. in regular Pascal, simply passing the underlying pointer type
  4382. does achieve regular call-by-reference semantics though;
  4383. formaldefs always have to be passed like that because their
  4384. contents can be replaced }
  4385. if paramanager.push_copyout_param(vs.varspez,vs.vardef,proccalloption) then
  4386. tmpresult:=tmpresult+'[';
  4387. { Add the parameter type. }
  4388. if not jvmaddencodedtype(vs.vardef,false,tmpresult,signature,founderror) then
  4389. { an internalerror here is also triggered in case of errors in the source code }
  4390. tmpresult:='<error>';
  4391. end;
  4392. end;
  4393. tmpresult:=tmpresult+')';
  4394. { And the type of the function result (void in case of a procedure and
  4395. constructor). }
  4396. if (proctypeoption in [potype_constructor,potype_class_constructor]) then
  4397. jvmaddencodedtype(voidtype,false,tmpresult,signature,founderror)
  4398. else if not jvmaddencodedtype(returndef,false,tmpresult,signature,founderror) then
  4399. { an internalerror here is also triggered in case of errors in the source code }
  4400. tmpresult:='<error>';
  4401. result:=tmpresult;
  4402. end;
  4403. {$endif jvm}
  4404. procedure tprocdef.setmangledname(const s : TSymStr);
  4405. begin
  4406. { This is not allowed anymore, the forward declaration
  4407. already needs to create the correct mangledname, no changes
  4408. afterwards are allowed (PFV) }
  4409. { Exception: interface definitions in mode macpas, since in that }
  4410. { case no reference to the old name can exist yet (JM) }
  4411. {$ifdef symansistr}
  4412. if _mangledname<>'' then
  4413. if ((m_mac in current_settings.modeswitches) and
  4414. (interfacedef)) then
  4415. _mangledname:=''
  4416. else
  4417. internalerror(200411171);
  4418. {$else symansistr}
  4419. if assigned(_mangledname) then
  4420. if ((m_mac in current_settings.modeswitches) and
  4421. (interfacedef)) then
  4422. stringdispose(_mangledname)
  4423. else
  4424. internalerror(200411171);
  4425. {$endif symansistr}
  4426. {$ifdef jvm}
  4427. { this routine can be called for compilerproces. can't set mangled
  4428. name since it must be calculated, but it uses import_name when set
  4429. -> set that one }
  4430. import_name:=stringdup(s);
  4431. include(procoptions,po_has_importname);
  4432. include(procoptions,po_has_mangledname);
  4433. {$else}
  4434. {$ifdef compress}
  4435. {$error add support for symansistr}
  4436. _mangledname:=stringdup(minilzw_encode(s));
  4437. {$else}
  4438. {$ifdef symansistr}
  4439. _mangledname:=s;
  4440. {$else symansistr}
  4441. _mangledname:=stringdup(s);
  4442. {$endif symansistr}
  4443. {$endif}
  4444. {$endif jvm}
  4445. include(procoptions,po_has_mangledname);
  4446. end;
  4447. {***************************************************************************
  4448. TPROCVARDEF
  4449. ***************************************************************************}
  4450. constructor tprocvardef.create(level:byte);
  4451. begin
  4452. inherited create(procvardef,level);
  4453. end;
  4454. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  4455. begin
  4456. inherited ppuload(procvardef,ppufile);
  4457. { load para symtable }
  4458. parast:=tparasymtable.create(self,ppufile.getbyte);
  4459. {$ifdef jvm}
  4460. ppufile.getderef(classdefderef);
  4461. {$endif}
  4462. tparasymtable(parast).ppuload(ppufile);
  4463. end;
  4464. function tprocvardef.getcopy : tstoreddef;
  4465. var
  4466. i : tcallercallee;
  4467. j : longint;
  4468. begin
  4469. result:=tprocvardef.create(parast.symtablelevel);
  4470. tprocvardef(result).returndef:=returndef;
  4471. tprocvardef(result).returndefderef:=returndefderef;
  4472. tprocvardef(result).parast:=parast.getcopy;
  4473. tprocvardef(result).savesize:=savesize;
  4474. { create paralist copy }
  4475. tprocvardef(result).paras:=tparalist.create(false);
  4476. tprocvardef(result).paras.count:=paras.count;
  4477. for j:=0 to paras.count-1 do
  4478. tprocvardef(result).paras[j]:=paras[j];
  4479. tprocvardef(result).proctypeoption:=proctypeoption;
  4480. tprocvardef(result).proccalloption:=proccalloption;
  4481. tprocvardef(result).procoptions:=procoptions;
  4482. tprocvardef(result).callerargareasize:=callerargareasize;
  4483. tprocvardef(result).calleeargareasize:=calleeargareasize;
  4484. tprocvardef(result).maxparacount:=maxparacount;
  4485. tprocvardef(result).minparacount:=minparacount;
  4486. for i:=low(tcallercallee) to high(tcallercallee) do
  4487. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  4488. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  4489. {$ifdef m68k}
  4490. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  4491. {$endif}
  4492. {$ifdef jvm}
  4493. tprocvardef(result).classdef:=classdef;
  4494. {$endif}
  4495. end;
  4496. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  4497. begin
  4498. inherited ppuwrite(ppufile);
  4499. { Save the para symtable level (necessary to distinguish nested
  4500. procvars) }
  4501. ppufile.putbyte(parast.symtablelevel);
  4502. {$ifdef jvm}
  4503. ppufile.putderef(classdefderef);
  4504. {$endif}
  4505. { Write this entry }
  4506. ppufile.writeentry(ibprocvardef);
  4507. { Save the para symtable, this is taken from the interface }
  4508. tparasymtable(parast).ppuwrite(ppufile);
  4509. end;
  4510. {$ifdef jvm}
  4511. procedure tprocvardef.buildderef;
  4512. begin
  4513. inherited buildderef;
  4514. classdefderef.build(classdef);
  4515. end;
  4516. procedure tprocvardef.deref;
  4517. begin
  4518. inherited deref;
  4519. classdef:=tobjectdef(classdefderef.resolve);
  4520. end;
  4521. {$endif}
  4522. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  4523. begin
  4524. case t of
  4525. gs_para :
  4526. GetSymtable:=parast;
  4527. else
  4528. GetSymtable:=nil;
  4529. end;
  4530. end;
  4531. function tprocvardef.size : asizeint;
  4532. begin
  4533. if ((po_methodpointer in procoptions) or
  4534. is_nested_pd(self)) and
  4535. not(po_addressonly in procoptions) then
  4536. size:=2*sizeof(pint)
  4537. else
  4538. size:=sizeof(pint);
  4539. end;
  4540. function tprocvardef.is_methodpointer:boolean;
  4541. begin
  4542. result:=(po_methodpointer in procoptions);
  4543. end;
  4544. function tprocvardef.is_addressonly:boolean;
  4545. begin
  4546. result:=(not(po_methodpointer in procoptions) and
  4547. not is_nested_pd(self)) or
  4548. (po_addressonly in procoptions);
  4549. end;
  4550. function tprocvardef.getmangledparaname:TSymStr;
  4551. begin
  4552. if not(po_methodpointer in procoptions) then
  4553. if not is_nested_pd(self) then
  4554. result:='procvar'
  4555. else
  4556. result:='nestedprovar'
  4557. else
  4558. result:='procvarofobj'
  4559. end;
  4560. function tprocvardef.is_publishable : boolean;
  4561. begin
  4562. is_publishable:=(po_methodpointer in procoptions);
  4563. end;
  4564. function tprocvardef.GetTypeName : string;
  4565. var
  4566. s: string;
  4567. pno : tprocnameoptions;
  4568. begin
  4569. {$ifdef EXTDEBUG}
  4570. pno:=[pno_showhidden];
  4571. {$else EXTDEBUG}
  4572. pno:=[];
  4573. {$endif EXTDEBUG}
  4574. s:='<';
  4575. if po_classmethod in procoptions then
  4576. s := s+'class method type of'
  4577. else
  4578. if po_addressonly in procoptions then
  4579. s := s+'address of'
  4580. else
  4581. s := s+'procedure variable type of';
  4582. if assigned(returndef) and
  4583. (returndef<>voidtype) then
  4584. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  4585. else
  4586. s:=s+' procedure'+typename_paras(pno);
  4587. if po_methodpointer in procoptions then
  4588. s := s+' of object';
  4589. if is_nested_pd(self) then
  4590. s := s+' is nested';
  4591. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  4592. end;
  4593. {***************************************************************************
  4594. TOBJECTDEF
  4595. ***************************************************************************}
  4596. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  4597. begin
  4598. inherited create(n,objectdef);
  4599. fcurrent_dispid:=0;
  4600. objecttype:=ot;
  4601. childof:=nil;
  4602. if objecttype=odt_helper then
  4603. owner.includeoption(sto_has_helper);
  4604. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  4605. { create space for vmt !! }
  4606. vmtentries:=TFPList.Create;
  4607. vmt_offset:=0;
  4608. set_parent(c);
  4609. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  4610. prepareguid;
  4611. { setup implemented interfaces }
  4612. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  4613. ImplementedInterfaces:=TFPObjectList.Create(true)
  4614. else
  4615. ImplementedInterfaces:=nil;
  4616. writing_class_record_dbginfo:=false;
  4617. end;
  4618. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  4619. var
  4620. i,
  4621. implintfcount : longint;
  4622. d, getterd : tderef;
  4623. ImplIntf : TImplementedInterface;
  4624. vmtentry : pvmtentry;
  4625. begin
  4626. inherited ppuload(objectdef,ppufile);
  4627. objecttype:=tobjecttyp(ppufile.getbyte);
  4628. objextname:=stringdup(ppufile.getstring);
  4629. { only used for external Objective-C classes/protocols }
  4630. if (objextname^='') then
  4631. stringdispose(objextname);
  4632. symtable:=tObjectSymtable.create(self,objrealname^,0);
  4633. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  4634. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  4635. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  4636. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  4637. vmt_offset:=ppufile.getlongint;
  4638. ppufile.getderef(childofderef);
  4639. { load guid }
  4640. iidstr:=nil;
  4641. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  4642. begin
  4643. new(iidguid);
  4644. ppufile.getguid(iidguid^);
  4645. iidstr:=stringdup(ppufile.getstring);
  4646. end;
  4647. abstractcnt:=ppufile.getlongint;
  4648. if objecttype=odt_helper then
  4649. ppufile.getderef(extendeddefderef);
  4650. vmtentries:=TFPList.Create;
  4651. vmtentries.count:=ppufile.getlongint;
  4652. for i:=0 to vmtentries.count-1 do
  4653. begin
  4654. ppufile.getderef(d);
  4655. new(vmtentry);
  4656. vmtentry^.procdef:=nil;
  4657. vmtentry^.procdefderef:=d;
  4658. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  4659. vmtentries[i]:=vmtentry;
  4660. end;
  4661. { load implemented interfaces }
  4662. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  4663. begin
  4664. ImplementedInterfaces:=TFPObjectList.Create(true);
  4665. implintfcount:=ppufile.getlongint;
  4666. for i:=0 to implintfcount-1 do
  4667. begin
  4668. ppufile.getderef(d);
  4669. ppufile.getderef(getterd);
  4670. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  4671. ImplIntf.IOffset:=ppufile.getlongint;
  4672. byte(ImplIntf.IType):=ppufile.getbyte;
  4673. ImplementedInterfaces.Add(ImplIntf);
  4674. end;
  4675. end
  4676. else
  4677. ImplementedInterfaces:=nil;
  4678. if df_copied_def in defoptions then
  4679. ppufile.getderef(cloneddefderef)
  4680. else
  4681. tObjectSymtable(symtable).ppuload(ppufile);
  4682. { handles the predefined class tobject }
  4683. { the last TOBJECT which is loaded gets }
  4684. { it ! }
  4685. if (childof=nil) and
  4686. (objecttype in [odt_class,odt_javaclass]) and
  4687. (objname^='TOBJECT') then
  4688. class_tobject:=self;
  4689. if (childof=nil) and
  4690. (objecttype=odt_interfacecom) then
  4691. if (objname^='IUNKNOWN') then
  4692. interface_iunknown:=self
  4693. else
  4694. if (objname^='IDISPATCH') then
  4695. interface_idispatch:=self;
  4696. if (childof=nil) and
  4697. (objecttype=odt_objcclass) and
  4698. (objname^='PROTOCOL') then
  4699. objc_protocoltype:=self;
  4700. if (objecttype=odt_javaclass) and
  4701. not(oo_is_formal in objectoptions) then
  4702. begin
  4703. if (objname^='JLOBJECT') then
  4704. java_jlobject:=self
  4705. else if (objname^='JLTHROWABLE') then
  4706. java_jlthrowable:=self
  4707. else if (objname^='FPCBASERECORDTYPE') then
  4708. java_fpcbaserecordtype:=self
  4709. else if (objname^='JLSTRING') then
  4710. java_jlstring:=self
  4711. else if (objname^='ANSISTRINGCLASS') then
  4712. java_ansistring:=self
  4713. else if (objname^='SHORTSTRINGCLASS') then
  4714. java_shortstring:=self
  4715. else if (objname^='JLENUM') then
  4716. java_jlenum:=self
  4717. else if (objname^='JUENUMSET') then
  4718. java_juenumset:=self
  4719. else if (objname^='FPCBITSET') then
  4720. java_jubitset:=self
  4721. else if (objname^='FPCBASEPROCVARTYPE') then
  4722. java_procvarbase:=self;
  4723. end;
  4724. writing_class_record_dbginfo:=false;
  4725. end;
  4726. destructor tobjectdef.destroy;
  4727. begin
  4728. if assigned(symtable) then
  4729. begin
  4730. symtable.free;
  4731. symtable:=nil;
  4732. end;
  4733. stringdispose(objextname);
  4734. stringdispose(iidstr);
  4735. if assigned(ImplementedInterfaces) then
  4736. begin
  4737. ImplementedInterfaces.free;
  4738. ImplementedInterfaces:=nil;
  4739. end;
  4740. if assigned(iidguid) then
  4741. begin
  4742. dispose(iidguid);
  4743. iidguid:=nil;
  4744. end;
  4745. if assigned(vmtentries) then
  4746. begin
  4747. resetvmtentries;
  4748. vmtentries.free;
  4749. vmtentries:=nil;
  4750. end;
  4751. if assigned(vmcallstaticinfo) then
  4752. begin
  4753. freemem(vmcallstaticinfo);
  4754. vmcallstaticinfo:=nil;
  4755. end;
  4756. inherited destroy;
  4757. end;
  4758. function tobjectdef.getcopy : tstoreddef;
  4759. var
  4760. i : longint;
  4761. begin
  4762. result:=tobjectdef.create(objecttype,objrealname^,childof);
  4763. { the constructor allocates a symtable which we release to avoid memory leaks }
  4764. tobjectdef(result).symtable.free;
  4765. tobjectdef(result).symtable:=symtable.getcopy;
  4766. if assigned(objextname) then
  4767. tobjectdef(result).objextname:=stringdup(objextname^);
  4768. if assigned(import_lib) then
  4769. tobjectdef(result).import_lib:=stringdup(import_lib^);
  4770. tobjectdef(result).objectoptions:=objectoptions;
  4771. include(tobjectdef(result).defoptions,df_copied_def);
  4772. tobjectdef(result).extendeddef:=extendeddef;
  4773. if assigned(tcinitcode) then
  4774. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  4775. tobjectdef(result).vmt_offset:=vmt_offset;
  4776. if assigned(iidguid) then
  4777. begin
  4778. new(tobjectdef(result).iidguid);
  4779. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  4780. end;
  4781. if assigned(iidstr) then
  4782. tobjectdef(result).iidstr:=stringdup(iidstr^);
  4783. tobjectdef(result).abstractcnt:=abstractcnt;
  4784. if assigned(ImplementedInterfaces) then
  4785. begin
  4786. for i:=0 to ImplementedInterfaces.count-1 do
  4787. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  4788. end;
  4789. if assigned(vmtentries) then
  4790. begin
  4791. tobjectdef(result).vmtentries:=TFPList.Create;
  4792. tobjectdef(result).copyvmtentries(self);
  4793. end;
  4794. end;
  4795. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  4796. var
  4797. i : longint;
  4798. vmtentry : pvmtentry;
  4799. ImplIntf : TImplementedInterface;
  4800. old_do_indirect_crc: boolean;
  4801. begin
  4802. { if class1 in unit A changes, and class2 in unit B inherits from it
  4803. (so unit B uses unit A), then unit B with class2 will be recompiled.
  4804. However, if there is also a class3 in unit C that only depends on
  4805. unit B, then unit C will not be recompiled because nothing changed
  4806. to the interface of unit B. Nevertheless, unit C can indirectly
  4807. depend on unit A via derefs, and these must be updated -> the
  4808. indirect crc keeps track of such changes. }
  4809. old_do_indirect_crc:=ppufile.do_indirect_crc;
  4810. ppufile.do_indirect_crc:=true;
  4811. inherited ppuwrite(ppufile);
  4812. ppufile.putbyte(byte(objecttype));
  4813. if assigned(objextname) then
  4814. ppufile.putstring(objextname^)
  4815. else
  4816. ppufile.putstring('');
  4817. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  4818. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  4819. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  4820. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  4821. ppufile.putlongint(vmt_offset);
  4822. ppufile.putderef(childofderef);
  4823. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  4824. begin
  4825. ppufile.putguid(iidguid^);
  4826. ppufile.putstring(iidstr^);
  4827. end;
  4828. ppufile.putlongint(abstractcnt);
  4829. if objecttype=odt_helper then
  4830. ppufile.putderef(extendeddefderef);
  4831. ppufile.putlongint(vmtentries.count);
  4832. for i:=0 to vmtentries.count-1 do
  4833. begin
  4834. vmtentry:=pvmtentry(vmtentries[i]);
  4835. ppufile.putderef(vmtentry^.procdefderef);
  4836. ppufile.putbyte(byte(vmtentry^.visibility));
  4837. end;
  4838. if assigned(ImplementedInterfaces) then
  4839. begin
  4840. ppufile.putlongint(ImplementedInterfaces.Count);
  4841. for i:=0 to ImplementedInterfaces.Count-1 do
  4842. begin
  4843. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  4844. ppufile.putderef(ImplIntf.intfdefderef);
  4845. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  4846. ppufile.putlongint(ImplIntf.Ioffset);
  4847. ppufile.putbyte(byte(ImplIntf.IType));
  4848. end;
  4849. end;
  4850. if df_copied_def in defoptions then
  4851. ppufile.putderef(cloneddefderef);
  4852. ppufile.writeentry(ibobjectdef);
  4853. if not(df_copied_def in defoptions) then
  4854. tObjectSymtable(symtable).ppuwrite(ppufile);
  4855. ppufile.do_indirect_crc:=old_do_indirect_crc;
  4856. end;
  4857. function tobjectdef.GetTypeName:string;
  4858. begin
  4859. { in this case we will go in endless recursion, because then }
  4860. { there is no tsym associated yet with the def. It can occur }
  4861. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  4862. { instead of the actual type name }
  4863. if not assigned(typesym) then
  4864. result:='<Currently Parsed Class>'
  4865. else
  4866. result:=typesymbolprettyname;
  4867. end;
  4868. procedure tobjectdef.buildderef;
  4869. var
  4870. i : longint;
  4871. vmtentry : pvmtentry;
  4872. begin
  4873. inherited buildderef;
  4874. childofderef.build(childof);
  4875. if df_copied_def in defoptions then
  4876. cloneddefderef.build(symtable.defowner)
  4877. else
  4878. tstoredsymtable(symtable).buildderef;
  4879. if objecttype=odt_helper then
  4880. extendeddefderef.build(extendeddef);
  4881. for i:=0 to vmtentries.count-1 do
  4882. begin
  4883. vmtentry:=pvmtentry(vmtentries[i]);
  4884. vmtentry^.procdefderef.build(vmtentry^.procdef);
  4885. end;
  4886. if assigned(ImplementedInterfaces) then
  4887. begin
  4888. for i:=0 to ImplementedInterfaces.count-1 do
  4889. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  4890. end;
  4891. end;
  4892. procedure tobjectdef.deref;
  4893. var
  4894. i : longint;
  4895. vmtentry : pvmtentry;
  4896. begin
  4897. inherited deref;
  4898. childof:=tobjectdef(childofderef.resolve);
  4899. if df_copied_def in defoptions then
  4900. begin
  4901. cloneddef:=tobjectdef(cloneddefderef.resolve);
  4902. symtable:=cloneddef.symtable.getcopy;
  4903. end
  4904. else
  4905. tstoredsymtable(symtable).deref;
  4906. fillgenericparas(symtable);
  4907. if objecttype=odt_helper then
  4908. extendeddef:=tdef(extendeddefderef.resolve);
  4909. for i:=0 to vmtentries.count-1 do
  4910. begin
  4911. vmtentry:=pvmtentry(vmtentries[i]);
  4912. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  4913. end;
  4914. if assigned(ImplementedInterfaces) then
  4915. begin
  4916. for i:=0 to ImplementedInterfaces.count-1 do
  4917. TImplementedInterface(ImplementedInterfaces[i]).deref;
  4918. end;
  4919. end;
  4920. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  4921. var
  4922. pd: tprocdef absolute def;
  4923. st: tsymtable;
  4924. psym: tsym;
  4925. nname: TIDString;
  4926. begin
  4927. if (tdef(def).typ<>procdef) then
  4928. exit;
  4929. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  4930. owner = symtable in which objcclassdef is defined
  4931. }
  4932. st:=pd.owner.defowner.owner;
  4933. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  4934. { check for an existing procsym with our special name }
  4935. psym:=tsym(st.find(nname));
  4936. if not assigned(psym) then
  4937. begin
  4938. psym:=tprocsym.create(nname);
  4939. { avoid warning about this symbol being unused }
  4940. psym.IncRefCount;
  4941. { don't check for duplicates:
  4942. a) we checked above
  4943. b) in case we are in the implementation section of a unit, this
  4944. will also check for this symbol in the interface section
  4945. (since you normally cannot have symbols with the same name
  4946. both interface and implementation), and it's possible to
  4947. have class helpers for the same class in the interface and
  4948. in the implementation, and they cannot be merged since only
  4949. the once in the interface must be saved to the ppu/visible
  4950. from other units }
  4951. st.insert(psym,false);
  4952. end
  4953. else if (psym.typ<>procsym) then
  4954. internalerror(2009111501);
  4955. { add ourselves to this special procsym }
  4956. tprocsym(psym).procdeflist.add(def);
  4957. end;
  4958. procedure tobjectdef.buildderefimpl;
  4959. begin
  4960. inherited buildderefimpl;
  4961. if not (df_copied_def in defoptions) then
  4962. tstoredsymtable(symtable).buildderefimpl;
  4963. end;
  4964. procedure tobjectdef.derefimpl;
  4965. begin
  4966. inherited derefimpl;
  4967. if not (df_copied_def in defoptions) then
  4968. tstoredsymtable(symtable).derefimpl;
  4969. { the procdefs are not owned by the class helper procsyms, so they
  4970. are not stored/restored either -> re-add them here }
  4971. if (objecttype=odt_objcclass) or
  4972. (oo_is_classhelper in objectoptions) then
  4973. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  4974. end;
  4975. procedure tobjectdef.resetvmtentries;
  4976. var
  4977. i : longint;
  4978. begin
  4979. for i:=0 to vmtentries.Count-1 do
  4980. Dispose(pvmtentry(vmtentries[i]));
  4981. vmtentries.clear;
  4982. end;
  4983. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  4984. var
  4985. i : longint;
  4986. vmtentry : pvmtentry;
  4987. begin
  4988. resetvmtentries;
  4989. vmtentries.count:=objdef.vmtentries.count;
  4990. for i:=0 to objdef.vmtentries.count-1 do
  4991. begin
  4992. new(vmtentry);
  4993. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  4994. vmtentries[i]:=vmtentry;
  4995. end;
  4996. end;
  4997. function tobjectdef.getparentdef:tdef;
  4998. begin
  4999. { TODO: Remove getparentdef hack}
  5000. { With 2 forward declared classes with the child class before the
  5001. parent class the child class is written earlier to the ppu. Leaving it
  5002. possible to have a reference to the parent class for property overriding,
  5003. but the parent class still has the childof not resolved yet (PFV) }
  5004. if childof=nil then
  5005. childof:=tobjectdef(childofderef.resolve);
  5006. result:=childof;
  5007. end;
  5008. procedure tobjectdef.prepareguid;
  5009. begin
  5010. { set up guid }
  5011. if not assigned(iidguid) then
  5012. begin
  5013. new(iidguid);
  5014. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  5015. end;
  5016. { setup iidstring }
  5017. if not assigned(iidstr) then
  5018. iidstr:=stringdup(''); { default is empty string }
  5019. end;
  5020. procedure tobjectdef.set_parent( c : tobjectdef);
  5021. begin
  5022. if assigned(childof) then
  5023. exit;
  5024. childof:=c;
  5025. if not assigned(c) then
  5026. exit;
  5027. { inherit options and status }
  5028. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  5029. { initially has the same number of abstract methods as the parent }
  5030. abstractcnt:=c.abstractcnt;
  5031. { add the data of the anchestor class/object }
  5032. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  5033. begin
  5034. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  5035. { inherit recordalignment }
  5036. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  5037. { if both the parent and this record use C-alignment, also inherit
  5038. the current field alignment }
  5039. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  5040. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  5041. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  5042. { the padding is not inherited for Objective-C classes (maybe not
  5043. for cppclass either?) }
  5044. if objecttype=odt_objcclass then
  5045. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  5046. if (oo_has_vmt in objectoptions) and
  5047. (oo_has_vmt in c.objectoptions) then
  5048. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  5049. { if parent has a vmt field then the offset is the same for the child PM }
  5050. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  5051. begin
  5052. vmt_offset:=c.vmt_offset;
  5053. include(objectoptions,oo_has_vmt);
  5054. end;
  5055. end;
  5056. end;
  5057. procedure tobjectdef.insertvmt;
  5058. var
  5059. vs: tfieldvarsym;
  5060. begin
  5061. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5062. exit;
  5063. if (oo_has_vmt in objectoptions) then
  5064. internalerror(12345)
  5065. else
  5066. begin
  5067. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  5068. tObjectSymtable(symtable).fieldalignment);
  5069. if (tf_requires_proper_alignment in target_info.flags) then
  5070. begin
  5071. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  5072. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  5073. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  5074. end;
  5075. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  5076. hidesym(vs);
  5077. tObjectSymtable(symtable).insert(vs);
  5078. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  5079. if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  5080. vmt_offset:=vs.fieldoffset
  5081. else
  5082. vmt_offset:=vs.fieldoffset div 8;
  5083. include(objectoptions,oo_has_vmt);
  5084. end;
  5085. end;
  5086. procedure tobjectdef.check_forwards;
  5087. begin
  5088. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  5089. inherited;
  5090. if (oo_is_forward in objectoptions) then
  5091. begin
  5092. { ok, in future, the forward can be resolved }
  5093. Message1(sym_e_class_forward_not_resolved,objrealname^);
  5094. exclude(objectoptions,oo_is_forward);
  5095. end;
  5096. end;
  5097. { true if prot implements d (or if they are equal) }
  5098. function is_related_interface_multiple(prot: tobjectdef; d : tdef) : boolean;
  5099. var
  5100. i : longint;
  5101. begin
  5102. { objcprotocols have multiple inheritance, all protocols from which
  5103. the current protocol inherits are stored in implementedinterfaces }
  5104. result:=prot=d;
  5105. if result then
  5106. exit;
  5107. for i:=0 to prot.ImplementedInterfaces.count-1 do
  5108. begin
  5109. result:=is_related_interface_multiple(TImplementedInterface(prot.ImplementedInterfaces[i]).intfdef,d);
  5110. if result then
  5111. exit;
  5112. end;
  5113. end;
  5114. { true, if self inherits from d (or if they are equal) }
  5115. function tobjectdef.is_related(d : tdef) : boolean;
  5116. var
  5117. realself,
  5118. hp : tobjectdef;
  5119. begin
  5120. if (d.typ=objectdef) then
  5121. d:=find_real_class_definition(tobjectdef(d),false);
  5122. realself:=find_real_class_definition(self,false);
  5123. if realself=d then
  5124. begin
  5125. is_related:=true;
  5126. exit;
  5127. end;
  5128. if (d.typ<>objectdef) then
  5129. begin
  5130. is_related:=false;
  5131. exit;
  5132. end;
  5133. { Objective-C protocols and Java interfaces can use multiple
  5134. inheritance }
  5135. if (realself.objecttype in [odt_objcprotocol,odt_interfacejava]) then
  5136. begin
  5137. is_related:=is_related_interface_multiple(realself,d);
  5138. exit
  5139. end;
  5140. { formally declared Objective-C and Java classes match Objective-C/Java
  5141. classes with the same name. In case of Java, the package must also
  5142. match (still required even though we looked up the real definitions
  5143. above, because these may be two different formal declarations that
  5144. cannot be resolved yet) }
  5145. if (realself.objecttype in [odt_objcclass,odt_javaclass]) and
  5146. (tobjectdef(d).objecttype=objecttype) and
  5147. ((oo_is_formal in objectoptions) or
  5148. (oo_is_formal in tobjectdef(d).objectoptions)) and
  5149. (objrealname^=tobjectdef(d).objrealname^) then
  5150. begin
  5151. { check package name for Java }
  5152. if objecttype=odt_objcclass then
  5153. is_related:=true
  5154. else
  5155. begin
  5156. is_related:=
  5157. assigned(import_lib)=assigned(tobjectdef(d).import_lib);
  5158. if is_related and
  5159. assigned(import_lib) then
  5160. is_related:=import_lib^=tobjectdef(d).import_lib^;
  5161. end;
  5162. exit;
  5163. end;
  5164. hp:=realself.childof;
  5165. while assigned(hp) do
  5166. begin
  5167. if hp=d then
  5168. begin
  5169. is_related:=true;
  5170. exit;
  5171. end;
  5172. hp:=hp.childof;
  5173. end;
  5174. is_related:=false;
  5175. end;
  5176. function tobjectdef.find_destructor: tprocdef;
  5177. var
  5178. objdef: tobjectdef;
  5179. begin
  5180. objdef:=self;
  5181. while assigned(objdef) do
  5182. begin
  5183. result:=objdef.find_procdef_bytype(potype_destructor);
  5184. if assigned(result) then
  5185. exit;
  5186. objdef:=objdef.childof;
  5187. end;
  5188. result:=nil;
  5189. end;
  5190. function tobjectdef.implements_any_interfaces: boolean;
  5191. begin
  5192. result := (ImplementedInterfaces.Count > 0) or
  5193. (assigned(childof) and childof.implements_any_interfaces);
  5194. end;
  5195. function tobjectdef.size : asizeint;
  5196. begin
  5197. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5198. result:=sizeof(pint)
  5199. else
  5200. result:=tObjectSymtable(symtable).datasize;
  5201. end;
  5202. function tobjectdef.alignment:shortint;
  5203. begin
  5204. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5205. alignment:=sizeof(pint)
  5206. else
  5207. alignment:=tObjectSymtable(symtable).recordalignment;
  5208. end;
  5209. function tobjectdef.vmtmethodoffset(index:longint):longint;
  5210. begin
  5211. { for offset of methods for classes, see rtl/inc/objpash.inc }
  5212. case objecttype of
  5213. odt_class:
  5214. { the +2*sizeof(pint) is size and -size }
  5215. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  5216. odt_helper,
  5217. odt_objcclass,
  5218. odt_objcprotocol:
  5219. vmtmethodoffset:=0;
  5220. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  5221. vmtmethodoffset:=index*sizeof(pint);
  5222. odt_javaclass,
  5223. odt_interfacejava:
  5224. { invalid }
  5225. vmtmethodoffset:=-1;
  5226. else
  5227. {$ifdef WITHDMT}
  5228. vmtmethodoffset:=(index+4)*sizeof(pint);
  5229. {$else WITHDMT}
  5230. vmtmethodoffset:=(index+3)*sizeof(pint);
  5231. {$endif WITHDMT}
  5232. end;
  5233. end;
  5234. function tobjectdef.vmt_mangledname : TSymStr;
  5235. begin
  5236. if not(oo_has_vmt in objectoptions) then
  5237. Message1(parser_n_object_has_no_vmt,objrealname^);
  5238. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  5239. end;
  5240. function tobjectdef.needs_inittable : boolean;
  5241. begin
  5242. case objecttype of
  5243. odt_helper,
  5244. odt_class :
  5245. needs_inittable:=false;
  5246. odt_dispinterface,
  5247. odt_interfacecom:
  5248. needs_inittable:=true;
  5249. odt_interfacecorba:
  5250. needs_inittable:=is_related(interface_iunknown);
  5251. odt_object:
  5252. needs_inittable:=
  5253. tObjectSymtable(symtable).needs_init_final or
  5254. (assigned(childof) and
  5255. childof.needs_inittable);
  5256. odt_cppclass,
  5257. odt_objcclass,
  5258. odt_objcprotocol,
  5259. odt_javaclass,
  5260. odt_interfacejava:
  5261. needs_inittable:=false;
  5262. else
  5263. internalerror(200108267);
  5264. end;
  5265. end;
  5266. function tobjectdef.needs_separate_initrtti : boolean;
  5267. begin
  5268. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  5269. end;
  5270. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  5271. begin
  5272. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  5273. result:=inherited rtti_mangledname(rt)
  5274. else
  5275. begin
  5276. { necessary in case of a dynamic array of nsobject, or
  5277. if an nsobject field appears in a record that needs
  5278. init/finalisation }
  5279. if rt=initrtti then
  5280. begin
  5281. result:=voidpointertype.rtti_mangledname(rt);
  5282. exit;
  5283. end;
  5284. if not(target_info.system in systems_objc_nfabi) then
  5285. begin
  5286. result:=target_asm.labelprefix;
  5287. case objecttype of
  5288. odt_objcclass:
  5289. begin
  5290. case rt of
  5291. objcclassrtti:
  5292. if not(oo_is_classhelper in objectoptions) then
  5293. result:=result+'_OBJC_CLASS_'
  5294. else
  5295. result:=result+'_OBJC_CATEGORY_';
  5296. objcmetartti:
  5297. if not(oo_is_classhelper in objectoptions) then
  5298. result:=result+'_OBJC_METACLASS_'
  5299. else
  5300. internalerror(2009111511);
  5301. else
  5302. internalerror(2009092302);
  5303. end;
  5304. end;
  5305. odt_objcprotocol:
  5306. result:=result+'_OBJC_PROTOCOL_';
  5307. end;
  5308. end
  5309. else
  5310. begin
  5311. case objecttype of
  5312. odt_objcclass:
  5313. begin
  5314. if (oo_is_classhelper in objectoptions) and
  5315. (rt<>objcclassrtti) then
  5316. internalerror(2009111512);
  5317. case rt of
  5318. objcclassrtti:
  5319. if not(oo_is_classhelper in objectoptions) then
  5320. result:='_OBJC_CLASS_$_'
  5321. else
  5322. result:='_OBJC_$_CATEGORY_';
  5323. objcmetartti:
  5324. result:='_OBJC_METACLASS_$_';
  5325. objcclassrortti:
  5326. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  5327. objcmetarortti:
  5328. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  5329. else
  5330. internalerror(2009092303);
  5331. end;
  5332. end;
  5333. odt_objcprotocol:
  5334. begin
  5335. result:=lower(target_asm.labelprefix);
  5336. case rt of
  5337. objcclassrtti:
  5338. result:=result+'_OBJC_PROTOCOL_$_';
  5339. objcmetartti:
  5340. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  5341. else
  5342. internalerror(2009092501);
  5343. end;
  5344. end;
  5345. end;
  5346. end;
  5347. result:=result+objextname^;
  5348. end;
  5349. end;
  5350. function tobjectdef.members_need_inittable : boolean;
  5351. begin
  5352. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  5353. end;
  5354. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  5355. var
  5356. ImplIntf : TImplementedInterface;
  5357. i : longint;
  5358. begin
  5359. result:=nil;
  5360. if not assigned(ImplementedInterfaces) then
  5361. exit;
  5362. for i:=0 to ImplementedInterfaces.Count-1 do
  5363. begin
  5364. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  5365. if ImplIntf.intfdef=aintfdef then
  5366. begin
  5367. result:=ImplIntf;
  5368. exit;
  5369. end;
  5370. end;
  5371. end;
  5372. function tobjectdef.is_publishable : boolean;
  5373. begin
  5374. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  5375. end;
  5376. function tobjectdef.get_next_dispid: longint;
  5377. begin
  5378. inc(fcurrent_dispid);
  5379. result:=fcurrent_dispid;
  5380. end;
  5381. function tobjectdef.search_enumerator_get: tprocdef;
  5382. begin
  5383. result:=inherited;
  5384. if not assigned(result) and assigned(childof) then
  5385. result:=childof.search_enumerator_get;
  5386. end;
  5387. function tobjectdef.search_enumerator_move: tprocdef;
  5388. begin
  5389. result:=inherited;
  5390. if not assigned(result) and assigned(childof) then
  5391. result:=childof.search_enumerator_move;
  5392. end;
  5393. function tobjectdef.search_enumerator_current: tsym;
  5394. begin
  5395. result:=inherited;
  5396. if not assigned(result) and assigned(childof) then
  5397. result:=childof.search_enumerator_current;
  5398. end;
  5399. procedure tobjectdef.register_created_classref_type;
  5400. begin
  5401. if not classref_created_in_current_module then
  5402. begin
  5403. classref_created_in_current_module:=true;
  5404. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  5405. end;
  5406. end;
  5407. procedure tobjectdef.register_created_object_type;
  5408. begin
  5409. if not created_in_current_module then
  5410. begin
  5411. created_in_current_module:=true;
  5412. current_module.wpoinfo.addcreatedobjtype(self);
  5413. end;
  5414. end;
  5415. procedure tobjectdef.register_maybe_created_object_type;
  5416. begin
  5417. { if we know it has been created for sure, no need
  5418. to also record that it maybe can be created in
  5419. this module
  5420. }
  5421. if not (created_in_current_module) and
  5422. not (maybe_created_in_current_module) then
  5423. begin
  5424. maybe_created_in_current_module:=true;
  5425. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  5426. end;
  5427. end;
  5428. procedure tobjectdef.register_vmt_call(index: longint);
  5429. begin
  5430. if (is_object(self) or is_class(self)) then
  5431. current_module.wpoinfo.addcalledvmtentry(self,index);
  5432. end;
  5433. procedure check_and_finish_msg(data: tobject; arg: pointer);
  5434. var
  5435. def: tdef absolute data;
  5436. pd: tprocdef absolute data;
  5437. i,
  5438. paracount: longint;
  5439. begin
  5440. if (def.typ=procdef) then
  5441. begin
  5442. { add all messages also under a dummy name to the symtable in
  5443. which the objcclass/protocol/category is declared, so they can
  5444. be called via id.<name>
  5445. }
  5446. create_class_helper_for_procdef(pd,nil);
  5447. { we have to wait until now to set the mangled name because it
  5448. depends on the (possibly external) class name, which is defined
  5449. at the very end. }
  5450. if not(po_msgstr in pd.procoptions) then
  5451. begin
  5452. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  5453. { recover to avoid internalerror later on }
  5454. include(pd.procoptions,po_msgstr);
  5455. pd.messageinf.str:=stringdup('MissingDeclaration');
  5456. end;
  5457. { Mangled name is already set in case this is a copy of
  5458. another type. }
  5459. if not(po_has_mangledname in pd.procoptions) then
  5460. begin
  5461. { check whether the number of formal parameters is correct,
  5462. and whether they have valid Objective-C types }
  5463. paracount:=0;
  5464. for i:=1 to length(pd.messageinf.str^) do
  5465. if pd.messageinf.str^[i]=':' then
  5466. inc(paracount);
  5467. for i:=0 to pd.paras.count-1 do
  5468. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  5469. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  5470. dec(paracount);
  5471. if (paracount<>0) then
  5472. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  5473. pd.setmangledname(pd.objcmangledname);
  5474. end
  5475. else
  5476. { all checks already done }
  5477. exit;
  5478. if not(oo_is_external in pd.struct.objectoptions) then
  5479. begin
  5480. if (po_varargs in pd.procoptions) then
  5481. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  5482. else
  5483. begin
  5484. { check for "array of const" parameters }
  5485. for i:=0 to pd.parast.symlist.count-1 do
  5486. begin
  5487. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  5488. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  5489. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  5490. end;
  5491. end;
  5492. end;
  5493. end;
  5494. end;
  5495. procedure mark_private_fields_used(data: tobject; arg: pointer);
  5496. var
  5497. sym: tsym absolute data;
  5498. begin
  5499. if (sym.typ=fieldvarsym) and
  5500. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  5501. sym.IncRefCount;
  5502. end;
  5503. procedure tobjectdef.finish_objc_data;
  5504. begin
  5505. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  5506. if (oo_is_external in objectoptions) then
  5507. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  5508. end;
  5509. procedure verify_objc_vardef(data: tobject; arg: pointer);
  5510. var
  5511. sym: tabstractvarsym absolute data;
  5512. res: pboolean absolute arg;
  5513. founderrordef: tdef;
  5514. begin
  5515. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  5516. exit;
  5517. if (sym.typ=paravarsym) and
  5518. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  5519. is_array_of_const(tparavarsym(sym).vardef)) then
  5520. exit;
  5521. if not objcchecktype(sym.vardef,founderrordef) then
  5522. begin
  5523. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  5524. res^:=false;
  5525. end;
  5526. end;
  5527. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  5528. var
  5529. def: tdef absolute data;
  5530. res: pboolean absolute arg;
  5531. founderrordef: tdef;
  5532. begin
  5533. if (def.typ<>procdef) then
  5534. exit;
  5535. { check parameter types for validity }
  5536. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  5537. { check the result type for validity }
  5538. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  5539. begin
  5540. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  5541. res^:=false;
  5542. end;
  5543. end;
  5544. function tobjectdef.check_objc_types: boolean;
  5545. begin
  5546. { done in separate step from finish_objc_data, because when
  5547. finish_objc_data is called, not all forwarddefs have been resolved
  5548. yet and we need to know all types here }
  5549. result:=true;
  5550. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  5551. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  5552. end;
  5553. procedure do_cpp_import_info(data: tobject; arg: pointer);
  5554. var
  5555. def: tdef absolute data;
  5556. pd: tprocdef absolute data;
  5557. begin
  5558. if (def.typ=procdef) then
  5559. begin
  5560. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  5561. if (oo_is_external in pd.struct.objectoptions) then
  5562. begin
  5563. { copied from psub.read_proc }
  5564. if assigned(tobjectdef(pd.struct).import_lib) then
  5565. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  5566. else
  5567. begin
  5568. { add import name to external list for DLL scanning }
  5569. if tf_has_dllscanner in target_info.flags then
  5570. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  5571. end;
  5572. end;
  5573. end;
  5574. end;
  5575. procedure tobjectdef.finish_cpp_data;
  5576. begin
  5577. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  5578. end;
  5579. {****************************************************************************
  5580. TImplementedInterface
  5581. ****************************************************************************}
  5582. function TImplementedInterface.GetIOffset: longint;
  5583. begin
  5584. if (fIOffset=-1) and
  5585. (IType in [etFieldValue,etFieldValueClass]) then
  5586. result:=tfieldvarsym(ImplementsField).fieldoffset
  5587. else
  5588. result:=fIOffset;
  5589. end;
  5590. constructor TImplementedInterface.create(aintf: tobjectdef);
  5591. begin
  5592. inherited create;
  5593. intfdef:=aintf;
  5594. IOffset:=-1;
  5595. IType:=etStandard;
  5596. NameMappings:=nil;
  5597. procdefs:=nil;
  5598. end;
  5599. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  5600. begin
  5601. inherited create;
  5602. intfdef:=nil;
  5603. intfdefderef:=intfd;
  5604. ImplementsGetterDeref:=getterd;
  5605. IOffset:=-1;
  5606. IType:=etStandard;
  5607. NameMappings:=nil;
  5608. procdefs:=nil;
  5609. end;
  5610. destructor TImplementedInterface.destroy;
  5611. var
  5612. i : longint;
  5613. mappedname : pshortstring;
  5614. begin
  5615. if assigned(NameMappings) then
  5616. begin
  5617. for i:=0 to NameMappings.Count-1 do
  5618. begin
  5619. mappedname:=pshortstring(NameMappings[i]);
  5620. stringdispose(mappedname);
  5621. end;
  5622. NameMappings.free;
  5623. NameMappings:=nil;
  5624. end;
  5625. if assigned(procdefs) then
  5626. begin
  5627. procdefs.free;
  5628. procdefs:=nil;
  5629. end;
  5630. inherited destroy;
  5631. end;
  5632. procedure TImplementedInterface.buildderef;
  5633. begin
  5634. intfdefderef.build(intfdef);
  5635. ImplementsGetterDeref.build(ImplementsGetter);
  5636. end;
  5637. procedure TImplementedInterface.deref;
  5638. begin
  5639. intfdef:=tobjectdef(intfdefderef.resolve);
  5640. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  5641. end;
  5642. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  5643. begin
  5644. if not assigned(NameMappings) then
  5645. NameMappings:=TFPHashList.Create;
  5646. NameMappings.Add(origname,stringdup(newname));
  5647. end;
  5648. function TImplementedInterface.GetMapping(const origname: string):string;
  5649. var
  5650. mappedname : pshortstring;
  5651. begin
  5652. result:='';
  5653. if not assigned(NameMappings) then
  5654. exit;
  5655. mappedname:=PShortstring(NameMappings.Find(origname));
  5656. if assigned(mappedname) then
  5657. result:=mappedname^;
  5658. end;
  5659. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  5660. begin
  5661. if not assigned(procdefs) then
  5662. procdefs:=TFPObjectList.Create(false);
  5663. { duplicate entries must be stored, because multiple }
  5664. { interfaces can declare methods with the same name }
  5665. { and all of these get their own VMT entry }
  5666. procdefs.Add(pd);
  5667. end;
  5668. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  5669. var
  5670. i : longint;
  5671. begin
  5672. result:=false;
  5673. { interfaces being implemented through delegation are not mergable (FK) }
  5674. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  5675. exit;
  5676. weight:=0;
  5677. { empty interface is mergeable }
  5678. if ProcDefs.Count=0 then
  5679. begin
  5680. result:=true;
  5681. exit;
  5682. end;
  5683. { The interface to merge must at least the number of
  5684. procedures of this interface }
  5685. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  5686. exit;
  5687. for i:=0 to ProcDefs.Count-1 do
  5688. begin
  5689. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  5690. exit;
  5691. end;
  5692. weight:=ProcDefs.Count;
  5693. result:=true;
  5694. end;
  5695. function TImplementedInterface.getcopy:TImplementedInterface;
  5696. begin
  5697. Result:=TImplementedInterface.Create(nil);
  5698. { 1) the procdefs list will be freed once for each copy
  5699. 2) since the procdefs list owns its elements, those will also be freed for each copy
  5700. 3) idem for the name mappings
  5701. }
  5702. { warning: this is completely wrong on so many levels...
  5703. Move(pointer(self)^,pointer(result)^,InstanceSize);
  5704. We need to make clean copies of the different fields
  5705. this is not implemented yet, and thus we generate an internal
  5706. error instead PM 2011-06-14 }
  5707. internalerror(2011061401);
  5708. end;
  5709. {****************************************************************************
  5710. TFORWARDDEF
  5711. ****************************************************************************}
  5712. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  5713. begin
  5714. inherited create(forwarddef);
  5715. tosymname:=stringdup(s);
  5716. forwardpos:=pos;
  5717. end;
  5718. function tforwarddef.GetTypeName:string;
  5719. begin
  5720. GetTypeName:='unresolved forward to '+tosymname^;
  5721. end;
  5722. destructor tforwarddef.destroy;
  5723. begin
  5724. stringdispose(tosymname);
  5725. inherited destroy;
  5726. end;
  5727. function tforwarddef.getcopy:tstoreddef;
  5728. begin
  5729. result:=tforwarddef.create(tosymname^, forwardpos);
  5730. end;
  5731. {****************************************************************************
  5732. TUNDEFINEDDEF
  5733. ****************************************************************************}
  5734. constructor tundefineddef.create;
  5735. begin
  5736. inherited create(undefineddef);
  5737. end;
  5738. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  5739. begin
  5740. inherited ppuload(undefineddef,ppufile);
  5741. end;
  5742. function tundefineddef.GetTypeName:string;
  5743. begin
  5744. GetTypeName:='<undefined type>';
  5745. end;
  5746. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  5747. begin
  5748. inherited ppuwrite(ppufile);
  5749. ppufile.writeentry(ibundefineddef);
  5750. end;
  5751. {****************************************************************************
  5752. TERRORDEF
  5753. ****************************************************************************}
  5754. constructor terrordef.create;
  5755. begin
  5756. inherited create(errordef);
  5757. { prevent consecutive faults }
  5758. savesize:=1;
  5759. end;
  5760. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  5761. begin
  5762. { Can't write errordefs to ppu }
  5763. internalerror(200411063);
  5764. end;
  5765. function terrordef.GetTypeName:string;
  5766. begin
  5767. GetTypeName:='<erroneous type>';
  5768. end;
  5769. function terrordef.getmangledparaname:TSymStr;
  5770. begin
  5771. getmangledparaname:='error';
  5772. end;
  5773. {****************************************************************************
  5774. Definition Helpers
  5775. ****************************************************************************}
  5776. function is_interfacecom(def: tdef): boolean;
  5777. begin
  5778. is_interfacecom:=
  5779. assigned(def) and
  5780. (def.typ=objectdef) and
  5781. (tobjectdef(def).objecttype=odt_interfacecom);
  5782. end;
  5783. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  5784. begin
  5785. is_interfacecom_or_dispinterface:=
  5786. assigned(def) and
  5787. (def.typ=objectdef) and
  5788. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  5789. end;
  5790. function is_any_interface_kind(def: tdef): boolean;
  5791. begin
  5792. result:=
  5793. assigned(def) and
  5794. (def.typ=objectdef) and
  5795. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  5796. is_objccategory(def));
  5797. end;
  5798. function is_interfacecorba(def: tdef): boolean;
  5799. begin
  5800. is_interfacecorba:=
  5801. assigned(def) and
  5802. (def.typ=objectdef) and
  5803. (tobjectdef(def).objecttype=odt_interfacecorba);
  5804. end;
  5805. function is_interface(def: tdef): boolean;
  5806. begin
  5807. is_interface:=
  5808. assigned(def) and
  5809. (def.typ=objectdef) and
  5810. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  5811. end;
  5812. function is_dispinterface(def: tdef): boolean;
  5813. begin
  5814. result:=
  5815. assigned(def) and
  5816. (def.typ=objectdef) and
  5817. (tobjectdef(def).objecttype=odt_dispinterface);
  5818. end;
  5819. function is_class(def: tdef): boolean;
  5820. begin
  5821. is_class:=
  5822. assigned(def) and
  5823. (def.typ=objectdef) and
  5824. (tobjectdef(def).objecttype=odt_class);
  5825. end;
  5826. function is_object(def: tdef): boolean;
  5827. begin
  5828. is_object:=
  5829. assigned(def) and
  5830. (def.typ=objectdef) and
  5831. (tobjectdef(def).objecttype=odt_object);
  5832. end;
  5833. function is_cppclass(def: tdef): boolean;
  5834. begin
  5835. is_cppclass:=
  5836. assigned(def) and
  5837. (def.typ=objectdef) and
  5838. (tobjectdef(def).objecttype=odt_cppclass);
  5839. end;
  5840. function is_objcclass(def: tdef): boolean;
  5841. begin
  5842. is_objcclass:=
  5843. assigned(def) and
  5844. (def.typ=objectdef) and
  5845. (tobjectdef(def).objecttype=odt_objcclass);
  5846. end;
  5847. function is_objectpascal_helper(def: tdef): boolean;
  5848. begin
  5849. result:=
  5850. assigned(def) and
  5851. (def.typ=objectdef) and
  5852. (tobjectdef(def).objecttype=odt_helper);
  5853. end;
  5854. function is_objcclassref(def: tdef): boolean;
  5855. begin
  5856. is_objcclassref:=
  5857. assigned(def) and
  5858. (def.typ=classrefdef) and
  5859. is_objcclass(tclassrefdef(def).pointeddef);
  5860. end;
  5861. function is_objcprotocol(def: tdef): boolean;
  5862. begin
  5863. result:=
  5864. assigned(def) and
  5865. (def.typ=objectdef) and
  5866. (tobjectdef(def).objecttype=odt_objcprotocol);
  5867. end;
  5868. function is_objccategory(def: tdef): boolean;
  5869. begin
  5870. result:=
  5871. assigned(def) and
  5872. (def.typ=objectdef) and
  5873. { if used as a forward type }
  5874. ((tobjectdef(def).objecttype=odt_objccategory) or
  5875. { if used as after it has been resolved }
  5876. ((tobjectdef(def).objecttype=odt_objcclass) and
  5877. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  5878. end;
  5879. function is_objc_class_or_protocol(def: tdef): boolean;
  5880. begin
  5881. result:=
  5882. assigned(def) and
  5883. (def.typ=objectdef) and
  5884. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  5885. end;
  5886. function is_objc_protocol_or_category(def: tdef): boolean;
  5887. begin
  5888. result:=
  5889. assigned(def) and
  5890. (def.typ=objectdef) and
  5891. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  5892. ((tobjectdef(def).objecttype = odt_objcclass) and
  5893. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  5894. end;
  5895. function is_classhelper(def: tdef): boolean;
  5896. begin
  5897. result:=
  5898. is_objectpascal_helper(def) or
  5899. is_objccategory(def);
  5900. end;
  5901. function is_class_or_interface(def: tdef): boolean;
  5902. begin
  5903. result:=
  5904. assigned(def) and
  5905. (def.typ=objectdef) and
  5906. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  5907. end;
  5908. function is_class_or_interface_or_objc(def: tdef): boolean;
  5909. begin
  5910. result:=
  5911. assigned(def) and
  5912. (def.typ=objectdef) and
  5913. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  5914. end;
  5915. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  5916. begin
  5917. result:=
  5918. assigned(def) and
  5919. (def.typ=objectdef) and
  5920. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  5921. end;
  5922. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  5923. begin
  5924. result:=
  5925. assigned(def) and
  5926. (def.typ=objectdef) and
  5927. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  5928. end;
  5929. function is_class_or_interface_or_object(def: tdef): boolean;
  5930. begin
  5931. result:=
  5932. assigned(def) and
  5933. (def.typ=objectdef) and
  5934. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  5935. end;
  5936. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  5937. begin
  5938. result:=
  5939. assigned(def) and
  5940. (def.typ=objectdef) and
  5941. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  5942. end;
  5943. function is_implicit_pointer_object_type(def: tdef): boolean;
  5944. begin
  5945. result:=
  5946. assigned(def) and
  5947. (((def.typ=objectdef) and
  5948. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  5949. ((target_info.system in systems_jvm) and
  5950. (def.typ=recorddef)));
  5951. end;
  5952. function is_class_or_object(def: tdef): boolean;
  5953. begin
  5954. result:=
  5955. assigned(def) and
  5956. (def.typ=objectdef) and
  5957. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  5958. end;
  5959. function is_record(def: tdef): boolean;
  5960. begin
  5961. result:=
  5962. assigned(def) and
  5963. (def.typ=recorddef);
  5964. end;
  5965. function is_javaclass(def: tdef): boolean;
  5966. begin
  5967. result:=
  5968. assigned(def) and
  5969. (def.typ=objectdef) and
  5970. (tobjectdef(def).objecttype=odt_javaclass);
  5971. end;
  5972. function is_javaclassref(def: tdef): boolean;
  5973. begin
  5974. is_javaclassref:=
  5975. assigned(def) and
  5976. (def.typ=classrefdef) and
  5977. is_javaclass(tclassrefdef(def).pointeddef);
  5978. end;
  5979. function is_javainterface(def: tdef): boolean;
  5980. begin
  5981. result:=
  5982. assigned(def) and
  5983. (def.typ=objectdef) and
  5984. (tobjectdef(def).objecttype=odt_interfacejava);
  5985. end;
  5986. function is_java_class_or_interface(def: tdef): boolean;
  5987. begin
  5988. result:=
  5989. assigned(def) and
  5990. (def.typ=objectdef) and
  5991. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  5992. end;
  5993. procedure loadobjctypes;
  5994. begin
  5995. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  5996. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  5997. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  5998. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  5999. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  6000. end;
  6001. procedure maybeloadcocoatypes;
  6002. var
  6003. tsym: ttypesym;
  6004. cocoaunit: string[15];
  6005. begin
  6006. if assigned(objc_fastenumeration) then
  6007. exit;
  6008. if not(target_info.system in [system_arm_darwin,system_i386_iphonesim]) then
  6009. cocoaunit:='COCOAALL'
  6010. else
  6011. cocoaunit:='IPHONEALL';
  6012. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  6013. if assigned(tsym) then
  6014. objc_fastenumeration:=tobjectdef(tsym.typedef)
  6015. else
  6016. objc_fastenumeration:=nil;
  6017. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  6018. if assigned(tsym) then
  6019. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  6020. else
  6021. objc_fastenumerationstate:=nil;
  6022. end;
  6023. function use_vectorfpu(def : tdef) : boolean;
  6024. begin
  6025. {$ifdef x86}
  6026. {$define use_vectorfpuimplemented}
  6027. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  6028. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  6029. {$endif x86}
  6030. {$ifdef arm}
  6031. {$define use_vectorfpuimplemented}
  6032. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  6033. {$endif arm}
  6034. {$ifndef use_vectorfpuimplemented}
  6035. use_vectorfpu:=false;
  6036. {$endif}
  6037. end;
  6038. function getpointerdef(def: tdef): tpointerdef;
  6039. var
  6040. res: PHashSetItem;
  6041. oldsymtablestack: tsymtablestack;
  6042. begin
  6043. if not assigned(current_module) then
  6044. internalerror(2011071101);
  6045. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  6046. if not assigned(res^.Data) then
  6047. begin
  6048. { since these pointerdefs can be reused anywhere in the current
  6049. unit, add them to the global/staticsymtable }
  6050. oldsymtablestack:=symtablestack;
  6051. { do not simply push/pop current_module.localsymtable, because
  6052. that can have side-effects (e.g., it removes helpers) }
  6053. symtablestack:=nil;
  6054. res^.Data:=tpointerdef.create(def);
  6055. if assigned(current_module.localsymtable) then
  6056. current_module.localsymtable.insertdef(tdef(res^.Data))
  6057. else
  6058. current_module.globalsymtable.insertdef(tdef(res^.Data));
  6059. symtablestack:=oldsymtablestack;
  6060. end;
  6061. result:=tpointerdef(res^.Data);
  6062. end;
  6063. function getsingletonarraydef(def: tdef): tarraydef;
  6064. begin
  6065. result:=getarraydef(def,1);
  6066. end;
  6067. function getarraydef(def: tdef; elecount: asizeint): tarraydef;
  6068. var
  6069. res: PHashSetItem;
  6070. oldsymtablestack: tsymtablestack;
  6071. arrdesc: packed record
  6072. def: tdef;
  6073. elecount: asizeint;
  6074. end;
  6075. begin
  6076. if not assigned(current_module) then
  6077. internalerror(2011081301);
  6078. arrdesc.def:=def;
  6079. arrdesc.elecount:=elecount;
  6080. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  6081. if not assigned(res^.Data) then
  6082. begin
  6083. { since these arraydef can be reused anywhere in the current
  6084. unit, add them to the global/staticsymtable }
  6085. oldsymtablestack:=symtablestack;
  6086. symtablestack:=nil;
  6087. res^.Data:=tarraydef.create(0,elecount-1,ptrsinttype);
  6088. tarraydef(res^.Data).elementdef:=def;
  6089. if assigned(current_module.localsymtable) then
  6090. current_module.localsymtable.insertdef(tdef(res^.Data))
  6091. else
  6092. current_module.globalsymtable.insertdef(tdef(res^.Data));
  6093. symtablestack:=oldsymtablestack;
  6094. end;
  6095. result:=tarraydef(res^.Data);
  6096. end;
  6097. end.