symdef.pas 270 KB

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