symdef.pas 270 KB

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