symtable.pas 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. This unit handles the symbol tables
  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 symtable;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,cclasses,
  23. { global }
  24. cpuinfo,globtype,tokens,
  25. { symtable }
  26. symconst,symbase,symtype,symdef,symsym,
  27. { ppu }
  28. ppu,
  29. { assembler }
  30. aasmtai,aasmdata
  31. ;
  32. {****************************************************************************
  33. Symtable types
  34. ****************************************************************************}
  35. type
  36. tstoredsymtable = class(TSymtable)
  37. private
  38. b_needs_init_final : boolean;
  39. procedure _needs_init_final(sym:TObject;arg:pointer);
  40. procedure check_forward(sym:TObject;arg:pointer);
  41. procedure labeldefined(sym:TObject;arg:pointer);
  42. procedure varsymbolused(sym:TObject;arg:pointer);
  43. procedure TestPrivate(sym:TObject;arg:pointer);
  44. procedure objectprivatesymbolused(sym:TObject;arg:pointer);
  45. procedure loaddefs(ppufile:tcompilerppufile);
  46. procedure loadsyms(ppufile:tcompilerppufile);
  47. procedure writedefs(ppufile:tcompilerppufile);
  48. procedure writesyms(ppufile:tcompilerppufile);
  49. public
  50. procedure insert(sym:TSymEntry;checkdup:boolean=true);override;
  51. procedure delete(sym:TSymEntry);override;
  52. { load/write }
  53. procedure ppuload(ppufile:tcompilerppufile);virtual;
  54. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  55. procedure buildderef;virtual;
  56. procedure buildderefimpl;virtual;
  57. procedure deref;virtual;
  58. procedure derefimpl;virtual;
  59. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  60. procedure allsymbolsused;
  61. procedure allprivatesused;
  62. procedure check_forwards;
  63. procedure checklabels;
  64. function needs_init_final : boolean;
  65. procedure testfordefaultproperty(sym:TObject;arg:pointer);
  66. end;
  67. tabstractrecordsymtable = class(tstoredsymtable)
  68. public
  69. usefieldalignment, { alignment to use for fields (PACKRECORDS value), C_alignment is C style }
  70. recordalignment, { alignment desired when inserting this record }
  71. fieldalignment, { alignment current alignment used when fields are inserted }
  72. padalignment : shortint; { size to a multiple of which the symtable has to be rounded up }
  73. constructor create(const n:string;usealign:shortint);
  74. procedure ppuload(ppufile:tcompilerppufile);override;
  75. procedure ppuwrite(ppufile:tcompilerppufile);override;
  76. procedure alignrecord(fieldoffset:asizeint;varalign:shortint);
  77. procedure addfield(sym:tfieldvarsym;vis:tvisibility);
  78. procedure addalignmentpadding;
  79. procedure insertdef(def:TDefEntry);override;
  80. function is_packed: boolean;
  81. function has_single_field(out sym:tfieldvarsym): boolean;
  82. function get_unit_symtable: tsymtable;
  83. protected
  84. { size in bytes including padding }
  85. _datasize : asizeint;
  86. { size in bits of the data in case of bitpacked record. Only important during construction, }
  87. { no need to save in/restore from ppu file. datasize is always (databitsize+7) div 8. }
  88. databitsize : asizeint;
  89. { size in bytes of padding }
  90. _paddingsize : word;
  91. procedure setdatasize(val: asizeint);
  92. public
  93. function iscurrentunit: boolean; override;
  94. property datasize : asizeint read _datasize write setdatasize;
  95. property paddingsize: word read _paddingsize write _paddingsize;
  96. end;
  97. trecordsymtable = class(tabstractrecordsymtable)
  98. public
  99. constructor create(const n:string;usealign:shortint);
  100. procedure insertunionst(unionst : trecordsymtable;offset : longint);
  101. end;
  102. tObjectSymtable = class(tabstractrecordsymtable)
  103. public
  104. constructor create(adefowner:tdef;const n:string;usealign:shortint);
  105. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  106. end;
  107. { tabstractlocalsymtable }
  108. tabstractlocalsymtable = class(tstoredsymtable)
  109. public
  110. procedure ppuwrite(ppufile:tcompilerppufile);override;
  111. function count_locals:longint;
  112. end;
  113. tlocalsymtable = class(tabstractlocalsymtable)
  114. public
  115. constructor create(adefowner:tdef;level:byte);
  116. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  117. end;
  118. { tparasymtable }
  119. tparasymtable = class(tabstractlocalsymtable)
  120. public
  121. readonly: boolean;
  122. constructor create(adefowner:tdef;level:byte);
  123. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  124. procedure insertdef(def:TDefEntry);override;
  125. end;
  126. tabstractuniTSymtable = class(tstoredsymtable)
  127. public
  128. constructor create(const n : string;id:word);
  129. function iscurrentunit:boolean;override;
  130. end;
  131. tglobalsymtable = class(tabstractuniTSymtable)
  132. public
  133. unittypecount : word;
  134. constructor create(const n : string;id:word);
  135. procedure ppuload(ppufile:tcompilerppufile);override;
  136. procedure ppuwrite(ppufile:tcompilerppufile);override;
  137. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  138. end;
  139. tstaticsymtable = class(tabstractuniTSymtable)
  140. public
  141. constructor create(const n : string;id:word);
  142. procedure ppuload(ppufile:tcompilerppufile);override;
  143. procedure ppuwrite(ppufile:tcompilerppufile);override;
  144. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  145. end;
  146. twithsymtable = class(TSymtable)
  147. withrefnode : tobject; { tnode }
  148. constructor create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  149. destructor destroy;override;
  150. procedure clear;override;
  151. procedure insertdef(def:TDefEntry);override;
  152. end;
  153. tstt_excepTSymtable = class(TSymtable)
  154. public
  155. constructor create;
  156. end;
  157. tmacrosymtable = class(tstoredsymtable)
  158. public
  159. constructor create(exported: boolean);
  160. end;
  161. { tenumsymtable }
  162. tenumsymtable = class(tstoredsymtable)
  163. public
  164. procedure insert(sym: TSymEntry; checkdup: boolean = true); override;
  165. constructor create(adefowner:tdef);
  166. end;
  167. { tarraysymtable }
  168. tarraysymtable = class(tstoredsymtable)
  169. public
  170. procedure insertdef(def:TDefEntry);override;
  171. constructor create(adefowner:tdef);
  172. end;
  173. var
  174. systemunit : tglobalsymtable; { pointer to the system unit }
  175. {****************************************************************************
  176. Functions
  177. ****************************************************************************}
  178. {*** Misc ***}
  179. function FullTypeName(def,otherdef:tdef):string;
  180. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  181. procedure incompatibletypes(def1,def2:tdef);
  182. procedure hidesym(sym:TSymEntry);
  183. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  184. {*** Search ***}
  185. procedure addsymref(sym:tsym);
  186. function is_owned_by(childdef,ownerdef:tabstractrecorddef):boolean;
  187. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  188. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  189. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  190. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  191. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  192. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  193. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  194. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;searchhelper:boolean):boolean;
  195. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  196. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  197. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  198. { searches symbols inside of a helper's implementation }
  199. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;aHasInherited:boolean):boolean;
  200. function search_system_type(const s: TIDString): ttypesym;
  201. function try_search_system_type(const s: TIDString): ttypesym;
  202. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  203. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  204. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  205. function search_enumerator_operator(from_def,to_def:Tdef):Tprocdef;
  206. { searches for the helper definition that's currently active for pd }
  207. function search_last_objectpascal_helper(pd,contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  208. { searches whether the symbol s is available in the currently active }
  209. { helper for pd }
  210. function search_objectpascal_helper(pd,contextclassh : tabstractrecorddef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  211. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  212. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  213. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  214. {and returns it if found. Returns nil otherwise.}
  215. function search_macro(const s : string):tsym;
  216. { Additionally to searching for a macro, also checks whether it's still }
  217. { actually defined (could be disable using "undef") }
  218. function defined_macro(const s : string):boolean;
  219. {*** Object Helpers ***}
  220. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  221. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  222. {*** Macro Helpers ***}
  223. {If called initially, the following procedures manipulate macros in }
  224. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  225. {Name can be given in any case (it will be converted to upper case).}
  226. procedure def_system_macro(const name : string);
  227. procedure set_system_macro(const name, value : string);
  228. procedure set_system_compvar(const name, value : string);
  229. procedure undef_system_macro(const name : string);
  230. {*** symtable stack ***}
  231. { $ifdef DEBUG
  232. procedure test_symtablestack;
  233. procedure list_symtablestack;
  234. $endif DEBUG}
  235. {$ifdef UNITALIASES}
  236. type
  237. punit_alias = ^tunit_alias;
  238. tunit_alias = object(TNamedIndexItem)
  239. newname : pshortstring;
  240. constructor init(const n:string);
  241. destructor done;virtual;
  242. end;
  243. var
  244. unitaliases : pdictionary;
  245. procedure addunitalias(const n:string);
  246. function getunitalias(const n:string):string;
  247. {$endif UNITALIASES}
  248. {*** Init / Done ***}
  249. procedure IniTSymtable;
  250. procedure DoneSymtable;
  251. const
  252. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] = (
  253. { NOTOKEN } 'error',
  254. { _PLUS } 'plus',
  255. { _MINUS } 'minus',
  256. { _STAR } 'star',
  257. { _SLASH } 'slash',
  258. { _EQ } 'equal',
  259. { _GT } 'greater',
  260. { _LT } 'lower',
  261. { _GTE } 'greater_or_equal',
  262. { _LTE } 'lower_or_equal',
  263. { _NE } 'not_equal',
  264. { _SYMDIF } 'sym_diff',
  265. { _STARSTAR } 'starstar',
  266. { _OP_AS } 'as',
  267. { _OP_IN } 'in',
  268. { _OP_IS } 'is',
  269. { _OP_OR } 'or',
  270. { _OP_AND } 'and',
  271. { _OP_DIV } 'div',
  272. { _OP_MOD } 'mod',
  273. { _OP_NOT } 'not',
  274. { _OP_SHL } 'shl',
  275. { _OP_SHR } 'shr',
  276. { _OP_XOR } 'xor',
  277. { _ASSIGNMENT } 'assign',
  278. { _OP_EXPLICIT } 'explicit',
  279. { _OP_ENUMERATOR } 'enumerator',
  280. { _OP_INC } 'inc',
  281. { _OP_DEC } 'dec');
  282. implementation
  283. uses
  284. { global }
  285. verbose,globals,
  286. { target }
  287. systems,
  288. { symtable }
  289. symutil,defcmp,defutil,
  290. { module }
  291. fmodule,
  292. { codegen }
  293. procinfo
  294. ;
  295. var
  296. dupnr : longint; { unique number for duplicate symbols }
  297. {*****************************************************************************
  298. TStoredSymtable
  299. *****************************************************************************}
  300. procedure tstoredsymtable.insert(sym:TSymEntry;checkdup:boolean=true);
  301. begin
  302. inherited insert(sym,checkdup);
  303. end;
  304. procedure tstoredsymtable.delete(sym:TSymEntry);
  305. begin
  306. inherited delete(sym);
  307. end;
  308. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  309. begin
  310. { load the table's flags }
  311. if ppufile.readentry<>ibsymtableoptions then
  312. Message(unit_f_ppu_read_error);
  313. ppufile.getsmallset(tableoptions);
  314. { load definitions }
  315. loaddefs(ppufile);
  316. { load symbols }
  317. loadsyms(ppufile);
  318. end;
  319. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  320. begin
  321. { write the table's flags }
  322. ppufile.putsmallset(tableoptions);
  323. ppufile.writeentry(ibsymtableoptions);
  324. { write definitions }
  325. writedefs(ppufile);
  326. { write symbols }
  327. writesyms(ppufile);
  328. end;
  329. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  330. var
  331. def : tdef;
  332. b : byte;
  333. begin
  334. { load start of definition section, which holds the amount of defs }
  335. if ppufile.readentry<>ibstartdefs then
  336. Message(unit_f_ppu_read_error);
  337. { read definitions }
  338. repeat
  339. b:=ppufile.readentry;
  340. case b of
  341. ibpointerdef : def:=tpointerdef.ppuload(ppufile);
  342. ibarraydef : def:=tarraydef.ppuload(ppufile);
  343. iborddef : def:=torddef.ppuload(ppufile);
  344. ibfloatdef : def:=tfloatdef.ppuload(ppufile);
  345. ibprocdef : def:=tprocdef.ppuload(ppufile);
  346. ibshortstringdef : def:=tstringdef.loadshort(ppufile);
  347. iblongstringdef : def:=tstringdef.loadlong(ppufile);
  348. ibansistringdef : def:=tstringdef.loadansi(ppufile);
  349. ibwidestringdef : def:=tstringdef.loadwide(ppufile);
  350. ibunicodestringdef : def:=tstringdef.loadunicode(ppufile);
  351. ibrecorddef : def:=trecorddef.ppuload(ppufile);
  352. ibobjectdef : def:=tobjectdef.ppuload(ppufile);
  353. ibenumdef : def:=tenumdef.ppuload(ppufile);
  354. ibsetdef : def:=tsetdef.ppuload(ppufile);
  355. ibprocvardef : def:=tprocvardef.ppuload(ppufile);
  356. ibfiledef : def:=tfiledef.ppuload(ppufile);
  357. ibclassrefdef : def:=tclassrefdef.ppuload(ppufile);
  358. ibformaldef : def:=tformaldef.ppuload(ppufile);
  359. ibvariantdef : def:=tvariantdef.ppuload(ppufile);
  360. ibundefineddef : def:=tundefineddef.ppuload(ppufile);
  361. ibenddefs : break;
  362. ibend : Message(unit_f_ppu_read_error);
  363. else
  364. Message1(unit_f_ppu_invalid_entry,tostr(b));
  365. end;
  366. InsertDef(def);
  367. until false;
  368. end;
  369. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  370. var
  371. b : byte;
  372. sym : tsym;
  373. begin
  374. { load start of definition section, which holds the amount of defs }
  375. if ppufile.readentry<>ibstartsyms then
  376. Message(unit_f_ppu_read_error);
  377. { now read the symbols }
  378. repeat
  379. b:=ppufile.readentry;
  380. case b of
  381. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  382. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  383. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  384. ibstaticvarsym : sym:=tstaticvarsym.ppuload(ppufile);
  385. iblocalvarsym : sym:=tlocalvarsym.ppuload(ppufile);
  386. ibparavarsym : sym:=tparavarsym.ppuload(ppufile);
  387. ibfieldvarsym : sym:=tfieldvarsym.ppuload(ppufile);
  388. ibabsolutevarsym : sym:=tabsolutevarsym.ppuload(ppufile);
  389. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  390. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  391. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  392. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  393. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  394. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  395. ibendsyms : break;
  396. ibend : Message(unit_f_ppu_read_error);
  397. else
  398. Message1(unit_f_ppu_invalid_entry,tostr(b));
  399. end;
  400. Insert(sym,false);
  401. until false;
  402. end;
  403. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  404. var
  405. i : longint;
  406. def : tstoreddef;
  407. begin
  408. { each definition get a number, write then the amount of defs to the
  409. ibstartdef entry }
  410. ppufile.putlongint(DefList.count);
  411. ppufile.writeentry(ibstartdefs);
  412. { now write the definition }
  413. for i:=0 to DefList.Count-1 do
  414. begin
  415. def:=tstoreddef(DefList[i]);
  416. def.ppuwrite(ppufile);
  417. end;
  418. { write end of definitions }
  419. ppufile.writeentry(ibenddefs);
  420. end;
  421. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  422. var
  423. i : longint;
  424. sym : Tstoredsym;
  425. begin
  426. { each definition get a number, write then the amount of syms and the
  427. datasize to the ibsymdef entry }
  428. ppufile.putlongint(SymList.count);
  429. ppufile.writeentry(ibstartsyms);
  430. { foreach is used to write all symbols }
  431. for i:=0 to SymList.Count-1 do
  432. begin
  433. sym:=tstoredsym(SymList[i]);
  434. sym.ppuwrite(ppufile);
  435. end;
  436. { end of symbols }
  437. ppufile.writeentry(ibendsyms);
  438. end;
  439. procedure tstoredsymtable.buildderef;
  440. var
  441. i : longint;
  442. def : tstoreddef;
  443. sym : tstoredsym;
  444. begin
  445. { interface definitions }
  446. for i:=0 to DefList.Count-1 do
  447. begin
  448. def:=tstoreddef(DefList[i]);
  449. def.buildderef;
  450. end;
  451. { interface symbols }
  452. for i:=0 to SymList.Count-1 do
  453. begin
  454. sym:=tstoredsym(SymList[i]);
  455. sym.buildderef;
  456. end;
  457. end;
  458. procedure tstoredsymtable.buildderefimpl;
  459. var
  460. i : longint;
  461. def : tstoreddef;
  462. begin
  463. { implementation definitions }
  464. for i:=0 to DefList.Count-1 do
  465. begin
  466. def:=tstoreddef(DefList[i]);
  467. def.buildderefimpl;
  468. end;
  469. end;
  470. procedure tstoredsymtable.deref;
  471. var
  472. i : longint;
  473. def : tstoreddef;
  474. sym : tstoredsym;
  475. begin
  476. { first deref the interface ttype symbols. This is needs
  477. to be done before the interface defs are derefed, because
  478. the interface defs can contain references to the type symbols
  479. which then already need to contain a resolved typedef field (PFV) }
  480. for i:=0 to SymList.Count-1 do
  481. begin
  482. sym:=tstoredsym(SymList[i]);
  483. if sym.typ=typesym then
  484. sym.deref;
  485. end;
  486. { interface definitions }
  487. for i:=0 to DefList.Count-1 do
  488. begin
  489. def:=tstoreddef(DefList[i]);
  490. def.deref;
  491. end;
  492. { interface symbols }
  493. for i:=0 to SymList.Count-1 do
  494. begin
  495. sym:=tstoredsym(SymList[i]);
  496. if sym.typ<>typesym then
  497. sym.deref;
  498. end;
  499. end;
  500. procedure tstoredsymtable.derefimpl;
  501. var
  502. i : longint;
  503. def : tstoreddef;
  504. begin
  505. { implementation definitions }
  506. for i:=0 to DefList.Count-1 do
  507. begin
  508. def:=tstoreddef(DefList[i]);
  509. def.derefimpl;
  510. end;
  511. end;
  512. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  513. var
  514. hsym : tsym;
  515. begin
  516. hsym:=tsym(FindWithHash(hashedid));
  517. if assigned(hsym) then
  518. DuplicateSym(hashedid,sym,hsym);
  519. result:=assigned(hsym);
  520. end;
  521. {**************************************
  522. Callbacks
  523. **************************************}
  524. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  525. begin
  526. if tsym(sym).typ=procsym then
  527. tprocsym(sym).check_forward
  528. { check also object method table }
  529. { we needn't to test the def list }
  530. { because each object has to have a type sym,
  531. only test objects declarations, not type renamings }
  532. else
  533. if (tsym(sym).typ=typesym) and
  534. assigned(ttypesym(sym).typedef) and
  535. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  536. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) then
  537. tabstractrecorddef(ttypesym(sym).typedef).check_forwards;
  538. end;
  539. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  540. begin
  541. if (tsym(sym).typ=labelsym) and
  542. not(tlabelsym(sym).defined) then
  543. begin
  544. if tlabelsym(sym).used then
  545. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  546. else
  547. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  548. end;
  549. end;
  550. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  551. begin
  552. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  553. ((tsym(sym).owner.symtabletype in
  554. [parasymtable,localsymtable,ObjectSymtable,recordsymtable,staticsymtable])) then
  555. begin
  556. { unused symbol should be reported only if no }
  557. { error is reported }
  558. { if the symbol is in a register it is used }
  559. { also don't count the value parameters which have local copies }
  560. { also don't claim for high param of open parameters (PM) }
  561. { also don't complain about unused symbols in generic procedures }
  562. { and methods }
  563. if (Errorcount<>0) or
  564. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) or
  565. (sp_internal in tsym(sym).symoptions) or
  566. ((assigned(tsym(sym).owner.defowner) and
  567. (tsym(sym).owner.defowner.typ=procdef) and
  568. (df_generic in tprocdef(tsym(sym).owner.defowner).defoptions))) then
  569. exit;
  570. if (tstoredsym(sym).refs=0) then
  571. begin
  572. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  573. begin
  574. { don't warn about the result of constructors }
  575. if ((tsym(sym).owner.symtabletype<>localsymtable) or
  576. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor)) and
  577. not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  578. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  579. end
  580. else if (tsym(sym).owner.symtabletype=parasymtable) then
  581. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).prettyname)
  582. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  583. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname)
  584. else
  585. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).prettyname);
  586. end
  587. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  588. begin
  589. if (tsym(sym).owner.symtabletype=parasymtable) then
  590. begin
  591. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out,vs_constref]) and
  592. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  593. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).prettyname)
  594. end
  595. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  596. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname)
  597. else if tabstractvarsym(sym).varoptions*[vo_is_funcret,vo_is_public,vo_is_external]=[] then
  598. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).prettyname);
  599. end
  600. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  601. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  602. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).prettyname)
  603. end
  604. else if ((tsym(sym).owner.symtabletype in
  605. [ObjectSymtable,parasymtable,localsymtable,staticsymtable,recordsymtable])) then
  606. begin
  607. if (Errorcount<>0) or
  608. (sp_internal in tsym(sym).symoptions) then
  609. exit;
  610. { do not claim for inherited private fields !! }
  611. if (tsym(sym).refs=0) and (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  612. case tsym(sym).typ of
  613. typesym:
  614. MessagePos2(tsym(sym).fileinfo,sym_n_private_type_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  615. constsym:
  616. MessagePos2(tsym(sym).fileinfo,sym_n_private_const_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  617. propertysym:
  618. MessagePos2(tsym(sym).fileinfo,sym_n_private_property_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  619. else
  620. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  621. end
  622. { units references are problematic }
  623. else
  624. begin
  625. if (tsym(sym).refs=0) and
  626. not(tsym(sym).typ in [enumsym,unitsym]) and
  627. not(is_funcret_sym(tsym(sym))) and
  628. { don't complain about compiler generated syms for specializations, see also #13405 }
  629. not((tsym(sym).typ=typesym) and (df_specialization in ttypesym(sym).typedef.defoptions) and
  630. (pos('$',ttypesym(sym).Realname)<>0)) and
  631. (
  632. (tsym(sym).typ<>procsym) or
  633. ((tsym(sym).owner.symtabletype=staticsymtable) and
  634. not current_module.is_unit)
  635. ) and
  636. { don't complain about alias for hidden _cmd parameter to
  637. obj-c methods }
  638. not((tsym(sym).typ in [localvarsym,paravarsym,absolutevarsym]) and
  639. (vo_is_msgsel in tabstractvarsym(sym).varoptions)) then
  640. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).prettyname);
  641. end;
  642. end;
  643. end;
  644. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  645. begin
  646. if tsym(sym).visibility in [vis_private,vis_strictprivate] then
  647. varsymbolused(sym,arg);
  648. end;
  649. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  650. begin
  651. {
  652. Don't test simple object aliases PM
  653. }
  654. if (tsym(sym).typ=typesym) and
  655. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) and
  656. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  657. tabstractrecorddef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  658. end;
  659. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  660. begin
  661. if (tsym(sym).typ=propertysym) and
  662. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  663. ppointer(arg)^:=sym;
  664. end;
  665. {***********************************************
  666. Process all entries
  667. ***********************************************}
  668. { checks, if all procsyms and methods are defined }
  669. procedure tstoredsymtable.check_forwards;
  670. begin
  671. SymList.ForEachCall(@check_forward,nil);
  672. end;
  673. procedure tstoredsymtable.checklabels;
  674. begin
  675. SymList.ForEachCall(@labeldefined,nil);
  676. end;
  677. procedure tstoredsymtable.allsymbolsused;
  678. begin
  679. SymList.ForEachCall(@varsymbolused,nil);
  680. end;
  681. procedure tstoredsymtable.allprivatesused;
  682. begin
  683. SymList.ForEachCall(@objectprivatesymbolused,nil);
  684. end;
  685. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  686. begin
  687. if b_needs_init_final then
  688. exit;
  689. { don't check static symbols - they can be present in structures only and
  690. always have a reference to a symbol defined on unit level }
  691. if sp_static in tsym(sym).symoptions then
  692. exit;
  693. case tsym(sym).typ of
  694. fieldvarsym,
  695. staticvarsym,
  696. localvarsym,
  697. paravarsym :
  698. begin
  699. if is_managed_type(tabstractvarsym(sym).vardef) then
  700. b_needs_init_final:=true;
  701. end;
  702. end;
  703. end;
  704. { returns true, if p contains data which needs init/final code }
  705. function tstoredsymtable.needs_init_final : boolean;
  706. begin
  707. b_needs_init_final:=false;
  708. SymList.ForEachCall(@_needs_init_final,nil);
  709. needs_init_final:=b_needs_init_final;
  710. end;
  711. {****************************************************************************
  712. TAbstractRecordSymtable
  713. ****************************************************************************}
  714. constructor tabstractrecordsymtable.create(const n:string;usealign:shortint);
  715. begin
  716. inherited create(n);
  717. moduleid:=current_module.moduleid;
  718. _datasize:=0;
  719. databitsize:=0;
  720. recordalignment:=1;
  721. usefieldalignment:=usealign;
  722. padalignment:=1;
  723. { recordalign C_alignment means C record packing, that starts
  724. with an alignment of 1 }
  725. case usealign of
  726. C_alignment,
  727. bit_alignment:
  728. fieldalignment:=1;
  729. mac68k_alignment:
  730. fieldalignment:=2;
  731. else
  732. fieldalignment:=usealign;
  733. end;
  734. end;
  735. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  736. begin
  737. if ppufile.readentry<>ibrecsymtableoptions then
  738. Message(unit_f_ppu_read_error);
  739. recordalignment:=shortint(ppufile.getbyte);
  740. usefieldalignment:=shortint(ppufile.getbyte);
  741. if (usefieldalignment=C_alignment) then
  742. fieldalignment:=shortint(ppufile.getbyte);
  743. inherited ppuload(ppufile);
  744. end;
  745. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  746. var
  747. oldtyp : byte;
  748. begin
  749. oldtyp:=ppufile.entrytyp;
  750. ppufile.entrytyp:=subentryid;
  751. { in case of classes using C alignment, the alignment of the parent
  752. affects the alignment of fields of the childs }
  753. ppufile.putbyte(byte(recordalignment));
  754. ppufile.putbyte(byte(usefieldalignment));
  755. if (usefieldalignment=C_alignment) then
  756. ppufile.putbyte(byte(fieldalignment));
  757. ppufile.writeentry(ibrecsymtableoptions);
  758. inherited ppuwrite(ppufile);
  759. ppufile.entrytyp:=oldtyp;
  760. end;
  761. function field2recordalignment(fieldoffs, fieldalign: asizeint): asizeint;
  762. begin
  763. { optimal alignment of the record when declaring a variable of this }
  764. { type is independent of the packrecords setting }
  765. if (fieldoffs mod fieldalign) = 0 then
  766. result:=fieldalign
  767. else if (fieldalign >= 16) and
  768. ((fieldoffs mod 16) = 0) and
  769. ((fieldalign mod 16) = 0) then
  770. result:=16
  771. else if (fieldalign >= 8) and
  772. ((fieldoffs mod 8) = 0) and
  773. ((fieldalign mod 8) = 0) then
  774. result:=8
  775. else if (fieldalign >= 4) and
  776. ((fieldoffs mod 4) = 0) and
  777. ((fieldalign mod 4) = 0) then
  778. result:=4
  779. else if (fieldalign >= 2) and
  780. ((fieldoffs mod 2) = 0) and
  781. ((fieldalign mod 2) = 0) then
  782. result:=2
  783. else
  784. result:=1;
  785. end;
  786. procedure tabstractrecordsymtable.alignrecord(fieldoffset:asizeint;varalign:shortint);
  787. var
  788. varalignrecord: shortint;
  789. begin
  790. case usefieldalignment of
  791. C_alignment:
  792. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  793. mac68k_alignment:
  794. varalignrecord:=2;
  795. else
  796. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  797. end;
  798. recordalignment:=max(recordalignment,varalignrecord);
  799. end;
  800. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  801. var
  802. l : asizeint;
  803. varalignfield,
  804. varalign : shortint;
  805. vardef : tdef;
  806. begin
  807. if (sym.owner<>self) then
  808. internalerror(200602031);
  809. if sym.fieldoffset<>-1 then
  810. internalerror(200602032);
  811. { set visibility for the symbol }
  812. sym.visibility:=vis;
  813. { this symbol can't be loaded to a register }
  814. sym.varregable:=vr_none;
  815. { Calculate field offset }
  816. l:=sym.getsize;
  817. vardef:=sym.vardef;
  818. varalign:=vardef.alignment;
  819. case usefieldalignment of
  820. bit_alignment:
  821. begin
  822. { bitpacking only happens for ordinals, the rest is aligned at }
  823. { 1 byte (compatible with GPC/GCC) }
  824. if is_ordinal(vardef) then
  825. begin
  826. sym.fieldoffset:=databitsize;
  827. l:=sym.getpackedbitsize;
  828. end
  829. else
  830. begin
  831. databitsize:=_datasize*8;
  832. sym.fieldoffset:=databitsize;
  833. if (l>high(asizeint) div 8) then
  834. Message(sym_e_segment_too_large);
  835. l:=l*8;
  836. end;
  837. if varalign=0 then
  838. varalign:=size_2_align(l);
  839. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  840. { bit packed records are limited to high(aint) bits }
  841. { instead of bytes to avoid double precision }
  842. { arithmetic in offset calculations }
  843. if int64(l)>high(asizeint)-sym.fieldoffset then
  844. begin
  845. Message(sym_e_segment_too_large);
  846. _datasize:=high(asizeint);
  847. databitsize:=high(asizeint);
  848. end
  849. else
  850. begin
  851. databitsize:=sym.fieldoffset+l;
  852. _datasize:=(databitsize+7) div 8;
  853. end;
  854. { rest is not applicable }
  855. exit;
  856. end;
  857. { Calc the alignment size for C style records }
  858. C_alignment:
  859. begin
  860. if (varalign>4) and
  861. ((varalign mod 4)<>0) and
  862. (vardef.typ=arraydef) then
  863. Message1(sym_w_wrong_C_pack,vardef.typename);
  864. if varalign=0 then
  865. varalign:=l;
  866. if (fieldalignment<current_settings.alignment.maxCrecordalign) then
  867. begin
  868. if (varalign>16) and (fieldalignment<32) then
  869. fieldalignment:=32
  870. else if (varalign>12) and (fieldalignment<16) then
  871. fieldalignment:=16
  872. { 12 is needed for long double }
  873. else if (varalign>8) and (fieldalignment<12) then
  874. fieldalignment:=12
  875. else if (varalign>4) and (fieldalignment<8) then
  876. fieldalignment:=8
  877. else if (varalign>2) and (fieldalignment<4) then
  878. fieldalignment:=4
  879. else if (varalign>1) and (fieldalignment<2) then
  880. fieldalignment:=2;
  881. end;
  882. fieldalignment:=min(fieldalignment,current_settings.alignment.maxCrecordalign);
  883. end;
  884. mac68k_alignment:
  885. begin
  886. { mac68k alignment (C description):
  887. * char is aligned to 1 byte
  888. * everything else (except vector) is aligned to 2 bytes
  889. * vector is aligned to 16 bytes
  890. }
  891. if l>1 then
  892. fieldalignment:=2
  893. else
  894. fieldalignment:=1;
  895. varalign:=2;
  896. end;
  897. end;
  898. if varalign=0 then
  899. varalign:=size_2_align(l);
  900. varalignfield:=used_align(varalign,current_settings.alignment.recordalignmin,fieldalignment);
  901. sym.fieldoffset:=align(_datasize,varalignfield);
  902. if l>high(asizeint)-sym.fieldoffset then
  903. begin
  904. Message(sym_e_segment_too_large);
  905. _datasize:=high(asizeint);
  906. end
  907. else
  908. _datasize:=sym.fieldoffset+l;
  909. { Calc alignment needed for this record }
  910. alignrecord(sym.fieldoffset,varalign);
  911. end;
  912. procedure tabstractrecordsymtable.addalignmentpadding;
  913. var
  914. padded_datasize: asizeint;
  915. begin
  916. { make the record size aligned correctly so it can be
  917. used as elements in an array. For C records we
  918. use the fieldalignment, because that is updated with the
  919. used alignment. }
  920. if (padalignment = 1) then
  921. case usefieldalignment of
  922. C_alignment:
  923. padalignment:=fieldalignment;
  924. { bitpacked }
  925. bit_alignment:
  926. padalignment:=1;
  927. { mac68k: always round to multiple of 2 }
  928. mac68k_alignment:
  929. padalignment:=2;
  930. { default/no packrecords specified }
  931. 0:
  932. padalignment:=recordalignment
  933. { specific packrecords setting -> use as upper limit }
  934. else
  935. padalignment:=min(recordalignment,usefieldalignment);
  936. end;
  937. padded_datasize:=align(_datasize,padalignment);
  938. _paddingsize:=padded_datasize-_datasize;
  939. _datasize:=padded_datasize;
  940. end;
  941. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  942. begin
  943. { Enums must also be available outside the record scope,
  944. insert in the owner of this symtable }
  945. if def.typ=enumdef then
  946. defowner.owner.insertdef(def)
  947. else
  948. inherited insertdef(def);
  949. end;
  950. function tabstractrecordsymtable.is_packed: boolean;
  951. begin
  952. result:=usefieldalignment=bit_alignment;
  953. end;
  954. function tabstractrecordsymtable.has_single_field(out sym: tfieldvarsym): boolean;
  955. var
  956. i: longint;
  957. begin
  958. result:=false;
  959. { If a record contains a union, it does not contain a "single
  960. non-composite field" in the context of certain ABIs requiring
  961. special treatment for such records }
  962. if (defowner.typ=recorddef) and
  963. trecorddef(defowner).isunion then
  964. exit;
  965. { a record/object can contain other things than fields }
  966. for i:=0 to SymList.Count-1 do
  967. begin
  968. if tsym(symlist[i]).typ=fieldvarsym then
  969. begin
  970. if result then
  971. begin
  972. result:=false;
  973. exit;
  974. end;
  975. result:=true;
  976. sym:=tfieldvarsym(symlist[i])
  977. end;
  978. end;
  979. end;
  980. function tabstractrecordsymtable.get_unit_symtable: tsymtable;
  981. begin
  982. result:=defowner.owner;
  983. while assigned(result) and (result.symtabletype in [ObjectSymtable,recordsymtable]) do
  984. result:=result.defowner.owner;
  985. end;
  986. procedure tabstractrecordsymtable.setdatasize(val: asizeint);
  987. begin
  988. _datasize:=val;
  989. if (usefieldalignment=bit_alignment) then
  990. { can overflow in non bitpacked records }
  991. databitsize:=val*8;
  992. end;
  993. function tabstractrecordsymtable.iscurrentunit: boolean;
  994. begin
  995. Result := Assigned(current_module) and (current_module.moduleid=moduleid);
  996. end;
  997. {****************************************************************************
  998. TRecordSymtable
  999. ****************************************************************************}
  1000. constructor trecordsymtable.create(const n:string;usealign:shortint);
  1001. begin
  1002. inherited create(n,usealign);
  1003. symtabletype:=recordsymtable;
  1004. end;
  1005. { this procedure is reserved for inserting case variant into
  1006. a record symtable }
  1007. { the offset is the location of the start of the variant
  1008. and datasize and dataalignment corresponds to
  1009. the complete size (see code in pdecl unit) PM }
  1010. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  1011. var
  1012. sym : tsym;
  1013. def : tdef;
  1014. i : integer;
  1015. varalignrecord,varalign,
  1016. storesize,storealign : aint;
  1017. bitsize: aint;
  1018. begin
  1019. storesize:=_datasize;
  1020. storealign:=fieldalignment;
  1021. _datasize:=offset;
  1022. if (usefieldalignment=bit_alignment) then
  1023. databitsize:=offset*8;
  1024. { We move the ownership of the defs and symbols to the new recordsymtable.
  1025. The old unionsymtable keeps the references, but doesn't own the
  1026. objects anymore }
  1027. unionst.DefList.OwnsObjects:=false;
  1028. unionst.SymList.OwnsObjects:=false;
  1029. { copy symbols }
  1030. for i:=0 to unionst.SymList.Count-1 do
  1031. begin
  1032. sym:=TSym(unionst.SymList[i]);
  1033. if sym.typ<>fieldvarsym then
  1034. internalerror(200601272);
  1035. if tfieldvarsym(sym).fieldoffset=0 then
  1036. include(tfieldvarsym(sym).varoptions,vo_is_first_field);
  1037. { add to this record symtable }
  1038. // unionst.SymList.List.List^[i].Data:=nil;
  1039. sym.ChangeOwner(self);
  1040. varalign:=tfieldvarsym(sym).vardef.alignment;
  1041. if varalign=0 then
  1042. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  1043. { retrieve size }
  1044. if (usefieldalignment=bit_alignment) then
  1045. begin
  1046. { bit packed records are limited to high(aint) bits }
  1047. { instead of bytes to avoid double precision }
  1048. { arithmetic in offset calculations }
  1049. if is_ordinal(tfieldvarsym(sym).vardef) then
  1050. bitsize:=tfieldvarsym(sym).getpackedbitsize
  1051. else
  1052. begin
  1053. bitsize:=tfieldvarsym(sym).getsize;
  1054. if (bitsize>high(asizeint) div 8) then
  1055. Message(sym_e_segment_too_large);
  1056. bitsize:=bitsize*8;
  1057. end;
  1058. if bitsize>high(asizeint)-databitsize then
  1059. begin
  1060. Message(sym_e_segment_too_large);
  1061. _datasize:=high(asizeint);
  1062. databitsize:=high(asizeint);
  1063. end
  1064. else
  1065. begin
  1066. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  1067. _datasize:=(databitsize+7) div 8;
  1068. end;
  1069. tfieldvarsym(sym).fieldoffset:=databitsize;
  1070. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  1071. end
  1072. else
  1073. begin
  1074. if tfieldvarsym(sym).getsize>high(asizeint)-_datasize then
  1075. begin
  1076. Message(sym_e_segment_too_large);
  1077. _datasize:=high(asizeint);
  1078. end
  1079. else
  1080. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  1081. { update address }
  1082. tfieldvarsym(sym).fieldoffset:=_datasize;
  1083. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  1084. end;
  1085. { update alignment of this record }
  1086. if (usefieldalignment<>C_alignment) and
  1087. (usefieldalignment<>mac68k_alignment) then
  1088. recordalignment:=max(recordalignment,varalignrecord);
  1089. end;
  1090. { update alignment for C records }
  1091. if (usefieldalignment=C_alignment) and
  1092. (usefieldalignment<>mac68k_alignment) then
  1093. recordalignment:=max(recordalignment,unionst.recordalignment);
  1094. { Register defs in the new record symtable }
  1095. for i:=0 to unionst.DefList.Count-1 do
  1096. begin
  1097. def:=TDef(unionst.DefList[i]);
  1098. def.ChangeOwner(self);
  1099. end;
  1100. _datasize:=storesize;
  1101. fieldalignment:=storealign;
  1102. { If a record contains a union, it does not contain a "single
  1103. non-composite field" in the context of certain ABIs requiring
  1104. special treatment for such records }
  1105. if defowner.typ=recorddef then
  1106. trecorddef(defowner).isunion:=true;
  1107. end;
  1108. {****************************************************************************
  1109. TObjectSymtable
  1110. ****************************************************************************}
  1111. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign:shortint);
  1112. begin
  1113. inherited create(n,usealign);
  1114. symtabletype:=ObjectSymtable;
  1115. defowner:=adefowner;
  1116. end;
  1117. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1118. var
  1119. hsym : tsym;
  1120. begin
  1121. result:=false;
  1122. if not assigned(defowner) then
  1123. internalerror(200602061);
  1124. { procsym and propertysym have special code
  1125. to override values in inherited classes. For other
  1126. symbols check for duplicates }
  1127. if not(sym.typ in [procsym,propertysym]) then
  1128. begin
  1129. { but private ids can be reused }
  1130. hsym:=search_struct_member(tobjectdef(defowner),hashedid.id);
  1131. if assigned(hsym) and
  1132. (
  1133. (
  1134. not(m_delphi in current_settings.modeswitches) and
  1135. is_visible_for_object(hsym,tobjectdef(defowner))
  1136. ) or
  1137. (
  1138. { In Delphi, you can repeat members of a parent class. You can't }
  1139. { do this for objects however, and you (obviouly) can't }
  1140. { declare two fields with the same name in a single class }
  1141. (m_delphi in current_settings.modeswitches) and
  1142. (
  1143. is_object(tdef(defowner)) or
  1144. (hsym.owner = self)
  1145. )
  1146. )
  1147. ) then
  1148. begin
  1149. DuplicateSym(hashedid,sym,hsym);
  1150. result:=true;
  1151. end;
  1152. end
  1153. else
  1154. begin
  1155. if not(m_duplicate_names in current_settings.modeswitches) then
  1156. result:=inherited checkduplicate(hashedid,sym);
  1157. end;
  1158. end;
  1159. {****************************************************************************
  1160. TAbstractLocalSymtable
  1161. ****************************************************************************}
  1162. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1163. var
  1164. oldtyp : byte;
  1165. begin
  1166. oldtyp:=ppufile.entrytyp;
  1167. ppufile.entrytyp:=subentryid;
  1168. inherited ppuwrite(ppufile);
  1169. ppufile.entrytyp:=oldtyp;
  1170. end;
  1171. function tabstractlocalsymtable.count_locals:longint;
  1172. var
  1173. i : longint;
  1174. sym : tsym;
  1175. begin
  1176. result:=0;
  1177. for i:=0 to SymList.Count-1 do
  1178. begin
  1179. sym:=tsym(SymList[i]);
  1180. { Count only varsyms, but ignore the funcretsym }
  1181. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1182. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1183. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1184. (tstoredsym(sym).refs>0)) then
  1185. inc(result);
  1186. end;
  1187. end;
  1188. {****************************************************************************
  1189. TLocalSymtable
  1190. ****************************************************************************}
  1191. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1192. begin
  1193. inherited create('');
  1194. defowner:=adefowner;
  1195. symtabletype:=localsymtable;
  1196. symtablelevel:=level;
  1197. end;
  1198. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1199. var
  1200. hsym : tsym;
  1201. begin
  1202. if not assigned(defowner) or
  1203. (defowner.typ<>procdef) then
  1204. internalerror(200602042);
  1205. result:=false;
  1206. hsym:=tsym(FindWithHash(hashedid));
  1207. if assigned(hsym) then
  1208. begin
  1209. { a local and the function can have the same
  1210. name in TP and Delphi, but RESULT not }
  1211. if (m_duplicate_names in current_settings.modeswitches) and
  1212. (hsym.typ in [absolutevarsym,localvarsym]) and
  1213. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1214. not((m_result in current_settings.modeswitches) and
  1215. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1216. HideSym(hsym)
  1217. else
  1218. DuplicateSym(hashedid,sym,hsym);
  1219. result:=true;
  1220. exit;
  1221. end;
  1222. { check also parasymtable, this needs to be done here because
  1223. of the special situation with the funcret sym that needs to be
  1224. hidden for tp and delphi modes }
  1225. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1226. if assigned(hsym) then
  1227. begin
  1228. { a local and the function can have the same
  1229. name in TP and Delphi, but RESULT not }
  1230. if (m_duplicate_names in current_settings.modeswitches) and
  1231. (sym.typ in [absolutevarsym,localvarsym]) and
  1232. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1233. not((m_result in current_settings.modeswitches) and
  1234. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1235. Hidesym(sym)
  1236. else
  1237. DuplicateSym(hashedid,sym,hsym);
  1238. result:=true;
  1239. exit;
  1240. end;
  1241. { check ObjectSymtable, skip this for funcret sym because
  1242. that will always be positive because it has the same name
  1243. as the procsym }
  1244. if not is_funcret_sym(sym) and
  1245. (defowner.typ=procdef) and
  1246. assigned(tprocdef(defowner).struct) and
  1247. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  1248. (
  1249. not(m_delphi in current_settings.modeswitches) or
  1250. is_object(tprocdef(defowner).struct)
  1251. ) then
  1252. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  1253. end;
  1254. {****************************************************************************
  1255. TParaSymtable
  1256. ****************************************************************************}
  1257. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1258. begin
  1259. inherited create('');
  1260. readonly:=false;
  1261. defowner:=adefowner;
  1262. symtabletype:=parasymtable;
  1263. symtablelevel:=level;
  1264. end;
  1265. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1266. begin
  1267. result:=inherited checkduplicate(hashedid,sym);
  1268. if result then
  1269. exit;
  1270. if not(m_duplicate_names in current_settings.modeswitches) and
  1271. (defowner.typ=procdef) and
  1272. assigned(tprocdef(defowner).struct) and
  1273. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  1274. (
  1275. not(m_delphi in current_settings.modeswitches) or
  1276. is_object(tprocdef(defowner).struct)
  1277. ) then
  1278. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  1279. end;
  1280. procedure tparasymtable.insertdef(def: TDefEntry);
  1281. begin
  1282. if readonly then
  1283. defowner.owner.insertdef(def)
  1284. else
  1285. inherited insertdef(def);
  1286. end;
  1287. {****************************************************************************
  1288. TAbstractUniTSymtable
  1289. ****************************************************************************}
  1290. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1291. begin
  1292. inherited create(n);
  1293. moduleid:=id;
  1294. end;
  1295. function tabstractuniTSymtable.iscurrentunit:boolean;
  1296. begin
  1297. result:=assigned(current_module) and
  1298. (
  1299. (current_module.globalsymtable=self) or
  1300. (current_module.localsymtable=self)
  1301. );
  1302. end;
  1303. {****************************************************************************
  1304. TStaticSymtable
  1305. ****************************************************************************}
  1306. constructor tstaticsymtable.create(const n : string;id:word);
  1307. begin
  1308. inherited create(n,id);
  1309. symtabletype:=staticsymtable;
  1310. symtablelevel:=main_program_level;
  1311. currentvisibility:=vis_private;
  1312. end;
  1313. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1314. begin
  1315. inherited ppuload(ppufile);
  1316. { now we can deref the syms and defs }
  1317. deref;
  1318. end;
  1319. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1320. begin
  1321. inherited ppuwrite(ppufile);
  1322. end;
  1323. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1324. var
  1325. hsym : tsym;
  1326. begin
  1327. result:=false;
  1328. hsym:=tsym(FindWithHash(hashedid));
  1329. if assigned(hsym) then
  1330. begin
  1331. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1332. unit, the unit can then not be accessed anymore using
  1333. <unit>.<id>, so we can hide the symbol }
  1334. if (m_delphi in current_settings.modeswitches) and
  1335. (hsym.typ=symconst.unitsym) then
  1336. HideSym(hsym)
  1337. else
  1338. DuplicateSym(hashedid,sym,hsym);
  1339. result:=true;
  1340. exit;
  1341. end;
  1342. if (current_module.localsymtable=self) and
  1343. assigned(current_module.globalsymtable) then
  1344. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1345. end;
  1346. {****************************************************************************
  1347. TGlobalSymtable
  1348. ****************************************************************************}
  1349. constructor tglobalsymtable.create(const n : string;id:word);
  1350. begin
  1351. inherited create(n,id);
  1352. symtabletype:=globalsymtable;
  1353. symtablelevel:=main_program_level;
  1354. end;
  1355. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1356. begin
  1357. inherited ppuload(ppufile);
  1358. { now we can deref the syms and defs }
  1359. deref;
  1360. end;
  1361. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1362. begin
  1363. { write the symtable entries }
  1364. inherited ppuwrite(ppufile);
  1365. end;
  1366. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1367. var
  1368. hsym : tsym;
  1369. begin
  1370. result:=false;
  1371. hsym:=tsym(FindWithHash(hashedid));
  1372. if assigned(hsym) then
  1373. begin
  1374. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1375. unit, the unit can then not be accessed anymore using
  1376. <unit>.<id>, so we can hide the symbol }
  1377. if (m_delphi in current_settings.modeswitches) and
  1378. (hsym.typ=symconst.unitsym) then
  1379. HideSym(hsym)
  1380. else
  1381. DuplicateSym(hashedid,sym,hsym);
  1382. result:=true;
  1383. exit;
  1384. end;
  1385. end;
  1386. {****************************************************************************
  1387. TWITHSYMTABLE
  1388. ****************************************************************************}
  1389. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1390. begin
  1391. inherited create('');
  1392. symtabletype:=withsymtable;
  1393. withrefnode:=refnode;
  1394. { Replace SymList with the passed symlist }
  1395. SymList.free;
  1396. SymList:=ASymList;
  1397. defowner:=aowner;
  1398. end;
  1399. destructor twithsymtable.destroy;
  1400. begin
  1401. withrefnode.free;
  1402. { Disable SymList because we don't Own it }
  1403. SymList:=nil;
  1404. inherited destroy;
  1405. end;
  1406. procedure twithsymtable.clear;
  1407. begin
  1408. { remove no entry from a withsymtable as it is only a pointer to the
  1409. recorddef or objectdef symtable }
  1410. end;
  1411. procedure twithsymtable.insertdef(def:TDefEntry);
  1412. begin
  1413. { Definitions can't be registered in the withsymtable
  1414. because the withsymtable is removed after the with block.
  1415. We can't easily solve it here because the next symtable in the
  1416. stack is not known. }
  1417. internalerror(200602046);
  1418. end;
  1419. {****************************************************************************
  1420. TSTT_ExceptionSymtable
  1421. ****************************************************************************}
  1422. constructor tstt_excepTSymtable.create;
  1423. begin
  1424. inherited create('');
  1425. symtabletype:=stt_excepTSymtable;
  1426. end;
  1427. {****************************************************************************
  1428. TMacroSymtable
  1429. ****************************************************************************}
  1430. constructor tmacrosymtable.create(exported: boolean);
  1431. begin
  1432. inherited create('');
  1433. if exported then
  1434. symtabletype:=exportedmacrosymtable
  1435. else
  1436. symtabletype:=localmacrosymtable;
  1437. symtablelevel:=main_program_level;
  1438. end;
  1439. {****************************************************************************
  1440. TEnumSymtable
  1441. ****************************************************************************}
  1442. procedure tenumsymtable.insert(sym: TSymEntry; checkdup: boolean);
  1443. var
  1444. value: longint;
  1445. def: tenumdef;
  1446. begin
  1447. // defowner = nil only when we are loading from ppu
  1448. if defowner<>nil then
  1449. begin
  1450. { First entry? Then we need to set the minval }
  1451. value:=tenumsym(sym).value;
  1452. def:=tenumdef(defowner);
  1453. if SymList.count=0 then
  1454. begin
  1455. if value>0 then
  1456. def.has_jumps:=true;
  1457. def.setmin(value);
  1458. def.setmax(value);
  1459. end
  1460. else
  1461. begin
  1462. { check for jumps }
  1463. if value>def.max+1 then
  1464. def.has_jumps:=true;
  1465. { update low and high }
  1466. if def.min>value then
  1467. def.setmin(value);
  1468. if def.max<value then
  1469. def.setmax(value);
  1470. end;
  1471. end;
  1472. inherited insert(sym, checkdup);
  1473. end;
  1474. constructor tenumsymtable.create(adefowner: tdef);
  1475. begin
  1476. inherited Create('');
  1477. symtabletype:=enumsymtable;
  1478. defowner:=adefowner;
  1479. end;
  1480. {****************************************************************************
  1481. TArraySymtable
  1482. ****************************************************************************}
  1483. procedure tarraysymtable.insertdef(def: TDefEntry);
  1484. begin
  1485. { Enums must also be available outside the record scope,
  1486. insert in the owner of this symtable }
  1487. if def.typ=enumdef then
  1488. defowner.owner.insertdef(def)
  1489. else
  1490. inherited insertdef(def);
  1491. end;
  1492. constructor tarraysymtable.create(adefowner: tdef);
  1493. begin
  1494. inherited Create('');
  1495. symtabletype:=arraysymtable;
  1496. defowner:=adefowner;
  1497. end;
  1498. {*****************************************************************************
  1499. Helper Routines
  1500. *****************************************************************************}
  1501. function FullTypeName(def,otherdef:tdef):string;
  1502. var
  1503. s1,s2 : string;
  1504. begin
  1505. if def.typ in [objectdef,recorddef] then
  1506. s1:=tabstractrecorddef(def).RttiName
  1507. else
  1508. s1:=def.typename;
  1509. { When the names are the same try to include the unit name }
  1510. if assigned(otherdef) and
  1511. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1512. begin
  1513. s2:=otherdef.typename;
  1514. if upper(s1)=upper(s2) then
  1515. s1:=def.owner.realname^+'.'+s1;
  1516. end;
  1517. FullTypeName:=s1;
  1518. end;
  1519. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  1520. begin
  1521. result:='';
  1522. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  1523. begin
  1524. if (result='') then
  1525. result:=symtable.name^
  1526. else
  1527. result:=symtable.name^+delimiter+result;
  1528. symtable:=symtable.defowner.owner;
  1529. end;
  1530. end;
  1531. procedure incompatibletypes(def1,def2:tdef);
  1532. begin
  1533. { When there is an errordef there is already an error message show }
  1534. if (def2.typ=errordef) or
  1535. (def1.typ=errordef) then
  1536. exit;
  1537. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1538. end;
  1539. procedure hidesym(sym:TSymEntry);
  1540. begin
  1541. sym.realname:='$hidden'+sym.realname;
  1542. tsym(sym).visibility:=vis_hidden;
  1543. end;
  1544. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1545. var
  1546. st : TSymtable;
  1547. begin
  1548. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1549. { Write hint where the original symbol was found }
  1550. st:=finduniTSymtable(origsym.owner);
  1551. with tsym(origsym).fileinfo do
  1552. begin
  1553. if assigned(st) and
  1554. (st.symtabletype=globalsymtable) and
  1555. st.iscurrentunit then
  1556. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1557. else if assigned(st.name) then
  1558. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1559. end;
  1560. { Rename duplicate sym to an unreachable name, but it can be
  1561. inserted in the symtable without errors }
  1562. inc(dupnr);
  1563. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1564. if assigned(dupsym) then
  1565. include(tsym(dupsym).symoptions,sp_implicitrename);
  1566. end;
  1567. {*****************************************************************************
  1568. Search
  1569. *****************************************************************************}
  1570. procedure addsymref(sym:tsym);
  1571. begin
  1572. { symbol uses count }
  1573. sym.IncRefCount;
  1574. { unit uses count }
  1575. if assigned(current_module) and
  1576. (sym.owner.symtabletype=globalsymtable) then
  1577. begin
  1578. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1579. internalerror(200501152);
  1580. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1581. end;
  1582. end;
  1583. function is_owned_by(childdef,ownerdef:tabstractrecorddef):boolean;
  1584. begin
  1585. result:=childdef=ownerdef;
  1586. if not result and (childdef.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1587. result:=is_owned_by(tabstractrecorddef(childdef.owner.defowner),ownerdef);
  1588. end;
  1589. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  1590. var
  1591. symownerdef : tabstractrecorddef;
  1592. begin
  1593. result:=false;
  1594. { Get objdectdef owner of the symtable for the is_related checks }
  1595. if not assigned(symst) or
  1596. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  1597. internalerror(200810285);
  1598. symownerdef:=tabstractrecorddef(symst.defowner);
  1599. case symvisibility of
  1600. vis_private :
  1601. begin
  1602. { private symbols are allowed when we are in the same
  1603. module as they are defined }
  1604. result:=(
  1605. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1606. (symownerdef.owner.iscurrentunit)
  1607. ) or
  1608. ( // the case of specialize inside the generic declaration
  1609. (symownerdef.owner.symtabletype = objectsymtable) and
  1610. (
  1611. assigned(current_structdef) and
  1612. (
  1613. (current_structdef=symownerdef) or
  1614. (current_structdef.owner.iscurrentunit)
  1615. )
  1616. ) or
  1617. (
  1618. not assigned(current_structdef) and
  1619. (symownerdef.owner.iscurrentunit)
  1620. )
  1621. );
  1622. end;
  1623. vis_strictprivate :
  1624. begin
  1625. result:=assigned(current_structdef) and
  1626. is_owned_by(current_structdef,symownerdef);
  1627. end;
  1628. vis_strictprotected :
  1629. begin
  1630. result:=(
  1631. assigned(current_structdef) and
  1632. (current_structdef.is_related(symownerdef) or
  1633. is_owned_by(current_structdef,symownerdef))
  1634. ) or
  1635. (
  1636. { helpers can access strict protected symbols }
  1637. is_objectpascal_helper(contextobjdef) and
  1638. tobjectdef(contextobjdef).extendeddef.is_related(symownerdef)
  1639. );
  1640. end;
  1641. vis_protected :
  1642. begin
  1643. { protected symbols are visible in the module that defines them and
  1644. also visible to related objects. The related object must be defined
  1645. in the current module }
  1646. result:=(
  1647. (
  1648. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1649. (symownerdef.owner.iscurrentunit)
  1650. ) or
  1651. (
  1652. assigned(contextobjdef) and
  1653. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable]) and
  1654. (contextobjdef.owner.iscurrentunit) and
  1655. contextobjdef.is_related(symownerdef)
  1656. ) or
  1657. ( // the case of specialize inside the generic declaration
  1658. (symownerdef.owner.symtabletype = objectsymtable) and
  1659. (
  1660. assigned(current_structdef) and
  1661. (
  1662. (current_structdef=symownerdef) or
  1663. (current_structdef.owner.iscurrentunit)
  1664. )
  1665. ) or
  1666. (
  1667. not assigned(current_structdef) and
  1668. (symownerdef.owner.iscurrentunit)
  1669. ) or
  1670. (
  1671. { helpers can access protected symbols }
  1672. is_objectpascal_helper(contextobjdef) and
  1673. tobjectdef(contextobjdef).extendeddef.is_related(symownerdef)
  1674. )
  1675. )
  1676. );
  1677. end;
  1678. vis_public,
  1679. vis_published :
  1680. result:=true;
  1681. end;
  1682. end;
  1683. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  1684. begin
  1685. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  1686. end;
  1687. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  1688. var
  1689. i : longint;
  1690. pd : tprocdef;
  1691. begin
  1692. if sym.typ=procsym then
  1693. begin
  1694. { A procsym is visible, when there is at least one of the procdefs visible }
  1695. result:=false;
  1696. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1697. begin
  1698. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1699. if (pd.owner=sym.owner) and
  1700. is_visible_for_object(pd,contextobjdef) then
  1701. begin
  1702. result:=true;
  1703. exit;
  1704. end;
  1705. end;
  1706. end
  1707. else
  1708. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  1709. end;
  1710. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1711. var
  1712. hashedid : THashedIDString;
  1713. contextstructdef : tabstractrecorddef;
  1714. stackitem : psymtablestackitem;
  1715. begin
  1716. result:=false;
  1717. hashedid.id:=s;
  1718. stackitem:=symtablestack.stack;
  1719. while assigned(stackitem) do
  1720. begin
  1721. srsymtable:=stackitem^.symtable;
  1722. if (srsymtable.symtabletype=objectsymtable) then
  1723. begin
  1724. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,true) then
  1725. begin
  1726. result:=true;
  1727. exit;
  1728. end;
  1729. end
  1730. else
  1731. begin
  1732. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1733. if assigned(srsym) then
  1734. begin
  1735. { use the class from withsymtable only when it is
  1736. defined in this unit }
  1737. if (srsymtable.symtabletype=withsymtable) and
  1738. assigned(srsymtable.defowner) and
  1739. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  1740. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1741. (srsymtable.defowner.owner.iscurrentunit) then
  1742. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  1743. else
  1744. contextstructdef:=current_structdef;
  1745. if not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  1746. is_visible_for_object(srsym,contextstructdef) then
  1747. begin
  1748. { we need to know if a procedure references symbols
  1749. in the static symtable, because then it can't be
  1750. inlined from outside this unit }
  1751. if assigned(current_procinfo) and
  1752. (srsym.owner.symtabletype=staticsymtable) then
  1753. include(current_procinfo.flags,pi_uses_static_symtable);
  1754. addsymref(srsym);
  1755. result:=true;
  1756. exit;
  1757. end;
  1758. end;
  1759. end;
  1760. stackitem:=stackitem^.next;
  1761. end;
  1762. srsym:=nil;
  1763. srsymtable:=nil;
  1764. end;
  1765. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1766. var
  1767. hashedid : THashedIDString;
  1768. stackitem : psymtablestackitem;
  1769. classh : tobjectdef;
  1770. begin
  1771. result:=false;
  1772. hashedid.id:=s;
  1773. stackitem:=symtablestack.stack;
  1774. while assigned(stackitem) do
  1775. begin
  1776. {
  1777. It is not possible to have type symbols in:
  1778. parameters
  1779. Exception are classes, objects, records, generic definitions and specializations
  1780. that have the parameterized types inserted in the symtable.
  1781. }
  1782. srsymtable:=stackitem^.symtable;
  1783. if (srsymtable.symtabletype=ObjectSymtable) then
  1784. begin
  1785. classh:=tobjectdef(srsymtable.defowner);
  1786. while assigned(classh) do
  1787. begin
  1788. srsymtable:=classh.symtable;
  1789. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1790. if assigned(srsym) and
  1791. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  1792. is_visible_for_object(srsym,current_structdef) then
  1793. begin
  1794. addsymref(srsym);
  1795. result:=true;
  1796. exit;
  1797. end;
  1798. classh:=classh.childof;
  1799. end;
  1800. end
  1801. else
  1802. begin
  1803. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1804. if assigned(srsym) and
  1805. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  1806. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  1807. begin
  1808. { we need to know if a procedure references symbols
  1809. in the static symtable, because then it can't be
  1810. inlined from outside this unit }
  1811. if assigned(current_procinfo) and
  1812. (srsym.owner.symtabletype=staticsymtable) then
  1813. include(current_procinfo.flags,pi_uses_static_symtable);
  1814. addsymref(srsym);
  1815. result:=true;
  1816. exit;
  1817. end;
  1818. end;
  1819. stackitem:=stackitem^.next;
  1820. end;
  1821. result:=false;
  1822. srsym:=nil;
  1823. srsymtable:=nil;
  1824. end;
  1825. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1826. var
  1827. pmod : tmodule;
  1828. begin
  1829. pmod:=tmodule(pm);
  1830. result:=false;
  1831. if assigned(pmod.globalsymtable) then
  1832. begin
  1833. srsym:=tsym(pmod.globalsymtable.Find(s));
  1834. if assigned(srsym) then
  1835. begin
  1836. srsymtable:=pmod.globalsymtable;
  1837. addsymref(srsym);
  1838. result:=true;
  1839. exit;
  1840. end;
  1841. end;
  1842. { If the module is the current unit we also need
  1843. to search the local symtable }
  1844. if (pmod=current_module) and
  1845. assigned(pmod.localsymtable) then
  1846. begin
  1847. srsym:=tsym(pmod.localsymtable.Find(s));
  1848. if assigned(srsym) then
  1849. begin
  1850. srsymtable:=pmod.localsymtable;
  1851. addsymref(srsym);
  1852. result:=true;
  1853. exit;
  1854. end;
  1855. end;
  1856. srsym:=nil;
  1857. srsymtable:=nil;
  1858. end;
  1859. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  1860. var
  1861. stackitem : psymtablestackitem;
  1862. begin
  1863. result:=false;
  1864. stackitem:=symtablestack.stack;
  1865. while assigned(stackitem) do
  1866. begin
  1867. srsymtable:=stackitem^.symtable;
  1868. if (srsymtable.symtabletype=globalsymtable) and
  1869. (srsymtable.name^=unitname) then
  1870. begin
  1871. srsym:=tsym(srsymtable.find(symname));
  1872. if not assigned(srsym) then
  1873. break;
  1874. result:=true;
  1875. exit;
  1876. end;
  1877. stackitem:=stackitem^.next;
  1878. end;
  1879. { If the module is the current unit we also need
  1880. to search the local symtable }
  1881. if (current_module.globalsymtable=srsymtable) and
  1882. assigned(current_module.localsymtable) then
  1883. begin
  1884. srsymtable:=current_module.localsymtable;
  1885. srsym:=tsym(srsymtable.find(symname));
  1886. if assigned(srsym) then
  1887. begin
  1888. result:=true;
  1889. exit;
  1890. end;
  1891. end;
  1892. end;
  1893. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  1894. var
  1895. hashedid : THashedIDString;
  1896. stackitem : psymtablestackitem;
  1897. srsymtable : tsymtable;
  1898. srsym : tsym;
  1899. formalname,
  1900. foundname : shortstring;
  1901. formalnameptr,
  1902. foundnameptr: pshortstring;
  1903. begin
  1904. { not a formal definition -> return it }
  1905. if not(oo_is_formal in pd.objectoptions) then
  1906. begin
  1907. result:=pd;
  1908. exit;
  1909. end;
  1910. hashedid.id:=pd.typesym.name;
  1911. stackitem:=symtablestack.stack;
  1912. while assigned(stackitem) do
  1913. begin
  1914. srsymtable:=stackitem^.symtable;
  1915. { ObjC classes can't appear in generics or as nested class
  1916. definitions. Java classes can. }
  1917. if not(srsymtable.symtabletype in [recordsymtable,parasymtable]) or
  1918. (is_java_class_or_interface(pd) and
  1919. (srsymtable.symtabletype=ObjectSymtable)) then
  1920. begin
  1921. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1922. if assigned(srsym) and
  1923. (srsym.typ=typesym) and
  1924. (ttypesym(srsym).typedef.typ=objectdef) and
  1925. (tobjectdef(ttypesym(srsym).typedef).objecttype=pd.objecttype) and
  1926. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  1927. begin
  1928. if not(oo_is_forward in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  1929. begin
  1930. { the external name for the formal and the real
  1931. definition must match (forward declarations don't have
  1932. an external name set yet) }
  1933. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) or
  1934. assigned(pd.import_lib) then
  1935. begin
  1936. if assigned(pd.import_lib) then
  1937. formalname:=pd.import_lib^
  1938. else
  1939. formalname:='';
  1940. formalname:=formalname+'.'+pd.objextname^;
  1941. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) then
  1942. foundname:=tobjectdef(ttypesym(srsym).typedef).import_lib^+'.'
  1943. else
  1944. foundname:='';
  1945. foundname:=foundname+tobjectdef(ttypesym(srsym).typedef).objextname^;
  1946. formalnameptr:=@formalname;
  1947. foundnameptr:=@foundname;
  1948. end
  1949. else
  1950. begin
  1951. formalnameptr:=pd.objextname;
  1952. foundnameptr:=tobjectdef(ttypesym(srsym).typedef).objextname;
  1953. end;
  1954. if foundnameptr^<>formalnameptr^ then
  1955. begin
  1956. Message2(sym_e_external_class_name_mismatch1,formalnameptr^,pd.typename);
  1957. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,foundnameptr^);
  1958. end;
  1959. end;
  1960. result:=tobjectdef(ttypesym(srsym).typedef);
  1961. if assigned(current_procinfo) and
  1962. (srsym.owner.symtabletype=staticsymtable) then
  1963. include(current_procinfo.flags,pi_uses_static_symtable);
  1964. addsymref(srsym);
  1965. exit;
  1966. end;
  1967. end;
  1968. stackitem:=stackitem^.next;
  1969. end;
  1970. { nothing found: optionally give an error and return the original
  1971. (empty) one }
  1972. if erroronfailure then
  1973. Message1(sym_e_formal_class_not_resolved,pd.objrealname^);
  1974. result:=pd;
  1975. end;
  1976. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;searchhelper:boolean):boolean;
  1977. var
  1978. hashedid : THashedIDString;
  1979. orgclass : tobjectdef;
  1980. i : longint;
  1981. begin
  1982. orgclass:=classh;
  1983. { in case this is a formal class, first find the real definition }
  1984. if assigned(classh) then
  1985. begin
  1986. if (oo_is_formal in classh.objectoptions) then
  1987. classh:=find_real_class_definition(classh,true);
  1988. { The contextclassh is used for visibility. The classh must be equal to
  1989. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  1990. parent or a class helper. }
  1991. if not (contextclassh.is_related(classh) or
  1992. (assigned(contextclassh.extendeddef) and
  1993. (contextclassh.extendeddef.typ=objectdef) and
  1994. contextclassh.extendeddef.is_related(classh))) then
  1995. internalerror(200811161);
  1996. end;
  1997. result:=false;
  1998. hashedid.id:=s;
  1999. { an Objective-C protocol or Java interface can inherit from multiple
  2000. other protocols/interfaces -> use ImplementedInterfaces instead }
  2001. if is_objcprotocol(classh) or
  2002. is_javainterface(classh) then
  2003. begin
  2004. srsymtable:=classh.symtable;
  2005. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2006. if assigned(srsym) and
  2007. is_visible_for_object(srsym,contextclassh) then
  2008. begin
  2009. addsymref(srsym);
  2010. result:=true;
  2011. exit;
  2012. end;
  2013. for i:=0 to classh.ImplementedInterfaces.count-1 do
  2014. begin
  2015. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,false) then
  2016. begin
  2017. result:=true;
  2018. exit;
  2019. end;
  2020. end;
  2021. end
  2022. else
  2023. if is_objectpascal_helper(classh) then
  2024. begin
  2025. { helpers have their own obscure search logic... }
  2026. result:=searchsym_in_helper(classh,contextclassh,s,srsym,srsymtable,false);
  2027. if result then
  2028. exit;
  2029. end
  2030. else
  2031. begin
  2032. while assigned(classh) do
  2033. begin
  2034. { search for a class helper method first if this is an Object
  2035. Pascal class }
  2036. if is_class(classh) and searchhelper then
  2037. begin
  2038. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  2039. if result then
  2040. { if the procsym is overloaded we need to use the
  2041. "original" symbol; the helper symbol will be found when
  2042. searching for overloads }
  2043. if (srsym.typ<>procsym) or
  2044. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  2045. exit;
  2046. end;
  2047. srsymtable:=classh.symtable;
  2048. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2049. if assigned(srsym) and
  2050. is_visible_for_object(srsym,contextclassh) then
  2051. begin
  2052. addsymref(srsym);
  2053. result:=true;
  2054. exit;
  2055. end;
  2056. classh:=classh.childof;
  2057. end;
  2058. end;
  2059. if is_objcclass(orgclass) then
  2060. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  2061. else
  2062. begin
  2063. srsym:=nil;
  2064. srsymtable:=nil;
  2065. end;
  2066. end;
  2067. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2068. var
  2069. hashedid : THashedIDString;
  2070. begin
  2071. result:=false;
  2072. hashedid.id:=s;
  2073. { search for a record helper method first }
  2074. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  2075. if result then
  2076. { if the procsym is overloaded we need to use the
  2077. "original" symbol; the helper symbol will be found when
  2078. searching for overloads }
  2079. if (srsym.typ<>procsym) or
  2080. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  2081. exit;
  2082. srsymtable:=recordh.symtable;
  2083. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2084. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  2085. begin
  2086. addsymref(srsym);
  2087. result:=true;
  2088. exit;
  2089. end;
  2090. srsym:=nil;
  2091. srsymtable:=nil;
  2092. end;
  2093. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2094. var
  2095. def : tdef;
  2096. i : longint;
  2097. begin
  2098. { in case this is a formal class, first find the real definition }
  2099. if assigned(classh) and
  2100. (oo_is_formal in classh.objectoptions) then
  2101. classh:=find_real_class_definition(classh,true);
  2102. result:=false;
  2103. def:=nil;
  2104. while assigned(classh) do
  2105. begin
  2106. for i:=0 to classh.symtable.DefList.Count-1 do
  2107. begin
  2108. def:=tstoreddef(classh.symtable.DefList[i]);
  2109. { Find also all hidden private methods to
  2110. be compatible with delphi, see tw6203 (PFV) }
  2111. if (def.typ=procdef) and
  2112. (po_msgint in tprocdef(def).procoptions) and
  2113. (tprocdef(def).messageinf.i=msgid) then
  2114. begin
  2115. srdef:=def;
  2116. srsym:=tprocdef(def).procsym;
  2117. srsymtable:=classh.symtable;
  2118. addsymref(srsym);
  2119. result:=true;
  2120. exit;
  2121. end;
  2122. end;
  2123. classh:=classh.childof;
  2124. end;
  2125. srdef:=nil;
  2126. srsym:=nil;
  2127. srsymtable:=nil;
  2128. end;
  2129. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2130. var
  2131. def : tdef;
  2132. i : longint;
  2133. begin
  2134. { in case this is a formal class, first find the real definition }
  2135. if assigned(classh) and
  2136. (oo_is_formal in classh.objectoptions) then
  2137. classh:=find_real_class_definition(classh,true);
  2138. result:=false;
  2139. def:=nil;
  2140. while assigned(classh) do
  2141. begin
  2142. for i:=0 to classh.symtable.DefList.Count-1 do
  2143. begin
  2144. def:=tstoreddef(classh.symtable.DefList[i]);
  2145. { Find also all hidden private methods to
  2146. be compatible with delphi, see tw6203 (PFV) }
  2147. if (def.typ=procdef) and
  2148. (po_msgstr in tprocdef(def).procoptions) and
  2149. (tprocdef(def).messageinf.str^=s) then
  2150. begin
  2151. srsym:=tprocdef(def).procsym;
  2152. srsymtable:=classh.symtable;
  2153. addsymref(srsym);
  2154. result:=true;
  2155. exit;
  2156. end;
  2157. end;
  2158. classh:=classh.childof;
  2159. end;
  2160. srsym:=nil;
  2161. srsymtable:=nil;
  2162. end;
  2163. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;aHasInherited:boolean):boolean;
  2164. var
  2165. hashedid : THashedIDString;
  2166. parentclassh : tobjectdef;
  2167. begin
  2168. result:=false;
  2169. if not is_objectpascal_helper(classh) then
  2170. Internalerror(2011030101);
  2171. hashedid.id:=s;
  2172. { in a helper things are a bit more complex:
  2173. 1. search the symbol in the helper (if not "inherited")
  2174. 2. search the symbol in the extended type
  2175. 3. search the symbol in the parent helpers
  2176. 4. only classes: search the symbol in the parents of the extended type
  2177. }
  2178. if not aHasInherited then
  2179. begin
  2180. { search in the helper itself }
  2181. srsymtable:=classh.symtable;
  2182. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2183. if assigned(srsym) and
  2184. is_visible_for_object(srsym,contextclassh) then
  2185. begin
  2186. addsymref(srsym);
  2187. result:=true;
  2188. exit;
  2189. end;
  2190. end;
  2191. { now search in the extended type itself }
  2192. srsymtable:=classh.extendeddef.symtable;
  2193. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2194. if assigned(srsym) and
  2195. is_visible_for_object(srsym,contextclassh) then
  2196. begin
  2197. addsymref(srsym);
  2198. result:=true;
  2199. exit;
  2200. end;
  2201. { now search in the parent helpers }
  2202. parentclassh:=classh.childof;
  2203. while assigned(parentclassh) do
  2204. begin
  2205. srsymtable:=parentclassh.symtable;
  2206. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2207. if assigned(srsym) and
  2208. is_visible_for_object(srsym,contextclassh) then
  2209. begin
  2210. addsymref(srsym);
  2211. result:=true;
  2212. exit;
  2213. end;
  2214. parentclassh:=parentclassh.childof;
  2215. end;
  2216. if is_class(classh.extendeddef) then
  2217. { now search in the parents of the extended class (with helpers!) }
  2218. result:=searchsym_in_class(tobjectdef(classh.extendeddef).childof,contextclassh,s,srsym,srsymtable,true);
  2219. { addsymref is already called by searchsym_in_class }
  2220. end;
  2221. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  2222. var
  2223. sym : Tprocsym;
  2224. hashedid : THashedIDString;
  2225. curreq,
  2226. besteq : tequaltype;
  2227. currpd,
  2228. bestpd : tprocdef;
  2229. stackitem : psymtablestackitem;
  2230. begin
  2231. hashedid.id:=overloaded_names[assignment_type];
  2232. besteq:=te_incompatible;
  2233. bestpd:=nil;
  2234. stackitem:=symtablestack.stack;
  2235. while assigned(stackitem) do
  2236. begin
  2237. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  2238. if sym<>nil then
  2239. begin
  2240. if sym.typ<>procsym then
  2241. internalerror(200402031);
  2242. { if the source type is an alias then this is only the second choice,
  2243. if you mess with this code, check tw4093 }
  2244. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  2245. if curreq>besteq then
  2246. begin
  2247. besteq:=curreq;
  2248. bestpd:=currpd;
  2249. if (besteq=te_exact) then
  2250. break;
  2251. end;
  2252. end;
  2253. stackitem:=stackitem^.next;
  2254. end;
  2255. result:=bestpd;
  2256. end;
  2257. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  2258. begin
  2259. { search record/object symtable first for a suitable operator }
  2260. if from_def.typ in [recorddef,objectdef] then
  2261. symtablestack.push(tabstractrecorddef(from_def).symtable);
  2262. if to_def.typ in [recorddef,objectdef] then
  2263. symtablestack.push(tabstractrecorddef(to_def).symtable);
  2264. { if type conversion is explicit then search first for explicit
  2265. operator overload and if not found then use implicit operator }
  2266. if explicit then
  2267. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  2268. else
  2269. result:=nil;
  2270. if result=nil then
  2271. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  2272. { restore symtable stack }
  2273. if to_def.typ in [recorddef,objectdef] then
  2274. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  2275. if from_def.typ in [recorddef,objectdef] then
  2276. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  2277. end;
  2278. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  2279. var
  2280. sym : Tprocsym;
  2281. hashedid : THashedIDString;
  2282. curreq,
  2283. besteq : tequaltype;
  2284. currpd,
  2285. bestpd : tprocdef;
  2286. stackitem : psymtablestackitem;
  2287. begin
  2288. hashedid.id:='enumerator';
  2289. besteq:=te_incompatible;
  2290. bestpd:=nil;
  2291. stackitem:=symtablestack.stack;
  2292. while assigned(stackitem) do
  2293. begin
  2294. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  2295. if sym<>nil then
  2296. begin
  2297. if sym.typ<>procsym then
  2298. internalerror(200910241);
  2299. { if the source type is an alias then this is only the second choice,
  2300. if you mess with this code, check tw4093 }
  2301. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  2302. if curreq>besteq then
  2303. begin
  2304. besteq:=curreq;
  2305. bestpd:=currpd;
  2306. if (besteq=te_exact) then
  2307. break;
  2308. end;
  2309. end;
  2310. stackitem:=stackitem^.next;
  2311. end;
  2312. result:=bestpd;
  2313. end;
  2314. function search_system_type(const s: TIDString): ttypesym;
  2315. var
  2316. sym : tsym;
  2317. begin
  2318. sym:=tsym(systemunit.Find(s));
  2319. if not assigned(sym) or
  2320. (sym.typ<>typesym) then
  2321. cgmessage1(cg_f_unknown_system_type,s);
  2322. result:=ttypesym(sym);
  2323. end;
  2324. function try_search_system_type(const s: TIDString): ttypesym;
  2325. var
  2326. sym : tsym;
  2327. begin
  2328. sym:=tsym(systemunit.Find(s));
  2329. if not assigned(sym) then
  2330. result:=nil
  2331. else
  2332. begin
  2333. if sym.typ<>typesym then
  2334. cgmessage1(cg_f_unknown_system_type,s);
  2335. result:=ttypesym(sym);
  2336. end;
  2337. end;
  2338. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  2339. var
  2340. srsymtable: tsymtable;
  2341. sym: tsym;
  2342. begin
  2343. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  2344. (sym.typ=typesym) then
  2345. begin
  2346. result:=ttypesym(sym);
  2347. exit;
  2348. end
  2349. else
  2350. begin
  2351. if throwerror then
  2352. cgmessage2(cg_f_unknown_type_in_unit,typename,unitname);
  2353. result:=nil;
  2354. end;
  2355. end;
  2356. function search_last_objectpascal_helper(pd,contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  2357. var
  2358. s: string;
  2359. list: TFPObjectList;
  2360. i: integer;
  2361. st: tsymtable;
  2362. begin
  2363. result:=false;
  2364. odef:=nil;
  2365. { when there are no helpers active currently then we don't need to do
  2366. anything }
  2367. if current_module.extendeddefs.count=0 then
  2368. exit;
  2369. { no helpers for anonymous types }
  2370. if not assigned(pd.objrealname) or (pd.objrealname^='') then
  2371. exit;
  2372. { if pd is defined inside a procedure we must not use make_mangledname
  2373. (as a helper may not be defined in a procedure this is no problem...)}
  2374. st:=pd.owner;
  2375. while st.symtabletype in [objectsymtable,recordsymtable] do
  2376. st:=st.defowner.owner;
  2377. if st.symtabletype=localsymtable then
  2378. exit;
  2379. { the mangled name is used as the key for tmodule.extendeddefs }
  2380. s:=make_mangledname('',pd.symtable,'');
  2381. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  2382. if assigned(list) and (list.count>0) then
  2383. begin
  2384. i:=list.count-1;
  2385. repeat
  2386. odef:=tobjectdef(list[list.count-1]);
  2387. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  2388. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  2389. dec(i);
  2390. until result or (i<0);
  2391. if not result then
  2392. { just to be sure that noone uses odef }
  2393. odef:=nil;
  2394. end;
  2395. end;
  2396. function search_objectpascal_helper(pd,contextclassh : tabstractrecorddef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2397. var
  2398. hashedid : THashedIDString;
  2399. classh : tobjectdef;
  2400. i : integer;
  2401. pdef : tprocdef;
  2402. begin
  2403. result:=false;
  2404. { if there is no class helper for the class then there is no need to
  2405. search further }
  2406. if not search_last_objectpascal_helper(pd,contextclassh,classh) then
  2407. exit;
  2408. hashedid.id:=s;
  2409. repeat
  2410. srsymtable:=classh.symtable;
  2411. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2412. if srsym<>nil then
  2413. begin
  2414. if srsym.typ=propertysym then
  2415. begin
  2416. result:=true;
  2417. exit;
  2418. end;
  2419. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2420. begin
  2421. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  2422. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  2423. continue;
  2424. { we need to know if a procedure references symbols
  2425. in the static symtable, because then it can't be
  2426. inlined from outside this unit }
  2427. if assigned(current_procinfo) and
  2428. (srsym.owner.symtabletype=staticsymtable) then
  2429. include(current_procinfo.flags,pi_uses_static_symtable);
  2430. { the first found method wins }
  2431. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2432. srsymtable:=srsym.owner;
  2433. addsymref(srsym);
  2434. result:=true;
  2435. exit;
  2436. end;
  2437. end;
  2438. { try the helper parent if available }
  2439. classh:=classh.childof;
  2440. until classh=nil;
  2441. srsym:=nil;
  2442. srsymtable:=nil;
  2443. end;
  2444. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2445. var
  2446. hashedid : THashedIDString;
  2447. stackitem : psymtablestackitem;
  2448. i : longint;
  2449. defowner : tobjectdef;
  2450. begin
  2451. hashedid.id:=class_helper_prefix+s;
  2452. stackitem:=symtablestack.stack;
  2453. while assigned(stackitem) do
  2454. begin
  2455. srsymtable:=stackitem^.symtable;
  2456. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2457. if assigned(srsym) then
  2458. begin
  2459. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  2460. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  2461. (srsym.typ<>procsym) then
  2462. internalerror(2009111505);
  2463. { check whether this procsym includes a helper for this particular class }
  2464. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2465. begin
  2466. { does pd inherit from (or is the same as) the class
  2467. that this method's category extended?
  2468. Warning: this list contains both category and objcclass methods
  2469. (for id.randommethod), so only check category methods here
  2470. }
  2471. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  2472. if (oo_is_classhelper in defowner.objectoptions) and
  2473. pd.is_related(defowner.childof) then
  2474. begin
  2475. { we need to know if a procedure references symbols
  2476. in the static symtable, because then it can't be
  2477. inlined from outside this unit }
  2478. if assigned(current_procinfo) and
  2479. (srsym.owner.symtabletype=staticsymtable) then
  2480. include(current_procinfo.flags,pi_uses_static_symtable);
  2481. { no need to keep looking. There might be other
  2482. categories that extend this, a parent or child
  2483. class with a method with the same name (either
  2484. overriding this one, or overridden by this one),
  2485. but that doesn't matter as far as the basic
  2486. procsym is concerned.
  2487. }
  2488. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2489. srsymtable:=srsym.owner;
  2490. addsymref(srsym);
  2491. result:=true;
  2492. exit;
  2493. end;
  2494. end;
  2495. end;
  2496. stackitem:=stackitem^.next;
  2497. end;
  2498. srsym:=nil;
  2499. srsymtable:=nil;
  2500. result:=false;
  2501. end;
  2502. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2503. var
  2504. hashedid : THashedIDString;
  2505. stackitem : psymtablestackitem;
  2506. i : longint;
  2507. begin
  2508. hashedid.id:=class_helper_prefix+s;
  2509. stackitem:=symtablestack.stack;
  2510. while assigned(stackitem) do
  2511. begin
  2512. srsymtable:=stackitem^.symtable;
  2513. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2514. if assigned(srsym) then
  2515. begin
  2516. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  2517. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  2518. (srsym.typ<>procsym) then
  2519. internalerror(2009112005);
  2520. { check whether this procsym includes a helper for this particular class }
  2521. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2522. begin
  2523. { we need to know if a procedure references symbols
  2524. in the static symtable, because then it can't be
  2525. inlined from outside this unit }
  2526. if assigned(current_procinfo) and
  2527. (srsym.owner.symtabletype=staticsymtable) then
  2528. include(current_procinfo.flags,pi_uses_static_symtable);
  2529. { no need to keep looking. There might be other
  2530. methods with the same name, but that doesn't matter
  2531. as far as the basic procsym is concerned.
  2532. }
  2533. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2534. { We need the symtable in which the classhelper-like sym
  2535. is located, not the objectdef. The reason is that the
  2536. callnode will climb the symtablestack until it encounters
  2537. this symtable to start looking for overloads (and it won't
  2538. find the objectsymtable in which this method sym is
  2539. located
  2540. srsymtable:=srsym.owner;
  2541. }
  2542. addsymref(srsym);
  2543. result:=true;
  2544. exit;
  2545. end;
  2546. end;
  2547. stackitem:=stackitem^.next;
  2548. end;
  2549. srsym:=nil;
  2550. srsymtable:=nil;
  2551. result:=false;
  2552. end;
  2553. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  2554. { searches n in symtable of pd and all anchestors }
  2555. var
  2556. hashedid : THashedIDString;
  2557. srsym : tsym;
  2558. orgpd : tabstractrecorddef;
  2559. srsymtable : tsymtable;
  2560. begin
  2561. { in case this is a formal class, first find the real definition }
  2562. if (oo_is_formal in pd.objectoptions) then
  2563. pd:=find_real_class_definition(tobjectdef(pd),true);
  2564. if search_objectpascal_helper(pd, pd, s, result, srsymtable) then
  2565. exit;
  2566. hashedid.id:=s;
  2567. orgpd:=pd;
  2568. while assigned(pd) do
  2569. begin
  2570. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  2571. if assigned(srsym) then
  2572. begin
  2573. search_struct_member:=srsym;
  2574. exit;
  2575. end;
  2576. if pd.typ=objectdef then
  2577. pd:=tobjectdef(pd).childof
  2578. else
  2579. pd:=nil;
  2580. end;
  2581. { not found, now look for class helpers }
  2582. if is_objcclass(pd) then
  2583. search_objc_helper(tobjectdef(orgpd),s,result,srsymtable)
  2584. else
  2585. result:=nil;
  2586. end;
  2587. function search_macro(const s : string):tsym;
  2588. var
  2589. stackitem : psymtablestackitem;
  2590. hashedid : THashedIDString;
  2591. srsym : tsym;
  2592. begin
  2593. hashedid.id:=s;
  2594. { First search the localmacrosymtable before searching the
  2595. global macrosymtables from the units }
  2596. if assigned(current_module) then
  2597. begin
  2598. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  2599. if assigned(srsym) then
  2600. begin
  2601. result:= srsym;
  2602. exit;
  2603. end;
  2604. end;
  2605. stackitem:=macrosymtablestack.stack;
  2606. while assigned(stackitem) do
  2607. begin
  2608. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  2609. if assigned(srsym) then
  2610. begin
  2611. result:= srsym;
  2612. exit;
  2613. end;
  2614. stackitem:=stackitem^.next;
  2615. end;
  2616. result:= nil;
  2617. end;
  2618. function defined_macro(const s : string):boolean;
  2619. var
  2620. mac: tmacro;
  2621. begin
  2622. mac:=tmacro(search_macro(s));
  2623. if assigned(mac) then
  2624. begin
  2625. mac.is_used:=true;
  2626. defined_macro:=mac.defined;
  2627. end
  2628. else
  2629. defined_macro:=false;
  2630. end;
  2631. {****************************************************************************
  2632. Object Helpers
  2633. ****************************************************************************}
  2634. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  2635. { returns the default property of a class, searches also anchestors }
  2636. var
  2637. _defaultprop : tpropertysym;
  2638. helperpd : tobjectdef;
  2639. begin
  2640. _defaultprop:=nil;
  2641. { first search in helper's hierarchy }
  2642. if search_last_objectpascal_helper(pd,nil,helperpd) then
  2643. while assigned(helperpd) do
  2644. begin
  2645. helperpd.symtable.SymList.ForEachCall(@tstoredsymtable(helperpd.symtable).testfordefaultproperty,@_defaultprop);
  2646. if assigned(_defaultprop) then
  2647. break;
  2648. helperpd:=helperpd.childof;
  2649. end;
  2650. if assigned(_defaultprop) then
  2651. begin
  2652. search_default_property:=_defaultprop;
  2653. exit;
  2654. end;
  2655. { now search in the type's hierarchy itself }
  2656. while assigned(pd) do
  2657. begin
  2658. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  2659. if assigned(_defaultprop) then
  2660. break;
  2661. if (pd.typ=objectdef) then
  2662. pd:=tobjectdef(pd).childof
  2663. else
  2664. break;
  2665. end;
  2666. search_default_property:=_defaultprop;
  2667. end;
  2668. {****************************************************************************
  2669. Macro Helpers
  2670. ****************************************************************************}
  2671. procedure def_system_macro(const name : string);
  2672. var
  2673. mac : tmacro;
  2674. s: string;
  2675. begin
  2676. if name = '' then
  2677. internalerror(2004121202);
  2678. s:= upper(name);
  2679. mac:=tmacro(search_macro(s));
  2680. if not assigned(mac) then
  2681. begin
  2682. mac:=tmacro.create(s);
  2683. if assigned(current_module) then
  2684. current_module.localmacrosymtable.insert(mac)
  2685. else
  2686. initialmacrosymtable.insert(mac);
  2687. end;
  2688. Message1(parser_c_macro_defined,mac.name);
  2689. mac.defined:=true;
  2690. end;
  2691. procedure set_system_macro(const name, value : string);
  2692. var
  2693. mac : tmacro;
  2694. s: string;
  2695. begin
  2696. if name = '' then
  2697. internalerror(2004121203);
  2698. s:= upper(name);
  2699. mac:=tmacro(search_macro(s));
  2700. if not assigned(mac) then
  2701. begin
  2702. mac:=tmacro.create(s);
  2703. if assigned(current_module) then
  2704. current_module.localmacrosymtable.insert(mac)
  2705. else
  2706. initialmacrosymtable.insert(mac);
  2707. end
  2708. else
  2709. begin
  2710. mac.is_compiler_var:=false;
  2711. if assigned(mac.buftext) then
  2712. freemem(mac.buftext,mac.buflen);
  2713. end;
  2714. Message2(parser_c_macro_set_to,mac.name,value);
  2715. mac.buflen:=length(value);
  2716. getmem(mac.buftext,mac.buflen);
  2717. move(value[1],mac.buftext^,mac.buflen);
  2718. mac.defined:=true;
  2719. end;
  2720. procedure set_system_compvar(const name, value : string);
  2721. var
  2722. mac : tmacro;
  2723. s: string;
  2724. begin
  2725. if name = '' then
  2726. internalerror(2004121204);
  2727. s:= upper(name);
  2728. mac:=tmacro(search_macro(s));
  2729. if not assigned(mac) then
  2730. begin
  2731. mac:=tmacro.create(s);
  2732. mac.is_compiler_var:=true;
  2733. if assigned(current_module) then
  2734. current_module.localmacrosymtable.insert(mac)
  2735. else
  2736. initialmacrosymtable.insert(mac);
  2737. end
  2738. else
  2739. begin
  2740. mac.is_compiler_var:=true;
  2741. if assigned(mac.buftext) then
  2742. freemem(mac.buftext,mac.buflen);
  2743. end;
  2744. Message2(parser_c_macro_set_to,mac.name,value);
  2745. mac.buflen:=length(value);
  2746. getmem(mac.buftext,mac.buflen);
  2747. move(value[1],mac.buftext^,mac.buflen);
  2748. mac.defined:=true;
  2749. end;
  2750. procedure undef_system_macro(const name : string);
  2751. var
  2752. mac : tmacro;
  2753. s: string;
  2754. begin
  2755. if name = '' then
  2756. internalerror(2004121205);
  2757. s:= upper(name);
  2758. mac:=tmacro(search_macro(s));
  2759. if not assigned(mac) then
  2760. {If not found, then it's already undefined.}
  2761. else
  2762. begin
  2763. Message1(parser_c_macro_undefined,mac.name);
  2764. mac.defined:=false;
  2765. mac.is_compiler_var:=false;
  2766. { delete old definition }
  2767. if assigned(mac.buftext) then
  2768. begin
  2769. freemem(mac.buftext,mac.buflen);
  2770. mac.buftext:=nil;
  2771. end;
  2772. end;
  2773. end;
  2774. {$ifdef UNITALIASES}
  2775. {****************************************************************************
  2776. TUNIT_ALIAS
  2777. ****************************************************************************}
  2778. constructor tunit_alias.create(const n:string);
  2779. var
  2780. i : longint;
  2781. begin
  2782. i:=pos('=',n);
  2783. if i=0 then
  2784. fail;
  2785. inherited createname(Copy(n,1,i-1));
  2786. newname:=stringdup(Copy(n,i+1,255));
  2787. end;
  2788. destructor tunit_alias.destroy;
  2789. begin
  2790. stringdispose(newname);
  2791. inherited destroy;
  2792. end;
  2793. procedure addunitalias(const n:string);
  2794. begin
  2795. unitaliases^.insert(tunit_alias,init(Upper(n))));
  2796. end;
  2797. function getunitalias(const n:string):string;
  2798. var
  2799. p : punit_alias;
  2800. begin
  2801. p:=punit_alias(unitaliases^.Find(Upper(n)));
  2802. if assigned(p) then
  2803. getunitalias:=punit_alias(p).newname^
  2804. else
  2805. getunitalias:=n;
  2806. end;
  2807. {$endif UNITALIASES}
  2808. {****************************************************************************
  2809. Init/Done Symtable
  2810. ****************************************************************************}
  2811. procedure InitSymtable;
  2812. begin
  2813. { Reset symbolstack }
  2814. symtablestack:=nil;
  2815. systemunit:=nil;
  2816. { create error syms and def }
  2817. generrorsym:=terrorsym.create;
  2818. generrordef:=terrordef.create;
  2819. { macros }
  2820. initialmacrosymtable:=tmacrosymtable.create(false);
  2821. macrosymtablestack:=TSymtablestack.create;
  2822. macrosymtablestack.push(initialmacrosymtable);
  2823. {$ifdef UNITALIASES}
  2824. { unit aliases }
  2825. unitaliases:=TFPHashObjectList.create;
  2826. {$endif}
  2827. { set some global vars to nil, might be important for the ide }
  2828. class_tobject:=nil;
  2829. interface_iunknown:=nil;
  2830. interface_idispatch:=nil;
  2831. rec_tguid:=nil;
  2832. dupnr:=0;
  2833. end;
  2834. procedure DoneSymtable;
  2835. begin
  2836. generrorsym.owner:=nil;
  2837. generrorsym.free;
  2838. generrordef.owner:=nil;
  2839. generrordef.free;
  2840. initialmacrosymtable.free;
  2841. macrosymtablestack.free;
  2842. {$ifdef UNITALIASES}
  2843. unitaliases.free;
  2844. {$endif}
  2845. end;
  2846. end.