symdef.pas 242 KB

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