pmodules.pas 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928
  1. {
  2. Copyright (c) 1998-2008 by Florian Klaempfl
  3. Handles the parsing and loading of the modules (ppufiles)
  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 pmodules;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses fmodule;
  21. function proc_unit(curr: tmodule):boolean;
  22. function parse_unit_interface_declarations(curr : tmodule) : boolean;
  23. function proc_unit_implementation(curr: tmodule):boolean;
  24. function proc_package(curr: tmodule) : boolean;
  25. function proc_program(curr: tmodule; islibrary : boolean) : boolean;
  26. function proc_program_declarations(curr : tmodule; islibrary : boolean) : boolean;
  27. procedure finish_unit(module:tmodule);
  28. implementation
  29. uses
  30. SysUtils,
  31. globtype,systems,tokens,
  32. cutils,cfileutl,cclasses,comphook,
  33. globals,verbose,finput,fppu,globstat,fpcp,fpkg,
  34. symconst,symbase,symtype,symdef,symsym,symtable,defutil,symcreat,
  35. wpoinfo,
  36. aasmtai,aasmdata,aasmbase,aasmcpu,
  37. cgbase,ngenutil,
  38. nbas,nutils,ncgutil,
  39. link,assemble,import,export,gendef,ppu,comprsrc,dbgbase,
  40. cresstr,procinfo,
  41. objcgutl,
  42. pkgutil,
  43. wpobase,
  44. scanner,pbase,pexpr,psystem,psub,pgenutil,pparautl,ncgvmt,ncgrtti,
  45. ctask,
  46. cpuinfo;
  47. procedure create_objectfile(curr : tmodule);
  48. var
  49. DLLScanner : TDLLScanner;
  50. s : string;
  51. KeepShared : TCmdStrList;
  52. begin
  53. { try to create import entries from system dlls }
  54. if (tf_has_dllscanner in target_info.flags) and
  55. (not curr.linkOtherSharedLibs.Empty) then
  56. begin
  57. { Init DLLScanner }
  58. if assigned(CDLLScanner[target_info.system]) then
  59. DLLScanner:=CDLLScanner[target_info.system].Create
  60. else
  61. internalerror(200104121);
  62. KeepShared:=TCmdStrList.Create;
  63. { Walk all shared libs }
  64. While not curr.linkOtherSharedLibs.Empty do
  65. begin
  66. S:=curr.linkOtherSharedLibs.Getusemask(link_always);
  67. if not DLLScanner.scan(s) then
  68. KeepShared.Concat(s);
  69. end;
  70. DLLscanner.Free;
  71. { Recreate import section }
  72. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  73. begin
  74. if assigned(current_asmdata.asmlists[al_imports]) then
  75. current_asmdata.asmlists[al_imports].clear
  76. else
  77. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  78. importlib.generatelib;
  79. end;
  80. { Readd the not processed files }
  81. while not KeepShared.Empty do
  82. begin
  83. s:=KeepShared.GetFirst;
  84. curr.linkOtherSharedLibs.add(s,link_always);
  85. end;
  86. KeepShared.Free;
  87. end;
  88. { allow a target-specific pass over all assembler code (used by LLVM
  89. to insert type definitions }
  90. cnodeutils.InsertObjectInfo;
  91. { Start and end module debuginfo, at least required for stabs
  92. to insert n_sourcefile lines }
  93. if (cs_debuginfo in current_settings.moduleswitches) or
  94. (cs_use_lineinfo in current_settings.globalswitches) then
  95. current_debuginfo.insertmoduleinfo;
  96. { create the .s file and assemble it }
  97. if not(create_smartlink_library) or not(tf_no_objectfiles_when_smartlinking in target_info.flags) then
  98. GenerateAsm(false);
  99. { Also create a smartlinked version ? }
  100. if create_smartlink_library then
  101. begin
  102. GenerateAsm(true);
  103. if (af_needar in target_asm.flags) then
  104. Linker.MakeStaticLibrary;
  105. end;
  106. { resource files }
  107. CompileResourceFiles;
  108. end;
  109. procedure insertobjectfile(curr : tmodule);
  110. { Insert the used object file for this unit in the used list for this unit }
  111. begin
  112. curr.linkunitofiles.add(curr.objfilename,link_static);
  113. curr.headerflags:=curr.headerflags or uf_static_linked;
  114. if create_smartlink_library then
  115. begin
  116. curr.linkunitstaticlibs.add(curr.staticlibfilename ,link_smart);
  117. curr.headerflags:=curr.headerflags or uf_smart_linked;
  118. end;
  119. if cs_lto in current_settings.moduleswitches then
  120. begin
  121. curr.linkunitofiles.add(ChangeFileExt(curr.objfilename,LTOExt),link_lto);
  122. curr.headerflags:=curr.headerflags or uf_lto_linked;
  123. end;
  124. end;
  125. procedure create_dwarf_frame;
  126. begin
  127. { Dwarf conflicts with smartlinking in separate .a files }
  128. if create_smartlink_library then
  129. exit;
  130. { Call frame information }
  131. { MWE: we write our own info, so dwarf asm support is not really needed }
  132. { if (af_supports_dwarf in target_asm.flags) and }
  133. { CFI is currently broken for Darwin }
  134. if not(target_info.system in systems_darwin) and
  135. (
  136. (tf_needs_dwarf_cfi in target_info.flags) or
  137. (target_dbg.id in [dbg_dwarf2, dbg_dwarf3])
  138. ) then
  139. begin
  140. current_asmdata.asmlists[al_dwarf_frame].Free;
  141. current_asmdata.asmlists[al_dwarf_frame] := TAsmList.create;
  142. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf_frame]);
  143. end;
  144. end;
  145. Function CheckResourcesUsed(curr : tmodule) : boolean;
  146. var
  147. hp : tused_unit;
  148. found : Boolean;
  149. begin
  150. CheckResourcesUsed:=tf_has_winlike_resources in target_info.flags;
  151. if not CheckResourcesUsed then exit;
  152. hp:=tused_unit(usedunits.first);
  153. found:=mf_has_resourcefiles in curr.moduleflags;
  154. while Assigned(hp) and not found do
  155. begin
  156. found:=mf_has_resourcefiles in hp.u.moduleflags;
  157. hp:=tused_unit(hp.next);
  158. end;
  159. CheckResourcesUsed:=found;
  160. end;
  161. function AddUnit(curr : tmodule; const s:string;addasused:boolean): tppumodule;
  162. var
  163. hp : tppumodule;
  164. unitsym : tunitsym;
  165. isnew,load_ok : boolean;
  166. begin
  167. { load unit }
  168. hp:=registerunit(curr,s,'',isnew);
  169. if isnew then
  170. usedunits.concat(tused_unit.create(hp,true,addasused,nil));
  171. load_ok:=hp.loadppu(curr);
  172. hp.adddependency(curr,curr.in_interface);
  173. if not load_ok then
  174. { We must schedule a compile. }
  175. task_handler.addmodule(hp);
  176. { add to symtable stack }
  177. if assigned(hp.globalsymtable) then
  178. symtablestack.push(hp.globalsymtable);
  179. if (m_mac in current_settings.modeswitches) and
  180. assigned(hp.globalmacrosymtable) then
  181. macrosymtablestack.push(hp.globalmacrosymtable);
  182. { insert unitsym }
  183. unitsym:=cunitsym.create(hp.modulename^,hp);
  184. inc(unitsym.refs);
  185. tabstractunitsymtable(curr.localsymtable).insertunit(unitsym);
  186. if addasused then
  187. { add to used units }
  188. curr.addusedunit(hp,false,unitsym);
  189. result:=hp;
  190. end;
  191. function AddUnit(curr :tmodule; const s:string):tppumodule;
  192. begin
  193. result:=AddUnit(curr,s,true);
  194. end;
  195. function maybeloadvariantsunit(curr : tmodule) : boolean;
  196. var
  197. hp : tmodule;
  198. addsystemnamespace : Boolean;
  199. begin
  200. result:=true;
  201. { Do we need the variants unit? Skip this
  202. for VarUtils unit for bootstrapping }
  203. if not(mf_uses_variants in curr.moduleflags) or
  204. (curr.modulename^='VARUTILS') or
  205. (curr.modulename^='SYSTEM.VARUTILS') then
  206. exit;
  207. { Variants unit already loaded? }
  208. hp:=tmodule(loaded_units.first);
  209. while assigned(hp) do
  210. begin
  211. if (hp.modulename^='VARIANTS') or (hp.modulename^='SYSTEM.VARIANTS') then
  212. exit;
  213. hp:=tmodule(hp.next);
  214. end;
  215. { Variants unit is not loaded yet, load it now }
  216. Message(parser_w_implicit_uses_of_variants_unit);
  217. addsystemnamespace:=namespacelist.Find('System')=Nil;
  218. if addsystemnamespace then
  219. namespacelist.concat('System');
  220. result:=AddUnit(curr,'variants').state in [ms_compiled,ms_processed];
  221. if addsystemnamespace then
  222. namespacelist.Remove('System');
  223. end;
  224. function MaybeRemoveResUnit(curr : tmodule) : boolean;
  225. var
  226. resources_used : boolean;
  227. hp : tmodule;
  228. uu : tused_unit;
  229. unitname : shortstring;
  230. begin
  231. { We simply remove the unit from:
  232. - usedunit list, so that things like init/finalization table won't
  233. contain references to this unit
  234. - loaded_units list, so that the unit object file doesn't get linked
  235. with the executable. }
  236. { Note: on windows we always need resources! }
  237. resources_used:=(target_info.system in systems_all_windows)
  238. or CheckResourcesUsed(curr);
  239. if (not resources_used) and (tf_has_winlike_resources in target_info.flags) then
  240. begin
  241. { resources aren't used, so we don't need this unit }
  242. if target_res.id=res_ext then
  243. unitname:='FPEXTRES'
  244. else
  245. unitname:='FPINTRES';
  246. Message1(unit_u_unload_resunit,unitname);
  247. { find the module }
  248. hp:=tmodule(loaded_units.first);
  249. while assigned(hp) do
  250. begin
  251. if hp.is_unit and (hp.modulename^=unitname) then break;
  252. hp:=tmodule(hp.next);
  253. end;
  254. if not assigned(hp) then
  255. internalerror(200801071);
  256. { find its tused_unit in the global list }
  257. uu:=tused_unit(usedunits.first);
  258. while assigned(uu) do
  259. begin
  260. if uu.u=hp then break;
  261. uu:=tused_unit(uu.next);
  262. end;
  263. if not assigned(uu) then
  264. internalerror(200801072);
  265. { remove the tused_unit }
  266. usedunits.Remove(uu);
  267. uu.Free;
  268. // Remove from local list
  269. uu:=tused_unit(curr.used_units.first);
  270. while assigned(uu) do
  271. begin
  272. if uu.u=hp then break;
  273. uu:=tused_unit(uu.next);
  274. end;
  275. if not assigned(uu) then
  276. internalerror(2024020701);
  277. curr.used_units.Remove(uu);
  278. uu.Free;
  279. { remove the module }
  280. loaded_units.Remove(hp);
  281. unloaded_units.Concat(hp);
  282. end;
  283. MaybeRemoveResUnit:=resources_used;
  284. end;
  285. function loadsystemunit(curr : tmodule) : boolean;
  286. var
  287. state: tglobalstate;
  288. sys : tmodule;
  289. begin
  290. Result:=False;
  291. { we are going to rebuild the symtablestack, clear it first }
  292. symtablestack.clear;
  293. macrosymtablestack.clear;
  294. { macro symtable }
  295. macrosymtablestack.push(initialmacrosymtable);
  296. { are we compiling the system unit? }
  297. if (cs_compilesystem in current_settings.moduleswitches) then
  298. begin
  299. systemunit:=tglobalsymtable(curr.localsymtable);
  300. { create system defines }
  301. create_intern_types;
  302. create_intern_symbols;
  303. { Set the owner of errorsym and errortype to symtable to
  304. prevent crashes when accessing .owner }
  305. generrorsym.owner:=systemunit;
  306. generrordef.owner:=systemunit;
  307. exit;
  308. end;
  309. { insert the system unit, it is allways the first. Load also the
  310. internal types from the system unit }
  311. Sys:=AddUnit(curr,'system');
  312. Result:=Assigned(Sys) and (Sys.State in [ms_processed,ms_compiled]);
  313. systemunit:=tglobalsymtable(symtablestack.top);
  314. { load_intern_types resets the scanner... }
  315. current_scanner.tempcloseinputfile;
  316. state:=tglobalstate.create(true);
  317. load_intern_types;
  318. state.restore(true);
  319. FreeAndNil(state);
  320. current_scanner.tempopeninputfile;
  321. { Set the owner of errorsym and errortype to symtable to
  322. prevent crashes when accessing .owner }
  323. generrorsym.owner:=systemunit;
  324. generrordef.owner:=systemunit;
  325. // Implicitly enable unicode strings in unicode RTL in modes objfpc/delphi.
  326. { TODO: Check if we should also do this for mode macpas }
  327. if not (cs_compilesystem in current_settings.moduleswitches) then
  328. if ([m_objfpc,m_delphi] * current_settings.modeswitches)<>[] then
  329. if is_systemunit_unicode then
  330. Include(current_settings.modeswitches,m_default_unicodestring);
  331. end;
  332. { Return true if all units were loaded, no recompilation needed. }
  333. function loaddefaultunits(curr :tmodule) : boolean;
  334. Procedure CheckAddUnit(s: string);
  335. var
  336. OK : boolean;
  337. m : TModule;
  338. begin
  339. m:=AddUnit(curr,s,true);
  340. OK:=assigned(m) and (m.state in [ms_processed,ms_compiled]);
  341. if not ok then
  342. Message2(unit_f_cant_find_ppu,s,curr.realmodulename^);
  343. Result:=ok and Result;
  344. end;
  345. begin
  346. Result:=True;
  347. { Units only required for main module }
  348. if not(curr.is_unit) then
  349. begin
  350. { Heaptrc unit, load heaptrace before any other units especially objpas }
  351. if (cs_use_heaptrc in current_settings.globalswitches) then
  352. CheckAddUnit('heaptrc');
  353. { Valgrind requires c memory manager }
  354. if (cs_gdb_valgrind in current_settings.globalswitches) or
  355. (([cs_sanitize_address]*current_settings.moduleswitches)<>[]) then
  356. CheckAddUnit('cmem');
  357. { Lineinfo unit }
  358. if (cs_use_lineinfo in current_settings.globalswitches) then begin
  359. case target_dbg.id of
  360. dbg_stabs:
  361. CheckAddUnit('lineinfo');
  362. dbg_stabx:
  363. CheckAddUnit('lnfogdb');
  364. else
  365. CheckAddUnit('lnfodwrf');
  366. end;
  367. end;
  368. {$ifdef cpufpemu}
  369. { Floating point emulation unit?
  370. softfpu must be in the system unit anyways (FK)
  371. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  372. CheckAddUnit('softfpu');
  373. }
  374. {$endif cpufpemu}
  375. { Which kind of resource support?
  376. Note: if resources aren't used this unit will be removed later,
  377. otherwise we need it here since it must be loaded quite early }
  378. if (tf_has_winlike_resources in target_info.flags) then
  379. if target_res.id=res_ext then
  380. CheckAddUnit('fpextres')
  381. else
  382. CheckAddUnit('fpintres');
  383. end
  384. else if (cs_checkpointer in current_settings.localswitches) then
  385. CheckAddUnit('heaptrc');
  386. { Objpas unit? }
  387. if m_objpas in current_settings.modeswitches then
  388. CheckAddUnit('objpas');
  389. { Macpas unit? }
  390. if m_mac in current_settings.modeswitches then
  391. CheckAddUnit('macpas');
  392. if m_iso in current_settings.modeswitches then
  393. CheckAddUnit('iso7185');
  394. if m_extpas in current_settings.modeswitches then
  395. begin
  396. { basic procedures for Extended Pascal are for now provided by the iso unit }
  397. CheckAddUnit('iso7185');
  398. CheckAddUnit('extpas');
  399. end;
  400. { blocks support? }
  401. if m_blocks in current_settings.modeswitches then
  402. CheckAddUnit('blockrtl');
  403. { Determine char size. }
  404. // Ansi RTL ?
  405. if not is_systemunit_unicode then
  406. begin
  407. if m_default_unicodestring in current_settings.modeswitches then
  408. CheckAddUnit('uuchar'); // redefines char as widechar
  409. end
  410. else
  411. begin
  412. // Unicode RTL
  413. if not (m_default_ansistring in current_settings.modeswitches) then
  414. if not (curr.modulename^<>'UACHAR') then
  415. CheckAddUnit('uachar'); // redefines char as ansichar
  416. end;
  417. { Objective-C support unit? }
  418. if (m_objectivec1 in current_settings.modeswitches) then
  419. begin
  420. { interface to Objective-C run time }
  421. CheckAddUnit('objc');
  422. loadobjctypes;
  423. { NSObject }
  424. if not(curr.is_unit) or
  425. (curr.modulename^<>'OBJCBASE') then
  426. CheckAddUnit('objcbase');
  427. end;
  428. { Profile unit? Needed for go32v2 only }
  429. if (cs_profile in current_settings.moduleswitches) and
  430. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  431. CheckAddUnit('profile');
  432. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  433. begin
  434. CheckAddUnit('fpcylix');
  435. CheckAddUnit('dynlibs');
  436. end;
  437. {$push}
  438. {$warn 6018 off} { Unreachable code due to compile time evaluation }
  439. { CPU targets with microcontroller support can add a controller specific unit }
  440. if ControllerSupport and (target_info.system in (systems_embedded+systems_freertos)) and
  441. (current_settings.controllertype<>ct_none) and
  442. (embedded_controllers[current_settings.controllertype].controllerunitstr<>'') and
  443. (embedded_controllers[current_settings.controllertype].controllerunitstr<>curr.modulename^) then
  444. CheckAddUnit(embedded_controllers[current_settings.controllertype].controllerunitstr);
  445. {$pop}
  446. {$ifdef XTENSA}
  447. if not(curr.is_unit) and (target_info.system=system_xtensa_freertos) then
  448. if (current_settings.controllertype=ct_esp32) then
  449. begin
  450. if (idf_version>=40100) and (idf_version<40200) then
  451. CheckAddUnit('espidf_40100')
  452. else if (idf_version>=40200) and (idf_version<40400) then
  453. CheckAddUnit('espidf_40200')
  454. else if idf_version>=40400 then
  455. CheckAddUnit('espidf_40400')
  456. else
  457. Comment(V_Warning, 'Unsupported esp-idf version');
  458. end
  459. else if (current_settings.controllertype=ct_esp8266) then
  460. begin
  461. if (idf_version>=30300) and (idf_version<30400) then
  462. CheckAddUnit('esp8266rtos_30300')
  463. else if idf_version>=30400 then
  464. CheckAddUnit('esp8266rtos_30400')
  465. else
  466. Comment(V_Warning, 'Unsupported esp-rtos version');
  467. end;
  468. {$endif XTENSA}
  469. {$ifdef RISCV32}
  470. if not(curr.is_unit) and (target_info.system=system_riscv32_freertos) then
  471. if (current_settings.controllertype=ct_esp32c3) then
  472. begin
  473. if idf_version>=50000 then
  474. CheckAddUnit('esp32c3idf_50000')
  475. else if idf_version>=40400 then
  476. CheckAddUnit('esp32c3idf_40400')
  477. else
  478. Comment(V_Warning, 'Unsupported esp-idf version');
  479. end;
  480. {$endif XTENSA}
  481. end;
  482. { Return true if all units were loaded, no recompilation needed. }
  483. function loadautounits(curr: tmodule) : boolean;
  484. Procedure CheckAddUnit(s: string);
  485. var
  486. OK : boolean;
  487. m : TModule;
  488. begin
  489. m:=AddUnit(curr,s,true);
  490. OK:=assigned(m) and (m.state in [ms_compiled,ms_processed]);
  491. Result:=ok and Result;
  492. end;
  493. var
  494. hs,s : string;
  495. begin
  496. Result:=True;
  497. hs:=autoloadunits;
  498. repeat
  499. s:=GetToken(hs,',');
  500. if s='' then
  501. break;
  502. CheckAddUnit(s);
  503. until false;
  504. end;
  505. procedure parseusesclause(curr: tmodule);
  506. var
  507. s,sorg : ansistring;
  508. fn : string;
  509. pu : tused_unit;
  510. hp2 : tmodule;
  511. unitsym : tunitsym;
  512. filepos : tfileposinfo;
  513. isnew : boolean;
  514. begin
  515. consume(_USES);
  516. repeat
  517. s:=pattern;
  518. sorg:=orgpattern;
  519. filepos:=current_tokenpos;
  520. consume(_ID);
  521. while token=_POINT do
  522. begin
  523. consume(_POINT);
  524. s:=s+'.'+pattern;
  525. sorg:=sorg+'.'+orgpattern;
  526. consume(_ID);
  527. end;
  528. { support "<unit> in '<file>'" construct, but not for tp7 }
  529. fn:='';
  530. if not(m_tp7 in current_settings.modeswitches) and
  531. try_to_consume(_OP_IN) then
  532. fn:=FixFileName(get_stringconst);
  533. { Give a warning if lineinfo is loaded }
  534. if s='LINEINFO' then
  535. begin
  536. Message(parser_w_no_lineinfo_use_switch);
  537. if (target_dbg.id in [dbg_dwarf2, dbg_dwarf3]) then
  538. s := 'LNFODWRF';
  539. sorg := s;
  540. end;
  541. { Give a warning if objpas is loaded }
  542. if s='OBJPAS' then
  543. Message(parser_w_no_objpas_use_mode);
  544. { Using the unit itself is not possible }
  545. if (s<>curr.modulename^) then
  546. begin
  547. { check if the unit is already used }
  548. hp2:=nil;
  549. pu:=tused_unit(curr.used_units.first);
  550. while assigned(pu) do
  551. begin
  552. if (pu.u.modulename^=s) then
  553. begin
  554. hp2:=pu.u;
  555. break;
  556. end;
  557. pu:=tused_unit(pu.next);
  558. end;
  559. if not assigned(hp2) then
  560. begin
  561. hp2:=registerunit(curr,sorg,fn,isnew);
  562. if isnew then
  563. usedunits.concat(tused_unit.create(hp2,curr.in_interface,true,nil));
  564. end
  565. else
  566. Message1(sym_e_duplicate_id,s);
  567. { Create unitsym, we need to use the name as specified, we
  568. can not use the modulename because that can be different
  569. when -Un is used }
  570. current_tokenpos:=filepos;
  571. unitsym:=cunitsym.create(sorg,nil);
  572. { the current module uses the unit hp2 }
  573. curr.addusedunit(hp2,true,unitsym);
  574. end
  575. else
  576. Message1(sym_e_duplicate_id,s);
  577. if token=_COMMA then
  578. begin
  579. pattern:='';
  580. consume(_COMMA);
  581. end
  582. else
  583. break;
  584. until false;
  585. end;
  586. function loadunits(curr: tmodule; frominterface : boolean) : boolean;
  587. var
  588. s : ansistring;
  589. pu : tused_unit;
  590. state: tglobalstate;
  591. isLoaded : Boolean;
  592. mwait : tmodule;
  593. lu : tmodule;
  594. procedure restorestate;
  595. begin
  596. state.restore(true);
  597. if assigned(current_scanner) and (current_module.scanner=current_scanner) then
  598. begin
  599. if assigned(current_scanner.inputfile) then
  600. current_scanner.tempopeninputfile;
  601. end;
  602. state.free;
  603. end;
  604. begin
  605. Result:=true;
  606. mwait:=nil;
  607. current_scanner.tempcloseinputfile;
  608. state:=tglobalstate.create(true);
  609. { Load the units }
  610. pu:=tused_unit(curr.used_units.first);
  611. while assigned(pu) do
  612. begin
  613. lu:=pu.u;
  614. { Only load the units that are in the current
  615. (interface/implementation) uses clause }
  616. if pu.in_uses and
  617. (pu.in_interface=frominterface) then
  618. begin
  619. if (lu.state in [ms_processed, ms_compiled,ms_compiling_waitimpl]) then
  620. isLoaded:=true
  621. else if (lu.state=ms_registered) then
  622. // try to load
  623. isLoaded:=tppumodule(lu).loadppu(curr)
  624. else
  625. isLoaded:=False;
  626. isLoaded:=IsLoaded and not lu.is_reset ;
  627. if not IsLoaded then
  628. begin
  629. if mwait=nil then
  630. mwait:=lu;
  631. // In case of is_reset, the task handler will discard the state if the module was already there
  632. task_handler.addmodule(lu);
  633. end;
  634. IsLoaded:=Isloaded and not curr.is_reset;
  635. Result:=Result and IsLoaded;
  636. { If we were reset, then used_units is no longer correct, and we must exit at once. }
  637. if curr.is_reset then
  638. break;
  639. { is our module compiled? then we can stop }
  640. if curr.state in [ms_compiled,ms_processed] then
  641. break;
  642. { add this unit to the dependencies }
  643. lu.adddependency(curr,frominterface);
  644. { check hints }
  645. pu.check_hints;
  646. end;
  647. pu:=tused_unit(pu.next);
  648. end;
  649. Restorestate;
  650. end;
  651. {
  652. Connect loaded units: check crc and add to symbol tables.
  653. this can only be called after all units were actually loaded!
  654. }
  655. procedure connect_loaded_units(_module : tmodule; preservest:tsymtable);
  656. var
  657. pu : tused_unit;
  658. sorg : ansistring;
  659. unitsymtable: tabstractunitsymtable;
  660. begin
  661. // writeln(_module.get_modulename,': Connecting units');
  662. pu:=tused_unit(_module.used_units.first);
  663. while assigned(pu) do
  664. begin
  665. {
  666. Writeln('Connect : ',Assigned(_module.modulename), ' ', assigned(pu.u), ' ' ,assigned(pu.u.modulename));
  667. if assigned(pu.u) then
  668. begin
  669. if assigned(pu.u.modulename) then
  670. Writeln(_module.modulename^,': Examining connect of file ',pu._fn,' (',pu.u.modulename^,')')
  671. else
  672. Writeln(_module.modulename^,': Examining connect of file ',pu._fn);
  673. end
  674. else
  675. Writeln(_module.modulename^,': Examining unit without module... ');
  676. }
  677. if not (pu.in_uses and
  678. (pu.in_interface=_module.in_interface)) then
  679. begin
  680. // writeln('Must not connect ',pu.u.modulename^,' (pu.in_interface: ',pu.in_interface,' <> module.in_interface',_module.in_interface,')');
  681. end
  682. else
  683. begin
  684. // writeln('Must connect ',pu.u.modulename^,'(sym: ',pu.unitsym.realname,')');
  685. { save crc values }
  686. pu.checksum:=pu.u.crc;
  687. pu.interface_checksum:=pu.u.interface_crc;
  688. pu.indirect_checksum:=pu.u.indirect_crc;
  689. if tppumodule(pu.u).nsprefix<>'' then
  690. begin
  691. { use the name as declared in the uses section for -Un }
  692. sorg:=tppumodule(pu.u).nsprefix+'.'+pu.unitsym.realname;
  693. { update unitsym now that we have access to the full name }
  694. pu.unitsym.free;
  695. pu.unitsym:=cunitsym.create(sorg,pu.u);
  696. end
  697. else
  698. begin
  699. { connect unitsym to the module }
  700. pu.unitsym.module:=pu.u;
  701. pu.unitsym.register_sym;
  702. end;
  703. {
  704. Add the unit symbol in the current symtable.
  705. localsymtable will be nil after the interface uses clause is parsed and the local symtable
  706. is moved to the global.
  707. }
  708. if assigned(_module.localsymtable) then
  709. unitsymtable:=tabstractunitsymtable(_module.localsymtable)
  710. else
  711. unitsymtable:=tabstractunitsymtable(_module.globalsymtable);
  712. // Writeln('Adding used unit sym ',pu.unitsym.realName,' to table ',unitsymtable.get_name);
  713. unitsymtable.insertunit(pu.unitsym);
  714. { add to symtable stack }
  715. // Writeln('Adding used unit symtable ',pu.u.globalsymtable.name^,' (',pu.u.globalsymtable.DefList.Count, ' defs) to stack');
  716. if assigned(preservest) then
  717. symtablestack.pushafter(pu.u.globalsymtable,preservest)
  718. else
  719. symtablestack.push(pu.u.globalsymtable);
  720. if (m_mac in current_settings.modeswitches) and
  721. assigned(pu.u.globalmacrosymtable) then
  722. macrosymtablestack.push(pu.u.globalmacrosymtable);
  723. end;
  724. pu:=tused_unit(pu.next);
  725. end;
  726. // writeln(_module.get_modulename,': Done Connecting units');
  727. end;
  728. procedure reset_all_defs(curr: tmodule);
  729. begin
  730. if assigned(curr.wpoinfo) then
  731. curr.wpoinfo.resetdefs;
  732. end;
  733. procedure free_localsymtables(st:TSymtable);
  734. var
  735. i : longint;
  736. def : tstoreddef;
  737. pd : tprocdef;
  738. begin
  739. for i:=0 to st.DefList.Count-1 do
  740. begin
  741. def:=tstoreddef(st.DefList[i]);
  742. if def.typ=procdef then
  743. begin
  744. pd:=tprocdef(def);
  745. if assigned(pd.localst) and
  746. (pd.localst.symtabletype<>staticsymtable) and
  747. not(po_inline in pd.procoptions) then
  748. begin
  749. free_localsymtables(pd.localst);
  750. pd.localst.free;
  751. pd.localst:=nil;
  752. end;
  753. pd.freeimplprocdefinfo;
  754. pd.done_paraloc_info(calleeside);
  755. end;
  756. end;
  757. end;
  758. procedure free_unregistered_localsymtable_elements(curr : tmodule);
  759. procedure remove_from_procdeflist(adef: tdef);
  760. var
  761. i: Integer;
  762. childdef: tdef;
  763. begin
  764. if adef=nil then exit;
  765. if (adef.typ in [objectdef, recorddef]) and (adef is tabstractrecorddef) then
  766. begin
  767. if tabstractrecorddef(adef).symtable<>nil then
  768. for i:=0 to tabstractrecorddef(adef).symtable.DefList.Count-1 do
  769. begin
  770. childdef:=tdef(tabstractrecorddef(adef).symtable.DefList[i]);
  771. remove_from_procdeflist(childdef);
  772. end;
  773. end
  774. else
  775. if adef.typ=procdef then
  776. begin
  777. tprocsym(tprocdef(adef).procsym).ProcdefList.Remove(adef);
  778. if tprocdef(adef).localst<>nil then
  779. for i:=0 to tprocdef(adef).localst.DefList.Count-1 do
  780. begin
  781. childdef:=tdef(tprocdef(adef).localst.DefList[i]);
  782. remove_from_procdeflist(childdef);
  783. end;
  784. end;
  785. end;
  786. var
  787. i: longint;
  788. def: tdef;
  789. sym: tsym;
  790. begin
  791. for i:=curr.localsymtable.deflist.count-1 downto 0 do
  792. begin
  793. def:=tdef(curr.localsymtable.deflist[i]);
  794. { since commit 48986 deflist might have NIL entries }
  795. if not assigned(def) then
  796. continue;
  797. { this also frees def, as the defs are owned by the symtable }
  798. if not def.is_registered and
  799. not(df_not_registered_no_free in def.defoptions) then
  800. begin
  801. { if it's a procdef, unregister it from its procsym first,
  802. unless that sym hasn't been registered either (it's possible
  803. to have one overload in the interface and another in the
  804. implementation) }
  805. remove_from_procdeflist(def);
  806. curr.localsymtable.deletedef(def);
  807. end;
  808. end;
  809. { from high to low so we hopefully have moves of less data }
  810. for i:=curr.localsymtable.symlist.count-1 downto 0 do
  811. begin
  812. sym:=tsym(curr.localsymtable.symlist[i]);
  813. { this also frees sym, as the symbols are owned by the symtable }
  814. if not sym.is_registered then
  815. curr.localsymtable.DeleteSym(sym);
  816. end;
  817. end;
  818. procedure setupglobalswitches;
  819. begin
  820. if (cs_create_pic in current_settings.moduleswitches) then
  821. begin
  822. def_system_macro('FPC_PIC');
  823. def_system_macro('PIC');
  824. end;
  825. end;
  826. function create_main_proc(const name:TSymStr;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  827. var
  828. ps : tprocsym;
  829. pd : tprocdef;
  830. begin
  831. { there should be no current_procinfo available }
  832. if assigned(current_procinfo) then
  833. internalerror(200304275);
  834. {Generate a procsym for main}
  835. ps:=cprocsym.create('$'+name);
  836. { always register the symbol }
  837. ps.register_sym;
  838. { main are allways used }
  839. inc(ps.refs);
  840. st.insertsym(ps);
  841. pd:=tprocdef(cnodeutils.create_main_procdef(target_info.cprefix+name,potype,ps));
  842. { We don't need a local symtable, change it into the static symtable }
  843. if not (potype in [potype_mainstub,potype_pkgstub,potype_libmainstub]) then
  844. begin
  845. pd.localst.free;
  846. pd.localst:=st;
  847. end
  848. else if (potype=potype_pkgstub) and
  849. (target_info.system in systems_all_windows+systems_nativent) then
  850. pd.proccalloption:=pocall_stdcall
  851. else
  852. pd.proccalloption:=pocall_cdecl;
  853. handle_calling_convention(pd,hcc_default_actions_impl);
  854. { set procinfo and current_procinfo.procdef }
  855. result:=tcgprocinfo(cprocinfo.create(nil));
  856. result.procdef:=pd;
  857. { main proc does always a call e.g. to init system unit }
  858. if potype<>potype_pkgstub then
  859. include(result.flags,pi_do_call);
  860. end;
  861. procedure release_main_proc(curr: tmodule; pi:tcgprocinfo);
  862. begin
  863. { remove localst as it was replaced by staticsymtable }
  864. pi.procdef.localst:=nil;
  865. { remove procinfo }
  866. curr.procinfo:=nil;
  867. pi.free;
  868. pi:=nil;
  869. end;
  870. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  871. procedure maybe_load_got(curr: tmodule);
  872. {$if defined(i386) or defined (sparcgen)}
  873. var
  874. gotvarsym : tstaticvarsym;
  875. {$endif i386 or sparcgen}
  876. begin
  877. {$if defined(i386) or defined(sparcgen)}
  878. if (cs_create_pic in current_settings.moduleswitches) and
  879. (tf_pic_uses_got in target_info.flags) then
  880. begin
  881. { insert symbol for got access in assembler code}
  882. gotvarsym:=cstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',
  883. vs_value,voidpointertype,[vo_is_external]);
  884. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  885. curr.localsymtable.insertsym(gotvarsym);
  886. { avoid unnecessary warnings }
  887. gotvarsym.varstate:=vs_read;
  888. gotvarsym.refs:=1;
  889. end;
  890. {$endif i386 or sparcgen}
  891. end;
  892. function gen_implicit_initfinal(curr: tmodule; flag:tmoduleflag;st:TSymtable):tcgprocinfo;
  893. begin
  894. { create procdef }
  895. case flag of
  896. mf_init :
  897. begin
  898. result:=create_main_proc(make_mangledname('',curr.localsymtable,'init_implicit$'),potype_unitinit,st);
  899. result.procdef.aliasnames.concat(make_mangledname('INIT$',curr.localsymtable,''));
  900. end;
  901. mf_finalize :
  902. begin
  903. result:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize_implicit$'),potype_unitfinalize,st);
  904. result.procdef.aliasnames.concat(make_mangledname('FINALIZE$',curr.localsymtable,''));
  905. if (not curr.is_unit) then
  906. result.procdef.aliasnames.concat('PASCALFINALIZE');
  907. end;
  908. else
  909. internalerror(200304253);
  910. end;
  911. result.code:=cnothingnode.create;
  912. end;
  913. procedure copy_macro(p:TObject; arg:pointer);
  914. begin
  915. TModule(arg).globalmacrosymtable.insertsym(tmacro(p).getcopy);
  916. end;
  917. function try_consume_hintdirective(var moduleopt:tmoduleoptions; var deprecatedmsg:pshortstring):boolean;
  918. var
  919. deprecated_seen,
  920. last_is_deprecated:boolean;
  921. begin
  922. try_consume_hintdirective:=false;
  923. deprecated_seen:=false;
  924. repeat
  925. last_is_deprecated:=false;
  926. case idtoken of
  927. _LIBRARY :
  928. begin
  929. include(moduleopt,mo_hint_library);
  930. try_consume_hintdirective:=true;
  931. end;
  932. _DEPRECATED :
  933. begin
  934. { allow deprecated only once }
  935. if deprecated_seen then
  936. break;
  937. include(moduleopt,mo_hint_deprecated);
  938. try_consume_hintdirective:=true;
  939. last_is_deprecated:=true;
  940. deprecated_seen:=true;
  941. end;
  942. _EXPERIMENTAL :
  943. begin
  944. include(moduleopt,mo_hint_experimental);
  945. try_consume_hintdirective:=true;
  946. end;
  947. _PLATFORM :
  948. begin
  949. include(moduleopt,mo_hint_platform);
  950. try_consume_hintdirective:=true;
  951. end;
  952. _UNIMPLEMENTED :
  953. begin
  954. include(moduleopt,mo_hint_unimplemented);
  955. try_consume_hintdirective:=true;
  956. end;
  957. else
  958. break;
  959. end;
  960. consume(Token);
  961. { handle deprecated message }
  962. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  963. begin
  964. if deprecatedmsg<>nil then
  965. internalerror(201001221);
  966. if token=_CSTRING then
  967. deprecatedmsg:=stringdup(cstringpattern)
  968. else
  969. deprecatedmsg:=stringdup(pattern);
  970. consume(token);
  971. include(moduleopt,mo_has_deprecated_msg);
  972. end;
  973. until false;
  974. end;
  975. {$ifdef jvm}
  976. procedure addmoduleclass(curr : tmodule);
  977. var
  978. def: tobjectdef;
  979. typesym: ttypesym;
  980. begin
  981. { java_jlobject may not have been parsed yet (system unit); in any
  982. case, we only use this to refer to the class type, so inheritance
  983. does not matter }
  984. def:=cobjectdef.create(odt_javaclass,'__FPC_JVM_Module_Class_Alias$',nil,true);
  985. include(def.objectoptions,oo_is_external);
  986. include(def.objectoptions,oo_is_sealed);
  987. def.objextname:=stringdup(curr.realmodulename^);
  988. typesym:=ctypesym.create('__FPC_JVM_Module_Class_Alias$',def);
  989. symtablestack.top.insertsym(typesym);
  990. end;
  991. {$endif jvm}
  992. type
  993. tfinishstate=record
  994. init_procinfo:tcgprocinfo;
  995. finalize_procinfo:tcgprocinfo;
  996. end;
  997. pfinishstate=^tfinishstate;
  998. function proc_unit_implementation(curr: tmodule):boolean;
  999. var
  1000. init_procinfo,
  1001. finalize_procinfo : tcgprocinfo;
  1002. i,j : integer;
  1003. finishstate:pfinishstate;
  1004. begin
  1005. result:=true;
  1006. init_procinfo:=nil;
  1007. finalize_procinfo:=nil;
  1008. finishstate:=nil;
  1009. set_current_module(curr);
  1010. { We get here only after used modules were loaded }
  1011. connect_loaded_units(curr,curr.globalsymtable);
  1012. { All units are read, now give them a number }
  1013. curr.updatemaps;
  1014. { Consume the semicolon if needed.
  1015. At this point the units in the uses clause have at least been parsed
  1016. and are connected, and conditional compilation expressions can
  1017. use the symbols from those units }
  1018. if curr.consume_semicolon_after_uses then
  1019. begin
  1020. consume(_SEMICOLON);
  1021. curr.consume_semicolon_after_uses:=false;
  1022. end;
  1023. { further, changing the globalsymtable is not allowed anymore }
  1024. curr.globalsymtable.sealed:=true;
  1025. symtablestack.push(curr.localsymtable);
  1026. if not curr.interface_only then
  1027. begin
  1028. Message1(parser_u_parsing_implementation,curr.modulename^);
  1029. if curr.in_interface then
  1030. internalerror(200212285);
  1031. { Compile the unit }
  1032. init_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'init$'),potype_unitinit,curr.localsymtable);
  1033. init_procinfo.procdef.aliasnames.concat(make_mangledname('INIT$',curr.localsymtable,''));
  1034. init_procinfo.parse_body;
  1035. { save file pos for debuginfo }
  1036. curr.mainfilepos:=init_procinfo.entrypos;
  1037. { parse finalization section }
  1038. if token=_FINALIZATION then
  1039. begin
  1040. { Compile the finalize }
  1041. finalize_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize$'),potype_unitfinalize,curr.localsymtable);
  1042. finalize_procinfo.procdef.aliasnames.concat(make_mangledname('FINALIZE$',curr.localsymtable,''));
  1043. finalize_procinfo.parse_body;
  1044. end
  1045. end;
  1046. { remove all units that we are waiting for that are already waiting for
  1047. us => breaking up circles }
  1048. for i:=0 to curr.waitingunits.count-1 do
  1049. for j:=curr.waitingforunit.count-1 downto 0 do
  1050. if curr.waitingunits[i]=curr.waitingforunit[j] then
  1051. curr.waitingforunit.delete(j);
  1052. {$ifdef DEBUG_UNITWAITING}
  1053. Writeln('Units waiting for ', curr.modulename^, ': ',
  1054. curr.waitingforunit.Count);
  1055. {$endif}
  1056. result:=curr.waitingforunit.count=0;
  1057. { save all information that is needed for finishing the unit }
  1058. New(finishstate);
  1059. finishstate^.init_procinfo:=init_procinfo;
  1060. finishstate^.finalize_procinfo:=finalize_procinfo;
  1061. curr.finishstate:=finishstate;
  1062. if result then
  1063. finish_unit(curr)
  1064. else
  1065. curr.state:=ms_compiling_waitfinish;
  1066. end;
  1067. function parse_unit_interface_declarations(curr : tmodule) : boolean;
  1068. begin
  1069. result:=true;
  1070. set_current_module(curr);
  1071. { update the symtable }
  1072. connect_loaded_units(curr,nil);
  1073. { We must do this again, because units can have been added to the list while another task was being handled }
  1074. curr.updatemaps;
  1075. { consume the semicolon after maps have been updated else conditional compiling expressions
  1076. might cause internal errors, see tw8611 }
  1077. if curr.consume_semicolon_after_uses then
  1078. begin
  1079. consume(_SEMICOLON);
  1080. curr.consume_semicolon_after_uses:=false;
  1081. end;
  1082. { now push our own symtable }
  1083. symtablestack.push(curr.globalsymtable);
  1084. { Dump stack
  1085. Write(curr.modulename^);
  1086. symtablestack.dump;
  1087. }
  1088. { ... parse the declarations }
  1089. Message1(parser_u_parsing_interface,curr.realmodulename^);
  1090. {$ifdef jvm}
  1091. { fake classdef to represent the class corresponding to the unit }
  1092. addmoduleclass(curr);
  1093. {$endif}
  1094. read_interface_declarations;
  1095. { Export macros defined in the interface for macpas. The macros
  1096. are put in the globalmacrosymtable that will only be used by other
  1097. units. The current unit continues to use the localmacrosymtable }
  1098. if (m_mac in current_settings.modeswitches) then
  1099. begin
  1100. curr.globalmacrosymtable:=tmacrosymtable.create(true);
  1101. curr.localmacrosymtable.SymList.ForEachCall(@copy_macro,curr);
  1102. end;
  1103. { leave when we got an error }
  1104. if (Errorcount>0) and not status.skip_error then
  1105. begin
  1106. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1107. status.skip_error:=true;
  1108. symtablestack.pop(curr.globalsymtable);
  1109. {$ifdef DEBUG_NODE_XML}
  1110. XMLFinalizeNodeFile('unit');
  1111. {$endif DEBUG_NODE_XML}
  1112. exit;
  1113. end;
  1114. { we need to be able to reference these in descendants,
  1115. so they must be generated and included in the interface }
  1116. if (target_cpu=tsystemcpu.cpu_wasm32) then
  1117. add_synthetic_interface_classes_for_st(curr.globalsymtable,true,false);
  1118. { Our interface is compiled, generate CRC and switch to implementation }
  1119. if not(cs_compilesystem in current_settings.moduleswitches) and
  1120. (Errorcount=0) then
  1121. tppumodule(curr).getppucrc;
  1122. curr.in_interface:=false;
  1123. curr.interface_compiled:=true;
  1124. { First reload all units depending on our interface, we need to do this
  1125. in the implementation part to prevent erroneous circular references }
  1126. tppumodule(curr).setdefgeneration;
  1127. tppumodule(curr).reload_flagged_units;
  1128. { Parse the implementation section }
  1129. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  1130. curr.interface_only:=true
  1131. else
  1132. curr.interface_only:=false;
  1133. parse_only:=false;
  1134. { create static symbol table }
  1135. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  1136. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1137. maybe_load_got(curr);
  1138. if not curr.interface_only then
  1139. begin
  1140. consume(_IMPLEMENTATION);
  1141. Message1(unit_u_loading_implementation_units,curr.modulename^);
  1142. { Read the implementation units }
  1143. if token=_USES then
  1144. begin
  1145. parseusesclause(curr);
  1146. if not loadunits(curr,false) then
  1147. curr.state:=ms_compiling_waitimpl;
  1148. { do not consume the semicolon yet, because the units in the uses clause
  1149. may not yet be loaded and conditional compilation expressions may
  1150. depend on symbols from those units }
  1151. curr.consume_semicolon_after_uses:=True;
  1152. end
  1153. else
  1154. curr.consume_semicolon_after_uses:=False;
  1155. end;
  1156. if curr.state in [ms_compiled,ms_processed] then
  1157. begin
  1158. // Writeln('Popping global symtable ?');
  1159. symtablestack.pop(curr.globalsymtable);
  1160. end;
  1161. { Can we continue compiling ? }
  1162. result:=curr.state<>ms_compiling_waitimpl;
  1163. if result then
  1164. result:=proc_unit_implementation(curr)
  1165. end;
  1166. function proc_unit(curr: tmodule):boolean;
  1167. var
  1168. main_file: tinputfile;
  1169. s1,s2 : ^string; {Saves stack space}
  1170. unitname : ansistring;
  1171. unitname8 : string[8];
  1172. feature : tfeature;
  1173. load_ok : boolean;
  1174. begin
  1175. result:=true;
  1176. if m_mac in current_settings.modeswitches then
  1177. curr.mode_switch_allowed:= false;
  1178. consume(_UNIT);
  1179. if curr.is_initial then
  1180. Status.IsExe:=false;
  1181. unitname:=orgpattern;
  1182. consume(_ID);
  1183. while token=_POINT do
  1184. begin
  1185. consume(_POINT);
  1186. unitname:=unitname+'.'+orgpattern;
  1187. consume(_ID);
  1188. end;
  1189. { create filenames and unit name }
  1190. main_file := current_scanner.inputfile;
  1191. while assigned(main_file.next) do
  1192. main_file := main_file.next;
  1193. new(s1);
  1194. s1^:=curr.modulename^;
  1195. curr.SetFileName(main_file.path+main_file.name,true);
  1196. curr.SetModuleName(unitname);
  1197. {$ifdef DEBUG_NODE_XML}
  1198. XMLInitializeNodeFile('unit', unitname);
  1199. {$endif DEBUG_NODE_XML}
  1200. { check for system unit }
  1201. new(s2);
  1202. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name),''));
  1203. unitname8:=copy(curr.modulename^,1,8);
  1204. if (cs_check_unit_name in current_settings.globalswitches) and
  1205. (
  1206. not(
  1207. (curr.modulename^=s2^) or
  1208. (
  1209. (length(curr.modulename^)>8) and
  1210. (unitname8=s2^)
  1211. )
  1212. )
  1213. or
  1214. (
  1215. (length(s1^)>8) and
  1216. (s1^<>curr.modulename^)
  1217. )
  1218. ) then
  1219. Message2(unit_e_illegal_unit_name,curr.realmodulename^,s1^);
  1220. if (curr.modulename^='SYSTEM') then
  1221. include(current_settings.moduleswitches,cs_compilesystem);
  1222. dispose(s2);
  1223. dispose(s1);
  1224. if (target_info.system in systems_unit_program_exports) then
  1225. exportlib.preparelib(curr.realmodulename^);
  1226. { parse hint directives }
  1227. try_consume_hintdirective(curr.moduleoptions, curr.deprecatedmsg);
  1228. consume(_SEMICOLON);
  1229. { handle the global switches, do this before interface, because after interface has been
  1230. read, all following directives are parsed as well }
  1231. setupglobalswitches;
  1232. { generate now the global symboltable,
  1233. define first as local to overcome dependency conflicts }
  1234. curr.localsymtable:=tglobalsymtable.create(curr.modulename^,curr.moduleid);
  1235. { insert unitsym of this unit to prevent other units having
  1236. the same name }
  1237. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  1238. { load default system unit, it must be loaded before interface is parsed
  1239. else we cannot use e.g. feature switches before the next real token }
  1240. load_ok:=loadsystemunit(curr);
  1241. { system unit is loaded, now insert feature defines }
  1242. for feature:=low(tfeature) to high(tfeature) do
  1243. if feature in features then
  1244. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1245. consume(_INTERFACE);
  1246. { global switches are read, so further changes aren't allowed }
  1247. curr.in_global:=false;
  1248. message1(unit_u_loading_interface_units,curr.modulename^);
  1249. { update status }
  1250. status.currentmodule:=curr.realmodulename^;
  1251. { maybe turn off m_objpas if we are compiling objpas }
  1252. if (curr.modulename^='OBJPAS') then
  1253. exclude(current_settings.modeswitches,m_objpas);
  1254. { maybe turn off m_mac if we are compiling macpas }
  1255. if (curr.modulename^='MACPAS') then
  1256. exclude(current_settings.modeswitches,m_mac);
  1257. parse_only:=true;
  1258. { load default units, like language mode units }
  1259. if not(cs_compilesystem in current_settings.moduleswitches) then
  1260. load_ok:=loaddefaultunits(curr) and load_ok;
  1261. { insert qualifier for the system unit (allows system.writeln) }
  1262. if not(cs_compilesystem in current_settings.moduleswitches) and
  1263. (token=_USES) then
  1264. begin
  1265. // We do this as late as possible.
  1266. if Assigned(curr) then
  1267. curr.Loadlocalnamespacelist
  1268. else
  1269. current_namespacelist:=Nil;
  1270. parseusesclause(curr);
  1271. load_ok:=loadunits(curr,true) and load_ok;
  1272. { has it been compiled at a higher level ?}
  1273. if curr.state in [ms_compiled,ms_processed] then
  1274. begin
  1275. Message1(parser_u_already_compiled,curr.realmodulename^);
  1276. exit;
  1277. end;
  1278. curr.consume_semicolon_after_uses:=true;
  1279. end
  1280. else
  1281. curr.consume_semicolon_after_uses:=false;
  1282. { move the global symtable from the temporary local to global }
  1283. current_module.globalsymtable:=current_module.localsymtable;
  1284. current_module.localsymtable:=nil;
  1285. { Now we check if we can continue. }
  1286. if not load_ok then
  1287. curr.state:=ms_compiling_waitintf;
  1288. { create whole program optimisation information (may already be
  1289. updated in the interface, e.g., in case of classrefdef typed
  1290. constants }
  1291. curr.wpoinfo:=tunitwpoinfo.create;
  1292. { Can we continue compiling ? }
  1293. result:=curr.state<>ms_compiling_waitintf;
  1294. if result then
  1295. result:=parse_unit_interface_declarations(curr);
  1296. end;
  1297. procedure finish_unit(module:tmodule);
  1298. function is_assembler_generated:boolean;
  1299. var
  1300. hal : tasmlisttype;
  1301. begin
  1302. result:=false;
  1303. if Errorcount=0 then
  1304. begin
  1305. for hal:=low(TasmlistType) to high(TasmlistType) do
  1306. if not current_asmdata.asmlists[hal].empty then
  1307. begin
  1308. result:=true;
  1309. exit;
  1310. end;
  1311. end;
  1312. end;
  1313. procedure module_is_done(curr: tmodule);inline;
  1314. begin
  1315. dispose(pfinishstate(curr.finishstate));
  1316. curr.finishstate:=nil;
  1317. end;
  1318. var
  1319. {$ifdef EXTDEBUG}
  1320. store_crc,
  1321. {$endif EXTDEBUG}
  1322. store_interface_crc,
  1323. store_indirect_crc: cardinal;
  1324. force_init_final : boolean;
  1325. init_procinfo,
  1326. finalize_procinfo : tcgprocinfo;
  1327. i : longint;
  1328. ag : boolean;
  1329. finishstate : tfinishstate;
  1330. waitingmodule : tmodule;
  1331. begin
  1332. { curr is now module }
  1333. if not assigned(module.finishstate) then
  1334. internalerror(2012091801);
  1335. finishstate:=pfinishstate(module.finishstate)^;
  1336. finalize_procinfo:=finishstate.finalize_procinfo;
  1337. init_procinfo:=finishstate.init_procinfo;
  1338. { Generate specializations of objectdefs methods }
  1339. generate_specialization_procs;
  1340. // This needs to be done before we generate the VMTs
  1341. if (target_cpu=tsystemcpu.cpu_wasm32) then
  1342. begin
  1343. add_synthetic_interface_classes_for_st(module.globalsymtable,false,true);
  1344. add_synthetic_interface_classes_for_st(module.localsymtable,true,true);
  1345. end;
  1346. { generate construction functions for all attributes in the unit:
  1347. this must be done before writing the VMTs because
  1348. during VMT writing the extended field info is written }
  1349. generate_attr_constrs(current_module.used_rtti_attrs);
  1350. { Generate VMTs }
  1351. if Errorcount=0 then
  1352. begin
  1353. write_vmts(module.globalsymtable,true);
  1354. write_vmts(module.localsymtable,false);
  1355. end;
  1356. { add implementations for synthetic method declarations added by
  1357. the compiler }
  1358. add_synthetic_method_implementations(module.globalsymtable);
  1359. add_synthetic_method_implementations(module.localsymtable);
  1360. { if the unit contains ansi/widestrings, initialization and
  1361. finalization code must be forced }
  1362. force_init_final:=tglobalsymtable(module.globalsymtable).needs_init_final or
  1363. tstaticsymtable(module.localsymtable).needs_init_final;
  1364. { should we force unit initialization? }
  1365. { this is a hack, but how can it be done better ? }
  1366. { Now the sole purpose of this is to change 'init' to 'init_implicit',
  1367. is it needed at all? (Sergei) }
  1368. { it's needed in case cnodeutils.force_init = true }
  1369. if (force_init_final or cnodeutils.force_init) and
  1370. (
  1371. not assigned(init_procinfo) or
  1372. has_no_code(init_procinfo.code)
  1373. ) then
  1374. begin
  1375. { first release the not used init procinfo }
  1376. if assigned(init_procinfo) then
  1377. begin
  1378. release_proc_symbol(init_procinfo.procdef);
  1379. release_main_proc(module,init_procinfo);
  1380. end;
  1381. init_procinfo:=gen_implicit_initfinal(module,mf_init,module.localsymtable);
  1382. end;
  1383. if (force_init_final or cnodeutils.force_final) and
  1384. (
  1385. not assigned(finalize_procinfo) or
  1386. has_no_code(finalize_procinfo.code)
  1387. ) then
  1388. begin
  1389. { first release the not used finalize procinfo }
  1390. if assigned(finalize_procinfo) then
  1391. begin
  1392. release_proc_symbol(finalize_procinfo.procdef);
  1393. release_main_proc(module,finalize_procinfo);
  1394. end;
  1395. finalize_procinfo:=gen_implicit_initfinal(module,mf_finalize,module.localsymtable);
  1396. end;
  1397. { Now both init and finalize bodies are read and it is known
  1398. which variables are used in both init and finalize we can now
  1399. generate the code. This is required to prevent putting a variable in
  1400. a register that is also used in the finalize body (PFV) }
  1401. if assigned(init_procinfo) then
  1402. begin
  1403. if (force_init_final or cnodeutils.force_init) or
  1404. not(has_no_code(init_procinfo.code)) then
  1405. begin
  1406. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  1407. init_procinfo.generate_code_tree;
  1408. include(module.moduleflags,mf_init);
  1409. end
  1410. else
  1411. release_proc_symbol(init_procinfo.procdef);
  1412. init_procinfo.resetprocdef;
  1413. release_main_proc(module,init_procinfo);
  1414. end;
  1415. if assigned(finalize_procinfo) then
  1416. begin
  1417. if force_init_final or
  1418. cnodeutils.force_init or
  1419. not(has_no_code(finalize_procinfo.code)) then
  1420. begin
  1421. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  1422. finalize_procinfo.generate_code_tree;
  1423. include(module.moduleflags,mf_finalize);
  1424. end
  1425. else
  1426. release_proc_symbol(finalize_procinfo.procdef);
  1427. finalize_procinfo.resetprocdef;
  1428. release_main_proc(module,finalize_procinfo);
  1429. end;
  1430. symtablestack.pop(module.localsymtable);
  1431. symtablestack.pop(module.globalsymtable);
  1432. { the last char should always be a point }
  1433. { Do not attempt to read next token after dot,
  1434. there may be a #0 when the unit was finished in a separate stage }
  1435. consume_last_dot;
  1436. { reset wpo flags for all defs }
  1437. reset_all_defs(module);
  1438. if (Errorcount=0) then
  1439. begin
  1440. { tests, if all (interface) forwards are resolved }
  1441. tstoredsymtable(module.globalsymtable).check_forwards;
  1442. { check if all private fields are used }
  1443. tstoredsymtable(module.globalsymtable).allprivatesused;
  1444. { test static symtable }
  1445. tstoredsymtable(module.localsymtable).allsymbolsused;
  1446. tstoredsymtable(module.localsymtable).allprivatesused;
  1447. tstoredsymtable(module.localsymtable).check_forwards;
  1448. tstoredsymtable(module.localsymtable).checklabels;
  1449. { used units }
  1450. module.allunitsused;
  1451. end;
  1452. { leave when we got an error }
  1453. if (Errorcount>0) and not status.skip_error then
  1454. begin
  1455. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1456. status.skip_error:=true;
  1457. module_is_done(module);
  1458. {$ifdef DEBUG_NODE_XML}
  1459. XMLFinalizeNodeFile('unit');
  1460. {$endif DEBUG_NODE_XML}
  1461. exit;
  1462. end;
  1463. { if an Objective-C module, generate rtti and module info }
  1464. MaybeGenerateObjectiveCImageInfo(module.globalsymtable,module.localsymtable);
  1465. { do we need to add the variants unit? }
  1466. maybeloadvariantsunit(module);
  1467. { generate rtti/init tables }
  1468. write_persistent_type_info(module.globalsymtable,true);
  1469. write_persistent_type_info(module.localsymtable,false);
  1470. { Tables }
  1471. cnodeutils.InsertThreadvars;
  1472. { Resource strings }
  1473. GenerateResourceStrings;
  1474. { Widestring typed constants }
  1475. cnodeutils.InsertWideInits;
  1476. { Resourcestring references }
  1477. cnodeutils.InsertResStrInits;
  1478. { generate debuginfo }
  1479. if (cs_debuginfo in current_settings.moduleswitches) then
  1480. current_debuginfo.inserttypeinfo;
  1481. { generate imports }
  1482. if module.ImportLibraryList.Count>0 then
  1483. importlib.generatelib;
  1484. { insert own objectfile, or say that it's in a library
  1485. (no check for an .o when loading) }
  1486. ag:=is_assembler_generated;
  1487. if ag then
  1488. insertobjectfile(module)
  1489. else
  1490. begin
  1491. module.headerflags:=module.headerflags or uf_no_link;
  1492. exclude(module.moduleflags,mf_has_stabs_debuginfo);
  1493. exclude(module.moduleflags,mf_has_dwarf_debuginfo);
  1494. end;
  1495. if ag then
  1496. begin
  1497. { create callframe info }
  1498. create_dwarf_frame;
  1499. { assemble }
  1500. create_objectfile(module);
  1501. end;
  1502. { Write out the ppufile after the object file has been created }
  1503. store_interface_crc:=module.interface_crc;
  1504. store_indirect_crc:=module.indirect_crc;
  1505. {$ifdef EXTDEBUG}
  1506. store_crc:=module.crc;
  1507. {$endif EXTDEBUG}
  1508. if (Errorcount=0) then
  1509. tppumodule(module).writeppu;
  1510. if not(cs_compilesystem in current_settings.moduleswitches) then
  1511. begin
  1512. if store_interface_crc<>module.interface_crc then
  1513. Message1(unit_u_interface_crc_changed,module.ppufilename);
  1514. if store_indirect_crc<>module.indirect_crc then
  1515. Message1(unit_u_indirect_crc_changed,module.ppufilename);
  1516. end;
  1517. {$ifdef EXTDEBUG}
  1518. if not(cs_compilesystem in current_settings.moduleswitches) then
  1519. if (store_crc<>module.crc) then
  1520. Message1(unit_u_implementation_crc_changed,module.ppufilename);
  1521. {$endif EXTDEBUG}
  1522. { release unregistered defs/syms from the localsymtable }
  1523. free_unregistered_localsymtable_elements(module);
  1524. { release local symtables that are not needed anymore }
  1525. free_localsymtables(module.globalsymtable);
  1526. free_localsymtables(module.localsymtable);
  1527. { leave when we got an error }
  1528. if (Errorcount>0) and not status.skip_error then
  1529. begin
  1530. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1531. status.skip_error:=true;
  1532. module_is_done(module);
  1533. {$ifdef DEBUG_NODE_XML}
  1534. XMLFinalizeNodeFile('unit');
  1535. {$endif DEBUG_NODE_XML}
  1536. exit;
  1537. end;
  1538. {$ifdef debug_devirt}
  1539. { print out all instantiated class/object types }
  1540. writeln('constructed object/class/classreftypes in ',module.realmodulename^);
  1541. for i := 0 to module.wpoinfo.createdobjtypes.count-1 do
  1542. begin
  1543. write(' ',tdef(module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1544. case tdef(module.wpoinfo.createdobjtypes[i]).typ of
  1545. objectdef:
  1546. case tobjectdef(module.wpoinfo.createdobjtypes[i]).objecttype of
  1547. odt_object:
  1548. writeln(' (object)');
  1549. odt_class:
  1550. writeln(' (class)');
  1551. else
  1552. internalerror(2008101103);
  1553. end;
  1554. else
  1555. internalerror(2008101104);
  1556. end;
  1557. end;
  1558. for i := 0 to module.wpoinfo.createdclassrefobjtypes.count-1 do
  1559. begin
  1560. write(' Class Of ',tdef(module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1561. case tdef(module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1562. objectdef:
  1563. case tobjectdef(module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1564. odt_class:
  1565. writeln(' (classrefdef)');
  1566. else
  1567. internalerror(2008101105);
  1568. end
  1569. else
  1570. internalerror(2008101102);
  1571. end;
  1572. end;
  1573. {$endif debug_devirt}
  1574. Message1(unit_u_finished_compiling,module.modulename^);
  1575. module_is_done(module);
  1576. module.end_of_parsing;
  1577. for i:=0 to module.waitingunits.count-1 do
  1578. begin
  1579. waitingmodule:=tmodule(module.waitingunits[i]);
  1580. waitingmodule.remove_from_waitingforunits(module);
  1581. end;
  1582. {$ifdef DEBUG_NODE_XML}
  1583. XMLFinalizeNodeFile('unit');
  1584. {$endif DEBUG_NODE_XML}
  1585. end;
  1586. function proc_package(curr: tmodule) : boolean;
  1587. var
  1588. main_file : tinputfile;
  1589. hp,hp2 : tmodule;
  1590. pkg : tpcppackage;
  1591. main_procinfo : tcgprocinfo;
  1592. force_init_final : boolean;
  1593. uu : tused_unit;
  1594. module_name: ansistring;
  1595. pentry: ppackageentry;
  1596. feature : tfeature;
  1597. begin
  1598. Result:=True;
  1599. Status.IsPackage:=true;
  1600. Status.IsExe:=true;
  1601. parse_only:=false;
  1602. main_procinfo:=nil;
  1603. {init_procinfo:=nil;
  1604. finalize_procinfo:=nil;}
  1605. if not (tf_supports_packages in target_info.flags) then
  1606. message1(parser_e_packages_not_supported,target_info.name);
  1607. if not RelocSectionSetExplicitly then
  1608. RelocSection:=true;
  1609. { Relocation works only without stabs under Windows when }
  1610. { external linker (LD) is used. LD generates relocs for }
  1611. { stab sections which is not loaded in memory. It causes }
  1612. { AV error when DLL is loaded and relocation is needed. }
  1613. { Internal linker does not have this problem. }
  1614. if RelocSection and
  1615. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1616. (cs_link_extern in current_settings.globalswitches) then
  1617. begin
  1618. include(current_settings.globalswitches,cs_link_strip);
  1619. { Warning stabs info does not work with reloc section !! }
  1620. if (cs_debuginfo in current_settings.moduleswitches) and
  1621. (target_dbg.id=dbg_stabs) then
  1622. begin
  1623. Message1(parser_w_parser_reloc_no_debug,curr.mainsource);
  1624. Message(parser_w_parser_win32_debug_needs_WN);
  1625. exclude(current_settings.moduleswitches,cs_debuginfo);
  1626. end;
  1627. end;
  1628. { get correct output names }
  1629. main_file := current_scanner.inputfile;
  1630. while assigned(main_file.next) do
  1631. main_file := main_file.next;
  1632. curr.SetFileName(main_file.path+main_file.name,true);
  1633. { consume _PACKAGE word }
  1634. consume(_ID);
  1635. module_name:=orgpattern;
  1636. consume(_ID);
  1637. while token=_POINT do
  1638. begin
  1639. consume(_POINT);
  1640. module_name:=module_name+'.'+orgpattern;
  1641. consume(_ID);
  1642. end;
  1643. curr.setmodulename(module_name);
  1644. curr.ispackage:=true;
  1645. exportlib.preparelib(module_name);
  1646. pkg:=tpcppackage.create(module_name);
  1647. if tf_library_needs_pic in target_info.flags then
  1648. include(current_settings.moduleswitches,cs_create_pic);
  1649. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1650. read, all following directives are parsed as well }
  1651. setupglobalswitches;
  1652. {$ifdef DEBUG_NODE_XML}
  1653. XMLInitializeNodeFile('package', module_name);
  1654. {$endif DEBUG_NODE_XML}
  1655. consume(_SEMICOLON);
  1656. { global switches are read, so further changes aren't allowed }
  1657. curr.in_global:=false;
  1658. { set implementation flag }
  1659. curr.in_interface:=false;
  1660. curr.interface_compiled:=true;
  1661. { insert after the unit symbol tables the static symbol table }
  1662. { of the program }
  1663. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  1664. { ensure that no packages are picked up from the options }
  1665. packagelist.clear;
  1666. // There should always be a requires, except for the system package. So we load here
  1667. if Assigned(curr) then
  1668. curr.Loadlocalnamespacelist
  1669. else
  1670. current_namespacelist:=Nil;
  1671. {Read the packages used by the package we compile.}
  1672. if (token=_ID) and (idtoken=_REQUIRES) then
  1673. begin
  1674. { consume _REQUIRES word }
  1675. consume(_ID);
  1676. while true do
  1677. begin
  1678. if token=_ID then
  1679. begin
  1680. module_name:=orgpattern;
  1681. consume(_ID);
  1682. while token=_POINT do
  1683. begin
  1684. consume(_POINT);
  1685. module_name:=module_name+'.'+orgpattern;
  1686. consume(_ID);
  1687. end;
  1688. add_package(module_name,false,true);
  1689. end
  1690. else
  1691. consume(_ID);
  1692. if token=_COMMA then
  1693. consume(_COMMA)
  1694. else
  1695. break;
  1696. end;
  1697. consume(_SEMICOLON);
  1698. end;
  1699. { now load all packages, so that we can determine whether a unit is
  1700. already provided by one of the loaded packages }
  1701. load_packages;
  1702. if packagelist.Count>0 then
  1703. begin
  1704. { this means the SYSTEM unit *must* be part of one of the required
  1705. packages, so load it }
  1706. AddUnit(curr,'system',false);
  1707. systemunit:=tglobalsymtable(symtablestack.top);
  1708. load_intern_types;
  1709. { system unit is loaded, now insert feature defines }
  1710. for feature:=low(tfeature) to high(tfeature) do
  1711. if feature in features then
  1712. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1713. end;
  1714. {Load the units used by the program we compile.}
  1715. if (token=_ID) and (idtoken=_CONTAINS) then
  1716. begin
  1717. { consume _CONTAINS word }
  1718. consume(_ID);
  1719. while true do
  1720. begin
  1721. if token=_ID then
  1722. begin
  1723. module_name:=orgpattern;
  1724. consume(_ID);
  1725. while token=_POINT do
  1726. begin
  1727. consume(_POINT);
  1728. module_name:=module_name+'.'+orgpattern;
  1729. consume(_ID);
  1730. end;
  1731. hp:=AddUnit(curr,module_name);
  1732. if (hp.modulename^='SYSTEM') and not assigned(systemunit) then
  1733. begin
  1734. systemunit:=tglobalsymtable(hp.globalsymtable);
  1735. load_intern_types;
  1736. end;
  1737. end
  1738. else
  1739. consume(_ID);
  1740. if token=_COMMA then
  1741. consume(_COMMA)
  1742. else break;
  1743. end;
  1744. consume(_SEMICOLON);
  1745. end;
  1746. { All units are read, now give them a number }
  1747. curr.updatemaps;
  1748. hp:=tmodule(loaded_units.first);
  1749. while assigned(hp) do
  1750. begin
  1751. if (hp<>curr) and not assigned(hp.package) then
  1752. begin
  1753. if mf_package_deny in hp.moduleflags then
  1754. message1(package_e_unit_deny_package,hp.realmodulename^);
  1755. { part of the package's used, aka contained units? }
  1756. uu:=tused_unit(curr.used_units.first);
  1757. while assigned(uu) do
  1758. begin
  1759. if uu.u=hp then
  1760. break;
  1761. uu:=tused_unit(uu.next);
  1762. end;
  1763. if not assigned(uu) then
  1764. message2(package_n_implicit_unit_import,hp.realmodulename^,curr.realmodulename^);
  1765. end;
  1766. { was this unit listed as a contained unit? If so => error }
  1767. if (hp<>curr) and assigned(hp.package) then
  1768. begin
  1769. uu:=tused_unit(curr.used_units.first);
  1770. while assigned(uu) do
  1771. begin
  1772. if uu.u=hp then
  1773. break;
  1774. uu:=tused_unit(uu.next);
  1775. end;
  1776. if assigned(uu) then
  1777. message2(package_e_unit_already_contained_in_package,hp.realmodulename^,hp.package.realpackagename^);
  1778. end;
  1779. hp:=tmodule(hp.next);
  1780. end;
  1781. {Insert the name of the main program into the symbol table.}
  1782. if curr.realmodulename^<>'' then
  1783. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  1784. Message1(parser_u_parsing_implementation,curr.mainsource);
  1785. symtablestack.push(curr.localsymtable);
  1786. { create whole program optimisation information }
  1787. curr.wpoinfo:=tunitwpoinfo.create;
  1788. { should we force unit initialization? }
  1789. force_init_final:=tstaticsymtable(curr.localsymtable).needs_init_final;
  1790. if force_init_final or cnodeutils.force_init then
  1791. {init_procinfo:=gen_implicit_initfinal(mf_init,curr.localsymtable)};
  1792. { Add symbol to the exports section for win32 so smartlinking a
  1793. DLL will include the edata section }
  1794. if assigned(exportlib) and
  1795. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1796. (mf_has_exports in curr.moduleflags) then
  1797. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',curr.localsymtable,''),0));
  1798. { all labels must be defined before generating code }
  1799. if Errorcount=0 then
  1800. tstoredsymtable(curr.localsymtable).checklabels;
  1801. symtablestack.pop(curr.localsymtable);
  1802. { consume the last point }
  1803. consume(_END);
  1804. consume(_POINT);
  1805. if (Errorcount=0) then
  1806. begin
  1807. { test static symtable }
  1808. tstoredsymtable(curr.localsymtable).allsymbolsused;
  1809. tstoredsymtable(curr.localsymtable).allprivatesused;
  1810. tstoredsymtable(curr.localsymtable).check_forwards;
  1811. { Note: all contained units are considered as used }
  1812. end;
  1813. if target_info.system in systems_all_windows+systems_nativent then
  1814. begin
  1815. main_procinfo:=create_main_proc('_PkgEntryPoint',potype_pkgstub,curr.localsymtable);
  1816. main_procinfo.procdef.aliasnames.concat('_DLLMainCRTStartup');
  1817. main_procinfo.code:=generate_pkg_stub(main_procinfo.procdef);
  1818. main_procinfo.generate_code;
  1819. end;
  1820. {$ifdef DEBUG_NODE_XML}
  1821. XMLFinalizeNodeFile('package');
  1822. {$endif DEBUG_NODE_XML}
  1823. { leave when we got an error }
  1824. if (Errorcount>0) and not status.skip_error then
  1825. begin
  1826. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1827. status.skip_error:=true;
  1828. pkg.free;
  1829. exit;
  1830. end;
  1831. { remove all unused units, this happends when units are removed
  1832. from the uses clause in the source and the ppu was already being loaded }
  1833. hp:=tmodule(loaded_units.first);
  1834. while assigned(hp) do
  1835. begin
  1836. hp2:=hp;
  1837. hp:=tmodule(hp.next);
  1838. if assigned(hp2.package) then
  1839. add_package_unit_ref(hp2.package);
  1840. if hp2.is_unit and
  1841. not assigned(hp2.globalsymtable) then
  1842. loaded_units.remove(hp2);
  1843. end;
  1844. exportlib.ignoreduplicates:=true;
  1845. { force exports }
  1846. uu:=tused_unit(usedunits.first);
  1847. while assigned(uu) do
  1848. begin
  1849. if not assigned(systemunit) and (uu.u.modulename^='SYSTEM') then
  1850. begin
  1851. systemunit:=tglobalsymtable(uu.u.globalsymtable);
  1852. load_intern_types;
  1853. end;
  1854. if not assigned(uu.u.package) then
  1855. export_unit(uu.u);
  1856. uu:=tused_unit(uu.next);
  1857. end;
  1858. {$ifdef arm}
  1859. { Insert .pdata section for arm-wince.
  1860. It is needed for exception handling. }
  1861. if target_info.system in [system_arm_wince] then
  1862. InsertPData;
  1863. {$endif arm}
  1864. { generate debuginfo }
  1865. if (cs_debuginfo in current_settings.moduleswitches) then
  1866. current_debuginfo.inserttypeinfo;
  1867. exportlib.generatelib;
  1868. exportlib.ignoreduplicates:=false;
  1869. { create import libraries for all packages }
  1870. if packagelist.count>0 then
  1871. createimportlibfromexternals;
  1872. { generate imports }
  1873. if curr.ImportLibraryList.Count>0 then
  1874. importlib.generatelib;
  1875. { Reference all DEBUGINFO sections from the main .fpc section }
  1876. if (cs_debuginfo in current_settings.moduleswitches) then
  1877. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1878. { insert own objectfile }
  1879. insertobjectfile(curr);
  1880. { assemble and link }
  1881. create_objectfile(curr);
  1882. { We might need the symbols info if not using
  1883. the default do_extractsymbolinfo
  1884. which is a dummy function PM }
  1885. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1886. { release all local symtables that are not needed anymore }
  1887. if (not needsymbolinfo) then
  1888. free_localsymtables(curr.localsymtable);
  1889. { leave when we got an error }
  1890. if (Errorcount>0) and not status.skip_error then
  1891. begin
  1892. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1893. status.skip_error:=true;
  1894. pkg.free;
  1895. exit;
  1896. end;
  1897. if (not curr.is_unit) then
  1898. begin
  1899. { we add all loaded units that are not part of a package to the
  1900. package; this includes units in the "contains" section as well
  1901. as implicitely imported ones }
  1902. hp:=tmodule(loaded_units.first);
  1903. while assigned(hp) do
  1904. begin
  1905. if (hp<>curr) then
  1906. begin
  1907. if not assigned(hp.package) then
  1908. begin
  1909. pkg.addunit(hp);
  1910. check_for_indirect_package_usages(hp.used_units);
  1911. end
  1912. else
  1913. begin
  1914. pentry:=ppackageentry(packagelist.find(hp.package.packagename^));
  1915. if not assigned(pentry) then
  1916. internalerror(2015112301);
  1917. pkg.add_required_package(hp.package);
  1918. end;
  1919. end;
  1920. hp:=tmodule(hp.next);
  1921. end;
  1922. pkg.initmoduleinfo(curr);
  1923. { create the executable when we are at level 1 }
  1924. if (curr.is_initial) then
  1925. begin
  1926. { create global resource file by collecting all resource files }
  1927. CollectResourceFiles;
  1928. { write .def file }
  1929. if (cs_link_deffile in current_settings.globalswitches) then
  1930. deffile.writefile;
  1931. { generate the pcp file }
  1932. pkg.savepcp;
  1933. { insert all .o files from all loaded units and
  1934. unload the units, we don't need them anymore.
  1935. Keep the curr because that is still needed }
  1936. hp:=tmodule(loaded_units.first);
  1937. while assigned(hp) do
  1938. begin
  1939. { only link in those units which should become part of this
  1940. package }
  1941. if not assigned(hp.package) then
  1942. linker.AddModuleFiles(hp);
  1943. hp2:=tmodule(hp.next);
  1944. if (hp<>curr) and
  1945. (not needsymbolinfo) then
  1946. begin
  1947. loaded_units.remove(hp);
  1948. hp.free;
  1949. end;
  1950. hp:=hp2;
  1951. end;
  1952. { add the library of directly used packages }
  1953. add_package_libs(linker);
  1954. { and now link the package library }
  1955. linker.MakeSharedLibrary
  1956. end;
  1957. { Give Fatal with error count for linker errors }
  1958. if (Errorcount>0) and not status.skip_error then
  1959. begin
  1960. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1961. status.skip_error:=true;
  1962. end;
  1963. pkg.free;
  1964. end;
  1965. end;
  1966. procedure proc_create_executable(curr, sysinitmod: tmodule; islibrary : boolean);
  1967. var
  1968. program_uses_checkpointer : boolean;
  1969. hp,hp2 : tmodule;
  1970. begin
  1971. { create global resource file by collecting all resource files }
  1972. CollectResourceFiles;
  1973. { write .def file }
  1974. if (cs_link_deffile in current_settings.globalswitches) then
  1975. deffile.writefile;
  1976. { link SysInit (if any) first, to have behavior consistent with
  1977. assembler startup files }
  1978. if assigned(sysinitmod) then
  1979. linker.AddModuleFiles(sysinitmod);
  1980. { Does any unit use checkpointer function }
  1981. program_uses_checkpointer:=false;
  1982. { insert all .o files from all loaded units and
  1983. unload the units, we don't need them anymore.
  1984. Keep the curr because that is still needed }
  1985. hp:=tmodule(loaded_units.first);
  1986. while assigned(hp) do
  1987. begin
  1988. if (hp<>sysinitmod) and not assigned(hp.package) then
  1989. begin
  1990. linker.AddModuleFiles(hp);
  1991. if mf_checkpointer_called in hp.moduleflags then
  1992. program_uses_checkpointer:=true;
  1993. end;
  1994. hp2:=tmodule(hp.next);
  1995. if assigned(hp.package) then
  1996. add_package_unit_ref(hp.package);
  1997. if (hp<>curr) and
  1998. (not needsymbolinfo) then
  1999. begin
  2000. loaded_units.remove(hp);
  2001. hp.free;
  2002. end;
  2003. hp:=hp2;
  2004. end;
  2005. { free also unneeded units we didn't free before }
  2006. if not needsymbolinfo then
  2007. unloaded_units.Clear;
  2008. { Does any unit use checkpointer function }
  2009. if program_uses_checkpointer then
  2010. Message1(link_w_program_uses_checkpointer,curr.modulename^);
  2011. { add all directly used packages as libraries }
  2012. add_package_libs(linker);
  2013. { finally we can create an executable }
  2014. if curr.islibrary then
  2015. linker.MakeSharedLibrary
  2016. else
  2017. linker.MakeExecutable;
  2018. { collect all necessary information for whole-program optimization }
  2019. wpoinfomanager.extractwpoinfofromprogram;
  2020. end;
  2021. procedure proc_program_after_parsing(curr : tmodule; islibrary : boolean);
  2022. var
  2023. sysinitmod, hp,hp2 : tmodule;
  2024. resources_used : boolean;
  2025. begin
  2026. sysinitmod:=nil;
  2027. hp:=nil;
  2028. hp2:=nil;
  2029. resources_used:=false;
  2030. {$ifdef DEBUG_NODE_XML}
  2031. if IsLibrary then
  2032. XMLFinalizeNodeFile('library')
  2033. else
  2034. XMLFinalizeNodeFile('program');
  2035. {$endif DEBUG_NODE_XML}
  2036. { reset wpo flags for all defs }
  2037. reset_all_defs(curr);
  2038. if (Errorcount=0) then
  2039. begin
  2040. { test static symtable }
  2041. tstoredsymtable(curr.localsymtable).allsymbolsused;
  2042. tstoredsymtable(curr.localsymtable).allprivatesused;
  2043. tstoredsymtable(curr.localsymtable).check_forwards;
  2044. curr.allunitsused;
  2045. end;
  2046. { leave when we got an error }
  2047. if (Errorcount>0) and not status.skip_error then
  2048. begin
  2049. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2050. status.skip_error:=true;
  2051. exit;
  2052. end;
  2053. { remove all unused units, this happens when units are removed
  2054. from the uses clause in the source and the ppu was already being loaded }
  2055. hp:=tmodule(loaded_units.first);
  2056. while assigned(hp) do
  2057. begin
  2058. hp2:=hp;
  2059. hp:=tmodule(hp.next);
  2060. if hp2.is_unit and
  2061. not assigned(hp2.globalsymtable) then
  2062. begin
  2063. loaded_units.remove(hp2);
  2064. unloaded_units.concat(hp2);
  2065. end;
  2066. end;
  2067. { do we need to add the variants unit? }
  2068. maybeloadvariantsunit(curr);
  2069. { Now that everything has been compiled we know if we need resource
  2070. support. If not, remove the unit. }
  2071. resources_used:=MaybeRemoveResUnit(curr);
  2072. linker.initsysinitunitname;
  2073. if target_info.system in systems_internal_sysinit then
  2074. begin
  2075. { add start/halt unit }
  2076. sysinitmod:=AddUnit(curr,linker.sysinitunit);
  2077. end
  2078. else
  2079. sysinitmod:=nil;
  2080. {$ifdef arm}
  2081. { Insert .pdata section for arm-wince.
  2082. It is needed for exception handling. }
  2083. if target_info.system in [system_arm_wince] then
  2084. InsertPData;
  2085. {$endif arm}
  2086. cnodeutils.InsertThreadvars;
  2087. { generate rtti/init tables }
  2088. write_persistent_type_info(curr.localsymtable,false);
  2089. { if an Objective-C module, generate rtti and module info }
  2090. MaybeGenerateObjectiveCImageInfo(nil,curr.localsymtable);
  2091. { generate debuginfo }
  2092. if (cs_debuginfo in current_settings.moduleswitches) then
  2093. current_debuginfo.inserttypeinfo;
  2094. if islibrary or (target_info.system in systems_unit_program_exports) then
  2095. exportlib.generatelib;
  2096. { Reference all DEBUGINFO sections from the main .fpc section }
  2097. if (cs_debuginfo in current_settings.moduleswitches) then
  2098. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  2099. { Resource strings }
  2100. GenerateResourceStrings;
  2101. { Windows widestring needing initialization }
  2102. cnodeutils.InsertWideInits;
  2103. { Resourcestring references (const foo:string=someresourcestring) }
  2104. cnodeutils.InsertResStrInits;
  2105. { insert Tables and StackLength }
  2106. cnodeutils.InsertInitFinalTable(curr);
  2107. cnodeutils.InsertThreadvarTablesTable;
  2108. cnodeutils.InsertResourceTablesTable;
  2109. cnodeutils.InsertWideInitsTablesTable;
  2110. cnodeutils.InsertResStrTablesTable;
  2111. cnodeutils.InsertMemorySizes;
  2112. { Insert symbol to resource info }
  2113. cnodeutils.InsertResourceInfo(resources_used);
  2114. { create callframe info }
  2115. create_dwarf_frame;
  2116. { create import library for all packages }
  2117. if packagelist.count>0 then
  2118. createimportlibfromexternals;
  2119. { generate imports }
  2120. if curr.ImportLibraryList.Count>0 then
  2121. importlib.generatelib;
  2122. { insert own objectfile }
  2123. insertobjectfile(curr);
  2124. { assemble and link }
  2125. create_objectfile(curr);
  2126. { We might need the symbols info if not using
  2127. the default do_extractsymbolinfo
  2128. which is a dummy function PM }
  2129. needsymbolinfo:=
  2130. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  2131. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  2132. { release all local symtables that are not needed anymore }
  2133. if (not needsymbolinfo) then
  2134. free_localsymtables(curr.localsymtable);
  2135. { leave when we got an error }
  2136. if (Errorcount>0) and not status.skip_error then
  2137. begin
  2138. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2139. status.skip_error:=true;
  2140. exit;
  2141. end;
  2142. { create the executable when we are at level 1 }
  2143. if (not curr.is_unit) and (curr.is_initial) then
  2144. proc_create_executable(curr,sysinitmod,islibrary);
  2145. { Give Fatal with error count for linker errors }
  2146. if (Errorcount>0) and not status.skip_error then
  2147. begin
  2148. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2149. status.skip_error:=true;
  2150. end;
  2151. curr.state:=ms_processed;
  2152. end;
  2153. function proc_program_declarations(curr : tmodule; islibrary : boolean) : boolean;
  2154. var
  2155. initpd : tprocdef;
  2156. finalize_procinfo,
  2157. init_procinfo,
  2158. main_procinfo : tcgprocinfo;
  2159. force_init_final : boolean;
  2160. begin
  2161. result:=true;
  2162. main_procinfo:=nil;
  2163. init_procinfo:=nil;
  2164. finalize_procinfo:=nil;
  2165. set_current_module(curr);
  2166. { All units are read, now give them a number }
  2167. curr.updatemaps;
  2168. connect_loaded_units(curr,nil);
  2169. { consume the semicolon after maps have been updated else conditional compiling expressions
  2170. might cause internal errors, see tw8611 }
  2171. if curr.consume_semicolon_after_uses then
  2172. begin
  2173. consume(_SEMICOLON);
  2174. curr.consume_semicolon_after_uses:=false;
  2175. end;
  2176. {Insert the name of the main program into the symbol table.}
  2177. if curr.realmodulename^<>'' then
  2178. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  2179. Message1(parser_u_parsing_implementation,curr.mainsource);
  2180. symtablestack.push(curr.localsymtable);
  2181. {$ifdef jvm}
  2182. { fake classdef to represent the class corresponding to the unit }
  2183. addmoduleclass(curr);
  2184. {$endif}
  2185. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  2186. maybe_load_got(curr);
  2187. { create whole program optimisation information }
  2188. curr.wpoinfo:=tunitwpoinfo.create;
  2189. { The program intialization needs an alias, so it can be called
  2190. from the bootstrap code.}
  2191. if islibrary then
  2192. begin
  2193. initpd:=nil;
  2194. { ToDo: other systems that use indirect entry info, but check back with Windows! }
  2195. { we need to call FPC_LIBMAIN in sysinit which in turn will call PascalMain -> create dummy stub }
  2196. if target_info.system in systems_darwin then
  2197. begin
  2198. main_procinfo:=create_main_proc(make_mangledname('sysinitcallthrough',curr.localsymtable,'stub'),potype_libmainstub,curr.localsymtable);
  2199. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_LIBMAIN');
  2200. initpd:=main_procinfo.procdef;
  2201. main_procinfo.free;
  2202. end;
  2203. main_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,mainaliasname),potype_proginit,curr.localsymtable);
  2204. { Win32 startup code needs a single name }
  2205. if not(target_info.system in (systems_darwin+systems_aix)) then
  2206. main_procinfo.procdef.aliasnames.concat('PASCALMAIN')
  2207. else
  2208. main_procinfo.procdef.aliasnames.concat(target_info.Cprefix+'PASCALMAIN');
  2209. if not(target_info.system in systems_darwin) then
  2210. initpd:=main_procinfo.procdef;
  2211. cnodeutils.RegisterModuleInitFunction(initpd);
  2212. end
  2213. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macosclassic]+systems_darwin+systems_aix)) then
  2214. begin
  2215. { create a stub with the name of the desired main routine, with
  2216. the same signature as the C "main" function, and call through to
  2217. FPC_SYSTEMMAIN, which will initialise everything based on its
  2218. parameters. This function cannot be in the system unit, because
  2219. its name can be configured on the command line (for use with e.g.
  2220. SDL, where the main function should be called SDL_main) }
  2221. main_procinfo:=create_main_proc(mainaliasname,potype_mainstub,curr.localsymtable);
  2222. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_SYSTEMMAIN');
  2223. main_procinfo.free;
  2224. { now create the PASCALMAIN routine (which will be called from
  2225. FPC_SYSTEMMAIN) }
  2226. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,curr.localsymtable);
  2227. end
  2228. else
  2229. begin
  2230. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,curr.localsymtable);
  2231. main_procinfo.procdef.aliasnames.concat('PASCALMAIN');
  2232. end;
  2233. main_procinfo.parse_body;
  2234. { save file pos for debuginfo }
  2235. curr.mainfilepos:=main_procinfo.entrypos;
  2236. { finalize? }
  2237. if token=_FINALIZATION then
  2238. begin
  2239. { Parse the finalize }
  2240. finalize_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize$'),potype_unitfinalize,curr.localsymtable);
  2241. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',curr.localsymtable,''));
  2242. finalize_procinfo.procdef.aliasnames.concat('PASCALFINALIZE');
  2243. finalize_procinfo.parse_body;
  2244. end;
  2245. { Generate specializations of objectdefs methods }
  2246. if Errorcount=0 then
  2247. generate_specialization_procs;
  2248. { This needs to be done before we generate the VMTs }
  2249. if (target_cpu=tsystemcpu.cpu_wasm32) then
  2250. add_synthetic_interface_classes_for_st(curr.localsymtable,true,true);
  2251. { Generate VMTs }
  2252. if Errorcount=0 then
  2253. write_vmts(curr.localsymtable,false);
  2254. { add implementations for synthetic method declarations added by
  2255. the compiler }
  2256. add_synthetic_method_implementations(curr.localsymtable);
  2257. { generate construction functions for all attributes in the program }
  2258. generate_attr_constrs(curr.used_rtti_attrs);
  2259. { should we force unit initialization? }
  2260. force_init_final:=tstaticsymtable(curr.localsymtable).needs_init_final;
  2261. if force_init_final or cnodeutils.force_init then
  2262. init_procinfo:=gen_implicit_initfinal(curr,mf_init,curr.localsymtable);
  2263. { Add symbol to the exports section for win32 so smartlinking a
  2264. DLL will include the edata section }
  2265. if assigned(exportlib) and
  2266. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  2267. (mf_has_exports in curr.moduleflags) then
  2268. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',curr.localsymtable,''),0));
  2269. if (force_init_final or cnodeutils.force_final) and
  2270. (
  2271. not assigned(finalize_procinfo)
  2272. or has_no_code(finalize_procinfo.code)
  2273. ) then
  2274. begin
  2275. { first release the not used finalize procinfo }
  2276. if assigned(finalize_procinfo) then
  2277. begin
  2278. release_proc_symbol(finalize_procinfo.procdef);
  2279. release_main_proc(curr,finalize_procinfo);
  2280. end;
  2281. finalize_procinfo:=gen_implicit_initfinal(curr,mf_finalize,curr.localsymtable);
  2282. end;
  2283. { the finalization routine of libraries is generic (and all libraries need to }
  2284. { be finalized, so they can finalize any units they use }
  2285. { Place in "pure assembler" list so that the llvm assembler writer
  2286. directly emits the generated directives }
  2287. if (islibrary) then
  2288. cnodeutils.RegisterModuleFiniFunction(search_system_proc('fpc_lib_exit'));
  2289. { all labels must be defined before generating code }
  2290. if Errorcount=0 then
  2291. tstoredsymtable(curr.localsymtable).checklabels;
  2292. { See remark in unit init/final }
  2293. main_procinfo.generate_code_tree;
  2294. main_procinfo.resetprocdef;
  2295. release_main_proc(curr,main_procinfo);
  2296. if assigned(init_procinfo) then
  2297. begin
  2298. { initialization can be implicit only }
  2299. include(curr.moduleflags,mf_init);
  2300. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  2301. init_procinfo.generate_code;
  2302. init_procinfo.resetprocdef;
  2303. release_main_proc(curr,init_procinfo);
  2304. end;
  2305. if assigned(finalize_procinfo) then
  2306. begin
  2307. if force_init_final or
  2308. cnodeutils.force_init or
  2309. not(has_no_code(finalize_procinfo.code)) then
  2310. begin
  2311. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  2312. finalize_procinfo.generate_code_tree;
  2313. include(curr.moduleflags,mf_finalize);
  2314. end;
  2315. finalize_procinfo.resetprocdef;
  2316. release_main_proc(curr,finalize_procinfo);
  2317. end;
  2318. symtablestack.pop(curr.localsymtable);
  2319. { consume the last point }
  2320. consume(_POINT);
  2321. proc_program_after_parsing(curr,islibrary);
  2322. end;
  2323. procedure proc_library_header(curr: tmodule);
  2324. var
  2325. program_name : ansistring;
  2326. begin
  2327. consume(_LIBRARY);
  2328. program_name:=orgpattern;
  2329. consume(_ID);
  2330. while token=_POINT do
  2331. begin
  2332. consume(_POINT);
  2333. program_name:=program_name+'.'+orgpattern;
  2334. consume(_ID);
  2335. end;
  2336. curr.setmodulename(program_name);
  2337. curr.islibrary:=true;
  2338. exportlib.preparelib(program_name);
  2339. if tf_library_needs_pic in target_info.flags then
  2340. begin
  2341. include(current_settings.moduleswitches,cs_create_pic);
  2342. { also set create_pic for all unit compilation }
  2343. include(init_settings.moduleswitches,cs_create_pic);
  2344. end;
  2345. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  2346. read, all following directives are parsed as well }
  2347. setupglobalswitches;
  2348. {$ifdef DEBUG_NODE_XML}
  2349. XMLInitializeNodeFile('library', program_name);
  2350. {$endif DEBUG_NODE_XML}
  2351. end;
  2352. type
  2353. TProgramParam = record
  2354. name : ansistring;
  2355. nr : dword;
  2356. end;
  2357. TProgramParamArray = array of TProgramParam;
  2358. procedure proc_program_header(curr: tmodule; out sc : TProgramParamArray);
  2359. var
  2360. program_name : ansistring;
  2361. paramnum : integer;
  2362. begin
  2363. sc:=nil;
  2364. consume(_PROGRAM);
  2365. program_name:=orgpattern;
  2366. consume(_ID);
  2367. while token=_POINT do
  2368. begin
  2369. consume(_POINT);
  2370. program_name:=program_name+'.'+orgpattern;
  2371. consume(_ID);
  2372. end;
  2373. curr.setmodulename(program_name);
  2374. if (target_info.system in systems_unit_program_exports) then
  2375. exportlib.preparelib(program_name);
  2376. if token=_LKLAMMER then
  2377. begin
  2378. consume(_LKLAMMER);
  2379. paramnum:=1;
  2380. repeat
  2381. if m_isolike_program_para in current_settings.modeswitches then
  2382. begin
  2383. if (pattern<>'INPUT') and (pattern<>'OUTPUT') then
  2384. begin
  2385. { the symtablestack is not setup here, so text must be created later on }
  2386. Setlength(sc,length(sc)+1);
  2387. with sc[high(sc)] do
  2388. begin
  2389. name:=pattern;
  2390. nr:=paramnum;
  2391. end;
  2392. inc(paramnum);
  2393. end;
  2394. end;
  2395. consume(_ID);
  2396. until not try_to_consume(_COMMA);
  2397. consume(_RKLAMMER);
  2398. end;
  2399. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  2400. read, all following directives are parsed as well }
  2401. setupglobalswitches;
  2402. {$ifdef DEBUG_NODE_XML}
  2403. XMLInitializeNodeFile('program', program_name);
  2404. {$endif DEBUG_NODE_XML}
  2405. end;
  2406. function proc_program(curr: tmodule; islibrary : boolean) : boolean;
  2407. var
  2408. main_file : tinputfile;
  2409. consume_semicolon_after_loaded : boolean;
  2410. ps : tprogramparasym;
  2411. textsym : ttypesym;
  2412. sc : TProgramParamArray;
  2413. i : Longint;
  2414. feature : tfeature;
  2415. load_ok : boolean;
  2416. begin
  2417. result:=true;
  2418. Status.IsLibrary:=IsLibrary;
  2419. Status.IsPackage:=false;
  2420. Status.IsExe:=true;
  2421. parse_only:=false;
  2422. consume_semicolon_after_loaded:=false;
  2423. { make the compiler happy and avoid an uninitialized variable warning on Setlength(sc,length(sc)+1); }
  2424. sc:=nil;
  2425. { DLL defaults to create reloc info }
  2426. if islibrary or (target_info.system in [system_aarch64_win64]) then
  2427. begin
  2428. if not RelocSectionSetExplicitly then
  2429. RelocSection:=true;
  2430. end;
  2431. { Relocation works only without stabs under Windows when }
  2432. { external linker (LD) is used. LD generates relocs for }
  2433. { stab sections which is not loaded in memory. It causes }
  2434. { AV error when DLL is loaded and relocation is needed. }
  2435. { Internal linker does not have this problem. }
  2436. if RelocSection and
  2437. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  2438. (cs_link_extern in current_settings.globalswitches) then
  2439. begin
  2440. include(current_settings.globalswitches,cs_link_strip);
  2441. { Warning stabs info does not work with reloc section !! }
  2442. if (cs_debuginfo in current_settings.moduleswitches) and
  2443. (target_dbg.id=dbg_stabs) then
  2444. begin
  2445. Message1(parser_w_parser_reloc_no_debug,curr.mainsource);
  2446. Message(parser_w_parser_win32_debug_needs_WN);
  2447. exclude(current_settings.moduleswitches,cs_debuginfo);
  2448. end;
  2449. end;
  2450. { get correct output names }
  2451. main_file := current_scanner.inputfile;
  2452. while assigned(main_file.next) do
  2453. main_file := main_file.next;
  2454. curr.SetFileName(main_file.path+main_file.name,true);
  2455. if islibrary then
  2456. begin
  2457. proc_library_header(curr);
  2458. consume_semicolon_after_loaded:=true;
  2459. end
  2460. else if token=_PROGRAM then
  2461. { is there an program head ? }
  2462. begin
  2463. proc_program_header(curr,sc);
  2464. consume_semicolon_after_loaded:=true;
  2465. end
  2466. else
  2467. begin
  2468. if (target_info.system in systems_unit_program_exports) then
  2469. exportlib.preparelib(curr.realmodulename^);
  2470. { setup things using the switches }
  2471. setupglobalswitches;
  2472. {$ifdef DEBUG_NODE_XML}
  2473. XMLInitializeNodeFile('program', curr.realmodulename^);
  2474. {$endif DEBUG_NODE_XML}
  2475. end;
  2476. { load all packages, so we know whether a unit is contained inside a
  2477. package or not }
  2478. load_packages;
  2479. { set implementation flag }
  2480. curr.in_interface:=false;
  2481. curr.interface_compiled:=true;
  2482. { insert after the unit symbol tables the static symbol table
  2483. of the program }
  2484. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  2485. { load system unit }
  2486. load_ok:=loadsystemunit(curr);
  2487. { consume the semicolon now that the system unit is loaded }
  2488. if consume_semicolon_after_loaded then
  2489. consume(_SEMICOLON);
  2490. { global switches are read, so further changes aren't allowed }
  2491. curr.in_global:=false;
  2492. { system unit is loaded, now insert feature defines }
  2493. for feature:=low(tfeature) to high(tfeature) do
  2494. if feature in features then
  2495. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  2496. { load standard units, e.g objpas,profile unit }
  2497. load_ok:=loaddefaultunits(curr) and load_ok;
  2498. { Load units provided on the command line }
  2499. load_ok:=loadautounits(curr) and load_ok;
  2500. { insert iso program parameters }
  2501. if length(sc)>0 then
  2502. begin
  2503. textsym:=search_system_type('TEXT');
  2504. if not(assigned(textsym)) then
  2505. internalerror(2013011201);
  2506. for i:=0 to high(sc) do
  2507. begin
  2508. ps:=cprogramparasym.create(sc[i].name,sc[i].nr);
  2509. curr.localsymtable.insertsym(ps,true);
  2510. end;
  2511. end;
  2512. { Load the units used by the program we compile. }
  2513. if token=_USES then
  2514. begin
  2515. // We can do this here: if there is no uses then the namespace directive makes no sense.
  2516. if Assigned(curr) then
  2517. curr.Loadlocalnamespacelist
  2518. else
  2519. current_namespacelist:=Nil;
  2520. parseusesclause(curr);
  2521. load_ok:=loadunits(curr,false) and load_ok;
  2522. curr.consume_semicolon_after_uses:=true;
  2523. end
  2524. else
  2525. curr.consume_semicolon_after_uses:=false;
  2526. if not load_ok then
  2527. curr.state:=ms_compiling_wait;
  2528. { Can we continue compiling ? }
  2529. result:=curr.state<>ms_compiling_wait;
  2530. if result then
  2531. result:=proc_program_declarations(curr,islibrary)
  2532. end;
  2533. end.