symtable.pas 114 KB

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