symdef.pas 225 KB

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