2
0

symdef.pas 222 KB

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