symdef.pas 224 KB

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