symdef.pas 247 KB

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