symdef.pas 284 KB

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