symdef.pas 248 KB

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