symdef.pas 225 KB

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