symdef.pas 246 KB

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