symdef.pas 214 KB

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