symdef.pas 213 KB

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