pmodules.pas 106 KB

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