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