symdef.pas 282 KB

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