symdef.pas 280 KB

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