symdef.pas 244 KB

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