symdef.pas 247 KB

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