symdef.pas 267 KB

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