symdef.pas 180 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777
  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,aasmdata,
  33. cpubase,cpuinfo,
  34. cgbase,cgutils,
  35. parabase
  36. ;
  37. type
  38. {************************************************
  39. TDef
  40. ************************************************}
  41. { tstoreddef }
  42. tstoreddef = class(tdef)
  43. protected
  44. typesymderef : tderef;
  45. public
  46. {$ifdef EXTDEBUG}
  47. fileinfo : tfileposinfo;
  48. {$endif}
  49. { generic support }
  50. genericdef : tstoreddef;
  51. genericdefderef : tderef;
  52. generictokenbuf : tdynamicarray;
  53. constructor create(dt:tdeftyp);
  54. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  55. destructor destroy;override;
  56. function getcopy : tstoreddef;virtual;
  57. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  58. procedure buildderef;override;
  59. procedure buildderefimpl;override;
  60. procedure deref;override;
  61. procedure derefimpl;override;
  62. function size:asizeint;override;
  63. function getvardef:longint;override;
  64. function alignment:shortint;override;
  65. function is_publishable : boolean;override;
  66. function needs_inittable : boolean;override;
  67. function rtti_mangledname(rt:trttitype):string;override;
  68. function OwnerHierarchyName: string; override;
  69. function in_currentunit: boolean;
  70. { regvars }
  71. function is_intregable : boolean;
  72. function is_fpuregable : boolean;
  73. { generics }
  74. procedure initgeneric;
  75. private
  76. savesize : asizeuint;
  77. end;
  78. tfiletyp = (ft_text,ft_typed,ft_untyped);
  79. tfiledef = class(tstoreddef)
  80. filetyp : tfiletyp;
  81. typedfiledef : tdef;
  82. typedfiledefderef : tderef;
  83. constructor createtext;
  84. constructor createuntyped;
  85. constructor createtyped(def : tdef);
  86. constructor ppuload(ppufile:tcompilerppufile);
  87. function getcopy : tstoreddef;override;
  88. procedure ppuwrite(ppufile:tcompilerppufile);override;
  89. procedure buildderef;override;
  90. procedure deref;override;
  91. function GetTypeName:string;override;
  92. function getmangledparaname:string;override;
  93. procedure setsize;
  94. end;
  95. tvariantdef = class(tstoreddef)
  96. varianttype : tvarianttype;
  97. constructor create(v : tvarianttype);
  98. constructor ppuload(ppufile:tcompilerppufile);
  99. function getcopy : tstoreddef;override;
  100. function GetTypeName:string;override;
  101. procedure ppuwrite(ppufile:tcompilerppufile);override;
  102. function getvardef:longint;override;
  103. procedure setsize;
  104. function is_publishable : boolean;override;
  105. function needs_inittable : boolean;override;
  106. end;
  107. tformaldef = class(tstoreddef)
  108. typed:boolean;
  109. constructor create(Atyped:boolean);
  110. constructor ppuload(ppufile:tcompilerppufile);
  111. procedure ppuwrite(ppufile:tcompilerppufile);override;
  112. function GetTypeName:string;override;
  113. end;
  114. tforwarddef = class(tstoreddef)
  115. tosymname : pshortstring;
  116. forwardpos : tfileposinfo;
  117. constructor create(const s:string;const pos:tfileposinfo);
  118. destructor destroy;override;
  119. function getcopy:tstoreddef;override;
  120. function GetTypeName:string;override;
  121. end;
  122. tundefineddef = class(tstoreddef)
  123. constructor create;
  124. constructor ppuload(ppufile:tcompilerppufile);
  125. procedure ppuwrite(ppufile:tcompilerppufile);override;
  126. function GetTypeName:string;override;
  127. end;
  128. terrordef = class(tstoreddef)
  129. constructor create;
  130. procedure ppuwrite(ppufile:tcompilerppufile);override;
  131. function GetTypeName:string;override;
  132. function getmangledparaname : string;override;
  133. end;
  134. tabstractpointerdef = class(tstoreddef)
  135. pointeddef : tdef;
  136. pointeddefderef : tderef;
  137. constructor create(dt:tdeftyp;def:tdef);
  138. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  139. procedure ppuwrite(ppufile:tcompilerppufile);override;
  140. procedure buildderef;override;
  141. procedure deref;override;
  142. end;
  143. tpointerdef = class(tabstractpointerdef)
  144. is_far : boolean;
  145. has_pointer_math : boolean;
  146. constructor create(def:tdef);
  147. constructor createfar(def:tdef);
  148. function getcopy:tstoreddef;override;
  149. constructor ppuload(ppufile:tcompilerppufile);
  150. procedure ppuwrite(ppufile:tcompilerppufile);override;
  151. function GetTypeName:string;override;
  152. end;
  153. tprocdef = class;
  154. { tabstractrecorddef }
  155. tabstractrecorddef= class(tstoreddef)
  156. objname,
  157. objrealname : PShortString;
  158. symtable : TSymtable;
  159. cloneddef : tabstractrecorddef;
  160. cloneddefderef : tderef;
  161. objectoptions : tobjectoptions;
  162. constructor create(const n:string; dt:tdeftyp);
  163. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  164. procedure ppuwrite(ppufile:tcompilerppufile);override;
  165. destructor destroy; override;
  166. procedure check_forwards; virtual;
  167. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  168. function GetSymtable(t:tGetSymtable):TSymtable;override;
  169. function is_packed:boolean;
  170. function RttiName: string;
  171. { enumerator support }
  172. function search_enumerator_get: tprocdef; virtual;
  173. function search_enumerator_move: tprocdef; virtual;
  174. function search_enumerator_current: tsym; virtual;
  175. end;
  176. trecorddef = class(tabstractrecorddef)
  177. public
  178. isunion : boolean;
  179. constructor create(const n:string; p:TSymtable);
  180. constructor ppuload(ppufile:tcompilerppufile);
  181. destructor destroy;override;
  182. function getcopy : tstoreddef;override;
  183. procedure ppuwrite(ppufile:tcompilerppufile);override;
  184. procedure buildderef;override;
  185. procedure deref;override;
  186. function size:asizeint;override;
  187. function alignment : shortint;override;
  188. function padalignment: shortint;
  189. function GetTypeName:string;override;
  190. { debug }
  191. function needs_inittable : boolean;override;
  192. end;
  193. tobjectdef = class;
  194. { TImplementedInterface }
  195. TImplementedInterface = class
  196. IntfDef : tobjectdef;
  197. IntfDefDeref : tderef;
  198. IType : tinterfaceentrytype;
  199. IOffset : longint;
  200. VtblImplIntf : TImplementedInterface;
  201. NameMappings : TFPHashList;
  202. ProcDefs : TFPObjectList;
  203. ImplementsGetter : tsym;
  204. constructor create(aintf: tobjectdef);
  205. constructor create_deref(d:tderef);
  206. destructor destroy; override;
  207. function getcopy:TImplementedInterface;
  208. procedure buildderef;
  209. procedure deref;
  210. procedure AddMapping(const origname, newname: string);
  211. function GetMapping(const origname: string):string;
  212. procedure AddImplProc(pd:tprocdef);
  213. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  214. end;
  215. { tvmtentry }
  216. tvmtentry = record
  217. procdef : tprocdef;
  218. procdefderef : tderef;
  219. visibility : tvisibility;
  220. end;
  221. pvmtentry = ^tvmtentry;
  222. { tobjectdef }
  223. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  224. pmvcallstaticinfo = ^tmvcallstaticinfo;
  225. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  226. tobjectdef = class(tabstractrecorddef)
  227. private
  228. fcurrent_dispid: longint;
  229. public
  230. dwarf_struct_lab : tasmsymbol;
  231. childof : tobjectdef;
  232. childofderef : tderef;
  233. { for Object Pascal helpers }
  234. extendeddef : tabstractrecorddef;
  235. extendeddefderef: tderef;
  236. { for C++ classes: name of the library this class is imported from }
  237. import_lib,
  238. { for Objective-C: protocols and classes can have the same name there }
  239. objextname : pshortstring;
  240. { to be able to have a variable vmt position }
  241. { and no vmt field for objects without virtuals }
  242. vmtentries : TFPList;
  243. vmcallstaticinfo : pmvcallstaticinfo;
  244. vmt_offset : longint;
  245. objecttype : tobjecttyp;
  246. iidguid : pguid;
  247. iidstr : pshortstring;
  248. writing_class_record_dbginfo,
  249. { a class of this type has been created in this module }
  250. created_in_current_module,
  251. { a loadvmtnode for this class has been created in this
  252. module, so if a classrefdef variable of this or a parent
  253. class is used somewhere to instantiate a class, then this
  254. class may be instantiated
  255. }
  256. maybe_created_in_current_module,
  257. { a "class of" this particular class has been created in
  258. this module
  259. }
  260. classref_created_in_current_module : boolean;
  261. { store implemented interfaces defs and name mappings }
  262. ImplementedInterfaces : TFPObjectList;
  263. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef);
  264. constructor ppuload(ppufile:tcompilerppufile);
  265. destructor destroy;override;
  266. function getcopy : tstoreddef;override;
  267. procedure ppuwrite(ppufile:tcompilerppufile);override;
  268. function GetTypeName:string;override;
  269. procedure buildderef;override;
  270. procedure deref;override;
  271. procedure buildderefimpl;override;
  272. procedure derefimpl;override;
  273. procedure resetvmtentries;
  274. procedure copyvmtentries(objdef:tobjectdef);
  275. function getparentdef:tdef;override;
  276. function size : asizeint;override;
  277. function alignment:shortint;override;
  278. function vmtmethodoffset(index:longint):longint;
  279. function members_need_inittable : boolean;
  280. function find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  281. { this should be called when this class implements an interface }
  282. procedure prepareguid;
  283. function is_publishable : boolean;override;
  284. function is_related(d : tdef) : boolean;override;
  285. function needs_inittable : boolean;override;
  286. function rtti_mangledname(rt:trttitype):string;override;
  287. function vmt_mangledname : string;
  288. procedure check_forwards; override;
  289. procedure insertvmt;
  290. procedure set_parent(c : tobjectdef);
  291. function find_destructor: tprocdef;
  292. function implements_any_interfaces: boolean;
  293. { dispinterface support }
  294. function get_next_dispid: longint;
  295. { enumerator support }
  296. function search_enumerator_get: tprocdef; override;
  297. function search_enumerator_move: tprocdef; override;
  298. function search_enumerator_current: tsym; override;
  299. { WPO }
  300. procedure register_created_object_type;override;
  301. procedure register_maybe_created_object_type;
  302. procedure register_created_classref_type;
  303. procedure register_vmt_call(index:longint);
  304. { ObjC }
  305. procedure finish_objc_data;
  306. function check_objc_types: boolean;
  307. { C++ }
  308. procedure finish_cpp_data;
  309. end;
  310. tclassrefdef = class(tabstractpointerdef)
  311. constructor create(def:tdef);
  312. constructor ppuload(ppufile:tcompilerppufile);
  313. procedure ppuwrite(ppufile:tcompilerppufile);override;
  314. function getcopy:tstoreddef;override;
  315. function GetTypeName:string;override;
  316. function is_publishable : boolean;override;
  317. function rtti_mangledname(rt:trttitype):string;override;
  318. procedure register_created_object_type;override;
  319. end;
  320. tarraydef = class(tstoreddef)
  321. lowrange,
  322. highrange : asizeint;
  323. rangedef : tdef;
  324. rangedefderef : tderef;
  325. arrayoptions : tarraydefoptions;
  326. symtable : TSymtable;
  327. protected
  328. _elementdef : tdef;
  329. _elementdefderef : tderef;
  330. procedure setelementdef(def:tdef);
  331. public
  332. function elesize : asizeint;
  333. function elepackedbitsize : asizeint;
  334. function elecount : asizeuint;
  335. constructor create_from_pointer(def:tdef);
  336. constructor create(l,h:asizeint;def:tdef);
  337. constructor ppuload(ppufile:tcompilerppufile);
  338. destructor destroy; override;
  339. function getcopy : tstoreddef;override;
  340. procedure ppuwrite(ppufile:tcompilerppufile);override;
  341. function GetTypeName:string;override;
  342. function getmangledparaname : string;override;
  343. procedure buildderef;override;
  344. procedure deref;override;
  345. function size : asizeint;override;
  346. function alignment : shortint;override;
  347. { returns the label of the range check string }
  348. function needs_inittable : boolean;override;
  349. property elementdef : tdef read _elementdef write setelementdef;
  350. function is_publishable : boolean;override;
  351. end;
  352. torddef = class(tstoreddef)
  353. low,high : TConstExprInt;
  354. ordtype : tordtype;
  355. constructor create(t : tordtype;v,b : TConstExprInt);
  356. constructor ppuload(ppufile:tcompilerppufile);
  357. function getcopy : tstoreddef;override;
  358. procedure ppuwrite(ppufile:tcompilerppufile);override;
  359. function is_publishable : boolean;override;
  360. function GetTypeName:string;override;
  361. function alignment:shortint;override;
  362. procedure setsize;
  363. function packedbitsize: asizeint; override;
  364. function getvardef : longint;override;
  365. end;
  366. tfloatdef = class(tstoreddef)
  367. floattype : tfloattype;
  368. constructor create(t : tfloattype);
  369. constructor ppuload(ppufile:tcompilerppufile);
  370. function getcopy : tstoreddef;override;
  371. procedure ppuwrite(ppufile:tcompilerppufile);override;
  372. function GetTypeName:string;override;
  373. function is_publishable : boolean;override;
  374. function alignment:shortint;override;
  375. procedure setsize;
  376. function getvardef:longint;override;
  377. end;
  378. { tabstractprocdef }
  379. tabstractprocdef = class(tstoreddef)
  380. { saves a definition to the return type }
  381. returndef : tdef;
  382. returndefderef : tderef;
  383. parast : TSymtable;
  384. paras : tparalist;
  385. proctypeoption : tproctypeoption;
  386. proccalloption : tproccalloption;
  387. procoptions : tprocoptions;
  388. callerargareasize,
  389. calleeargareasize: pint;
  390. { number of user visibile parameters }
  391. maxparacount,
  392. minparacount : byte;
  393. {$ifdef m68k}
  394. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  395. {$endif}
  396. funcretloc : array[tcallercallee] of TCGPara;
  397. has_paraloc_info : tcallercallee; { paraloc info is available }
  398. constructor create(dt:tdeftyp;level:byte);
  399. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  400. destructor destroy;override;
  401. procedure ppuwrite(ppufile:tcompilerppufile);override;
  402. procedure buildderef;override;
  403. procedure deref;override;
  404. procedure calcparas;
  405. function typename_paras(showhidden:boolean): string;
  406. function is_methodpointer:boolean;virtual;
  407. function is_addressonly:boolean;virtual;
  408. function no_self_node:boolean;
  409. procedure check_mark_as_nested;
  410. procedure init_paraloc_info(side: tcallercallee);
  411. function stack_tainting_parameter(side: tcallercallee): boolean;
  412. private
  413. procedure count_para(p:TObject;arg:pointer);
  414. procedure insert_para(p:TObject;arg:pointer);
  415. end;
  416. tprocvardef = class(tabstractprocdef)
  417. constructor create(level:byte);
  418. constructor ppuload(ppufile:tcompilerppufile);
  419. function getcopy : tstoreddef;override;
  420. procedure ppuwrite(ppufile:tcompilerppufile);override;
  421. function GetSymtable(t:tGetSymtable):TSymtable;override;
  422. function size : asizeint;override;
  423. function GetTypeName:string;override;
  424. function is_publishable : boolean;override;
  425. function is_methodpointer:boolean;override;
  426. function is_addressonly:boolean;override;
  427. function getmangledparaname:string;override;
  428. end;
  429. tmessageinf = record
  430. case integer of
  431. 0 : (str : pshortstring);
  432. 1 : (i : longint);
  433. end;
  434. tinlininginfo = record
  435. { node tree }
  436. code : tnode;
  437. flags : tprocinfoflags;
  438. end;
  439. pinlininginfo = ^tinlininginfo;
  440. {$ifdef oldregvars}
  441. { register variables }
  442. pregvarinfo = ^tregvarinfo;
  443. tregvarinfo = record
  444. regvars : array[1..maxvarregs] of tsym;
  445. regvars_para : array[1..maxvarregs] of boolean;
  446. regvars_refs : array[1..maxvarregs] of longint;
  447. fpuregvars : array[1..maxfpuvarregs] of tsym;
  448. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  449. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  450. end;
  451. {$endif oldregvars}
  452. { tprocdef }
  453. tprocdef = class(tabstractprocdef)
  454. private
  455. _mangledname : pshortstring;
  456. public
  457. messageinf : tmessageinf;
  458. dispid : longint;
  459. {$ifndef EXTDEBUG}
  460. { where is this function defined and what were the symbol
  461. flags, needed here because there
  462. is only one symbol for all overloaded functions
  463. EXTDEBUG has fileinfo in tdef (PFV) }
  464. fileinfo : tfileposinfo;
  465. {$endif}
  466. symoptions : tsymoptions;
  467. deprecatedmsg : pshortstring;
  468. { symbol owning this definition }
  469. procsym : tsym;
  470. procsymderef : tderef;
  471. { alias names }
  472. aliasnames : TCmdStrList;
  473. { symtables }
  474. localst : TSymtable;
  475. funcretsym : tsym;
  476. funcretsymderef : tderef;
  477. struct : tabstractrecorddef;
  478. structderef : tderef;
  479. {$if defined(powerpc) or defined(m68k)}
  480. { library symbol for AmigaOS/MorphOS }
  481. libsym : tsym;
  482. libsymderef : tderef;
  483. {$endif powerpc or m68k}
  484. { name of the result variable to insert in the localsymtable }
  485. resultname : pshortstring;
  486. { import info }
  487. import_dll,
  488. import_name : pshortstring;
  489. { info for inlining the subroutine, if this pointer is nil,
  490. the procedure can't be inlined }
  491. inlininginfo : pinlininginfo;
  492. {$ifdef oldregvars}
  493. regvarinfo: pregvarinfo;
  494. {$endif oldregvars}
  495. { First/last assembler symbol/instruction in aasmoutput list.
  496. Note: initialised after compiling the code for the procdef, but
  497. not saved to/restored from ppu. Used when inserting debug info }
  498. procstarttai,
  499. procendtai : tai;
  500. import_nr : word;
  501. extnumber : word;
  502. {$ifdef i386}
  503. fpu_used : byte;
  504. {$endif i386}
  505. visibility : tvisibility;
  506. { true, if the procedure is only declared
  507. (forward procedure) }
  508. forwarddef,
  509. { true if the procedure is declared in the interface }
  510. interfacedef : boolean;
  511. { true if the procedure has a forward declaration }
  512. hasforward : boolean;
  513. { interrupt vector }
  514. interruptvector : longint;
  515. constructor create(level:byte);
  516. constructor ppuload(ppufile:tcompilerppufile);
  517. destructor destroy;override;
  518. procedure ppuwrite(ppufile:tcompilerppufile);override;
  519. procedure buildderef;override;
  520. procedure buildderefimpl;override;
  521. procedure deref;override;
  522. procedure derefimpl;override;
  523. function GetSymtable(t:tGetSymtable):TSymtable;override;
  524. function GetTypeName : string;override;
  525. function mangledname : string;
  526. procedure setmangledname(const s : string);
  527. function fullprocname(showhidden:boolean):string;
  528. function cplusplusmangledname : string;
  529. function objcmangledname : string;
  530. function is_methodpointer:boolean;override;
  531. function is_addressonly:boolean;override;
  532. procedure make_external;
  533. end;
  534. { single linked list of overloaded procs }
  535. pprocdeflist = ^tprocdeflist;
  536. tprocdeflist = record
  537. def : tprocdef;
  538. defderef : tderef;
  539. next : pprocdeflist;
  540. end;
  541. tstringdef = class(tstoreddef)
  542. stringtype : tstringtype;
  543. len : asizeint;
  544. constructor createshort(l : byte);
  545. constructor loadshort(ppufile:tcompilerppufile);
  546. constructor createlong(l : asizeint);
  547. constructor loadlong(ppufile:tcompilerppufile);
  548. constructor createansi;
  549. constructor loadansi(ppufile:tcompilerppufile);
  550. constructor createwide;
  551. constructor loadwide(ppufile:tcompilerppufile);
  552. constructor createunicode;
  553. constructor loadunicode(ppufile:tcompilerppufile);
  554. function getcopy : tstoreddef;override;
  555. function stringtypname:string;
  556. procedure ppuwrite(ppufile:tcompilerppufile);override;
  557. function GetTypeName:string;override;
  558. function getmangledparaname:string;override;
  559. function is_publishable : boolean;override;
  560. function alignment : shortint;override;
  561. function needs_inittable : boolean;override;
  562. function getvardef:longint;override;
  563. end;
  564. { tenumdef }
  565. tenumdef = class(tstoreddef)
  566. minval,
  567. maxval : asizeint;
  568. has_jumps : boolean;
  569. basedef : tenumdef;
  570. basedefderef : tderef;
  571. symtable : TSymtable;
  572. constructor create;
  573. constructor create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  574. constructor ppuload(ppufile:tcompilerppufile);
  575. destructor destroy;override;
  576. function getcopy : tstoreddef;override;
  577. procedure ppuwrite(ppufile:tcompilerppufile);override;
  578. procedure buildderef;override;
  579. procedure deref;override;
  580. function GetTypeName:string;override;
  581. function is_publishable : boolean;override;
  582. procedure calcsavesize;
  583. function packedbitsize: asizeint; override;
  584. procedure setmax(_max:asizeint);
  585. procedure setmin(_min:asizeint);
  586. function min:asizeint;
  587. function max:asizeint;
  588. function getfirstsym:tsym;
  589. end;
  590. tsetdef = class(tstoreddef)
  591. elementdef : tdef;
  592. elementdefderef : tderef;
  593. setbase,
  594. setmax : aword;
  595. constructor create(def:tdef;low, high : asizeint);
  596. constructor ppuload(ppufile:tcompilerppufile);
  597. function getcopy : tstoreddef;override;
  598. procedure ppuwrite(ppufile:tcompilerppufile);override;
  599. procedure buildderef;override;
  600. procedure deref;override;
  601. function GetTypeName:string;override;
  602. function is_publishable : boolean;override;
  603. end;
  604. tdefawaresymtablestack = class(TSymtablestack)
  605. private
  606. procedure addhelpers(st: TSymtable);
  607. procedure removehelpers(st: TSymtable);
  608. public
  609. procedure push(st: TSymtable); override;
  610. procedure pop(st: TSymtable); override;
  611. end;
  612. var
  613. current_structdef: tabstractrecorddef; { used for private functions check !! }
  614. current_genericdef: tstoreddef; { used to reject declaration of generic class inside generic class }
  615. current_specializedef: tstoreddef; { used to implement usage of generic class in itself }
  616. { default types }
  617. generrordef, { error in definition }
  618. voidpointertype, { pointer for Void-pointeddef }
  619. charpointertype, { pointer for Char-pointeddef }
  620. widecharpointertype, { pointer for WideChar-pointeddef }
  621. voidfarpointertype,
  622. cundefinedtype,
  623. cformaltype, { unique formal definition }
  624. ctypedformaltype, { unique typed formal definition }
  625. voidtype, { Void (procedure) }
  626. cchartype, { Char }
  627. cwidechartype, { WideChar }
  628. booltype, { boolean type }
  629. bool8type,
  630. bool16type,
  631. bool32type,
  632. bool64type, { implement me }
  633. u8inttype, { 8-Bit unsigned integer }
  634. s8inttype, { 8-Bit signed integer }
  635. u16inttype, { 16-Bit unsigned integer }
  636. s16inttype, { 16-Bit signed integer }
  637. u32inttype, { 32-Bit unsigned integer }
  638. s32inttype, { 32-Bit signed integer }
  639. u64inttype, { 64-bit unsigned integer }
  640. s64inttype, { 64-bit signed integer }
  641. s32floattype, { 32 bit floating point number }
  642. s64floattype, { 64 bit floating point number }
  643. s80floattype, { 80 bit floating point number }
  644. sc80floattype, { 80 bit floating point number but stored like in C }
  645. s64currencytype, { pointer to a currency type }
  646. cshortstringtype, { pointer to type of short string const }
  647. clongstringtype, { pointer to type of long string const }
  648. cansistringtype, { pointer to type of ansi string const }
  649. cwidestringtype, { pointer to type of wide string const }
  650. cunicodestringtype,
  651. openshortstringtype, { pointer to type of an open shortstring,
  652. needed for readln() }
  653. openchararraytype, { pointer to type of an open array of char,
  654. needed for readln() }
  655. cfiletype, { get the same definition for all file }
  656. { used for stabs }
  657. methodpointertype, { typecasting of methodpointers to extract self }
  658. hresultdef,
  659. { we use only one variant def for every variant class }
  660. cvarianttype,
  661. colevarianttype,
  662. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  663. sinttype,
  664. uinttype,
  665. { unsigned and signed ord type with the same size as a pointer }
  666. ptruinttype,
  667. ptrsinttype,
  668. { several types to simulate more or less C++ objects for GDB }
  669. vmttype,
  670. vmtarraytype,
  671. pvmttype : tdef; { type of classrefs, used for stabs }
  672. { pointer to the anchestor of all classes }
  673. class_tobject : tobjectdef;
  674. { pointer to the ancestor of all COM interfaces }
  675. interface_iunknown : tobjectdef;
  676. { pointer to the ancestor of all dispinterfaces }
  677. interface_idispatch : tobjectdef;
  678. { pointer to the TGUID type
  679. of all interfaces }
  680. rec_tguid : trecorddef;
  681. { pointer to jump buffer }
  682. rec_jmp_buf : trecorddef;
  683. { Objective-C base types }
  684. objc_metaclasstype,
  685. objc_superclasstype,
  686. objc_idtype,
  687. objc_seltype : tpointerdef;
  688. objc_objecttype : trecorddef;
  689. { base type of @protocol(protocolname) Objective-C statements }
  690. objc_protocoltype : tobjectdef;
  691. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  692. objc_fastenumeration : tobjectdef;
  693. objc_fastenumerationstate : trecorddef;
  694. const
  695. {$ifdef i386}
  696. pbestrealtype : ^tdef = @s80floattype;
  697. {$endif}
  698. {$ifdef x86_64}
  699. pbestrealtype : ^tdef = @s80floattype;
  700. {$endif}
  701. {$ifdef m68k}
  702. pbestrealtype : ^tdef = @s64floattype;
  703. {$endif}
  704. {$ifdef alpha}
  705. pbestrealtype : ^tdef = @s64floattype;
  706. {$endif}
  707. {$ifdef powerpc}
  708. pbestrealtype : ^tdef = @s64floattype;
  709. {$endif}
  710. {$ifdef POWERPC64}
  711. pbestrealtype : ^tdef = @s64floattype;
  712. {$endif}
  713. {$ifdef ia64}
  714. pbestrealtype : ^tdef = @s64floattype;
  715. {$endif}
  716. {$ifdef SPARC}
  717. pbestrealtype : ^tdef = @s64floattype;
  718. {$endif SPARC}
  719. {$ifdef vis}
  720. pbestrealtype : ^tdef = @s64floattype;
  721. {$endif vis}
  722. {$ifdef ARM}
  723. pbestrealtype : ^tdef = @s64floattype;
  724. {$endif ARM}
  725. {$ifdef MIPS}
  726. pbestrealtype : ^tdef = @s64floattype;
  727. {$endif MIPS}
  728. {$ifdef AVR}
  729. pbestrealtype : ^tdef = @s64floattype;
  730. {$endif AVR}
  731. {$ifdef AVR32}
  732. pbestrealtype : ^tdef = @s64floattype;
  733. {$endif AVR32}
  734. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  735. { should be in the types unit, but the types unit uses the node stuff :( }
  736. function is_interfacecom(def: tdef): boolean;
  737. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  738. function is_interfacecorba(def: tdef): boolean;
  739. function is_interface(def: tdef): boolean;
  740. function is_dispinterface(def: tdef): boolean;
  741. function is_object(def: tdef): boolean;
  742. function is_class(def: tdef): boolean;
  743. function is_cppclass(def: tdef): boolean;
  744. function is_objectpascal_helper(def: tdef): boolean;
  745. function is_objcclass(def: tdef): boolean;
  746. function is_objcclassref(def: tdef): boolean;
  747. function is_objcprotocol(def: tdef): boolean;
  748. function is_objccategory(def: tdef): boolean;
  749. function is_objc_class_or_protocol(def: tdef): boolean;
  750. function is_objc_protocol_or_category(def: tdef): boolean;
  751. function is_classhelper(def: tdef): boolean;
  752. function is_class_or_interface(def: tdef): boolean;
  753. function is_class_or_interface_or_objc(def: tdef): boolean;
  754. function is_class_or_interface_or_object(def: tdef): boolean;
  755. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  756. function is_implicit_pointer_object_type(def: tdef): boolean;
  757. function is_class_or_object(def: tdef): boolean;
  758. function is_record(def: tdef): boolean;
  759. procedure loadobjctypes;
  760. procedure maybeloadcocoatypes;
  761. function use_vectorfpu(def : tdef) : boolean;
  762. implementation
  763. uses
  764. SysUtils,
  765. cutils,
  766. { global }
  767. verbose,
  768. { target }
  769. systems,aasmcpu,paramgr,
  770. { symtable }
  771. symsym,symtable,symutil,defutil,objcdef,
  772. { module }
  773. fmodule,
  774. { other }
  775. gendef,
  776. fpccrc
  777. ;
  778. {****************************************************************************
  779. Helpers
  780. ****************************************************************************}
  781. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  782. var
  783. s,hs,
  784. prefix : string;
  785. oldlen,
  786. newlen,
  787. i : longint;
  788. crc : dword;
  789. hp : tparavarsym;
  790. begin
  791. prefix:='';
  792. if not assigned(st) then
  793. internalerror(200204212);
  794. { sub procedures }
  795. while (st.symtabletype=localsymtable) do
  796. begin
  797. if st.defowner.typ<>procdef then
  798. internalerror(200204173);
  799. { Add the full mangledname of procedure to prevent
  800. conflicts with 2 overloads having both a nested procedure
  801. with the same name, see tb0314 (PFV) }
  802. s:=tprocdef(st.defowner).procsym.name;
  803. oldlen:=length(s);
  804. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  805. begin
  806. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  807. if not(vo_is_hidden_para in hp.varoptions) then
  808. s:=s+'$'+hp.vardef.mangledparaname;
  809. end;
  810. if not is_void(tprocdef(st.defowner).returndef) then
  811. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  812. newlen:=length(s);
  813. { Replace with CRC if the parameter line is very long }
  814. if (newlen-oldlen>12) and
  815. ((newlen+length(prefix)>100) or (newlen-oldlen>32)) then
  816. begin
  817. crc:=0;
  818. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  819. begin
  820. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  821. if not(vo_is_hidden_para in hp.varoptions) then
  822. begin
  823. hs:=hp.vardef.mangledparaname;
  824. crc:=UpdateCrc32(crc,hs[1],length(hs));
  825. end;
  826. end;
  827. hs:=hp.vardef.mangledparaname;
  828. crc:=UpdateCrc32(crc,hs[1],length(hs));
  829. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  830. end;
  831. if prefix<>'' then
  832. prefix:=s+'_'+prefix
  833. else
  834. prefix:=s;
  835. if length(prefix)>100 then
  836. begin
  837. crc:=0;
  838. crc:=UpdateCrc32(crc,prefix[1],length(prefix));
  839. prefix:='$CRC'+hexstr(crc,8);
  840. end;
  841. st:=st.defowner.owner;
  842. end;
  843. { object/classes symtable, nested type definitions in classes require the while loop }
  844. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  845. begin
  846. if not (st.defowner.typ in [objectdef,recorddef]) then
  847. internalerror(200204174);
  848. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  849. st:=st.defowner.owner;
  850. end;
  851. { symtable must now be static or global }
  852. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  853. internalerror(200204175);
  854. result:='';
  855. if typeprefix<>'' then
  856. result:=result+typeprefix+'_';
  857. { Add P$ for program, which can have the same name as
  858. a unit }
  859. if (TSymtable(main_module.localsymtable)=st) and
  860. (not main_module.is_unit) then
  861. result:=result+'P$'+st.name^
  862. else
  863. result:=result+st.name^;
  864. if prefix<>'' then
  865. result:=result+'_'+prefix;
  866. if suffix<>'' then
  867. result:=result+'_'+suffix;
  868. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  869. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  870. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  871. { those in the debug map, leading to troubles with dsymutil). So always }
  872. { add an underscore on darwin. }
  873. if (target_info.system in systems_darwin) then
  874. result := '_' + result;
  875. end;
  876. {****************************************************************************
  877. TDEFAWARESYMTABLESTACK
  878. (symtablestack descendant that does some special actions on
  879. the pushed/popped symtables)
  880. ****************************************************************************}
  881. procedure tdefawaresymtablestack.addhelpers(st: TSymtable);
  882. var
  883. i: integer;
  884. s: string;
  885. list: TFPObjectList;
  886. def: tdef;
  887. begin
  888. { search the symtable from first to last; the helper to use will be the
  889. last one in the list }
  890. for i:=0 to st.symlist.count-1 do
  891. begin
  892. if not (st.symlist[i] is ttypesym) then
  893. continue;
  894. def:=ttypesym(st.SymList[i]).typedef;
  895. if is_objectpascal_helper(def) then
  896. begin
  897. s:=make_mangledname('',tobjectdef(def).extendeddef.symtable,'');
  898. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  899. if not assigned(list) then
  900. begin
  901. list:=TFPObjectList.Create(false);
  902. current_module.extendeddefs.Add(s,list);
  903. end;
  904. list.Add(def);
  905. end
  906. else
  907. { add nested helpers as well }
  908. if def.typ in [recorddef,objectdef] then
  909. addhelpers(tabstractrecorddef(def).symtable);
  910. end;
  911. end;
  912. procedure tdefawaresymtablestack.removehelpers(st: TSymtable);
  913. var
  914. i, j: integer;
  915. tmpst: TSymtable;
  916. list: TFPObjectList;
  917. begin
  918. for i:=current_module.extendeddefs.count-1 downto 0 do
  919. begin
  920. list:=TFPObjectList(current_module.extendeddefs[i]);
  921. for j:=list.count-1 downto 0 do
  922. begin
  923. if not (list[j] is tobjectdef) then
  924. Internalerror(2011031501);
  925. tmpst:=tobjectdef(list[j]).owner;
  926. repeat
  927. if tmpst=st then
  928. begin
  929. list.delete(j);
  930. break;
  931. end
  932. else
  933. begin
  934. if assigned(tmpst.defowner) then
  935. tmpst:=tmpst.defowner.owner
  936. else
  937. tmpst:=nil;
  938. end;
  939. until not assigned(tmpst) or (tmpst.symtabletype in [globalsymtable,staticsymtable]);
  940. end;
  941. if list.count=0 then
  942. current_module.extendeddefs.delete(i);
  943. end;
  944. end;
  945. procedure tdefawaresymtablestack.push(st: TSymtable);
  946. begin
  947. { nested helpers will be added as well }
  948. if (st.symtabletype in [globalsymtable,staticsymtable]) and
  949. (sto_has_helper in st.tableoptions) then
  950. addhelpers(st);
  951. inherited push(st);
  952. end;
  953. procedure tdefawaresymtablestack.pop(st: TSymtable);
  954. begin
  955. inherited pop(st);
  956. { nested helpers will be removed as well }
  957. if (st.symtabletype in [globalsymtable,staticsymtable]) and
  958. (sto_has_helper in st.tableoptions) then
  959. removehelpers(st);
  960. end;
  961. {****************************************************************************
  962. TDEF (base class for definitions)
  963. ****************************************************************************}
  964. constructor tstoreddef.create(dt:tdeftyp);
  965. var
  966. insertstack : psymtablestackitem;
  967. begin
  968. inherited create(dt);
  969. savesize := 0;
  970. {$ifdef EXTDEBUG}
  971. fileinfo := current_filepos;
  972. {$endif}
  973. generictokenbuf:=nil;
  974. genericdef:=nil;
  975. { Don't register forwarddefs, they are disposed at the
  976. end of an type block }
  977. if (dt=forwarddef) then
  978. exit;
  979. { Register in current_module }
  980. if assigned(current_module) then
  981. begin
  982. current_module.deflist.Add(self);
  983. DefId:=current_module.deflist.Count-1;
  984. end;
  985. { Register in symtable stack }
  986. if assigned(symtablestack) then
  987. begin
  988. insertstack:=symtablestack.stack;
  989. while assigned(insertstack) and
  990. (insertstack^.symtable.symtabletype=withsymtable) do
  991. insertstack:=insertstack^.next;
  992. if not assigned(insertstack) then
  993. internalerror(200602044);
  994. insertstack^.symtable.insertdef(self);
  995. end;
  996. end;
  997. destructor tstoreddef.destroy;
  998. begin
  999. { Direct calls are not allowed, use symtable.deletedef() }
  1000. if assigned(owner) then
  1001. internalerror(200612311);
  1002. if assigned(generictokenbuf) then
  1003. begin
  1004. generictokenbuf.free;
  1005. generictokenbuf:=nil;
  1006. end;
  1007. inherited destroy;
  1008. end;
  1009. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1010. var
  1011. sizeleft,i : longint;
  1012. buf : array[0..255] of byte;
  1013. begin
  1014. inherited create(dt);
  1015. DefId:=ppufile.getlongint;
  1016. current_module.deflist[DefId]:=self;
  1017. {$ifdef EXTDEBUG}
  1018. fillchar(fileinfo,sizeof(fileinfo),0);
  1019. {$endif}
  1020. { load }
  1021. ppufile.getderef(typesymderef);
  1022. ppufile.getsmallset(defoptions);
  1023. ppufile.getsmallset(defstates);
  1024. if df_generic in defoptions then
  1025. begin
  1026. sizeleft:=ppufile.getlongint;
  1027. initgeneric;
  1028. while sizeleft>0 do
  1029. begin
  1030. if sizeleft>sizeof(buf) then
  1031. i:=sizeof(buf)
  1032. else
  1033. i:=sizeleft;
  1034. ppufile.getdata(buf,i);
  1035. generictokenbuf.write(buf,i);
  1036. dec(sizeleft,i);
  1037. end;
  1038. end;
  1039. if df_specialization in defoptions then
  1040. ppufile.getderef(genericdefderef);
  1041. end;
  1042. function Tstoreddef.rtti_mangledname(rt:trttitype):string;
  1043. var
  1044. prefix : string[4];
  1045. begin
  1046. if rt=fullrtti then
  1047. begin
  1048. prefix:='RTTI';
  1049. include(defstates,ds_rtti_table_used);
  1050. end
  1051. else
  1052. begin
  1053. prefix:='INIT';
  1054. include(defstates,ds_init_table_used);
  1055. end;
  1056. if assigned(typesym) and
  1057. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  1058. result:=make_mangledname(prefix,owner,typesym.name)
  1059. else
  1060. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  1061. end;
  1062. function tstoreddef.OwnerHierarchyName: string;
  1063. var
  1064. tmp: tdef;
  1065. begin
  1066. tmp:=self;
  1067. result:='';
  1068. repeat
  1069. if tmp.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  1070. tmp:=tdef(tmp.owner.defowner)
  1071. else
  1072. break;
  1073. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  1074. until tmp=nil;
  1075. end;
  1076. function tstoreddef.in_currentunit: boolean;
  1077. var
  1078. st: tsymtable;
  1079. begin
  1080. st:=owner;
  1081. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  1082. st:=st.defowner.owner;
  1083. result:=st.iscurrentunit;
  1084. end;
  1085. function tstoreddef.getcopy : tstoreddef;
  1086. begin
  1087. Message(sym_e_cant_create_unique_type);
  1088. getcopy:=terrordef.create;
  1089. end;
  1090. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  1091. var
  1092. sizeleft,i : longint;
  1093. buf : array[0..255] of byte;
  1094. oldintfcrc : boolean;
  1095. begin
  1096. ppufile.putlongint(DefId);
  1097. ppufile.putderef(typesymderef);
  1098. ppufile.putsmallset(defoptions);
  1099. oldintfcrc:=ppufile.do_crc;
  1100. ppufile.do_crc:=false;
  1101. ppufile.putsmallset(defstates);
  1102. if df_generic in defoptions then
  1103. begin
  1104. if assigned(generictokenbuf) then
  1105. begin
  1106. sizeleft:=generictokenbuf.size;
  1107. generictokenbuf.seek(0);
  1108. end
  1109. else
  1110. sizeleft:=0;
  1111. ppufile.putlongint(sizeleft);
  1112. while sizeleft>0 do
  1113. begin
  1114. if sizeleft>sizeof(buf) then
  1115. i:=sizeof(buf)
  1116. else
  1117. i:=sizeleft;
  1118. generictokenbuf.read(buf,i);
  1119. ppufile.putdata(buf,i);
  1120. dec(sizeleft,i);
  1121. end;
  1122. end;
  1123. ppufile.do_crc:=oldintfcrc;
  1124. if df_specialization in defoptions then
  1125. ppufile.putderef(genericdefderef);
  1126. end;
  1127. procedure tstoreddef.buildderef;
  1128. begin
  1129. typesymderef.build(typesym);
  1130. genericdefderef.build(genericdef);
  1131. end;
  1132. procedure tstoreddef.buildderefimpl;
  1133. begin
  1134. end;
  1135. procedure tstoreddef.deref;
  1136. begin
  1137. typesym:=ttypesym(typesymderef.resolve);
  1138. if df_specialization in defoptions then
  1139. genericdef:=tstoreddef(genericdefderef.resolve);
  1140. end;
  1141. procedure tstoreddef.derefimpl;
  1142. begin
  1143. end;
  1144. function tstoreddef.size : asizeint;
  1145. begin
  1146. size:=savesize;
  1147. end;
  1148. function tstoreddef.getvardef:longint;
  1149. begin
  1150. result:=varUndefined;
  1151. end;
  1152. function tstoreddef.alignment : shortint;
  1153. begin
  1154. { natural alignment by default }
  1155. alignment:=size_2_align(savesize);
  1156. { can happen if savesize = 0, e.g. for voiddef or
  1157. an empty record
  1158. }
  1159. if (alignment=0) then
  1160. alignment:=1;
  1161. end;
  1162. { returns true, if the definition can be published }
  1163. function tstoreddef.is_publishable : boolean;
  1164. begin
  1165. is_publishable:=false;
  1166. end;
  1167. { needs an init table }
  1168. function tstoreddef.needs_inittable : boolean;
  1169. begin
  1170. needs_inittable:=false;
  1171. end;
  1172. function tstoreddef.is_intregable : boolean;
  1173. var
  1174. recsize,temp: longint;
  1175. begin
  1176. is_intregable:=false;
  1177. case typ of
  1178. orddef,
  1179. pointerdef,
  1180. enumdef,
  1181. classrefdef:
  1182. is_intregable:=true;
  1183. procvardef :
  1184. is_intregable:=tprocvardef(self).is_addressonly;
  1185. objectdef:
  1186. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  1187. setdef:
  1188. is_intregable:=is_smallset(self);
  1189. recorddef:
  1190. begin
  1191. recsize:=size;
  1192. is_intregable:=
  1193. ispowerof2(recsize,temp) and
  1194. (recsize <= sizeof(asizeint));
  1195. end;
  1196. end;
  1197. end;
  1198. function tstoreddef.is_fpuregable : boolean;
  1199. begin
  1200. {$ifdef x86}
  1201. result:=use_vectorfpu(self);
  1202. {$else x86}
  1203. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1204. {$endif x86}
  1205. end;
  1206. procedure tstoreddef.initgeneric;
  1207. begin
  1208. if assigned(generictokenbuf) then
  1209. internalerror(200512131);
  1210. generictokenbuf:=tdynamicarray.create(256);
  1211. end;
  1212. {****************************************************************************
  1213. Tstringdef
  1214. ****************************************************************************}
  1215. constructor tstringdef.createshort(l : byte);
  1216. begin
  1217. inherited create(stringdef);
  1218. stringtype:=st_shortstring;
  1219. len:=l;
  1220. savesize:=len+1;
  1221. end;
  1222. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1223. begin
  1224. inherited ppuload(stringdef,ppufile);
  1225. stringtype:=st_shortstring;
  1226. len:=ppufile.getbyte;
  1227. savesize:=len+1;
  1228. end;
  1229. constructor tstringdef.createlong(l : asizeint);
  1230. begin
  1231. inherited create(stringdef);
  1232. stringtype:=st_longstring;
  1233. len:=l;
  1234. savesize:=sizeof(pint);
  1235. end;
  1236. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1237. begin
  1238. inherited ppuload(stringdef,ppufile);
  1239. stringtype:=st_longstring;
  1240. len:=ppufile.getasizeint;
  1241. savesize:=sizeof(pint);
  1242. end;
  1243. constructor tstringdef.createansi;
  1244. begin
  1245. inherited create(stringdef);
  1246. stringtype:=st_ansistring;
  1247. len:=-1;
  1248. savesize:=sizeof(pint);
  1249. end;
  1250. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1251. begin
  1252. inherited ppuload(stringdef,ppufile);
  1253. stringtype:=st_ansistring;
  1254. len:=ppufile.getaint;
  1255. savesize:=sizeof(pint);
  1256. end;
  1257. constructor tstringdef.createwide;
  1258. begin
  1259. inherited create(stringdef);
  1260. stringtype:=st_widestring;
  1261. len:=-1;
  1262. savesize:=sizeof(pint);
  1263. end;
  1264. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1265. begin
  1266. inherited ppuload(stringdef,ppufile);
  1267. stringtype:=st_widestring;
  1268. len:=ppufile.getaint;
  1269. savesize:=sizeof(pint);
  1270. end;
  1271. constructor tstringdef.createunicode;
  1272. begin
  1273. inherited create(stringdef);
  1274. stringtype:=st_unicodestring;
  1275. len:=-1;
  1276. savesize:=sizeof(pint);
  1277. end;
  1278. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  1279. begin
  1280. inherited ppuload(stringdef,ppufile);
  1281. stringtype:=st_unicodestring;
  1282. len:=ppufile.getaint;
  1283. savesize:=sizeof(pint);
  1284. end;
  1285. function tstringdef.getcopy : tstoreddef;
  1286. begin
  1287. result:=tstringdef.create(typ);
  1288. result.typ:=stringdef;
  1289. tstringdef(result).stringtype:=stringtype;
  1290. tstringdef(result).len:=len;
  1291. tstringdef(result).savesize:=savesize;
  1292. end;
  1293. function tstringdef.stringtypname:string;
  1294. const
  1295. typname:array[tstringtype] of string[10]=(
  1296. 'shortstr','longstr','ansistr','widestr','unicodestr'
  1297. );
  1298. begin
  1299. stringtypname:=typname[stringtype];
  1300. end;
  1301. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1302. begin
  1303. inherited ppuwrite(ppufile);
  1304. if stringtype=st_shortstring then
  1305. begin
  1306. {$ifdef extdebug}
  1307. if len > 255 then internalerror(12122002);
  1308. {$endif}
  1309. ppufile.putbyte(byte(len))
  1310. end
  1311. else
  1312. ppufile.putaint(len);
  1313. case stringtype of
  1314. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1315. st_longstring : ppufile.writeentry(iblongstringdef);
  1316. st_ansistring : ppufile.writeentry(ibansistringdef);
  1317. st_widestring : ppufile.writeentry(ibwidestringdef);
  1318. st_unicodestring : ppufile.writeentry(ibunicodestringdef);
  1319. end;
  1320. end;
  1321. function tstringdef.needs_inittable : boolean;
  1322. begin
  1323. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1324. end;
  1325. function tstringdef.GetTypeName : string;
  1326. const
  1327. names : array[tstringtype] of string[15] = (
  1328. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  1329. begin
  1330. GetTypeName:=names[stringtype];
  1331. end;
  1332. function tstringdef.getvardef : longint;
  1333. const
  1334. vardef : array[tstringtype] of longint = (
  1335. varUndefined,varUndefined,varString,varOleStr,varUString);
  1336. begin
  1337. result:=vardef[stringtype];
  1338. end;
  1339. function tstringdef.alignment : shortint;
  1340. begin
  1341. case stringtype of
  1342. st_unicodestring,
  1343. st_widestring,
  1344. st_ansistring:
  1345. alignment:=size_2_align(savesize);
  1346. st_longstring,
  1347. st_shortstring:
  1348. { char to string accesses byte 0 and 1 with one word access }
  1349. if (tf_requires_proper_alignment in target_info.flags) or
  1350. { macpas needs an alignment of 2 (MetroWerks compatible) }
  1351. (m_mac in current_settings.modeswitches) then
  1352. alignment:=size_2_align(2)
  1353. else
  1354. alignment:=size_2_align(1);
  1355. else
  1356. internalerror(200412301);
  1357. end;
  1358. end;
  1359. function tstringdef.getmangledparaname : string;
  1360. begin
  1361. getmangledparaname:='STRING';
  1362. end;
  1363. function tstringdef.is_publishable : boolean;
  1364. begin
  1365. is_publishable:=true;
  1366. end;
  1367. {****************************************************************************
  1368. TENUMDEF
  1369. ****************************************************************************}
  1370. constructor tenumdef.create;
  1371. begin
  1372. inherited create(enumdef);
  1373. minval:=0;
  1374. maxval:=0;
  1375. calcsavesize;
  1376. has_jumps:=false;
  1377. basedef:=nil;
  1378. symtable:=tenumsymtable.create(self);
  1379. end;
  1380. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  1381. begin
  1382. inherited create(enumdef);
  1383. minval:=_min;
  1384. maxval:=_max;
  1385. basedef:=_basedef;
  1386. calcsavesize;
  1387. has_jumps:=false;
  1388. symtable:=basedef.symtable.getcopy;
  1389. include(defoptions, df_copied_def);
  1390. end;
  1391. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1392. begin
  1393. inherited ppuload(enumdef,ppufile);
  1394. minval:=ppufile.getaint;
  1395. maxval:=ppufile.getaint;
  1396. savesize:=ppufile.getaint;
  1397. has_jumps:=false;
  1398. if df_copied_def in defoptions then
  1399. begin
  1400. symtable:=nil;
  1401. ppufile.getderef(basedefderef);
  1402. end
  1403. else
  1404. begin
  1405. // create with nil defowner first to prevent values changes on insert
  1406. symtable:=tenumsymtable.create(nil);
  1407. tenumsymtable(symtable).ppuload(ppufile);
  1408. symtable.defowner:=self;
  1409. end;
  1410. end;
  1411. destructor tenumdef.destroy;
  1412. begin
  1413. symtable.free;
  1414. symtable:=nil;
  1415. inherited destroy;
  1416. end;
  1417. function tenumdef.getcopy : tstoreddef;
  1418. begin
  1419. if assigned(basedef) then
  1420. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1421. else
  1422. begin
  1423. result:=tenumdef.create;
  1424. tenumdef(result).minval:=minval;
  1425. tenumdef(result).maxval:=maxval;
  1426. tenumdef(result).symtable.free;
  1427. tenumdef(result).symtable:=symtable.getcopy;
  1428. tenumdef(result).basedef:=self;
  1429. end;
  1430. tenumdef(result).has_jumps:=has_jumps;
  1431. tenumdef(result).basedefderef:=basedefderef;
  1432. include(tenumdef(result).defoptions,df_copied_def);
  1433. end;
  1434. procedure tenumdef.calcsavesize;
  1435. begin
  1436. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1437. savesize:=8
  1438. else
  1439. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  1440. savesize:=4
  1441. else
  1442. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1443. savesize:=2
  1444. else
  1445. savesize:=1;
  1446. end;
  1447. function tenumdef.packedbitsize: asizeint;
  1448. var
  1449. sizeval: tconstexprint;
  1450. power: longint;
  1451. begin
  1452. result := 0;
  1453. if (minval >= 0) and
  1454. (maxval <= 1) then
  1455. result := 1
  1456. else
  1457. begin
  1458. if (minval>=0) then
  1459. sizeval:=maxval
  1460. else
  1461. { don't count 0 twice }
  1462. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  1463. { 256 must become 512 etc. }
  1464. nextpowerof2(sizeval+1,power);
  1465. result := power;
  1466. end;
  1467. end;
  1468. procedure tenumdef.setmax(_max:asizeint);
  1469. begin
  1470. maxval:=_max;
  1471. calcsavesize;
  1472. end;
  1473. procedure tenumdef.setmin(_min:asizeint);
  1474. begin
  1475. minval:=_min;
  1476. calcsavesize;
  1477. end;
  1478. function tenumdef.min:asizeint;
  1479. begin
  1480. min:=minval;
  1481. end;
  1482. function tenumdef.max:asizeint;
  1483. begin
  1484. max:=maxval;
  1485. end;
  1486. function tenumdef.getfirstsym: tsym;
  1487. var
  1488. i:integer;
  1489. begin
  1490. for i := 0 to symtable.SymList.Count - 1 do
  1491. begin
  1492. result:=tsym(symtable.SymList[i]);
  1493. if tenumsym(result).value=minval then
  1494. exit;
  1495. end;
  1496. result:=nil;
  1497. end;
  1498. procedure tenumdef.buildderef;
  1499. begin
  1500. inherited buildderef;
  1501. if df_copied_def in defoptions then
  1502. basedefderef.build(basedef)
  1503. else
  1504. tenumsymtable(symtable).buildderef;
  1505. end;
  1506. procedure tenumdef.deref;
  1507. begin
  1508. inherited deref;
  1509. if df_copied_def in defoptions then
  1510. begin
  1511. basedef:=tenumdef(basedefderef.resolve);
  1512. symtable:=basedef.symtable.getcopy;
  1513. end
  1514. else
  1515. tenumsymtable(symtable).deref;
  1516. end;
  1517. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1518. begin
  1519. inherited ppuwrite(ppufile);
  1520. ppufile.putaint(min);
  1521. ppufile.putaint(max);
  1522. ppufile.putaint(savesize);
  1523. if df_copied_def in defoptions then
  1524. ppufile.putderef(basedefderef);
  1525. ppufile.writeentry(ibenumdef);
  1526. if not (df_copied_def in defoptions) then
  1527. tenumsymtable(symtable).ppuwrite(ppufile);
  1528. end;
  1529. function tenumdef.is_publishable : boolean;
  1530. begin
  1531. is_publishable:=true;
  1532. end;
  1533. function tenumdef.GetTypeName : string;
  1534. begin
  1535. GetTypeName:='<enumeration type>';
  1536. end;
  1537. {****************************************************************************
  1538. TORDDEF
  1539. ****************************************************************************}
  1540. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  1541. begin
  1542. inherited create(orddef);
  1543. low:=v;
  1544. high:=b;
  1545. ordtype:=t;
  1546. setsize;
  1547. end;
  1548. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1549. begin
  1550. inherited ppuload(orddef,ppufile);
  1551. ordtype:=tordtype(ppufile.getbyte);
  1552. low:=ppufile.getexprint;
  1553. high:=ppufile.getexprint;
  1554. setsize;
  1555. end;
  1556. function torddef.getcopy : tstoreddef;
  1557. begin
  1558. result:=torddef.create(ordtype,low,high);
  1559. result.typ:=orddef;
  1560. torddef(result).low:=low;
  1561. torddef(result).high:=high;
  1562. torddef(result).ordtype:=ordtype;
  1563. torddef(result).savesize:=savesize;
  1564. end;
  1565. function torddef.alignment:shortint;
  1566. begin
  1567. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) and
  1568. (ordtype in [s64bit,u64bit]) then
  1569. result := 4
  1570. else
  1571. result := inherited alignment;
  1572. end;
  1573. procedure torddef.setsize;
  1574. const
  1575. sizetbl : array[tordtype] of longint = (
  1576. 0,
  1577. 1,2,4,8,
  1578. 1,2,4,8,
  1579. 1,1,2,4,8,
  1580. 1,2,8
  1581. );
  1582. begin
  1583. savesize:=sizetbl[ordtype];
  1584. end;
  1585. function torddef.packedbitsize: asizeint;
  1586. var
  1587. sizeval: tconstexprint;
  1588. power: longint;
  1589. begin
  1590. result := 0;
  1591. if ordtype = uvoid then
  1592. exit;
  1593. {$ifndef cpu64bitalu}
  1594. if (ordtype in [s64bit,u64bit]) then
  1595. {$else not cpu64bitalu}
  1596. if (ordtype = u64bit) or
  1597. ((ordtype = s64bit) and
  1598. ((low <= (system.low(int64) div 2)) or
  1599. (high > (system.high(int64) div 2)))) then
  1600. {$endif cpu64bitalu}
  1601. result := 64
  1602. else if (low >= 0) and
  1603. (high <= 1) then
  1604. result := 1
  1605. else
  1606. begin
  1607. if (low>=0) then
  1608. sizeval:=high
  1609. else
  1610. { don't count 0 twice }
  1611. sizeval:=(cutils.max(-low,high)*2)-1;
  1612. { 256 must become 512 etc. }
  1613. nextpowerof2(sizeval+1,power);
  1614. result := power;
  1615. end;
  1616. end;
  1617. function torddef.getvardef : longint;
  1618. const
  1619. basetype2vardef : array[tordtype] of longint = (
  1620. varUndefined,
  1621. varbyte,varword,varlongword,varqword,
  1622. varshortint,varsmallint,varinteger,varint64,
  1623. varboolean,varboolean,varboolean,varUndefined,varUndefined,
  1624. varUndefined,varUndefined,varCurrency);
  1625. begin
  1626. result:=basetype2vardef[ordtype];
  1627. end;
  1628. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  1629. begin
  1630. inherited ppuwrite(ppufile);
  1631. ppufile.putbyte(byte(ordtype));
  1632. ppufile.putexprint(low);
  1633. ppufile.putexprint(high);
  1634. ppufile.writeentry(iborddef);
  1635. end;
  1636. function torddef.is_publishable : boolean;
  1637. begin
  1638. is_publishable:=(ordtype<>uvoid);
  1639. end;
  1640. function torddef.GetTypeName : string;
  1641. const
  1642. names : array[tordtype] of string[20] = (
  1643. 'untyped',
  1644. 'Byte','Word','DWord','QWord',
  1645. 'ShortInt','SmallInt','LongInt','Int64',
  1646. 'Boolean','ByteBool','WordBool','LongBool','QWordBool',
  1647. 'Char','WideChar','Currency');
  1648. begin
  1649. GetTypeName:=names[ordtype];
  1650. end;
  1651. {****************************************************************************
  1652. TFLOATDEF
  1653. ****************************************************************************}
  1654. constructor tfloatdef.create(t : tfloattype);
  1655. begin
  1656. inherited create(floatdef);
  1657. floattype:=t;
  1658. setsize;
  1659. end;
  1660. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  1661. begin
  1662. inherited ppuload(floatdef,ppufile);
  1663. floattype:=tfloattype(ppufile.getbyte);
  1664. setsize;
  1665. end;
  1666. function tfloatdef.getcopy : tstoreddef;
  1667. begin
  1668. result:=tfloatdef.create(floattype);
  1669. result.typ:=floatdef;
  1670. tfloatdef(result).savesize:=savesize;
  1671. end;
  1672. function tfloatdef.alignment:shortint;
  1673. begin
  1674. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) then
  1675. case floattype of
  1676. s80real: result:=16;
  1677. s64real,
  1678. s64currency,
  1679. s64comp : result:=4;
  1680. else
  1681. result := inherited alignment;
  1682. end
  1683. else
  1684. result := inherited alignment;
  1685. end;
  1686. procedure tfloatdef.setsize;
  1687. begin
  1688. case floattype of
  1689. s32real : savesize:=4;
  1690. s80real : savesize:=10;
  1691. sc80real:
  1692. if target_info.system in [system_i386_darwin,system_i386_iphonesim,system_x86_64_darwin,
  1693. system_x86_64_linux,system_x86_64_freebsd,
  1694. system_x86_64_solaris,system_x86_64_embedded] then
  1695. savesize:=16
  1696. else
  1697. savesize:=12;
  1698. s64real,
  1699. s64currency,
  1700. s64comp : savesize:=8;
  1701. else
  1702. savesize:=0;
  1703. end;
  1704. end;
  1705. function tfloatdef.getvardef : longint;
  1706. const
  1707. floattype2vardef : array[tfloattype] of longint = (
  1708. varSingle,varDouble,varUndefined,varUndefined,
  1709. varUndefined,varCurrency,varUndefined);
  1710. begin
  1711. if (upper(typename)='TDATETIME') and
  1712. assigned(owner) and
  1713. assigned(owner.name) and
  1714. (owner.name^='SYSTEM') then
  1715. result:=varDate
  1716. else
  1717. result:=floattype2vardef[floattype];
  1718. end;
  1719. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  1720. begin
  1721. inherited ppuwrite(ppufile);
  1722. ppufile.putbyte(byte(floattype));
  1723. ppufile.writeentry(ibfloatdef);
  1724. end;
  1725. function tfloatdef.is_publishable : boolean;
  1726. begin
  1727. is_publishable:=true;
  1728. end;
  1729. function tfloatdef.GetTypeName : string;
  1730. const
  1731. names : array[tfloattype] of string[20] = (
  1732. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  1733. begin
  1734. GetTypeName:=names[floattype];
  1735. end;
  1736. {****************************************************************************
  1737. TFILEDEF
  1738. ****************************************************************************}
  1739. constructor tfiledef.createtext;
  1740. begin
  1741. inherited create(filedef);
  1742. filetyp:=ft_text;
  1743. typedfiledef:=nil;
  1744. setsize;
  1745. end;
  1746. constructor tfiledef.createuntyped;
  1747. begin
  1748. inherited create(filedef);
  1749. filetyp:=ft_untyped;
  1750. typedfiledef:=nil;
  1751. setsize;
  1752. end;
  1753. constructor tfiledef.createtyped(def:tdef);
  1754. begin
  1755. inherited create(filedef);
  1756. filetyp:=ft_typed;
  1757. typedfiledef:=def;
  1758. setsize;
  1759. end;
  1760. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  1761. begin
  1762. inherited ppuload(filedef,ppufile);
  1763. filetyp:=tfiletyp(ppufile.getbyte);
  1764. if filetyp=ft_typed then
  1765. ppufile.getderef(typedfiledefderef)
  1766. else
  1767. typedfiledef:=nil;
  1768. setsize;
  1769. end;
  1770. function tfiledef.getcopy : tstoreddef;
  1771. begin
  1772. case filetyp of
  1773. ft_typed:
  1774. result:=tfiledef.createtyped(typedfiledef);
  1775. ft_untyped:
  1776. result:=tfiledef.createuntyped;
  1777. ft_text:
  1778. result:=tfiledef.createtext;
  1779. else
  1780. internalerror(2004121201);
  1781. end;
  1782. end;
  1783. procedure tfiledef.buildderef;
  1784. begin
  1785. inherited buildderef;
  1786. if filetyp=ft_typed then
  1787. typedfiledefderef.build(typedfiledef);
  1788. end;
  1789. procedure tfiledef.deref;
  1790. begin
  1791. inherited deref;
  1792. if filetyp=ft_typed then
  1793. typedfiledef:=tdef(typedfiledefderef.resolve);
  1794. end;
  1795. procedure tfiledef.setsize;
  1796. begin
  1797. {$ifdef cpu64bitaddr}
  1798. case filetyp of
  1799. ft_text :
  1800. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1801. savesize:=632{+8}
  1802. else
  1803. savesize:=628{+8};
  1804. ft_typed,
  1805. ft_untyped :
  1806. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1807. savesize:=372
  1808. else
  1809. savesize:=368;
  1810. end;
  1811. {$endif cpu64bitaddr}
  1812. {$ifdef cpu32bitaddr}
  1813. case filetyp of
  1814. ft_text :
  1815. savesize:=592{+4};
  1816. ft_typed,
  1817. ft_untyped :
  1818. savesize:=332;
  1819. end;
  1820. {$endif cpu32bitaddr}
  1821. {$ifdef cpu8bitaddr}
  1822. case filetyp of
  1823. ft_text :
  1824. savesize:=127;
  1825. ft_typed,
  1826. ft_untyped :
  1827. savesize:=127;
  1828. end;
  1829. {$endif cpu8bitaddr}
  1830. end;
  1831. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  1832. begin
  1833. inherited ppuwrite(ppufile);
  1834. ppufile.putbyte(byte(filetyp));
  1835. if filetyp=ft_typed then
  1836. ppufile.putderef(typedfiledefderef);
  1837. ppufile.writeentry(ibfiledef);
  1838. end;
  1839. function tfiledef.GetTypeName : string;
  1840. begin
  1841. case filetyp of
  1842. ft_untyped:
  1843. GetTypeName:='File';
  1844. ft_typed:
  1845. GetTypeName:='File Of '+typedfiledef.typename;
  1846. ft_text:
  1847. GetTypeName:='Text'
  1848. end;
  1849. end;
  1850. function tfiledef.getmangledparaname : string;
  1851. begin
  1852. case filetyp of
  1853. ft_untyped:
  1854. getmangledparaname:='FILE';
  1855. ft_typed:
  1856. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  1857. ft_text:
  1858. getmangledparaname:='TEXT'
  1859. end;
  1860. end;
  1861. {****************************************************************************
  1862. TVARIANTDEF
  1863. ****************************************************************************}
  1864. constructor tvariantdef.create(v : tvarianttype);
  1865. begin
  1866. inherited create(variantdef);
  1867. varianttype:=v;
  1868. setsize;
  1869. end;
  1870. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  1871. begin
  1872. inherited ppuload(variantdef,ppufile);
  1873. varianttype:=tvarianttype(ppufile.getbyte);
  1874. setsize;
  1875. end;
  1876. function tvariantdef.getcopy : tstoreddef;
  1877. begin
  1878. result:=tvariantdef.create(varianttype);
  1879. end;
  1880. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  1881. begin
  1882. inherited ppuwrite(ppufile);
  1883. ppufile.putbyte(byte(varianttype));
  1884. ppufile.writeentry(ibvariantdef);
  1885. end;
  1886. function tvariantdef.getvardef : longint;
  1887. begin
  1888. Result:=varVariant;
  1889. end;
  1890. procedure tvariantdef.setsize;
  1891. begin
  1892. {$ifdef cpu64bitaddr}
  1893. savesize:=24;
  1894. {$else cpu64bitaddr}
  1895. savesize:=16;
  1896. {$endif cpu64bitaddr}
  1897. end;
  1898. function tvariantdef.GetTypeName : string;
  1899. begin
  1900. case varianttype of
  1901. vt_normalvariant:
  1902. GetTypeName:='Variant';
  1903. vt_olevariant:
  1904. GetTypeName:='OleVariant';
  1905. end;
  1906. end;
  1907. function tvariantdef.needs_inittable : boolean;
  1908. begin
  1909. needs_inittable:=true;
  1910. end;
  1911. function tvariantdef.is_publishable : boolean;
  1912. begin
  1913. is_publishable:=true;
  1914. end;
  1915. {****************************************************************************
  1916. TABSTRACtpointerdef
  1917. ****************************************************************************}
  1918. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  1919. begin
  1920. inherited create(dt);
  1921. pointeddef:=def;
  1922. savesize:=sizeof(pint);
  1923. end;
  1924. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1925. begin
  1926. inherited ppuload(dt,ppufile);
  1927. ppufile.getderef(pointeddefderef);
  1928. savesize:=sizeof(pint);
  1929. end;
  1930. procedure tabstractpointerdef.buildderef;
  1931. begin
  1932. inherited buildderef;
  1933. pointeddefderef.build(pointeddef);
  1934. end;
  1935. procedure tabstractpointerdef.deref;
  1936. begin
  1937. inherited deref;
  1938. pointeddef:=tdef(pointeddefderef.resolve);
  1939. end;
  1940. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1941. begin
  1942. inherited ppuwrite(ppufile);
  1943. ppufile.putderef(pointeddefderef);
  1944. end;
  1945. {****************************************************************************
  1946. tpointerdef
  1947. ****************************************************************************}
  1948. constructor tpointerdef.create(def:tdef);
  1949. begin
  1950. inherited create(pointerdef,def);
  1951. is_far:=false;
  1952. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  1953. end;
  1954. constructor tpointerdef.createfar(def:tdef);
  1955. begin
  1956. inherited create(pointerdef,def);
  1957. is_far:=true;
  1958. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  1959. end;
  1960. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  1961. begin
  1962. inherited ppuload(pointerdef,ppufile);
  1963. is_far:=(ppufile.getbyte<>0);
  1964. has_pointer_math:=(ppufile.getbyte<>0);
  1965. end;
  1966. function tpointerdef.getcopy : tstoreddef;
  1967. begin
  1968. { don't use direct pointeddef if it is a forwarddef because in other case
  1969. one of them will be destroyed on forward type resolve and the second will
  1970. point to garbage }
  1971. if pointeddef.typ=forwarddef then
  1972. result:=tpointerdef.create(tforwarddef(pointeddef).getcopy)
  1973. else
  1974. result:=tpointerdef.create(pointeddef);
  1975. tpointerdef(result).is_far:=is_far;
  1976. tpointerdef(result).has_pointer_math:=has_pointer_math;
  1977. tpointerdef(result).savesize:=savesize;
  1978. end;
  1979. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1980. begin
  1981. inherited ppuwrite(ppufile);
  1982. ppufile.putbyte(byte(is_far));
  1983. ppufile.putbyte(byte(has_pointer_math));
  1984. ppufile.writeentry(ibpointerdef);
  1985. end;
  1986. function tpointerdef.GetTypeName : string;
  1987. begin
  1988. if is_far then
  1989. GetTypeName:='^'+pointeddef.typename+';far'
  1990. else
  1991. GetTypeName:='^'+pointeddef.typename;
  1992. end;
  1993. {****************************************************************************
  1994. TCLASSREFDEF
  1995. ****************************************************************************}
  1996. constructor tclassrefdef.create(def:tdef);
  1997. begin
  1998. inherited create(classrefdef,def);
  1999. end;
  2000. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  2001. begin
  2002. inherited ppuload(classrefdef,ppufile);
  2003. end;
  2004. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  2005. begin
  2006. inherited ppuwrite(ppufile);
  2007. ppufile.writeentry(ibclassrefdef);
  2008. end;
  2009. function tclassrefdef.getcopy:tstoreddef;
  2010. begin
  2011. if pointeddef.typ=forwarddef then
  2012. result:=tclassrefdef.create(tforwarddef(pointeddef).getcopy)
  2013. else
  2014. result:=tclassrefdef.create(pointeddef);
  2015. tclassrefdef(result).savesize:=savesize;
  2016. end;
  2017. function tclassrefdef.GetTypeName : string;
  2018. begin
  2019. GetTypeName:='Class Of '+pointeddef.typename;
  2020. end;
  2021. function tclassrefdef.is_publishable : boolean;
  2022. begin
  2023. result:=true;
  2024. end;
  2025. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  2026. begin
  2027. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  2028. result:=inherited rtti_mangledname(rt)
  2029. else
  2030. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  2031. end;
  2032. procedure tclassrefdef.register_created_object_type;
  2033. begin
  2034. tobjectdef(pointeddef).register_created_classref_type;
  2035. end;
  2036. {***************************************************************************
  2037. TSETDEF
  2038. ***************************************************************************}
  2039. constructor tsetdef.create(def:tdef;low, high : asizeint);
  2040. var
  2041. setallocbits: aint;
  2042. packedsavesize: aint;
  2043. begin
  2044. inherited create(setdef);
  2045. elementdef:=def;
  2046. setmax:=high;
  2047. if (current_settings.setalloc=0) then
  2048. begin
  2049. setbase:=0;
  2050. if (high<32) then
  2051. savesize:=Sizeof(longint)
  2052. else if (high<256) then
  2053. savesize:=32
  2054. else
  2055. savesize:=(high+7) div 8
  2056. end
  2057. else
  2058. begin
  2059. setallocbits:=current_settings.setalloc*8;
  2060. setbase:=low and not(setallocbits-1);
  2061. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  2062. savesize:=packedsavesize;
  2063. if savesize=3 then
  2064. savesize:=4;
  2065. end;
  2066. end;
  2067. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  2068. begin
  2069. inherited ppuload(setdef,ppufile);
  2070. ppufile.getderef(elementdefderef);
  2071. savesize:=ppufile.getaint;
  2072. setbase:=ppufile.getaint;
  2073. setmax:=ppufile.getaint;
  2074. end;
  2075. function tsetdef.getcopy : tstoreddef;
  2076. begin
  2077. result:=tsetdef.create(elementdef,setbase,setmax);
  2078. { the copy might have been created with a different setalloc setting }
  2079. tsetdef(result).savesize:=savesize;
  2080. end;
  2081. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  2082. begin
  2083. inherited ppuwrite(ppufile);
  2084. ppufile.putderef(elementdefderef);
  2085. ppufile.putaint(savesize);
  2086. ppufile.putaint(setbase);
  2087. ppufile.putaint(setmax);
  2088. ppufile.writeentry(ibsetdef);
  2089. end;
  2090. procedure tsetdef.buildderef;
  2091. begin
  2092. inherited buildderef;
  2093. elementdefderef.build(elementdef);
  2094. end;
  2095. procedure tsetdef.deref;
  2096. begin
  2097. inherited deref;
  2098. elementdef:=tdef(elementdefderef.resolve);
  2099. end;
  2100. function tsetdef.is_publishable : boolean;
  2101. begin
  2102. is_publishable:=savesize in [1,2,4];
  2103. end;
  2104. function tsetdef.GetTypeName : string;
  2105. begin
  2106. if assigned(elementdef) then
  2107. GetTypeName:='Set Of '+elementdef.typename
  2108. else
  2109. GetTypeName:='Empty Set';
  2110. end;
  2111. {***************************************************************************
  2112. TFORMALDEF
  2113. ***************************************************************************}
  2114. constructor tformaldef.create(Atyped:boolean);
  2115. begin
  2116. inherited create(formaldef);
  2117. typed:=Atyped;
  2118. savesize:=0;
  2119. end;
  2120. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  2121. begin
  2122. inherited ppuload(formaldef,ppufile);
  2123. typed:=boolean(ppufile.getbyte);
  2124. savesize:=0;
  2125. end;
  2126. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  2127. begin
  2128. inherited ppuwrite(ppufile);
  2129. ppufile.putbyte(byte(typed));
  2130. ppufile.writeentry(ibformaldef);
  2131. end;
  2132. function tformaldef.GetTypeName : string;
  2133. begin
  2134. if typed then
  2135. GetTypeName:='<Typed formal type>'
  2136. else
  2137. GetTypeName:='<Formal type>';
  2138. end;
  2139. {***************************************************************************
  2140. TARRAYDEF
  2141. ***************************************************************************}
  2142. constructor tarraydef.create(l,h:asizeint;def:tdef);
  2143. begin
  2144. inherited create(arraydef);
  2145. lowrange:=l;
  2146. highrange:=h;
  2147. rangedef:=def;
  2148. _elementdef:=nil;
  2149. arrayoptions:=[];
  2150. symtable:=tarraysymtable.create(self);
  2151. end;
  2152. destructor tarraydef.destroy;
  2153. begin
  2154. symtable.free;
  2155. symtable:=nil;
  2156. inherited;
  2157. end;
  2158. constructor tarraydef.create_from_pointer(def:tdef);
  2159. begin
  2160. { use -1 so that the elecount will not overflow }
  2161. self.create(0,high(aint)-1,s32inttype);
  2162. arrayoptions:=[ado_IsConvertedPointer];
  2163. setelementdef(def);
  2164. end;
  2165. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  2166. begin
  2167. inherited ppuload(arraydef,ppufile);
  2168. { the addresses are calculated later }
  2169. ppufile.getderef(_elementdefderef);
  2170. ppufile.getderef(rangedefderef);
  2171. lowrange:=ppufile.getaint;
  2172. highrange:=ppufile.getaint;
  2173. ppufile.getsmallset(arrayoptions);
  2174. symtable:=tarraysymtable.create(self);
  2175. tarraysymtable(symtable).ppuload(ppufile)
  2176. end;
  2177. function tarraydef.getcopy : tstoreddef;
  2178. begin
  2179. result:=tarraydef.create(lowrange,highrange,rangedef);
  2180. tarraydef(result).arrayoptions:=arrayoptions;
  2181. tarraydef(result)._elementdef:=_elementdef;
  2182. end;
  2183. procedure tarraydef.buildderef;
  2184. begin
  2185. inherited buildderef;
  2186. tarraysymtable(symtable).buildderef;
  2187. _elementdefderef.build(_elementdef);
  2188. rangedefderef.build(rangedef);
  2189. end;
  2190. procedure tarraydef.deref;
  2191. begin
  2192. inherited deref;
  2193. tarraysymtable(symtable).deref;
  2194. _elementdef:=tdef(_elementdefderef.resolve);
  2195. rangedef:=tdef(rangedefderef.resolve);
  2196. end;
  2197. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  2198. begin
  2199. inherited ppuwrite(ppufile);
  2200. ppufile.putderef(_elementdefderef);
  2201. ppufile.putderef(rangedefderef);
  2202. ppufile.putaint(lowrange);
  2203. ppufile.putaint(highrange);
  2204. ppufile.putsmallset(arrayoptions);
  2205. ppufile.writeentry(ibarraydef);
  2206. tarraysymtable(symtable).ppuwrite(ppufile);
  2207. end;
  2208. function tarraydef.elesize : asizeint;
  2209. begin
  2210. if (ado_IsBitPacked in arrayoptions) then
  2211. internalerror(2006080101);
  2212. if assigned(_elementdef) then
  2213. result:=_elementdef.size
  2214. else
  2215. result:=0;
  2216. end;
  2217. function tarraydef.elepackedbitsize : asizeint;
  2218. begin
  2219. if not(ado_IsBitPacked in arrayoptions) then
  2220. internalerror(2006080102);
  2221. if assigned(_elementdef) then
  2222. result:=_elementdef.packedbitsize
  2223. else
  2224. result:=0;
  2225. end;
  2226. function tarraydef.elecount : asizeuint;
  2227. var
  2228. qhigh,qlow : qword;
  2229. begin
  2230. if ado_IsDynamicArray in arrayoptions then
  2231. begin
  2232. result:=0;
  2233. exit;
  2234. end;
  2235. if (highrange>0) and (lowrange<0) then
  2236. begin
  2237. qhigh:=highrange;
  2238. qlow:=qword(-lowrange);
  2239. { prevent overflow, return 0 to indicate overflow }
  2240. if qhigh+qlow>qword(high(asizeint)-1) then
  2241. result:=0
  2242. else
  2243. result:=qhigh+qlow+1;
  2244. end
  2245. else
  2246. result:=int64(highrange)-lowrange+1;
  2247. end;
  2248. function tarraydef.size : asizeint;
  2249. var
  2250. cachedelecount : asizeuint;
  2251. cachedelesize : asizeint;
  2252. begin
  2253. if ado_IsDynamicArray in arrayoptions then
  2254. begin
  2255. size:=sizeof(pint);
  2256. exit;
  2257. end;
  2258. { Tarraydef.size may never be called for an open array! }
  2259. if highrange<lowrange then
  2260. internalerror(99080501);
  2261. if not (ado_IsBitPacked in arrayoptions) then
  2262. cachedelesize:=elesize
  2263. else
  2264. cachedelesize := elepackedbitsize;
  2265. cachedelecount:=elecount;
  2266. if (cachedelesize = 0) then
  2267. begin
  2268. size := 0;
  2269. exit;
  2270. end;
  2271. if (cachedelecount = 0) then
  2272. begin
  2273. size := -1;
  2274. exit;
  2275. end;
  2276. { prevent overflow, return -1 to indicate overflow }
  2277. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2278. if (cachedelecount > asizeuint(high(asizeint))) or
  2279. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  2280. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  2281. accessing the array, see ncgmem (PFV) }
  2282. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  2283. begin
  2284. result:=-1;
  2285. exit;
  2286. end;
  2287. result:=cachedelesize*asizeint(cachedelecount);
  2288. if (ado_IsBitPacked in arrayoptions) then
  2289. { can't just add 7 and divide by 8, because that may overflow }
  2290. result:=result div 8 + ord((result mod 8)<>0);
  2291. end;
  2292. procedure tarraydef.setelementdef(def:tdef);
  2293. begin
  2294. _elementdef:=def;
  2295. if not(
  2296. (ado_IsDynamicArray in arrayoptions) or
  2297. (ado_IsConvertedPointer in arrayoptions) or
  2298. (highrange<lowrange)
  2299. ) and
  2300. (size=-1) then
  2301. Message(sym_e_segment_too_large);
  2302. end;
  2303. function tarraydef.alignment : shortint;
  2304. begin
  2305. { alignment of dyn. arrays doesn't depend on the element size }
  2306. if (ado_IsDynamicArray in arrayoptions) then
  2307. alignment:=size_2_align(sizeof(pint))
  2308. { alignment is the alignment of the elements }
  2309. else if (elementdef.typ in [arraydef,recorddef,orddef,enumdef,floatdef]) or
  2310. ((elementdef.typ=objectdef) and
  2311. is_object(elementdef)) then
  2312. alignment:=elementdef.alignment
  2313. { alignment is the size of the elements }
  2314. else if not (ado_IsBitPacked in arrayoptions) then
  2315. alignment:=size_2_align(elesize)
  2316. else
  2317. alignment:=packedbitsloadsize(elepackedbitsize);
  2318. end;
  2319. function tarraydef.needs_inittable : boolean;
  2320. begin
  2321. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2322. end;
  2323. function tarraydef.GetTypeName : string;
  2324. begin
  2325. if (ado_IsConstString in arrayoptions) then
  2326. result:='Constant String'
  2327. else if (ado_isarrayofconst in arrayoptions) or
  2328. (ado_isConstructor in arrayoptions) then
  2329. begin
  2330. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2331. GetTypeName:='Array Of Const'
  2332. else
  2333. GetTypeName:='Array Of Const/Constant Open Array of '+elementdef.typename;
  2334. end
  2335. else if (ado_IsDynamicArray in arrayoptions) then
  2336. GetTypeName:='Dynamic Array Of '+elementdef.typename
  2337. else if ((highrange=-1) and (lowrange=0)) then
  2338. GetTypeName:='Open Array Of '+elementdef.typename
  2339. else
  2340. begin
  2341. result := '';
  2342. if (ado_IsBitPacked in arrayoptions) then
  2343. result:='Packed ';
  2344. if rangedef.typ=enumdef then
  2345. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2346. else
  2347. result:=result+'Array['+tostr(lowrange)+'..'+
  2348. tostr(highrange)+'] Of '+elementdef.typename
  2349. end;
  2350. end;
  2351. function tarraydef.getmangledparaname : string;
  2352. begin
  2353. if ado_isarrayofconst in arrayoptions then
  2354. getmangledparaname:='array_of_const'
  2355. else
  2356. if ((highrange=-1) and (lowrange=0)) then
  2357. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2358. else
  2359. internalerror(200204176);
  2360. end;
  2361. function tarraydef.is_publishable : boolean;
  2362. begin
  2363. Result:=ado_IsDynamicArray in arrayoptions;
  2364. end;
  2365. {***************************************************************************
  2366. tabstractrecorddef
  2367. ***************************************************************************}
  2368. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp);
  2369. begin
  2370. inherited create(dt);
  2371. objname:=stringdup(upper(n));
  2372. objrealname:=stringdup(n);
  2373. objectoptions:=[];
  2374. end;
  2375. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2376. begin
  2377. inherited ppuload(dt,ppufile);
  2378. objrealname:=stringdup(ppufile.getstring);
  2379. objname:=stringdup(upper(objrealname^));
  2380. ppufile.getsmallset(objectoptions);
  2381. end;
  2382. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  2383. begin
  2384. inherited ppuwrite(ppufile);
  2385. ppufile.putstring(objrealname^);
  2386. ppufile.putsmallset(objectoptions);
  2387. end;
  2388. destructor tabstractrecorddef.destroy;
  2389. begin
  2390. stringdispose(objname);
  2391. stringdispose(objrealname);
  2392. inherited destroy;
  2393. end;
  2394. procedure tabstractrecorddef.check_forwards;
  2395. begin
  2396. tstoredsymtable(symtable).check_forwards;
  2397. end;
  2398. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  2399. var
  2400. i: longint;
  2401. sym: tsym;
  2402. begin
  2403. for i:=0 to symtable.SymList.Count-1 do
  2404. begin
  2405. sym:=tsym(symtable.SymList[i]);
  2406. if sym.typ=procsym then
  2407. begin
  2408. result:=tprocsym(sym).find_procdef_bytype(pt);
  2409. if assigned(result) then
  2410. exit;
  2411. end;
  2412. end;
  2413. result:=nil;
  2414. end;
  2415. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2416. begin
  2417. if t=gs_record then
  2418. GetSymtable:=symtable
  2419. else
  2420. GetSymtable:=nil;
  2421. end;
  2422. function tabstractrecorddef.is_packed:boolean;
  2423. begin
  2424. result:=tabstractrecordsymtable(symtable).is_packed;
  2425. end;
  2426. function tabstractrecorddef.RttiName: string;
  2427. begin
  2428. Result:=OwnerHierarchyName+objrealname^;
  2429. end;
  2430. function tabstractrecorddef.search_enumerator_get: tprocdef;
  2431. var
  2432. sym : tsym;
  2433. i : integer;
  2434. pd : tprocdef;
  2435. hashedid : THashedIDString;
  2436. begin
  2437. result:=nil;
  2438. hashedid.id:='GETENUMERATOR';
  2439. sym:=tsym(symtable.FindWithHash(hashedid));
  2440. if assigned(sym) and (sym.typ=procsym) then
  2441. begin
  2442. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  2443. begin
  2444. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  2445. if (pd.proctypeoption = potype_function) and
  2446. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  2447. (pd.visibility >= vis_public) then
  2448. begin
  2449. result:=pd;
  2450. exit;
  2451. end;
  2452. end;
  2453. end;
  2454. end;
  2455. function tabstractrecorddef.search_enumerator_move: tprocdef;
  2456. var
  2457. sym : tsym;
  2458. i : integer;
  2459. pd : tprocdef;
  2460. hashedid : THashedIDString;
  2461. begin
  2462. result:=nil;
  2463. // first search for po_enumerator_movenext method modifier
  2464. // then search for public function MoveNext: Boolean
  2465. for i:=0 to symtable.SymList.Count-1 do
  2466. begin
  2467. sym:=TSym(symtable.SymList[i]);
  2468. if (sym.typ=procsym) then
  2469. begin
  2470. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  2471. if assigned(pd) then
  2472. begin
  2473. result:=pd;
  2474. exit;
  2475. end;
  2476. end;
  2477. end;
  2478. hashedid.id:='MOVENEXT';
  2479. sym:=tsym(symtable.FindWithHash(hashedid));
  2480. if assigned(sym) and (sym.typ=procsym) then
  2481. begin
  2482. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  2483. begin
  2484. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  2485. if (pd.proctypeoption = potype_function) and
  2486. is_boolean(pd.returndef) and
  2487. (pd.minparacount = 0) and
  2488. (pd.visibility >= vis_public) then
  2489. begin
  2490. result:=pd;
  2491. exit;
  2492. end;
  2493. end;
  2494. end;
  2495. end;
  2496. function tabstractrecorddef.search_enumerator_current: tsym;
  2497. var
  2498. sym: tsym;
  2499. i: integer;
  2500. hashedid : THashedIDString;
  2501. begin
  2502. result:=nil;
  2503. // first search for ppo_enumerator_current property modifier
  2504. // then search for public property Current
  2505. for i:=0 to symtable.SymList.Count-1 do
  2506. begin
  2507. sym:=TSym(symtable.SymList[i]);
  2508. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  2509. begin
  2510. result:=sym;
  2511. exit;
  2512. end;
  2513. end;
  2514. hashedid.id:='CURRENT';
  2515. sym:=tsym(symtable.FindWithHash(hashedid));
  2516. if assigned(sym) and (sym.typ=propertysym) and
  2517. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  2518. begin
  2519. result:=sym;
  2520. exit;
  2521. end;
  2522. end;
  2523. {***************************************************************************
  2524. trecorddef
  2525. ***************************************************************************}
  2526. constructor trecorddef.create(const n:string; p:TSymtable);
  2527. begin
  2528. inherited create(n,recorddef);
  2529. symtable:=p;
  2530. { we can own the symtable only if nobody else owns a copy so far }
  2531. if symtable.refcount=1 then
  2532. symtable.defowner:=self;
  2533. isunion:=false;
  2534. end;
  2535. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2536. begin
  2537. inherited ppuload(recorddef,ppufile);
  2538. if df_copied_def in defoptions then
  2539. ppufile.getderef(cloneddefderef)
  2540. else
  2541. begin
  2542. symtable:=trecordsymtable.create(objrealname^,0);
  2543. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2544. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2545. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2546. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2547. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2548. trecordsymtable(symtable).ppuload(ppufile);
  2549. { requires usefieldalignment to be set }
  2550. symtable.defowner:=self;
  2551. end;
  2552. isunion:=false;
  2553. end;
  2554. destructor trecorddef.destroy;
  2555. begin
  2556. if assigned(symtable) then
  2557. begin
  2558. symtable.free;
  2559. symtable:=nil;
  2560. end;
  2561. inherited destroy;
  2562. end;
  2563. function trecorddef.getcopy : tstoreddef;
  2564. begin
  2565. result:=trecorddef.create(objrealname^,symtable.getcopy);
  2566. trecorddef(result).isunion:=isunion;
  2567. include(trecorddef(result).defoptions,df_copied_def);
  2568. end;
  2569. function trecorddef.needs_inittable : boolean;
  2570. begin
  2571. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2572. end;
  2573. procedure trecorddef.buildderef;
  2574. begin
  2575. inherited buildderef;
  2576. if df_copied_def in defoptions then
  2577. cloneddefderef.build(symtable.defowner)
  2578. else
  2579. tstoredsymtable(symtable).buildderef;
  2580. end;
  2581. procedure trecorddef.deref;
  2582. begin
  2583. inherited deref;
  2584. { now dereference the definitions }
  2585. if df_copied_def in defoptions then
  2586. begin
  2587. cloneddef:=trecorddef(cloneddefderef.resolve);
  2588. symtable:=cloneddef.symtable.getcopy;
  2589. end
  2590. else
  2591. tstoredsymtable(symtable).deref;
  2592. { assign TGUID? load only from system unit }
  2593. if not(assigned(rec_tguid)) and
  2594. (upper(typename)='TGUID') and
  2595. assigned(owner) and
  2596. assigned(owner.name) and
  2597. (owner.name^='SYSTEM') then
  2598. rec_tguid:=self;
  2599. { assign JMP_BUF? load only from system unit }
  2600. if not(assigned(rec_jmp_buf)) and
  2601. (upper(typename)='JMP_BUF') and
  2602. assigned(owner) and
  2603. assigned(owner.name) and
  2604. (owner.name^='SYSTEM') then
  2605. rec_jmp_buf:=self;
  2606. end;
  2607. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2608. begin
  2609. inherited ppuwrite(ppufile);
  2610. if df_copied_def in defoptions then
  2611. ppufile.putderef(cloneddefderef)
  2612. else
  2613. begin
  2614. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2615. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2616. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2617. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2618. ppufile.putaint(trecordsymtable(symtable).datasize);
  2619. end;
  2620. ppufile.writeentry(ibrecorddef);
  2621. if not(df_copied_def in defoptions) then
  2622. trecordsymtable(symtable).ppuwrite(ppufile);
  2623. end;
  2624. function trecorddef.size:asizeint;
  2625. begin
  2626. result:=trecordsymtable(symtable).datasize;
  2627. end;
  2628. function trecorddef.alignment:shortint;
  2629. begin
  2630. alignment:=trecordsymtable(symtable).recordalignment;
  2631. end;
  2632. function trecorddef.padalignment:shortint;
  2633. begin
  2634. padalignment := trecordsymtable(symtable).padalignment;
  2635. end;
  2636. function trecorddef.GetTypeName : string;
  2637. begin
  2638. GetTypeName:='<record type>'
  2639. end;
  2640. {***************************************************************************
  2641. TABSTRACTPROCDEF
  2642. ***************************************************************************}
  2643. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2644. begin
  2645. inherited create(dt);
  2646. parast:=tparasymtable.create(self,level);
  2647. paras:=nil;
  2648. minparacount:=0;
  2649. maxparacount:=0;
  2650. proctypeoption:=potype_none;
  2651. proccalloption:=pocall_none;
  2652. procoptions:=[];
  2653. returndef:=voidtype;
  2654. savesize:=sizeof(pint);
  2655. callerargareasize:=0;
  2656. calleeargareasize:=0;
  2657. has_paraloc_info:=callnoside;
  2658. funcretloc[callerside].init;
  2659. funcretloc[calleeside].init;
  2660. check_mark_as_nested;
  2661. end;
  2662. destructor tabstractprocdef.destroy;
  2663. begin
  2664. if assigned(paras) then
  2665. begin
  2666. {$ifdef MEMDEBUG}
  2667. memprocpara.start;
  2668. {$endif MEMDEBUG}
  2669. paras.free;
  2670. paras:=nil;
  2671. {$ifdef MEMDEBUG}
  2672. memprocpara.stop;
  2673. {$endif MEMDEBUG}
  2674. end;
  2675. if assigned(parast) then
  2676. begin
  2677. {$ifdef MEMDEBUG}
  2678. memprocparast.start;
  2679. {$endif MEMDEBUG}
  2680. parast.free;
  2681. parast:=nil;
  2682. {$ifdef MEMDEBUG}
  2683. memprocparast.stop;
  2684. {$endif MEMDEBUG}
  2685. end;
  2686. funcretloc[callerside].done;
  2687. funcretloc[calleeside].done;
  2688. inherited destroy;
  2689. end;
  2690. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2691. begin
  2692. if (tsym(p).typ<>paravarsym) then
  2693. exit;
  2694. inc(plongint(arg)^);
  2695. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2696. begin
  2697. if not assigned(tparavarsym(p).defaultconstsym) then
  2698. inc(minparacount);
  2699. inc(maxparacount);
  2700. end;
  2701. end;
  2702. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  2703. begin
  2704. if (tsym(p).typ<>paravarsym) then
  2705. exit;
  2706. paras.add(p);
  2707. end;
  2708. procedure tabstractprocdef.calcparas;
  2709. var
  2710. paracount : longint;
  2711. begin
  2712. { This can already be assigned when
  2713. we need to reresolve this unit (PFV) }
  2714. if assigned(paras) then
  2715. paras.free;
  2716. paras:=tparalist.create(false);
  2717. paracount:=0;
  2718. minparacount:=0;
  2719. maxparacount:=0;
  2720. parast.SymList.ForEachCall(@count_para,@paracount);
  2721. paras.capacity:=paracount;
  2722. { Insert parameters in table }
  2723. parast.SymList.ForEachCall(@insert_para,nil);
  2724. { Order parameters }
  2725. paras.sortparas;
  2726. end;
  2727. procedure tabstractprocdef.buildderef;
  2728. begin
  2729. { released procdef? }
  2730. if not assigned(parast) then
  2731. exit;
  2732. inherited buildderef;
  2733. returndefderef.build(returndef);
  2734. { parast }
  2735. tparasymtable(parast).buildderef;
  2736. end;
  2737. procedure tabstractprocdef.deref;
  2738. begin
  2739. inherited deref;
  2740. returndef:=tdef(returndefderef.resolve);
  2741. { parast }
  2742. tparasymtable(parast).deref;
  2743. { recalculated parameters }
  2744. calcparas;
  2745. end;
  2746. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2747. begin
  2748. inherited ppuload(dt,ppufile);
  2749. parast:=nil;
  2750. Paras:=nil;
  2751. minparacount:=0;
  2752. maxparacount:=0;
  2753. ppufile.getderef(returndefderef);
  2754. { TODO: remove fpu_used loading}
  2755. ppufile.getbyte;
  2756. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2757. proccalloption:=tproccalloption(ppufile.getbyte);
  2758. ppufile.getnormalset(procoptions);
  2759. funcretloc[callerside].init;
  2760. if po_explicitparaloc in procoptions then
  2761. funcretloc[callerside].ppuload(ppufile);
  2762. savesize:=sizeof(pint);
  2763. if (po_explicitparaloc in procoptions) then
  2764. has_paraloc_info:=callerside;
  2765. end;
  2766. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2767. var
  2768. oldintfcrc : boolean;
  2769. begin
  2770. { released procdef? }
  2771. if not assigned(parast) then
  2772. exit;
  2773. inherited ppuwrite(ppufile);
  2774. ppufile.putderef(returndefderef);
  2775. oldintfcrc:=ppufile.do_interface_crc;
  2776. ppufile.do_interface_crc:=false;
  2777. ppufile.putbyte(0);
  2778. ppufile.putbyte(ord(proctypeoption));
  2779. ppufile.putbyte(ord(proccalloption));
  2780. ppufile.putnormalset(procoptions);
  2781. ppufile.do_interface_crc:=oldintfcrc;
  2782. if (po_explicitparaloc in procoptions) then
  2783. funcretloc[callerside].ppuwrite(ppufile);
  2784. end;
  2785. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2786. var
  2787. hs,s : string;
  2788. hp : TParavarsym;
  2789. hpc : tconstsym;
  2790. first : boolean;
  2791. i : integer;
  2792. begin
  2793. s:='';
  2794. first:=true;
  2795. for i:=0 to paras.count-1 do
  2796. begin
  2797. hp:=tparavarsym(paras[i]);
  2798. if not(vo_is_hidden_para in hp.varoptions) or
  2799. (showhidden) then
  2800. begin
  2801. if first then
  2802. begin
  2803. s:=s+'(';
  2804. first:=false;
  2805. end
  2806. else
  2807. s:=s+',';
  2808. if vo_is_hidden_para in hp.varoptions then
  2809. s:=s+'<';
  2810. case hp.varspez of
  2811. vs_var :
  2812. s:=s+'var ';
  2813. vs_const :
  2814. s:=s+'const ';
  2815. vs_out :
  2816. s:=s+'out ';
  2817. vs_constref :
  2818. s:=s+'constref ';
  2819. end;
  2820. if hp.univpara then
  2821. s:=s+'univ ';
  2822. if assigned(hp.vardef.typesym) then
  2823. begin
  2824. hs:=hp.vardef.typesym.realname;
  2825. if hs[1]<>'$' then
  2826. s:=s+hs
  2827. else
  2828. s:=s+hp.vardef.GetTypeName;
  2829. end
  2830. else
  2831. s:=s+hp.vardef.GetTypeName;
  2832. { default value }
  2833. if assigned(hp.defaultconstsym) then
  2834. begin
  2835. hpc:=tconstsym(hp.defaultconstsym);
  2836. hs:='';
  2837. case hpc.consttyp of
  2838. conststring,
  2839. constresourcestring :
  2840. begin
  2841. If hpc.value.len>0 then
  2842. begin
  2843. setLength(hs,hpc.value.len);
  2844. { don't write past the end of hs if the constant
  2845. is > 255 chars }
  2846. move(hpc.value.valueptr^,hs[1],length(hs));
  2847. { make sure that constant strings with newline chars
  2848. don't create a linebreak in the assembler code,
  2849. since comments are line-based. Also remove nulls
  2850. because the comments are written as a pchar. }
  2851. ReplaceCase(hs,#0,'.');
  2852. ReplaceCase(hs,#10,'.');
  2853. ReplaceCase(hs,#13,'.');
  2854. end;
  2855. end;
  2856. constreal :
  2857. str(pbestreal(hpc.value.valueptr)^,hs);
  2858. constpointer :
  2859. hs:=tostr(hpc.value.valueordptr);
  2860. constord :
  2861. begin
  2862. if is_boolean(hpc.constdef) then
  2863. begin
  2864. if hpc.value.valueord<>0 then
  2865. hs:='TRUE'
  2866. else
  2867. hs:='FALSE';
  2868. end
  2869. else
  2870. hs:=tostr(hpc.value.valueord);
  2871. end;
  2872. constnil :
  2873. hs:='nil';
  2874. constset :
  2875. hs:='<set>';
  2876. end;
  2877. if hs<>'' then
  2878. s:=s+'="'+hs+'"';
  2879. end;
  2880. if vo_is_hidden_para in hp.varoptions then
  2881. s:=s+'>';
  2882. end;
  2883. end;
  2884. if not first then
  2885. s:=s+')';
  2886. if (po_varargs in procoptions) then
  2887. s:=s+';VarArgs';
  2888. typename_paras:=s;
  2889. end;
  2890. function tabstractprocdef.is_methodpointer:boolean;
  2891. begin
  2892. result:=false;
  2893. end;
  2894. function tabstractprocdef.is_addressonly:boolean;
  2895. begin
  2896. result:=true;
  2897. end;
  2898. function tabstractprocdef.no_self_node: boolean;
  2899. begin
  2900. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  2901. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  2902. end;
  2903. procedure tabstractprocdef.check_mark_as_nested;
  2904. begin
  2905. { nested procvars require that nested functions use the Delphi-style
  2906. nested procedure calling convention }
  2907. if (parast.symtablelevel>normal_function_level) and
  2908. (m_nested_procvars in current_settings.modeswitches) then
  2909. include(procoptions,po_delphi_nested_cc);
  2910. end;
  2911. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  2912. begin
  2913. if (side in [callerside,callbothsides]) and
  2914. not(has_paraloc_info in [callerside,callbothsides]) then
  2915. begin
  2916. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  2917. if has_paraloc_info in [calleeside,callbothsides] then
  2918. has_paraloc_info:=callbothsides
  2919. else
  2920. has_paraloc_info:=callerside;
  2921. end;
  2922. if (side in [calleeside,callbothsides]) and
  2923. not(has_paraloc_info in [calleeside,callbothsides]) then
  2924. begin
  2925. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  2926. if has_paraloc_info in [callerside,callbothsides] then
  2927. has_paraloc_info:=callbothsides
  2928. else
  2929. has_paraloc_info:=calleeside;
  2930. end;
  2931. end;
  2932. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  2933. var
  2934. p: tparavarsym;
  2935. ploc: PCGParalocation;
  2936. i: longint;
  2937. begin
  2938. result:=false;
  2939. init_paraloc_info(side);
  2940. for i:=0 to parast.SymList.Count-1 do
  2941. if tsym(parast.SymList[i]).typ=paravarsym then
  2942. begin
  2943. p:=tparavarsym(parast.SymList[i]);
  2944. { check if no parameter is located on the stack }
  2945. if is_open_array(p.vardef) or
  2946. is_array_of_const(p.vardef) then
  2947. begin
  2948. result:=true;
  2949. exit;
  2950. end;
  2951. ploc:=p.paraloc[side].location;
  2952. while assigned(ploc) do
  2953. begin
  2954. if (ploc^.loc=LOC_REFERENCE) then
  2955. begin
  2956. result:=true;
  2957. exit
  2958. end;
  2959. ploc:=ploc^.next;
  2960. end;
  2961. end;
  2962. end;
  2963. {***************************************************************************
  2964. TPROCDEF
  2965. ***************************************************************************}
  2966. constructor tprocdef.create(level:byte);
  2967. begin
  2968. inherited create(procdef,level);
  2969. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  2970. _mangledname:=nil;
  2971. fileinfo:=current_filepos;
  2972. extnumber:=$ffff;
  2973. aliasnames:=TCmdStrList.create;
  2974. funcretsym:=nil;
  2975. forwarddef:=true;
  2976. interfacedef:=false;
  2977. hasforward:=false;
  2978. struct := nil;
  2979. import_dll:=nil;
  2980. import_name:=nil;
  2981. import_nr:=0;
  2982. inlininginfo:=nil;
  2983. deprecatedmsg:=nil;
  2984. {$ifdef i386}
  2985. fpu_used:=maxfpuregs;
  2986. {$endif i386}
  2987. interruptvector:=-1;
  2988. end;
  2989. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2990. var
  2991. i,aliasnamescount : longint;
  2992. level : byte;
  2993. begin
  2994. inherited ppuload(procdef,ppufile);
  2995. if po_has_mangledname in procoptions then
  2996. _mangledname:=stringdup(ppufile.getstring)
  2997. else
  2998. _mangledname:=nil;
  2999. extnumber:=ppufile.getword;
  3000. level:=ppufile.getbyte;
  3001. ppufile.getderef(structderef);
  3002. ppufile.getderef(procsymderef);
  3003. ppufile.getposinfo(fileinfo);
  3004. visibility:=tvisibility(ppufile.getbyte);
  3005. ppufile.getsmallset(symoptions);
  3006. if sp_has_deprecated_msg in symoptions then
  3007. deprecatedmsg:=stringdup(ppufile.getstring)
  3008. else
  3009. deprecatedmsg:=nil;
  3010. {$ifdef powerpc}
  3011. { library symbol for AmigaOS/MorphOS }
  3012. ppufile.getderef(libsymderef);
  3013. {$endif powerpc}
  3014. { import stuff }
  3015. if po_has_importdll in procoptions then
  3016. import_dll:=stringdup(ppufile.getstring)
  3017. else
  3018. import_dll:=nil;
  3019. if po_has_importname in procoptions then
  3020. import_name:=stringdup(ppufile.getstring)
  3021. else
  3022. import_name:=nil;
  3023. import_nr:=ppufile.getword;
  3024. if target_info.system in systems_interrupt_table then
  3025. interruptvector:=ppufile.getlongint;
  3026. if (po_msgint in procoptions) then
  3027. messageinf.i:=ppufile.getlongint;
  3028. if (po_msgstr in procoptions) then
  3029. messageinf.str:=stringdup(ppufile.getstring);
  3030. if (po_dispid in procoptions) then
  3031. dispid:=ppufile.getlongint;
  3032. { inline stuff }
  3033. if (po_has_inlininginfo in procoptions) then
  3034. begin
  3035. ppufile.getderef(funcretsymderef);
  3036. new(inlininginfo);
  3037. ppufile.getsmallset(inlininginfo^.flags);
  3038. end
  3039. else
  3040. begin
  3041. inlininginfo:=nil;
  3042. funcretsym:=nil;
  3043. end;
  3044. aliasnames:=TCmdStrList.create;
  3045. { count alias names }
  3046. aliasnamescount:=ppufile.getbyte;
  3047. for i:=1 to aliasnamescount do
  3048. aliasnames.insert(ppufile.getstring);
  3049. { load para symtable }
  3050. parast:=tparasymtable.create(self,level);
  3051. tparasymtable(parast).ppuload(ppufile);
  3052. { load local symtable }
  3053. if (po_has_inlininginfo in procoptions) then
  3054. begin
  3055. localst:=tlocalsymtable.create(self,level);
  3056. tlocalsymtable(localst).ppuload(ppufile);
  3057. end
  3058. else
  3059. localst:=nil;
  3060. { inline stuff }
  3061. if (po_has_inlininginfo in procoptions) then
  3062. inlininginfo^.code:=ppuloadnodetree(ppufile);
  3063. { default values for no persistent data }
  3064. if (cs_link_deffile in current_settings.globalswitches) and
  3065. (tf_need_export in target_info.flags) and
  3066. (po_exports in procoptions) then
  3067. deffile.AddExport(mangledname);
  3068. forwarddef:=false;
  3069. interfacedef:=false;
  3070. hasforward:=false;
  3071. { Disable po_has_inlining until the derefimpl is done }
  3072. exclude(procoptions,po_has_inlininginfo);
  3073. {$ifdef i386}
  3074. fpu_used:=maxfpuregs;
  3075. {$endif i386}
  3076. end;
  3077. destructor tprocdef.destroy;
  3078. begin
  3079. aliasnames.free;
  3080. aliasnames:=nil;
  3081. if assigned(localst) and
  3082. (localst.symtabletype<>staticsymtable) then
  3083. begin
  3084. {$ifdef MEMDEBUG}
  3085. memproclocalst.start;
  3086. {$endif MEMDEBUG}
  3087. localst.free;
  3088. localst:=nil;
  3089. {$ifdef MEMDEBUG}
  3090. memproclocalst.start;
  3091. {$endif MEMDEBUG}
  3092. end;
  3093. if assigned(inlininginfo) then
  3094. begin
  3095. {$ifdef MEMDEBUG}
  3096. memprocnodetree.start;
  3097. {$endif MEMDEBUG}
  3098. tnode(inlininginfo^.code).free;
  3099. {$ifdef MEMDEBUG}
  3100. memprocnodetree.start;
  3101. {$endif MEMDEBUG}
  3102. dispose(inlininginfo);
  3103. inlininginfo:=nil;
  3104. end;
  3105. stringdispose(resultname);
  3106. stringdispose(import_dll);
  3107. stringdispose(import_name);
  3108. stringdispose(deprecatedmsg);
  3109. if (po_msgstr in procoptions) then
  3110. stringdispose(messageinf.str);
  3111. if assigned(_mangledname) then
  3112. begin
  3113. {$ifdef MEMDEBUG}
  3114. memmanglednames.start;
  3115. {$endif MEMDEBUG}
  3116. stringdispose(_mangledname);
  3117. {$ifdef MEMDEBUG}
  3118. memmanglednames.stop;
  3119. {$endif MEMDEBUG}
  3120. end;
  3121. inherited destroy;
  3122. end;
  3123. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  3124. var
  3125. oldintfcrc : boolean;
  3126. aliasnamescount : longint;
  3127. item : TCmdStrListItem;
  3128. begin
  3129. { released procdef? }
  3130. if not assigned(parast) then
  3131. exit;
  3132. inherited ppuwrite(ppufile);
  3133. if po_has_mangledname in procoptions then
  3134. ppufile.putstring(_mangledname^);
  3135. ppufile.putword(extnumber);
  3136. ppufile.putbyte(parast.symtablelevel);
  3137. ppufile.putderef(structderef);
  3138. ppufile.putderef(procsymderef);
  3139. ppufile.putposinfo(fileinfo);
  3140. ppufile.putbyte(byte(visibility));
  3141. ppufile.putsmallset(symoptions);
  3142. if sp_has_deprecated_msg in symoptions then
  3143. ppufile.putstring(deprecatedmsg^);
  3144. {$ifdef powerpc}
  3145. { library symbol for AmigaOS/MorphOS }
  3146. ppufile.putderef(libsymderef);
  3147. {$endif powerpc}
  3148. { import }
  3149. if po_has_importdll in procoptions then
  3150. ppufile.putstring(import_dll^);
  3151. if po_has_importname in procoptions then
  3152. ppufile.putstring(import_name^);
  3153. ppufile.putword(import_nr);
  3154. if target_info.system in systems_interrupt_table then
  3155. ppufile.putlongint(interruptvector);
  3156. if (po_msgint in procoptions) then
  3157. ppufile.putlongint(messageinf.i);
  3158. if (po_msgstr in procoptions) then
  3159. ppufile.putstring(messageinf.str^);
  3160. if (po_dispid in procoptions) then
  3161. ppufile.putlongint(dispid);
  3162. { inline stuff }
  3163. oldintfcrc:=ppufile.do_crc;
  3164. ppufile.do_crc:=false;
  3165. if (po_has_inlininginfo in procoptions) then
  3166. begin
  3167. ppufile.putderef(funcretsymderef);
  3168. ppufile.putsmallset(inlininginfo^.flags);
  3169. end;
  3170. { count alias names }
  3171. aliasnamescount:=0;
  3172. item:=TCmdStrListItem(aliasnames.first);
  3173. while assigned(item) do
  3174. begin
  3175. inc(aliasnamescount);
  3176. item:=TCmdStrListItem(item.next);
  3177. end;
  3178. if aliasnamescount>255 then
  3179. internalerror(200711021);
  3180. ppufile.putbyte(aliasnamescount);
  3181. item:=TCmdStrListItem(aliasnames.first);
  3182. while assigned(item) do
  3183. begin
  3184. ppufile.putstring(item.str);
  3185. item:=TCmdStrListItem(item.next);
  3186. end;
  3187. ppufile.do_crc:=oldintfcrc;
  3188. { write this entry }
  3189. ppufile.writeentry(ibprocdef);
  3190. { Save the para symtable, this is taken from the interface }
  3191. tparasymtable(parast).ppuwrite(ppufile);
  3192. { save localsymtable for inline procedures or when local
  3193. browser info is requested, this has no influence on the crc }
  3194. if (po_has_inlininginfo in procoptions) then
  3195. begin
  3196. oldintfcrc:=ppufile.do_crc;
  3197. ppufile.do_crc:=false;
  3198. tlocalsymtable(localst).ppuwrite(ppufile);
  3199. ppufile.do_crc:=oldintfcrc;
  3200. end;
  3201. { node tree for inlining }
  3202. oldintfcrc:=ppufile.do_crc;
  3203. ppufile.do_crc:=false;
  3204. if (po_has_inlininginfo in procoptions) then
  3205. ppuwritenodetree(ppufile,inlininginfo^.code);
  3206. ppufile.do_crc:=oldintfcrc;
  3207. end;
  3208. function tprocdef.fullprocname(showhidden:boolean):string;
  3209. var
  3210. s : string;
  3211. t : ttoken;
  3212. begin
  3213. {$ifdef EXTDEBUG}
  3214. showhidden:=true;
  3215. {$endif EXTDEBUG}
  3216. s:='';
  3217. if assigned(struct) then
  3218. begin
  3219. s:=struct.RttiName+'.';
  3220. if (po_classmethod in procoptions) and
  3221. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  3222. s:='class ' + s;
  3223. end;
  3224. if proctypeoption=potype_operator then
  3225. begin
  3226. for t:=NOTOKEN to last_overloaded do
  3227. if procsym.realname='$'+overloaded_names[t] then
  3228. begin
  3229. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  3230. break;
  3231. end;
  3232. end
  3233. else
  3234. s:=s+procsym.realname+typename_paras(showhidden);
  3235. case proctypeoption of
  3236. potype_constructor:
  3237. s:='constructor '+s;
  3238. potype_destructor:
  3239. s:='destructor '+s;
  3240. potype_class_constructor:
  3241. s:='class constructor '+s;
  3242. potype_class_destructor:
  3243. s:='class destructor '+s;
  3244. else
  3245. if assigned(returndef) and
  3246. not(is_void(returndef)) then
  3247. s:=s+':'+returndef.GetTypeName;
  3248. end;
  3249. if owner.symtabletype=localsymtable then
  3250. s:=s+' is nested';
  3251. s:=s+';';
  3252. { forced calling convention? }
  3253. if (po_hascallingconvention in procoptions) then
  3254. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  3255. if (po_staticmethod in procoptions) and
  3256. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  3257. s:=s+' Static;';
  3258. fullprocname:=s;
  3259. end;
  3260. function tprocdef.is_methodpointer:boolean;
  3261. begin
  3262. { don't check assigned(_class), that's also the case for nested
  3263. procedures inside methods }
  3264. result:=owner.symtabletype=ObjectSymtable;
  3265. end;
  3266. function tprocdef.is_addressonly:boolean;
  3267. begin
  3268. result:=assigned(owner) and
  3269. (owner.symtabletype<>ObjectSymtable) and
  3270. (not(m_nested_procvars in current_settings.modeswitches) or
  3271. not is_nested_pd(self));
  3272. end;
  3273. procedure tprocdef.make_external;
  3274. begin
  3275. include(procoptions,po_external);
  3276. forwarddef:=false;
  3277. end;
  3278. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  3279. begin
  3280. case t of
  3281. gs_local :
  3282. GetSymtable:=localst;
  3283. gs_para :
  3284. GetSymtable:=parast;
  3285. else
  3286. GetSymtable:=nil;
  3287. end;
  3288. end;
  3289. procedure tprocdef.buildderef;
  3290. begin
  3291. inherited buildderef;
  3292. structderef.build(struct);
  3293. { procsym that originaly defined this definition, should be in the
  3294. same symtable }
  3295. procsymderef.build(procsym);
  3296. {$ifdef powerpc}
  3297. { library symbol for AmigaOS/MorphOS }
  3298. libsymderef.build(libsym);
  3299. {$endif powerpc}
  3300. end;
  3301. procedure tprocdef.buildderefimpl;
  3302. begin
  3303. inherited buildderefimpl;
  3304. { Localst is not available for main/unit init }
  3305. if assigned(localst) then
  3306. begin
  3307. tlocalsymtable(localst).buildderef;
  3308. tlocalsymtable(localst).buildderefimpl;
  3309. end;
  3310. { inline tree }
  3311. if (po_has_inlininginfo in procoptions) then
  3312. begin
  3313. funcretsymderef.build(funcretsym);
  3314. inlininginfo^.code.buildderefimpl;
  3315. end;
  3316. end;
  3317. procedure tprocdef.deref;
  3318. begin
  3319. inherited deref;
  3320. struct:=tabstractrecorddef(structderef.resolve);
  3321. { procsym that originaly defined this definition, should be in the
  3322. same symtable }
  3323. procsym:=tprocsym(procsymderef.resolve);
  3324. {$ifdef powerpc}
  3325. { library symbol for AmigaOS/MorphOS }
  3326. libsym:=tsym(libsymderef.resolve);
  3327. {$endif powerpc}
  3328. end;
  3329. procedure tprocdef.derefimpl;
  3330. begin
  3331. { Enable has_inlininginfo when the inlininginfo
  3332. structure is available. The has_inlininginfo was disabled
  3333. after the load, since the data was invalid }
  3334. if assigned(inlininginfo) then
  3335. include(procoptions,po_has_inlininginfo);
  3336. { Locals }
  3337. if assigned(localst) then
  3338. begin
  3339. tlocalsymtable(localst).deref;
  3340. tlocalsymtable(localst).derefimpl;
  3341. end;
  3342. { Inline }
  3343. if (po_has_inlininginfo in procoptions) then
  3344. begin
  3345. inlininginfo^.code.derefimpl;
  3346. { funcretsym, this is always located in the localst }
  3347. funcretsym:=tsym(funcretsymderef.resolve);
  3348. end
  3349. else
  3350. begin
  3351. { safety }
  3352. { Not safe! A unit may be reresolved after its interface has been
  3353. parsed but before its implementation has been parsed, and in that
  3354. case the funcretsym is still required!
  3355. funcretsym:=nil; }
  3356. end;
  3357. end;
  3358. function tprocdef.GetTypeName : string;
  3359. begin
  3360. GetTypeName := FullProcName(false);
  3361. end;
  3362. function tprocdef.mangledname : string;
  3363. var
  3364. hp : TParavarsym;
  3365. hs : string;
  3366. crc : dword;
  3367. newlen,
  3368. oldlen,
  3369. i : integer;
  3370. begin
  3371. if assigned(_mangledname) then
  3372. begin
  3373. {$ifdef compress}
  3374. mangledname:=minilzw_decode(_mangledname^);
  3375. {$else}
  3376. mangledname:=_mangledname^;
  3377. {$endif}
  3378. exit;
  3379. end;
  3380. { we need to use the symtable where the procsym is inserted,
  3381. because that is visible to the world }
  3382. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  3383. oldlen:=length(mangledname);
  3384. { add parameter types }
  3385. for i:=0 to paras.count-1 do
  3386. begin
  3387. hp:=tparavarsym(paras[i]);
  3388. if not(vo_is_hidden_para in hp.varoptions) then
  3389. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  3390. end;
  3391. { add resultdef, add $$ as separator to make it unique from a
  3392. parameter separator }
  3393. if not is_void(returndef) then
  3394. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  3395. newlen:=length(mangledname);
  3396. { Replace with CRC if the parameter line is very long }
  3397. if (newlen-oldlen>12) and
  3398. ((newlen>100) or (newlen-oldlen>64)) then
  3399. begin
  3400. crc:=0;
  3401. for i:=0 to paras.count-1 do
  3402. begin
  3403. hp:=tparavarsym(paras[i]);
  3404. if not(vo_is_hidden_para in hp.varoptions) then
  3405. begin
  3406. hs:=hp.vardef.mangledparaname;
  3407. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3408. end;
  3409. end;
  3410. hs:=hp.vardef.mangledparaname;
  3411. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3412. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  3413. end;
  3414. {$ifdef compress}
  3415. _mangledname:=stringdup(minilzw_encode(mangledname));
  3416. {$else}
  3417. _mangledname:=stringdup(mangledname);
  3418. {$endif}
  3419. end;
  3420. function tprocdef.cplusplusmangledname : string;
  3421. function getcppparaname(p : tdef) : string;
  3422. const
  3423. {$ifdef NAMEMANGLING_GCC2}
  3424. ordtype2str : array[tordtype] of string[2] = (
  3425. '',
  3426. 'Uc','Us','Ui','Us',
  3427. 'Sc','s','i','x',
  3428. 'b','b','b','b','b',
  3429. 'c','w','x');
  3430. {$else NAMEMANGLING_GCC2}
  3431. ordtype2str : array[tordtype] of string[1] = (
  3432. 'v',
  3433. 'h','t','j','y',
  3434. 'a','s','i','x',
  3435. 'b','b','b','b','b',
  3436. 'c','w','x');
  3437. floattype2str : array[tfloattype] of string[1] = (
  3438. 'f','d','e','e',
  3439. 'd','d','g');
  3440. {$endif NAMEMANGLING_GCC2}
  3441. var
  3442. s : string;
  3443. begin
  3444. case p.typ of
  3445. orddef:
  3446. s:=ordtype2str[torddef(p).ordtype];
  3447. pointerdef:
  3448. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  3449. {$ifndef NAMEMANGLING_GCC2}
  3450. floatdef:
  3451. s:=floattype2str[tfloatdef(p).floattype];
  3452. {$endif NAMEMANGLING_GCC2}
  3453. else
  3454. internalerror(2103001);
  3455. end;
  3456. getcppparaname:=s;
  3457. end;
  3458. var
  3459. s,s2 : string;
  3460. hp : TParavarsym;
  3461. i : integer;
  3462. begin
  3463. {$ifdef NAMEMANGLING_GCC2}
  3464. { outdated gcc 2.x name mangling scheme }
  3465. s := procsym.realname;
  3466. if procsym.owner.symtabletype=ObjectSymtable then
  3467. begin
  3468. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  3469. case proctypeoption of
  3470. potype_destructor:
  3471. s:='_$_'+tostr(length(s2))+s2;
  3472. potype_constructor:
  3473. s:='___'+tostr(length(s2))+s2;
  3474. else
  3475. s:='_'+s+'__'+tostr(length(s2))+s2;
  3476. end;
  3477. end
  3478. else s:=s+'__';
  3479. s:=s+'F';
  3480. { concat modifiers }
  3481. { !!!!! }
  3482. { now we handle the parameters }
  3483. if maxparacount>0 then
  3484. begin
  3485. for i:=0 to paras.count-1 do
  3486. begin
  3487. hp:=tparavarsym(paras[i]);
  3488. { no hidden parameters form part of a C++ mangled name:
  3489. a) self is not included
  3490. b) there are no "high" or other hidden parameters
  3491. }
  3492. if vo_is_hidden_para in hp.varoptions then
  3493. continue;
  3494. s2:=getcppparaname(hp.vardef);
  3495. if hp.varspez in [vs_var,vs_out] then
  3496. s2:='R'+s2;
  3497. s:=s+s2;
  3498. end;
  3499. end
  3500. else
  3501. s:=s+'v';
  3502. cplusplusmangledname:=s;
  3503. {$else NAMEMANGLING_GCC2}
  3504. { gcc 3.x and 4.x name mangling scheme }
  3505. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  3506. if procsym.owner.symtabletype=ObjectSymtable then
  3507. begin
  3508. s:='_ZN';
  3509. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  3510. s:=s+tostr(length(s2))+s2;
  3511. case proctypeoption of
  3512. potype_constructor:
  3513. s:=s+'C1';
  3514. potype_destructor:
  3515. s:=s+'D1';
  3516. else
  3517. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3518. end;
  3519. s:=s+'E';
  3520. end
  3521. else
  3522. s:=procsym.realname;
  3523. { now we handle the parameters }
  3524. if maxparacount>0 then
  3525. begin
  3526. for i:=0 to paras.count-1 do
  3527. begin
  3528. hp:=tparavarsym(paras[i]);
  3529. { no hidden parameters form part of a C++ mangled name:
  3530. a) self is not included
  3531. b) there are no "high" or other hidden parameters
  3532. }
  3533. if vo_is_hidden_para in hp.varoptions then
  3534. continue;
  3535. s2:=getcppparaname(hp.vardef);
  3536. if hp.varspez in [vs_var,vs_out] then
  3537. s2:='R'+s2;
  3538. s:=s+s2;
  3539. end;
  3540. end
  3541. else
  3542. s:=s+'v';
  3543. cplusplusmangledname:=s;
  3544. {$endif NAMEMANGLING_GCC2}
  3545. end;
  3546. function tprocdef.objcmangledname : string;
  3547. var
  3548. manglednamelen: longint;
  3549. iscatmethod : boolean;
  3550. begin
  3551. if not (po_msgstr in procoptions) then
  3552. internalerror(2009030901);
  3553. { we may very well need longer strings to handle these... }
  3554. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  3555. length('+"[ ]"')+length(messageinf.str^);
  3556. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  3557. if (iscatmethod) then
  3558. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  3559. if manglednamelen>255 then
  3560. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  3561. if not(po_classmethod in procoptions) then
  3562. result:='"-['
  3563. else
  3564. result:='"+[';
  3565. { quotes are necessary because the +/- otherwise confuse the assembler
  3566. into expecting a number
  3567. }
  3568. if iscatmethod then
  3569. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  3570. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  3571. if iscatmethod then
  3572. result:=result+')';
  3573. result:=result+' '+messageinf.str^+']"';
  3574. end;
  3575. procedure tprocdef.setmangledname(const s : string);
  3576. begin
  3577. { This is not allowed anymore, the forward declaration
  3578. already needs to create the correct mangledname, no changes
  3579. afterwards are allowed (PFV) }
  3580. { Exception: interface definitions in mode macpas, since in that }
  3581. { case no reference to the old name can exist yet (JM) }
  3582. if assigned(_mangledname) then
  3583. if ((m_mac in current_settings.modeswitches) and
  3584. (interfacedef)) then
  3585. stringdispose(_mangledname)
  3586. else
  3587. internalerror(200411171);
  3588. {$ifdef compress}
  3589. _mangledname:=stringdup(minilzw_encode(s));
  3590. {$else}
  3591. _mangledname:=stringdup(s);
  3592. {$endif}
  3593. include(procoptions,po_has_mangledname);
  3594. end;
  3595. {***************************************************************************
  3596. TPROCVARDEF
  3597. ***************************************************************************}
  3598. constructor tprocvardef.create(level:byte);
  3599. begin
  3600. inherited create(procvardef,level);
  3601. end;
  3602. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3603. begin
  3604. inherited ppuload(procvardef,ppufile);
  3605. { load para symtable }
  3606. parast:=tparasymtable.create(self,ppufile.getbyte);
  3607. tparasymtable(parast).ppuload(ppufile);
  3608. end;
  3609. function tprocvardef.getcopy : tstoreddef;
  3610. var
  3611. i : tcallercallee;
  3612. j : longint;
  3613. begin
  3614. result:=tprocvardef.create(parast.symtablelevel);
  3615. tprocvardef(result).returndef:=returndef;
  3616. tprocvardef(result).returndefderef:=returndefderef;
  3617. tprocvardef(result).parast:=parast.getcopy;
  3618. tprocvardef(result).savesize:=savesize;
  3619. { create paralist copy }
  3620. tprocvardef(result).paras:=tparalist.create(false);
  3621. tprocvardef(result).paras.count:=paras.count;
  3622. for j:=0 to paras.count-1 do
  3623. tprocvardef(result).paras[j]:=paras[j];
  3624. tprocvardef(result).proctypeoption:=proctypeoption;
  3625. tprocvardef(result).proccalloption:=proccalloption;
  3626. tprocvardef(result).procoptions:=procoptions;
  3627. tprocvardef(result).callerargareasize:=callerargareasize;
  3628. tprocvardef(result).calleeargareasize:=calleeargareasize;
  3629. tprocvardef(result).maxparacount:=maxparacount;
  3630. tprocvardef(result).minparacount:=minparacount;
  3631. for i:=low(tcallercallee) to high(tcallercallee) do
  3632. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  3633. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3634. {$ifdef m68k}
  3635. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3636. {$endif}
  3637. end;
  3638. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3639. begin
  3640. inherited ppuwrite(ppufile);
  3641. { Save the para symtable level (necessary to distinguish nested
  3642. procvars) }
  3643. ppufile.putbyte(parast.symtablelevel);
  3644. { Write this entry }
  3645. ppufile.writeentry(ibprocvardef);
  3646. { Save the para symtable, this is taken from the interface }
  3647. tparasymtable(parast).ppuwrite(ppufile);
  3648. end;
  3649. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3650. begin
  3651. case t of
  3652. gs_para :
  3653. GetSymtable:=parast;
  3654. else
  3655. GetSymtable:=nil;
  3656. end;
  3657. end;
  3658. function tprocvardef.size : asizeint;
  3659. begin
  3660. if ((po_methodpointer in procoptions) or
  3661. is_nested_pd(self)) and
  3662. not(po_addressonly in procoptions) then
  3663. size:=2*sizeof(pint)
  3664. else
  3665. size:=sizeof(pint);
  3666. end;
  3667. function tprocvardef.is_methodpointer:boolean;
  3668. begin
  3669. result:=(po_methodpointer in procoptions);
  3670. end;
  3671. function tprocvardef.is_addressonly:boolean;
  3672. begin
  3673. result:=(not(po_methodpointer in procoptions) and
  3674. not is_nested_pd(self)) or
  3675. (po_addressonly in procoptions);
  3676. end;
  3677. function tprocvardef.getmangledparaname:string;
  3678. begin
  3679. if not(po_methodpointer in procoptions) then
  3680. if not is_nested_pd(self) then
  3681. result:='procvar'
  3682. else
  3683. result:='nestedprovar'
  3684. else
  3685. result:='procvarofobj'
  3686. end;
  3687. function tprocvardef.is_publishable : boolean;
  3688. begin
  3689. is_publishable:=(po_methodpointer in procoptions);
  3690. end;
  3691. function tprocvardef.GetTypeName : string;
  3692. var
  3693. s: string;
  3694. showhidden : boolean;
  3695. begin
  3696. {$ifdef EXTDEBUG}
  3697. showhidden:=true;
  3698. {$else EXTDEBUG}
  3699. showhidden:=false;
  3700. {$endif EXTDEBUG}
  3701. s:='<';
  3702. if po_classmethod in procoptions then
  3703. s := s+'class method type of'
  3704. else
  3705. if po_addressonly in procoptions then
  3706. s := s+'address of'
  3707. else
  3708. s := s+'procedure variable type of';
  3709. if assigned(returndef) and
  3710. (returndef<>voidtype) then
  3711. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3712. else
  3713. s:=s+' procedure'+typename_paras(showhidden);
  3714. if po_methodpointer in procoptions then
  3715. s := s+' of object';
  3716. if is_nested_pd(self) then
  3717. s := s+' is nested';
  3718. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3719. end;
  3720. {***************************************************************************
  3721. TOBJECTDEF
  3722. ***************************************************************************}
  3723. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  3724. begin
  3725. inherited create(n,objectdef);
  3726. fcurrent_dispid:=0;
  3727. objecttype:=ot;
  3728. childof:=nil;
  3729. if objecttype=odt_helper then
  3730. owner.includeoption(sto_has_helper);
  3731. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3732. { create space for vmt !! }
  3733. vmtentries:=TFPList.Create;
  3734. vmt_offset:=0;
  3735. set_parent(c);
  3736. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3737. prepareguid;
  3738. { setup implemented interfaces }
  3739. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3740. ImplementedInterfaces:=TFPObjectList.Create(true)
  3741. else
  3742. ImplementedInterfaces:=nil;
  3743. writing_class_record_dbginfo:=false;
  3744. end;
  3745. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3746. var
  3747. i,
  3748. implintfcount : longint;
  3749. d : tderef;
  3750. ImplIntf : TImplementedInterface;
  3751. vmtentry : pvmtentry;
  3752. begin
  3753. inherited ppuload(objectdef,ppufile);
  3754. objecttype:=tobjecttyp(ppufile.getbyte);
  3755. objextname:=stringdup(ppufile.getstring);
  3756. { only used for external Objective-C classes/protocols }
  3757. if (objextname^='') then
  3758. stringdispose(objextname);
  3759. import_lib:=stringdup(ppufile.getstring);
  3760. { only used for external C++ classes }
  3761. if (import_lib^='') then
  3762. stringdispose(import_lib);
  3763. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3764. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3765. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3766. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3767. vmt_offset:=ppufile.getlongint;
  3768. ppufile.getderef(childofderef);
  3769. { load guid }
  3770. iidstr:=nil;
  3771. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3772. begin
  3773. new(iidguid);
  3774. ppufile.getguid(iidguid^);
  3775. iidstr:=stringdup(ppufile.getstring);
  3776. end;
  3777. if objecttype=odt_helper then
  3778. ppufile.getderef(extendeddefderef);
  3779. vmtentries:=TFPList.Create;
  3780. vmtentries.count:=ppufile.getlongint;
  3781. for i:=0 to vmtentries.count-1 do
  3782. begin
  3783. ppufile.getderef(d);
  3784. new(vmtentry);
  3785. vmtentry^.procdef:=nil;
  3786. vmtentry^.procdefderef:=d;
  3787. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  3788. vmtentries[i]:=vmtentry;
  3789. end;
  3790. { load implemented interfaces }
  3791. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3792. begin
  3793. ImplementedInterfaces:=TFPObjectList.Create(true);
  3794. implintfcount:=ppufile.getlongint;
  3795. for i:=0 to implintfcount-1 do
  3796. begin
  3797. ppufile.getderef(d);
  3798. ImplIntf:=TImplementedInterface.Create_deref(d);
  3799. ImplIntf.IOffset:=ppufile.getlongint;
  3800. ImplementedInterfaces.Add(ImplIntf);
  3801. end;
  3802. end
  3803. else
  3804. ImplementedInterfaces:=nil;
  3805. if df_copied_def in defoptions then
  3806. ppufile.getderef(cloneddefderef)
  3807. else
  3808. tObjectSymtable(symtable).ppuload(ppufile);
  3809. { handles the predefined class tobject }
  3810. { the last TOBJECT which is loaded gets }
  3811. { it ! }
  3812. if (childof=nil) and
  3813. (objecttype=odt_class) and
  3814. (objname^='TOBJECT') then
  3815. class_tobject:=self;
  3816. if (childof=nil) and
  3817. (objecttype=odt_interfacecom) then
  3818. if (objname^='IUNKNOWN') then
  3819. interface_iunknown:=self
  3820. else
  3821. if (objname^='IDISPATCH') then
  3822. interface_idispatch:=self;
  3823. if (childof=nil) and
  3824. (objecttype=odt_objcclass) and
  3825. (objname^='PROTOCOL') then
  3826. objc_protocoltype:=self;
  3827. writing_class_record_dbginfo:=false;
  3828. end;
  3829. destructor tobjectdef.destroy;
  3830. begin
  3831. if assigned(symtable) then
  3832. begin
  3833. symtable.free;
  3834. symtable:=nil;
  3835. end;
  3836. stringdispose(objextname);
  3837. stringdispose(import_lib);
  3838. stringdispose(iidstr);
  3839. if assigned(ImplementedInterfaces) then
  3840. begin
  3841. ImplementedInterfaces.free;
  3842. ImplementedInterfaces:=nil;
  3843. end;
  3844. if assigned(iidguid) then
  3845. begin
  3846. dispose(iidguid);
  3847. iidguid:=nil;
  3848. end;
  3849. if assigned(vmtentries) then
  3850. begin
  3851. resetvmtentries;
  3852. vmtentries.free;
  3853. vmtentries:=nil;
  3854. end;
  3855. if assigned(vmcallstaticinfo) then
  3856. begin
  3857. freemem(vmcallstaticinfo);
  3858. vmcallstaticinfo:=nil;
  3859. end;
  3860. inherited destroy;
  3861. end;
  3862. function tobjectdef.getcopy : tstoreddef;
  3863. var
  3864. i : longint;
  3865. begin
  3866. result:=tobjectdef.create(objecttype,objrealname^,childof);
  3867. { the constructor allocates a symtable which we release to avoid memory leaks }
  3868. tobjectdef(result).symtable.free;
  3869. tobjectdef(result).symtable:=symtable.getcopy;
  3870. if assigned(objextname) then
  3871. tobjectdef(result).objextname:=stringdup(objextname^);
  3872. if assigned(import_lib) then
  3873. tobjectdef(result).import_lib:=stringdup(import_lib^);
  3874. tobjectdef(result).objectoptions:=objectoptions;
  3875. include(tobjectdef(result).defoptions,df_copied_def);
  3876. tobjectdef(result).extendeddef:=extendeddef;
  3877. tobjectdef(result).vmt_offset:=vmt_offset;
  3878. if assigned(iidguid) then
  3879. begin
  3880. new(tobjectdef(result).iidguid);
  3881. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3882. end;
  3883. if assigned(iidstr) then
  3884. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3885. if assigned(ImplementedInterfaces) then
  3886. begin
  3887. for i:=0 to ImplementedInterfaces.count-1 do
  3888. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3889. end;
  3890. if assigned(vmtentries) then
  3891. begin
  3892. tobjectdef(result).vmtentries:=TFPList.Create;
  3893. tobjectdef(result).copyvmtentries(self);
  3894. end;
  3895. end;
  3896. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3897. var
  3898. i : longint;
  3899. vmtentry : pvmtentry;
  3900. ImplIntf : TImplementedInterface;
  3901. old_do_indirect_crc: boolean;
  3902. begin
  3903. { if class1 in unit A changes, and class2 in unit B inherits from it
  3904. (so unit B uses unit A), then unit B with class2 will be recompiled.
  3905. However, if there is also a class3 in unit C that only depends on
  3906. unit B, then unit C will not be recompiled because nothing changed
  3907. to the interface of unit B. Nevertheless, unit C can indirectly
  3908. depend on unit A via derefs, and these must be updated -> the
  3909. indirect crc keeps track of such changes. }
  3910. old_do_indirect_crc:=ppufile.do_indirect_crc;
  3911. ppufile.do_indirect_crc:=true;
  3912. inherited ppuwrite(ppufile);
  3913. ppufile.putbyte(byte(objecttype));
  3914. if assigned(objextname) then
  3915. ppufile.putstring(objextname^)
  3916. else
  3917. ppufile.putstring('');
  3918. if assigned(import_lib) then
  3919. ppufile.putstring(import_lib^)
  3920. else
  3921. ppufile.putstring('');
  3922. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3923. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3924. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3925. ppufile.putlongint(vmt_offset);
  3926. ppufile.putderef(childofderef);
  3927. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3928. begin
  3929. ppufile.putguid(iidguid^);
  3930. ppufile.putstring(iidstr^);
  3931. end;
  3932. if objecttype=odt_helper then
  3933. ppufile.putderef(extendeddefderef);
  3934. ppufile.putlongint(vmtentries.count);
  3935. for i:=0 to vmtentries.count-1 do
  3936. begin
  3937. vmtentry:=pvmtentry(vmtentries[i]);
  3938. ppufile.putderef(vmtentry^.procdefderef);
  3939. ppufile.putbyte(byte(vmtentry^.visibility));
  3940. end;
  3941. if assigned(ImplementedInterfaces) then
  3942. begin
  3943. ppufile.putlongint(ImplementedInterfaces.Count);
  3944. for i:=0 to ImplementedInterfaces.Count-1 do
  3945. begin
  3946. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3947. ppufile.putderef(ImplIntf.intfdefderef);
  3948. ppufile.putlongint(ImplIntf.Ioffset);
  3949. end;
  3950. end;
  3951. if df_copied_def in defoptions then
  3952. ppufile.putderef(cloneddefderef);
  3953. ppufile.writeentry(ibobjectdef);
  3954. if not(df_copied_def in defoptions) then
  3955. tObjectSymtable(symtable).ppuwrite(ppufile);
  3956. ppufile.do_indirect_crc:=old_do_indirect_crc;
  3957. end;
  3958. function tobjectdef.GetTypeName:string;
  3959. begin
  3960. { in this case we will go in endless recursion, because then }
  3961. { there is no tsym associated yet with the def. It can occur }
  3962. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3963. { instead of the actual type name }
  3964. if not assigned(typesym) then
  3965. result:='<Currently Parsed Class>'
  3966. else
  3967. result:=typename;
  3968. end;
  3969. procedure tobjectdef.buildderef;
  3970. var
  3971. i : longint;
  3972. vmtentry : pvmtentry;
  3973. begin
  3974. inherited buildderef;
  3975. childofderef.build(childof);
  3976. if df_copied_def in defoptions then
  3977. cloneddefderef.build(symtable.defowner)
  3978. else
  3979. tstoredsymtable(symtable).buildderef;
  3980. if objecttype=odt_helper then
  3981. extendeddefderef.build(extendeddef);
  3982. for i:=0 to vmtentries.count-1 do
  3983. begin
  3984. vmtentry:=pvmtentry(vmtentries[i]);
  3985. vmtentry^.procdefderef.build(vmtentry^.procdef);
  3986. end;
  3987. if assigned(ImplementedInterfaces) then
  3988. begin
  3989. for i:=0 to ImplementedInterfaces.count-1 do
  3990. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  3991. end;
  3992. end;
  3993. procedure tobjectdef.deref;
  3994. var
  3995. i : longint;
  3996. vmtentry : pvmtentry;
  3997. begin
  3998. inherited deref;
  3999. childof:=tobjectdef(childofderef.resolve);
  4000. if df_copied_def in defoptions then
  4001. begin
  4002. cloneddef:=tobjectdef(cloneddefderef.resolve);
  4003. symtable:=cloneddef.symtable.getcopy;
  4004. end
  4005. else
  4006. tstoredsymtable(symtable).deref;
  4007. if objecttype=odt_helper then
  4008. extendeddef:=tobjectdef(extendeddefderef.resolve);
  4009. for i:=0 to vmtentries.count-1 do
  4010. begin
  4011. vmtentry:=pvmtentry(vmtentries[i]);
  4012. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  4013. end;
  4014. if assigned(ImplementedInterfaces) then
  4015. begin
  4016. for i:=0 to ImplementedInterfaces.count-1 do
  4017. TImplementedInterface(ImplementedInterfaces[i]).deref;
  4018. end;
  4019. end;
  4020. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  4021. var
  4022. pd: tprocdef absolute def;
  4023. st: tsymtable;
  4024. psym: tsym;
  4025. nname: TIDString;
  4026. begin
  4027. if (tdef(def).typ<>procdef) then
  4028. exit;
  4029. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  4030. owner = symtable in which objcclassdef is defined
  4031. }
  4032. st:=pd.owner.defowner.owner;
  4033. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  4034. { check for an existing procsym with our special name }
  4035. psym:=tsym(st.find(nname));
  4036. if not assigned(psym) then
  4037. begin
  4038. psym:=tprocsym.create(nname);
  4039. { avoid warning about this symbol being unused }
  4040. psym.IncRefCount;
  4041. { don't check for duplicates:
  4042. a) we checked above
  4043. b) in case we are in the implementation section of a unit, this
  4044. will also check for this symbol in the interface section
  4045. (since you normally cannot have symbols with the same name
  4046. both interface and implementation), and it's possible to
  4047. have class helpers for the same class in the interface and
  4048. in the implementation, and they cannot be merged since only
  4049. the once in the interface must be saved to the ppu/visible
  4050. from other units }
  4051. st.insert(psym,false);
  4052. end
  4053. else if (psym.typ<>procsym) then
  4054. internalerror(2009111501);
  4055. { add ourselves to this special procsym }
  4056. tprocsym(psym).procdeflist.add(def);
  4057. end;
  4058. procedure tobjectdef.buildderefimpl;
  4059. begin
  4060. inherited buildderefimpl;
  4061. if not (df_copied_def in defoptions) then
  4062. tstoredsymtable(symtable).buildderefimpl;
  4063. end;
  4064. procedure tobjectdef.derefimpl;
  4065. begin
  4066. inherited derefimpl;
  4067. if not (df_copied_def in defoptions) then
  4068. tstoredsymtable(symtable).derefimpl;
  4069. { the procdefs are not owned by the class helper procsyms, so they
  4070. are not stored/restored either -> re-add them here }
  4071. if (objecttype=odt_objcclass) or
  4072. (oo_is_classhelper in objectoptions) then
  4073. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  4074. end;
  4075. procedure tobjectdef.resetvmtentries;
  4076. var
  4077. i : longint;
  4078. begin
  4079. for i:=0 to vmtentries.Count-1 do
  4080. Dispose(pvmtentry(vmtentries[i]));
  4081. vmtentries.clear;
  4082. end;
  4083. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  4084. var
  4085. i : longint;
  4086. vmtentry : pvmtentry;
  4087. begin
  4088. resetvmtentries;
  4089. vmtentries.count:=objdef.vmtentries.count;
  4090. for i:=0 to objdef.vmtentries.count-1 do
  4091. begin
  4092. new(vmtentry);
  4093. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  4094. vmtentries[i]:=vmtentry;
  4095. end;
  4096. end;
  4097. function tobjectdef.getparentdef:tdef;
  4098. begin
  4099. { TODO: Remove getparentdef hack}
  4100. { With 2 forward declared classes with the child class before the
  4101. parent class the child class is written earlier to the ppu. Leaving it
  4102. possible to have a reference to the parent class for property overriding,
  4103. but the parent class still has the childof not resolved yet (PFV) }
  4104. if childof=nil then
  4105. childof:=tobjectdef(childofderef.resolve);
  4106. result:=childof;
  4107. end;
  4108. procedure tobjectdef.prepareguid;
  4109. begin
  4110. { set up guid }
  4111. if not assigned(iidguid) then
  4112. begin
  4113. new(iidguid);
  4114. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  4115. end;
  4116. { setup iidstring }
  4117. if not assigned(iidstr) then
  4118. iidstr:=stringdup(''); { default is empty string }
  4119. end;
  4120. procedure tobjectdef.set_parent( c : tobjectdef);
  4121. begin
  4122. if assigned(childof) then
  4123. exit;
  4124. childof:=c;
  4125. if not assigned(c) then
  4126. exit;
  4127. { inherit options and status }
  4128. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  4129. { add the data of the anchestor class/object }
  4130. if (objecttype in [odt_class,odt_object,odt_objcclass]) then
  4131. begin
  4132. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  4133. { inherit recordalignment }
  4134. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  4135. if (oo_has_vmt in objectoptions) and
  4136. (oo_has_vmt in c.objectoptions) then
  4137. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  4138. { if parent has a vmt field then the offset is the same for the child PM }
  4139. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  4140. begin
  4141. vmt_offset:=c.vmt_offset;
  4142. include(objectoptions,oo_has_vmt);
  4143. end;
  4144. end;
  4145. end;
  4146. procedure tobjectdef.insertvmt;
  4147. var
  4148. vs: tfieldvarsym;
  4149. begin
  4150. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  4151. exit;
  4152. if (oo_has_vmt in objectoptions) then
  4153. internalerror(12345)
  4154. else
  4155. begin
  4156. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  4157. tObjectSymtable(symtable).fieldalignment);
  4158. if (tf_requires_proper_alignment in target_info.flags) then
  4159. begin
  4160. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  4161. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  4162. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  4163. end;
  4164. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  4165. hidesym(vs);
  4166. tObjectSymtable(symtable).insert(vs);
  4167. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  4168. if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  4169. vmt_offset:=vs.fieldoffset
  4170. else
  4171. vmt_offset:=vs.fieldoffset div 8;
  4172. include(objectoptions,oo_has_vmt);
  4173. end;
  4174. end;
  4175. procedure tobjectdef.check_forwards;
  4176. begin
  4177. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol]) then
  4178. inherited;
  4179. if (oo_is_forward in objectoptions) then
  4180. begin
  4181. { ok, in future, the forward can be resolved }
  4182. Message1(sym_e_class_forward_not_resolved,objrealname^);
  4183. exclude(objectoptions,oo_is_forward);
  4184. end;
  4185. end;
  4186. { true if prot implements d (or if they are equal) }
  4187. function is_related_protocol(prot: tobjectdef; d : tdef) : boolean;
  4188. var
  4189. i : longint;
  4190. begin
  4191. { objcprotocols have multiple inheritance, all protocols from which
  4192. the current protocol inherits are stored in implementedinterfaces }
  4193. result:=prot=d;
  4194. if result then
  4195. exit;
  4196. for i:=0 to prot.ImplementedInterfaces.count-1 do
  4197. begin
  4198. result:=is_related_protocol(TImplementedInterface(prot.ImplementedInterfaces[i]).intfdef,d);
  4199. if result then
  4200. exit;
  4201. end;
  4202. end;
  4203. { true, if self inherits from d (or if they are equal) }
  4204. function tobjectdef.is_related(d : tdef) : boolean;
  4205. var
  4206. hp : tobjectdef;
  4207. begin
  4208. if self=d then
  4209. begin
  4210. is_related:=true;
  4211. exit;
  4212. end;
  4213. if (d.typ<>objectdef) then
  4214. begin
  4215. is_related:=false;
  4216. exit;
  4217. end;
  4218. { Objective-C protocols can use multiple inheritance }
  4219. if (objecttype=odt_objcprotocol) then
  4220. begin
  4221. is_related:=is_related_protocol(self,d);
  4222. exit
  4223. end;
  4224. { formally declared Objective-C classes match Objective-C classes with
  4225. the same name }
  4226. if (objecttype=odt_objcclass) and
  4227. (tobjectdef(d).objecttype=odt_objcclass) and
  4228. ((oo_is_formal in objectoptions) or
  4229. (oo_is_formal in tobjectdef(d).objectoptions)) and
  4230. (objrealname^=tobjectdef(d).objrealname^) then
  4231. begin
  4232. is_related:=true;
  4233. exit;
  4234. end;
  4235. hp:=childof;
  4236. while assigned(hp) do
  4237. begin
  4238. if hp=d then
  4239. begin
  4240. is_related:=true;
  4241. exit;
  4242. end;
  4243. hp:=hp.childof;
  4244. end;
  4245. is_related:=false;
  4246. end;
  4247. function tobjectdef.find_destructor: tprocdef;
  4248. var
  4249. objdef: tobjectdef;
  4250. begin
  4251. objdef:=self;
  4252. while assigned(objdef) do
  4253. begin
  4254. result:=objdef.find_procdef_bytype(potype_destructor);
  4255. if assigned(result) then
  4256. exit;
  4257. objdef:=objdef.childof;
  4258. end;
  4259. result:=nil;
  4260. end;
  4261. function tobjectdef.implements_any_interfaces: boolean;
  4262. begin
  4263. result := (ImplementedInterfaces.Count > 0) or
  4264. (assigned(childof) and childof.implements_any_interfaces);
  4265. end;
  4266. function tobjectdef.size : asizeint;
  4267. begin
  4268. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper] then
  4269. result:=sizeof(pint)
  4270. else
  4271. result:=tObjectSymtable(symtable).datasize;
  4272. end;
  4273. function tobjectdef.alignment:shortint;
  4274. begin
  4275. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper] then
  4276. alignment:=sizeof(pint)
  4277. else
  4278. alignment:=tObjectSymtable(symtable).recordalignment;
  4279. end;
  4280. function tobjectdef.vmtmethodoffset(index:longint):longint;
  4281. begin
  4282. { for offset of methods for classes, see rtl/inc/objpash.inc }
  4283. case objecttype of
  4284. odt_class:
  4285. { the +2*sizeof(pint) is size and -size }
  4286. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  4287. odt_helper,
  4288. odt_objcclass,
  4289. odt_objcprotocol:
  4290. vmtmethodoffset:=0;
  4291. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  4292. vmtmethodoffset:=index*sizeof(pint);
  4293. else
  4294. {$ifdef WITHDMT}
  4295. vmtmethodoffset:=(index+4)*sizeof(pint);
  4296. {$else WITHDMT}
  4297. vmtmethodoffset:=(index+3)*sizeof(pint);
  4298. {$endif WITHDMT}
  4299. end;
  4300. end;
  4301. function tobjectdef.vmt_mangledname : string;
  4302. begin
  4303. if not(oo_has_vmt in objectoptions) then
  4304. Message1(parser_n_object_has_no_vmt,objrealname^);
  4305. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  4306. end;
  4307. function tobjectdef.needs_inittable : boolean;
  4308. begin
  4309. case objecttype of
  4310. odt_helper,
  4311. odt_class :
  4312. needs_inittable:=false;
  4313. odt_dispinterface,
  4314. odt_interfacecom:
  4315. needs_inittable:=true;
  4316. odt_interfacecorba:
  4317. needs_inittable:=is_related(interface_iunknown);
  4318. odt_object:
  4319. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  4320. odt_cppclass,
  4321. odt_objcclass,
  4322. odt_objcprotocol:
  4323. needs_inittable:=false;
  4324. else
  4325. internalerror(200108267);
  4326. end;
  4327. end;
  4328. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  4329. begin
  4330. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  4331. result:=inherited rtti_mangledname(rt)
  4332. else
  4333. begin
  4334. { necessary in case of a dynamic array of nsobject, or
  4335. if an nsobject field appears in a record that needs
  4336. init/finalisation }
  4337. if rt=initrtti then
  4338. begin
  4339. result:=voidpointertype.rtti_mangledname(rt);
  4340. exit;
  4341. end;
  4342. if not(target_info.system in systems_objc_nfabi) then
  4343. begin
  4344. result:=target_asm.labelprefix;
  4345. case objecttype of
  4346. odt_objcclass:
  4347. begin
  4348. case rt of
  4349. objcclassrtti:
  4350. if not(oo_is_classhelper in objectoptions) then
  4351. result:=result+'_OBJC_CLASS_'
  4352. else
  4353. result:=result+'_OBJC_CATEGORY_';
  4354. objcmetartti:
  4355. if not(oo_is_classhelper in objectoptions) then
  4356. result:=result+'_OBJC_METACLASS_'
  4357. else
  4358. internalerror(2009111511);
  4359. else
  4360. internalerror(2009092302);
  4361. end;
  4362. end;
  4363. odt_objcprotocol:
  4364. result:=result+'_OBJC_PROTOCOL_';
  4365. end;
  4366. end
  4367. else
  4368. begin
  4369. case objecttype of
  4370. odt_objcclass:
  4371. begin
  4372. if (oo_is_classhelper in objectoptions) and
  4373. (rt<>objcclassrtti) then
  4374. internalerror(2009111512);
  4375. case rt of
  4376. objcclassrtti:
  4377. if not(oo_is_classhelper in objectoptions) then
  4378. result:='_OBJC_CLASS_$_'
  4379. else
  4380. result:='_OBJC_$_CATEGORY_';
  4381. objcmetartti:
  4382. result:='_OBJC_METACLASS_$_';
  4383. objcclassrortti:
  4384. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  4385. objcmetarortti:
  4386. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  4387. else
  4388. internalerror(2009092303);
  4389. end;
  4390. end;
  4391. odt_objcprotocol:
  4392. begin
  4393. result:=lower(target_asm.labelprefix);
  4394. case rt of
  4395. objcclassrtti:
  4396. result:=result+'_OBJC_PROTOCOL_$_';
  4397. objcmetartti:
  4398. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  4399. else
  4400. internalerror(2009092501);
  4401. end;
  4402. end;
  4403. end;
  4404. end;
  4405. result:=result+objextname^;
  4406. end;
  4407. end;
  4408. function tobjectdef.members_need_inittable : boolean;
  4409. begin
  4410. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  4411. end;
  4412. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  4413. var
  4414. ImplIntf : TImplementedInterface;
  4415. i : longint;
  4416. begin
  4417. result:=nil;
  4418. if not assigned(ImplementedInterfaces) then
  4419. exit;
  4420. for i:=0 to ImplementedInterfaces.Count-1 do
  4421. begin
  4422. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  4423. if ImplIntf.intfdef=aintfdef then
  4424. begin
  4425. result:=ImplIntf;
  4426. exit;
  4427. end;
  4428. end;
  4429. end;
  4430. function tobjectdef.is_publishable : boolean;
  4431. begin
  4432. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  4433. end;
  4434. function tobjectdef.get_next_dispid: longint;
  4435. begin
  4436. inc(fcurrent_dispid);
  4437. result:=fcurrent_dispid;
  4438. end;
  4439. function tobjectdef.search_enumerator_get: tprocdef;
  4440. begin
  4441. result:=inherited;
  4442. if not assigned(result) and assigned(childof) then
  4443. result:=childof.search_enumerator_get;
  4444. end;
  4445. function tobjectdef.search_enumerator_move: tprocdef;
  4446. begin
  4447. result:=inherited;
  4448. if not assigned(result) and assigned(childof) then
  4449. result:=childof.search_enumerator_move;
  4450. end;
  4451. function tobjectdef.search_enumerator_current: tsym;
  4452. begin
  4453. result:=inherited;
  4454. if not assigned(result) and assigned(childof) then
  4455. result:=childof.search_enumerator_current;
  4456. end;
  4457. procedure tobjectdef.register_created_classref_type;
  4458. begin
  4459. if not classref_created_in_current_module then
  4460. begin
  4461. classref_created_in_current_module:=true;
  4462. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  4463. end;
  4464. end;
  4465. procedure tobjectdef.register_created_object_type;
  4466. begin
  4467. if not created_in_current_module then
  4468. begin
  4469. created_in_current_module:=true;
  4470. current_module.wpoinfo.addcreatedobjtype(self);
  4471. end;
  4472. end;
  4473. procedure tobjectdef.register_maybe_created_object_type;
  4474. begin
  4475. { if we know it has been created for sure, no need
  4476. to also record that it maybe can be created in
  4477. this module
  4478. }
  4479. if not (created_in_current_module) and
  4480. not (maybe_created_in_current_module) then
  4481. begin
  4482. maybe_created_in_current_module:=true;
  4483. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  4484. end;
  4485. end;
  4486. procedure tobjectdef.register_vmt_call(index: longint);
  4487. begin
  4488. if (is_object(self) or is_class(self)) then
  4489. current_module.wpoinfo.addcalledvmtentry(self,index);
  4490. end;
  4491. procedure check_and_finish_msg(data: tobject; arg: pointer);
  4492. var
  4493. def: tdef absolute data;
  4494. pd: tprocdef absolute data;
  4495. i,
  4496. paracount: longint;
  4497. begin
  4498. if (def.typ=procdef) then
  4499. begin
  4500. { add all messages also under a dummy name to the symtable in
  4501. which the objcclass/protocol/category is declared, so they can
  4502. be called via id.<name>
  4503. }
  4504. create_class_helper_for_procdef(pd,nil);
  4505. { we have to wait until now to set the mangled name because it
  4506. depends on the (possibly external) class name, which is defined
  4507. at the very end. }
  4508. if not(po_msgstr in pd.procoptions) then
  4509. begin
  4510. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  4511. { recover to avoid internalerror later on }
  4512. include(pd.procoptions,po_msgstr);
  4513. pd.messageinf.str:=stringdup('MissingDeclaration');
  4514. end;
  4515. { Mangled name is already set in case this is a copy of
  4516. another type. }
  4517. if not(po_has_mangledname in pd.procoptions) then
  4518. begin
  4519. { check whether the number of formal parameters is correct,
  4520. and whether they have valid Objective-C types }
  4521. paracount:=0;
  4522. for i:=1 to length(pd.messageinf.str^) do
  4523. if pd.messageinf.str^[i]=':' then
  4524. inc(paracount);
  4525. for i:=0 to pd.paras.count-1 do
  4526. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  4527. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  4528. dec(paracount);
  4529. if (paracount<>0) then
  4530. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  4531. pd.setmangledname(pd.objcmangledname);
  4532. end
  4533. else
  4534. { all checks already done }
  4535. exit;
  4536. if not(oo_is_external in pd.struct.objectoptions) then
  4537. begin
  4538. if (po_varargs in pd.procoptions) then
  4539. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  4540. else
  4541. begin
  4542. { check for "array of const" parameters }
  4543. for i:=0 to pd.parast.symlist.count-1 do
  4544. begin
  4545. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  4546. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  4547. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  4548. end;
  4549. end;
  4550. end;
  4551. end;
  4552. end;
  4553. procedure mark_private_fields_used(data: tobject; arg: pointer);
  4554. var
  4555. sym: tsym absolute data;
  4556. begin
  4557. if (sym.typ=fieldvarsym) and
  4558. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  4559. sym.IncRefCount;
  4560. end;
  4561. procedure tobjectdef.finish_objc_data;
  4562. begin
  4563. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  4564. if (oo_is_external in objectoptions) then
  4565. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  4566. end;
  4567. procedure verify_objc_vardef(data: tobject; arg: pointer);
  4568. var
  4569. sym: tabstractvarsym absolute data;
  4570. res: pboolean absolute arg;
  4571. founderrordef: tdef;
  4572. begin
  4573. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  4574. exit;
  4575. if (sym.typ=paravarsym) and
  4576. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  4577. is_array_of_const(tparavarsym(sym).vardef)) then
  4578. exit;
  4579. if not objcchecktype(sym.vardef,founderrordef) then
  4580. begin
  4581. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4582. res^:=false;
  4583. end;
  4584. end;
  4585. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  4586. var
  4587. def: tdef absolute data;
  4588. res: pboolean absolute arg;
  4589. founderrordef: tdef;
  4590. begin
  4591. if (def.typ<>procdef) then
  4592. exit;
  4593. { check parameter types for validity }
  4594. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  4595. { check the result type for validity }
  4596. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  4597. begin
  4598. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4599. res^:=false;
  4600. end;
  4601. end;
  4602. function tobjectdef.check_objc_types: boolean;
  4603. begin
  4604. { done in separate step from finish_objc_data, because when
  4605. finish_objc_data is called, not all forwarddefs have been resolved
  4606. yet and we need to know all types here }
  4607. result:=true;
  4608. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  4609. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  4610. end;
  4611. procedure do_cpp_import_info(data: tobject; arg: pointer);
  4612. var
  4613. def: tdef absolute data;
  4614. pd: tprocdef absolute data;
  4615. begin
  4616. if (def.typ=procdef) then
  4617. begin
  4618. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  4619. if (oo_is_external in pd.struct.objectoptions) then
  4620. begin
  4621. { copied from psub.read_proc }
  4622. if assigned(tobjectdef(pd.struct).import_lib) then
  4623. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  4624. else
  4625. begin
  4626. { add import name to external list for DLL scanning }
  4627. if tf_has_dllscanner in target_info.flags then
  4628. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  4629. end;
  4630. end;
  4631. end;
  4632. end;
  4633. procedure tobjectdef.finish_cpp_data;
  4634. begin
  4635. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  4636. end;
  4637. {****************************************************************************
  4638. TImplementedInterface
  4639. ****************************************************************************}
  4640. constructor TImplementedInterface.create(aintf: tobjectdef);
  4641. begin
  4642. inherited create;
  4643. intfdef:=aintf;
  4644. IOffset:=-1;
  4645. IType:=etStandard;
  4646. NameMappings:=nil;
  4647. procdefs:=nil;
  4648. end;
  4649. constructor TImplementedInterface.create_deref(d:tderef);
  4650. begin
  4651. inherited create;
  4652. intfdef:=nil;
  4653. intfdefderef:=d;
  4654. IOffset:=-1;
  4655. IType:=etStandard;
  4656. NameMappings:=nil;
  4657. procdefs:=nil;
  4658. end;
  4659. destructor TImplementedInterface.destroy;
  4660. var
  4661. i : longint;
  4662. mappedname : pshortstring;
  4663. begin
  4664. if assigned(NameMappings) then
  4665. begin
  4666. for i:=0 to NameMappings.Count-1 do
  4667. begin
  4668. mappedname:=pshortstring(NameMappings[i]);
  4669. stringdispose(mappedname);
  4670. end;
  4671. NameMappings.free;
  4672. NameMappings:=nil;
  4673. end;
  4674. if assigned(procdefs) then
  4675. begin
  4676. procdefs.free;
  4677. procdefs:=nil;
  4678. end;
  4679. inherited destroy;
  4680. end;
  4681. procedure TImplementedInterface.buildderef;
  4682. begin
  4683. intfdefderef.build(intfdef);
  4684. end;
  4685. procedure TImplementedInterface.deref;
  4686. begin
  4687. intfdef:=tobjectdef(intfdefderef.resolve);
  4688. end;
  4689. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  4690. begin
  4691. if not assigned(NameMappings) then
  4692. NameMappings:=TFPHashList.Create;
  4693. NameMappings.Add(origname,stringdup(newname));
  4694. end;
  4695. function TImplementedInterface.GetMapping(const origname: string):string;
  4696. var
  4697. mappedname : pshortstring;
  4698. begin
  4699. result:='';
  4700. if not assigned(NameMappings) then
  4701. exit;
  4702. mappedname:=PShortstring(NameMappings.Find(origname));
  4703. if assigned(mappedname) then
  4704. result:=mappedname^;
  4705. end;
  4706. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  4707. begin
  4708. if not assigned(procdefs) then
  4709. procdefs:=TFPObjectList.Create(false);
  4710. { duplicate entries must be stored, because multiple }
  4711. { interfaces can declare methods with the same name }
  4712. { and all of these get their own VMT entry }
  4713. procdefs.Add(pd);
  4714. end;
  4715. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  4716. var
  4717. i : longint;
  4718. begin
  4719. result:=false;
  4720. { interfaces being implemented through delegation are not mergable (FK) }
  4721. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  4722. exit;
  4723. weight:=0;
  4724. { empty interface is mergeable }
  4725. if ProcDefs.Count=0 then
  4726. begin
  4727. result:=true;
  4728. exit;
  4729. end;
  4730. { The interface to merge must at least the number of
  4731. procedures of this interface }
  4732. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  4733. exit;
  4734. for i:=0 to ProcDefs.Count-1 do
  4735. begin
  4736. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  4737. exit;
  4738. end;
  4739. weight:=ProcDefs.Count;
  4740. result:=true;
  4741. end;
  4742. function TImplementedInterface.getcopy:TImplementedInterface;
  4743. begin
  4744. Result:=TImplementedInterface.Create(nil);
  4745. { 1) the procdefs list will be freed once for each copy
  4746. 2) since the procdefs list owns its elements, those will also be freed for each copy
  4747. 3) idem for the name mappings
  4748. }
  4749. { warning: this is completely wrong on so many levels...
  4750. Move(pointer(self)^,pointer(result)^,InstanceSize);
  4751. We need to make clean copies of the different fields
  4752. this is not implemented yet, and thus we generate an internal
  4753. error instead PM 2011-06-14 }
  4754. internalerror(2011061401);
  4755. end;
  4756. {****************************************************************************
  4757. TFORWARDDEF
  4758. ****************************************************************************}
  4759. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  4760. begin
  4761. inherited create(forwarddef);
  4762. tosymname:=stringdup(s);
  4763. forwardpos:=pos;
  4764. end;
  4765. function tforwarddef.GetTypeName:string;
  4766. begin
  4767. GetTypeName:='unresolved forward to '+tosymname^;
  4768. end;
  4769. destructor tforwarddef.destroy;
  4770. begin
  4771. stringdispose(tosymname);
  4772. inherited destroy;
  4773. end;
  4774. function tforwarddef.getcopy:tstoreddef;
  4775. begin
  4776. result:=tforwarddef.create(tosymname^, forwardpos);
  4777. end;
  4778. {****************************************************************************
  4779. TUNDEFINEDDEF
  4780. ****************************************************************************}
  4781. constructor tundefineddef.create;
  4782. begin
  4783. inherited create(undefineddef);
  4784. end;
  4785. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  4786. begin
  4787. inherited ppuload(undefineddef,ppufile);
  4788. end;
  4789. function tundefineddef.GetTypeName:string;
  4790. begin
  4791. GetTypeName:='<undefined type>';
  4792. end;
  4793. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  4794. begin
  4795. inherited ppuwrite(ppufile);
  4796. ppufile.writeentry(ibundefineddef);
  4797. end;
  4798. {****************************************************************************
  4799. TERRORDEF
  4800. ****************************************************************************}
  4801. constructor terrordef.create;
  4802. begin
  4803. inherited create(errordef);
  4804. { prevent consecutive faults }
  4805. savesize:=1;
  4806. end;
  4807. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  4808. begin
  4809. { Can't write errordefs to ppu }
  4810. internalerror(200411063);
  4811. end;
  4812. function terrordef.GetTypeName:string;
  4813. begin
  4814. GetTypeName:='<erroneous type>';
  4815. end;
  4816. function terrordef.getmangledparaname:string;
  4817. begin
  4818. getmangledparaname:='error';
  4819. end;
  4820. {****************************************************************************
  4821. Definition Helpers
  4822. ****************************************************************************}
  4823. function is_interfacecom(def: tdef): boolean;
  4824. begin
  4825. is_interfacecom:=
  4826. assigned(def) and
  4827. (def.typ=objectdef) and
  4828. (tobjectdef(def).objecttype=odt_interfacecom);
  4829. end;
  4830. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  4831. begin
  4832. is_interfacecom_or_dispinterface:=
  4833. assigned(def) and
  4834. (def.typ=objectdef) and
  4835. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  4836. end;
  4837. function is_interfacecorba(def: tdef): boolean;
  4838. begin
  4839. is_interfacecorba:=
  4840. assigned(def) and
  4841. (def.typ=objectdef) and
  4842. (tobjectdef(def).objecttype=odt_interfacecorba);
  4843. end;
  4844. function is_interface(def: tdef): boolean;
  4845. begin
  4846. is_interface:=
  4847. assigned(def) and
  4848. (def.typ=objectdef) and
  4849. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  4850. end;
  4851. function is_dispinterface(def: tdef): boolean;
  4852. begin
  4853. result:=
  4854. assigned(def) and
  4855. (def.typ=objectdef) and
  4856. (tobjectdef(def).objecttype=odt_dispinterface);
  4857. end;
  4858. function is_class(def: tdef): boolean;
  4859. begin
  4860. is_class:=
  4861. assigned(def) and
  4862. (def.typ=objectdef) and
  4863. (tobjectdef(def).objecttype=odt_class);
  4864. end;
  4865. function is_object(def: tdef): boolean;
  4866. begin
  4867. is_object:=
  4868. assigned(def) and
  4869. (def.typ=objectdef) and
  4870. (tobjectdef(def).objecttype=odt_object);
  4871. end;
  4872. function is_cppclass(def: tdef): boolean;
  4873. begin
  4874. is_cppclass:=
  4875. assigned(def) and
  4876. (def.typ=objectdef) and
  4877. (tobjectdef(def).objecttype=odt_cppclass);
  4878. end;
  4879. function is_objcclass(def: tdef): boolean;
  4880. begin
  4881. is_objcclass:=
  4882. assigned(def) and
  4883. (def.typ=objectdef) and
  4884. (tobjectdef(def).objecttype=odt_objcclass);
  4885. end;
  4886. function is_objectpascal_helper(def: tdef): boolean;
  4887. begin
  4888. result:=
  4889. assigned(def) and
  4890. (def.typ=objectdef) and
  4891. (tobjectdef(def).objecttype=odt_helper);
  4892. end;
  4893. function is_objcclassref(def: tdef): boolean;
  4894. begin
  4895. is_objcclassref:=
  4896. assigned(def) and
  4897. (def.typ=classrefdef) and
  4898. is_objcclass(tclassrefdef(def).pointeddef);
  4899. end;
  4900. function is_objcprotocol(def: tdef): boolean;
  4901. begin
  4902. result:=
  4903. assigned(def) and
  4904. (def.typ=objectdef) and
  4905. (tobjectdef(def).objecttype=odt_objcprotocol);
  4906. end;
  4907. function is_objccategory(def: tdef): boolean;
  4908. begin
  4909. result:=
  4910. assigned(def) and
  4911. (def.typ=objectdef) and
  4912. { if used as a forward type }
  4913. ((tobjectdef(def).objecttype=odt_objccategory) or
  4914. { if used as after it has been resolved }
  4915. ((tobjectdef(def).objecttype=odt_objcclass) and
  4916. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4917. end;
  4918. function is_objc_class_or_protocol(def: tdef): boolean;
  4919. begin
  4920. result:=
  4921. assigned(def) and
  4922. (def.typ=objectdef) and
  4923. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  4924. end;
  4925. function is_objc_protocol_or_category(def: tdef): boolean;
  4926. begin
  4927. result:=
  4928. assigned(def) and
  4929. (def.typ=objectdef) and
  4930. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  4931. ((tobjectdef(def).objecttype = odt_objcclass) and
  4932. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4933. end;
  4934. function is_classhelper(def: tdef): boolean;
  4935. begin
  4936. result:=
  4937. is_objectpascal_helper(def) or
  4938. is_objccategory(def);
  4939. end;
  4940. function is_class_or_interface(def: tdef): boolean;
  4941. begin
  4942. result:=
  4943. assigned(def) and
  4944. (def.typ=objectdef) and
  4945. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  4946. end;
  4947. function is_class_or_interface_or_objc(def: tdef): boolean;
  4948. begin
  4949. result:=
  4950. assigned(def) and
  4951. (def.typ=objectdef) and
  4952. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  4953. end;
  4954. function is_class_or_interface_or_object(def: tdef): boolean;
  4955. begin
  4956. result:=
  4957. assigned(def) and
  4958. (def.typ=objectdef) and
  4959. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  4960. end;
  4961. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  4962. begin
  4963. result:=
  4964. assigned(def) and
  4965. (def.typ=objectdef) and
  4966. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  4967. end;
  4968. function is_implicit_pointer_object_type(def: tdef): boolean;
  4969. begin
  4970. result:=
  4971. assigned(def) and
  4972. (def.typ=objectdef) and
  4973. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol]);
  4974. end;
  4975. function is_class_or_object(def: tdef): boolean;
  4976. begin
  4977. result:=
  4978. assigned(def) and
  4979. (def.typ=objectdef) and
  4980. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  4981. end;
  4982. function is_record(def: tdef): boolean;
  4983. begin
  4984. result:=
  4985. assigned(def) and
  4986. (def.typ=recorddef);
  4987. end;
  4988. procedure loadobjctypes;
  4989. begin
  4990. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  4991. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  4992. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  4993. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  4994. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  4995. end;
  4996. procedure maybeloadcocoatypes;
  4997. var
  4998. tsym: ttypesym;
  4999. begin
  5000. if assigned(objc_fastenumeration) then
  5001. exit;
  5002. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONPROTOCOL',false);
  5003. if assigned(tsym) then
  5004. objc_fastenumeration:=tobjectdef(tsym.typedef)
  5005. else
  5006. objc_fastenumeration:=nil;
  5007. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONSTATE',false);
  5008. if assigned(tsym) then
  5009. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  5010. else
  5011. objc_fastenumerationstate:=nil;
  5012. end;
  5013. function use_vectorfpu(def : tdef) : boolean;
  5014. begin
  5015. {$ifdef x86}
  5016. {$define use_vectorfpuimplemented}
  5017. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  5018. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  5019. {$endif x86}
  5020. {$ifdef arm}
  5021. {$define use_vectorfpuimplemented}
  5022. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  5023. {$endif arm}
  5024. {$ifndef use_vectorfpuimplemented}
  5025. use_vectorfpu:=false;
  5026. {$endif}
  5027. end;
  5028. end.