symdef.pas 267 KB

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