symdef.pas 243 KB

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