symdef.pas 227 KB

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