psub.pas 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl, Daniel Mantione
  4. Does the parsing of the procedures/functions
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit psub;
  19. interface
  20. uses
  21. cobjects,
  22. symconst,tokens,symtable;
  23. const
  24. pd_global = $1; { directive must be global }
  25. pd_body = $2; { directive needs a body }
  26. pd_implemen = $4; { directive can be used implementation section }
  27. pd_interface = $8; { directive can be used interface section }
  28. pd_object = $10; { directive can be used object declaration }
  29. pd_procvar = $20; { directive can be used procvar declaration }
  30. pd_notobject = $40;{ directive can not be used object declaration }
  31. procedure compile_proc_body(const proc_names:Tstringcontainer;
  32. make_global,parent_has_class:boolean);
  33. procedure parse_proc_head(options:tproctypeoption);
  34. procedure parse_proc_dec;
  35. function is_proc_directive(tok:ttoken):boolean;
  36. procedure parse_var_proc_directives(var sym : psym);
  37. procedure parse_object_proc_directives(var sym : pprocsym);
  38. procedure read_proc;
  39. implementation
  40. uses
  41. globtype,systems,
  42. strings,globals,verbose,files,
  43. scanner,aasm,tree,types,
  44. import,gendef,htypechk,
  45. {$ifdef newcg}
  46. cgbase,
  47. {$else newcg}
  48. hcodegen,temp_gen,
  49. {$endif newcg}
  50. pass_1,cpubase,cpuasm
  51. {$ifndef NOPASS2}
  52. ,pass_2
  53. {$endif}
  54. {$ifdef GDB}
  55. ,gdb
  56. {$endif GDB}
  57. {$ifdef newcg}
  58. {$ifndef NOOPT}
  59. ,aopt
  60. {$endif}
  61. {$else}
  62. {$ifdef i386}
  63. ,tgeni386
  64. ,cgai386
  65. {$ifndef NOOPT}
  66. ,aopt386
  67. {$endif}
  68. {$endif}
  69. {$ifdef m68k}
  70. ,tgen68k,cga68k
  71. {$endif}
  72. {$endif newcg}
  73. { parser specific stuff }
  74. ,pbase,ptype,pdecl,pexpr,pstatmnt
  75. {$ifdef newcg}
  76. ,tgcpu,convtree,cgobj,tgeni386 { for the new code generator tgeni386 is only a dummy }
  77. {$endif newcg}
  78. ;
  79. var
  80. realname:string; { contains the real name of a procedure as it's typed }
  81. procedure parse_proc_head(options:tproctypeoption);
  82. var sp:stringid;
  83. pd:Pprocdef;
  84. paramoffset:longint;
  85. sym:Psym;
  86. hs:string;
  87. st : psymtable;
  88. overloaded_level:word;
  89. storepos,procstartfilepos : tfileposinfo;
  90. begin
  91. { Save the position where this procedure really starts and set col to 1 which
  92. looks nicer }
  93. procstartfilepos:=tokenpos;
  94. { procstartfilepos.column:=1; I do not agree here !!
  95. lets keep excat position PM }
  96. if (options=potype_operator) then
  97. begin
  98. sp:=overloaded_names[optoken];
  99. realname:=sp;
  100. end
  101. else
  102. begin
  103. sp:=pattern;
  104. realname:=orgpattern;
  105. consume(_ID);
  106. end;
  107. { method ? }
  108. if not(parse_only) and
  109. (lexlevel=normal_function_level) and
  110. try_to_consume(_POINT) then
  111. begin
  112. storepos:=tokenpos;
  113. tokenpos:=procstartfilepos;
  114. getsym(sp,true);
  115. sym:=srsym;
  116. tokenpos:=storepos;
  117. { load proc name }
  118. sp:=pattern;
  119. realname:=orgpattern;
  120. procstartfilepos:=tokenpos;
  121. { qualifier is class name ? }
  122. if (sym^.typ<>typesym) or
  123. (ptypesym(sym)^.restype.def^.deftype<>objectdef) then
  124. begin
  125. Message(parser_e_class_id_expected);
  126. aktprocsym:=nil;
  127. consume(_ID);
  128. end
  129. else
  130. begin
  131. { used to allow private syms to be seen }
  132. aktobjectdef:=pobjectdef(ptypesym(sym)^.restype.def);
  133. procinfo^._class:=pobjectdef(ptypesym(sym)^.restype.def);
  134. aktprocsym:=pprocsym(procinfo^._class^.symtable^.search(sp));
  135. consume(_ID);
  136. {The procedure has been found. So it is
  137. a global one. Set the flags to mark this.}
  138. procinfo^.flags:=procinfo^.flags or pi_is_global;
  139. aktobjectdef:=nil;
  140. { we solve this below }
  141. if not(assigned(aktprocsym)) then
  142. Message(parser_e_methode_id_expected);
  143. end;
  144. end
  145. else
  146. begin
  147. { check for constructor/destructor which is not allowed here }
  148. if (not parse_only) and
  149. (options in [potype_constructor,potype_destructor]) then
  150. Message(parser_e_constructors_always_objects);
  151. tokenpos:=procstartfilepos;
  152. aktprocsym:=pprocsym(symtablestack^.search(sp));
  153. if not(parse_only) then
  154. begin
  155. {The procedure we prepare for is in the implementation
  156. part of the unit we compile. It is also possible that we
  157. are compiling a program, which is also some kind of
  158. implementaion part.
  159. We need to find out if the procedure is global. If it is
  160. global, it is in the global symtable.}
  161. if not assigned(aktprocsym) and
  162. (symtablestack^.symtabletype=staticsymtable) then
  163. begin
  164. {Search the procedure in the global symtable.}
  165. aktprocsym:=Pprocsym(search_a_symtable(sp,globalsymtable));
  166. if assigned(aktprocsym) then
  167. begin
  168. {Check if it is a procedure.}
  169. if aktprocsym^.typ<>procsym then
  170. DuplicateSym(aktprocsym);
  171. {The procedure has been found. So it is
  172. a global one. Set the flags to mark this.}
  173. procinfo^.flags:=procinfo^.flags or pi_is_global;
  174. end;
  175. end;
  176. end;
  177. end;
  178. { Create the mangledname }
  179. {$ifndef UseNiceNames}
  180. if assigned(procinfo^._class) then
  181. begin
  182. if (pos('_$$_',procprefix)=0) then
  183. hs:=procprefix+'_$$_'+procinfo^._class^.objname^+'_$$_'+sp
  184. else
  185. hs:=procprefix+'_$'+sp;
  186. end
  187. else
  188. begin
  189. if lexlevel=normal_function_level then
  190. hs:=procprefix+'_'+sp
  191. else
  192. hs:=procprefix+'_$'+sp;
  193. end;
  194. {$else UseNiceNames}
  195. if assigned(procinfo^._class) then
  196. begin
  197. if (pos('_5Class_',procprefix)=0) then
  198. hs:=procprefix+'_5Class_'+procinfo^._class^.name^+'_'+tostr(length(sp))+sp
  199. else
  200. hs:=procprefix+'_'+tostr(length(sp))+sp;
  201. end
  202. else
  203. begin
  204. if lexlevel=normal_function_level then
  205. hs:=procprefix+'_'+tostr(length(sp))+sp
  206. else
  207. hs:=lowercase(procprefix)+'_'+tostr(length(sp))+sp;
  208. end;
  209. {$endif UseNiceNames}
  210. if assigned(aktprocsym) then
  211. begin
  212. { Check if overloaded is a procsym, we use a different error message
  213. for tp7 so it looks more compatible }
  214. if aktprocsym^.typ<>procsym then
  215. begin
  216. if (m_fpc in aktmodeswitches) then
  217. Message1(parser_e_overloaded_no_procedure,aktprocsym^.name)
  218. else
  219. DuplicateSym(aktprocsym);
  220. { try to recover by creating a new aktprocsym }
  221. tokenpos:=procstartfilepos;
  222. aktprocsym:=new(pprocsym,init(sp));
  223. end;
  224. end
  225. else
  226. begin
  227. { create a new procsym and set the real filepos }
  228. tokenpos:=procstartfilepos;
  229. aktprocsym:=new(pprocsym,init(sp));
  230. { for operator we have only one definition for each overloaded
  231. operation }
  232. if (options=potype_operator) then
  233. begin
  234. { the only problem is that nextoverloaded might not be in a unit
  235. known for the unit itself }
  236. { not anymore PM }
  237. if assigned(overloaded_operators[optoken]) then
  238. aktprocsym^.definition:=overloaded_operators[optoken]^.definition;
  239. {$ifndef DONOTCHAINOPERATORS}
  240. overloaded_operators[optoken]:=aktprocsym;
  241. {$endif DONOTCHAINOPERATORS}
  242. end;
  243. symtablestack^.insert(aktprocsym);
  244. end;
  245. st:=symtablestack;
  246. pd:=new(pprocdef,init);
  247. pd^.symtablelevel:=symtablestack^.symtablelevel;
  248. if assigned(procinfo^._class) then
  249. pd^._class := procinfo^._class;
  250. { set the options from the caller (podestructor or poconstructor) }
  251. pd^.proctypeoption:=options;
  252. { calculate the offset of the parameters }
  253. paramoffset:=8;
  254. { calculate frame pointer offset }
  255. if lexlevel>normal_function_level then
  256. begin
  257. procinfo^.framepointer_offset:=paramoffset;
  258. inc(paramoffset,target_os.size_of_pointer);
  259. { this is needed to get correct framepointer push for local
  260. forward functions !! }
  261. pd^.parast^.symtablelevel:=lexlevel;
  262. end;
  263. if assigned (procinfo^._Class) and
  264. not(procinfo^._Class^.is_class) and
  265. (pd^.proctypeoption in [potype_constructor,potype_destructor]) then
  266. inc(paramoffset,target_os.size_of_pointer);
  267. { self pointer offset }
  268. { self isn't pushed in nested procedure of methods }
  269. if assigned(procinfo^._class) and (lexlevel=normal_function_level) then
  270. begin
  271. procinfo^.selfpointer_offset:=paramoffset;
  272. if assigned(aktprocsym^.definition) and
  273. not(po_containsself in aktprocsym^.definition^.procoptions) then
  274. inc(paramoffset,target_os.size_of_pointer);
  275. end;
  276. { con/-destructor flag ? }
  277. if assigned (procinfo^._Class) and
  278. procinfo^._class^.is_class and
  279. (pd^.proctypeoption in [potype_destructor,potype_constructor]) then
  280. inc(paramoffset,target_os.size_of_pointer);
  281. procinfo^.para_offset:=paramoffset;
  282. pd^.parast^.datasize:=0;
  283. pd^.nextoverloaded:=aktprocsym^.definition;
  284. aktprocsym^.definition:=pd;
  285. { this is probably obsolete now PM }
  286. aktprocsym^.definition^.fileinfo:=procstartfilepos;
  287. aktprocsym^.definition^.setmangledname(hs);
  288. aktprocsym^.definition^.procsym:=aktprocsym;
  289. if not parse_only then
  290. begin
  291. overloaded_level:=0;
  292. { we need another procprefix !!! }
  293. { count, but only those in the same unit !!}
  294. while assigned(pd) and
  295. (pd^.owner^.symtabletype in [globalsymtable,staticsymtable]) do
  296. begin
  297. { only count already implemented functions }
  298. if not(pd^.forwarddef) then
  299. inc(overloaded_level);
  300. pd:=pd^.nextoverloaded;
  301. end;
  302. if overloaded_level>0 then
  303. procprefix:=hs+'$'+tostr(overloaded_level)+'$'
  304. else
  305. procprefix:=hs+'$';
  306. end;
  307. { this must also be inserted in the right symtable !! PM }
  308. { otherwise we get subbtle problems with
  309. definitions of args defs in staticsymtable for
  310. implementation of a global method }
  311. if token=_LKLAMMER then
  312. parameter_dec(aktprocsym^.definition);
  313. { so we only restore the symtable now }
  314. symtablestack:=st;
  315. if (options=potype_operator) then
  316. overloaded_operators[optoken]:=aktprocsym;
  317. end;
  318. procedure parse_proc_dec;
  319. var
  320. hs : string;
  321. isclassmethod : boolean;
  322. begin
  323. inc(lexlevel);
  324. { read class method }
  325. if token=_CLASS then
  326. begin
  327. consume(_CLASS);
  328. isclassmethod:=true;
  329. end
  330. else
  331. isclassmethod:=false;
  332. case token of
  333. _FUNCTION : begin
  334. consume(_FUNCTION);
  335. parse_proc_head(potype_none);
  336. if token<>_COLON then
  337. begin
  338. if not(aktprocsym^.definition^.forwarddef) or
  339. (m_repeat_forward in aktmodeswitches) then
  340. begin
  341. consume(_COLON);
  342. consume_all_until(_SEMICOLON);
  343. end;
  344. end
  345. else
  346. begin
  347. consume(_COLON);
  348. inc(testcurobject);
  349. single_type(aktprocsym^.definition^.rettype,hs,false);
  350. aktprocsym^.definition^.test_if_fpu_result;
  351. dec(testcurobject);
  352. end;
  353. end;
  354. _PROCEDURE : begin
  355. consume(_PROCEDURE);
  356. parse_proc_head(potype_none);
  357. aktprocsym^.definition^.rettype.def:=voiddef;
  358. end;
  359. _CONSTRUCTOR : begin
  360. consume(_CONSTRUCTOR);
  361. parse_proc_head(potype_constructor);
  362. if assigned(procinfo^._class) and
  363. procinfo^._class^.is_class then
  364. begin
  365. { CLASS constructors return the created instance }
  366. aktprocsym^.definition^.rettype.def:=procinfo^._class;
  367. end
  368. else
  369. begin
  370. { OBJECT constructors return a boolean }
  371. {$IfDef GDB}
  372. { GDB doesn't like unnamed types !}
  373. aktprocsym^.definition^.rettype.def:=globaldef('boolean');
  374. {$else GDB}
  375. aktprocsym^.definition^.rettype.def:=new(porddef,init(bool8bit,0,1));
  376. {$Endif GDB}
  377. end;
  378. end;
  379. _DESTRUCTOR : begin
  380. consume(_DESTRUCTOR);
  381. parse_proc_head(potype_destructor);
  382. aktprocsym^.definition^.rettype.def:=voiddef;
  383. end;
  384. _OPERATOR : begin
  385. if lexlevel>normal_function_level then
  386. Message(parser_e_no_local_operator);
  387. consume(_OPERATOR);
  388. if not(token in [_PLUS..last_overloaded]) then
  389. Message(parser_e_overload_operator_failed);
  390. optoken:=token;
  391. consume(Token);
  392. procinfo^.flags:=procinfo^.flags or pi_operator;
  393. parse_proc_head(potype_operator);
  394. if token<>_ID then
  395. begin
  396. opsym:=nil;
  397. if not(m_result in aktmodeswitches) then
  398. consume(_ID);
  399. end
  400. else
  401. begin
  402. opsym:=new(pvarsym,initdef(pattern,voiddef));
  403. consume(_ID);
  404. end;
  405. if not try_to_consume(_COLON) then
  406. begin
  407. consume(_COLON);
  408. aktprocsym^.definition^.rettype.def:=generrordef;
  409. consume_all_until(_SEMICOLON);
  410. end
  411. else
  412. begin
  413. single_type(aktprocsym^.definition^.rettype,hs,false);
  414. aktprocsym^.definition^.test_if_fpu_result;
  415. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  416. ((aktprocsym^.definition^.rettype.def^.deftype<>
  417. orddef) or (porddef(aktprocsym^.definition^.
  418. rettype.def)^.typ<>bool8bit)) then
  419. Message(parser_e_comparative_operator_return_boolean);
  420. if assigned(opsym) then
  421. opsym^.vartype.def:=aktprocsym^.definition^.rettype.def;
  422. { We need to add the return type in the mangledname
  423. to allow overloading with just different results !! (PM) }
  424. aktprocsym^.definition^.setmangledname(
  425. aktprocsym^.definition^.mangledname+'$$'+hs);
  426. if (optoken=_ASSIGNMENT) and
  427. is_equal(aktprocsym^.definition^.rettype.def,
  428. pvarsym(aktprocsym^.definition^.parast^.symindex^.first)^.vartype.def) then
  429. message(parser_e_no_such_assignment)
  430. else if not isoperatoracceptable(aktprocsym^.definition,optoken) then
  431. Message(parser_e_overload_impossible);
  432. end;
  433. end;
  434. end;
  435. if isclassmethod and
  436. assigned(aktprocsym) then
  437. {$ifdef INCLUDEOK}
  438. include(aktprocsym^.definition^.procoptions,po_classmethod);
  439. {$else}
  440. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_classmethod];
  441. {$endif}
  442. consume(_SEMICOLON);
  443. dec(lexlevel);
  444. end;
  445. {****************************************************************************
  446. Procedure directive handlers
  447. ****************************************************************************}
  448. {$ifdef tp}
  449. {$F+}
  450. {$endif}
  451. procedure pd_far(const procnames:Tstringcontainer);
  452. begin
  453. Message(parser_w_proc_far_ignored);
  454. end;
  455. procedure pd_near(const procnames:Tstringcontainer);
  456. begin
  457. Message(parser_w_proc_near_ignored);
  458. end;
  459. procedure pd_export(const procnames:Tstringcontainer);
  460. begin
  461. if assigned(procinfo^._class) then
  462. Message(parser_e_methods_dont_be_export);
  463. if lexlevel<>normal_function_level then
  464. Message(parser_e_dont_nest_export);
  465. { only os/2 needs this }
  466. if target_info.target=target_i386_os2 then
  467. begin
  468. procnames.insert(realname);
  469. procinfo^.exported:=true;
  470. if cs_link_deffile in aktglobalswitches then
  471. deffile.AddExport(aktprocsym^.definition^.mangledname);
  472. end;
  473. end;
  474. procedure pd_inline(const procnames:Tstringcontainer);
  475. begin
  476. if not(cs_support_inline in aktmoduleswitches) then
  477. Message(parser_e_proc_inline_not_supported);
  478. end;
  479. procedure pd_forward(const procnames:Tstringcontainer);
  480. begin
  481. aktprocsym^.definition^.forwarddef:=true;
  482. end;
  483. procedure pd_stdcall(const procnames:Tstringcontainer);
  484. begin
  485. end;
  486. procedure pd_safecall(const procnames:Tstringcontainer);
  487. begin
  488. end;
  489. procedure pd_alias(const procnames:Tstringcontainer);
  490. begin
  491. consume(_COLON);
  492. procnames.insert(get_stringconst);
  493. end;
  494. procedure pd_asmname(const procnames:Tstringcontainer);
  495. begin
  496. aktprocsym^.definition^.setmangledname(target_os.Cprefix+pattern);
  497. if token=_CCHAR then
  498. consume(_CCHAR)
  499. else
  500. consume(_CSTRING);
  501. { we don't need anything else }
  502. aktprocsym^.definition^.forwarddef:=false;
  503. end;
  504. procedure pd_intern(const procnames:Tstringcontainer);
  505. begin
  506. consume(_COLON);
  507. aktprocsym^.definition^.extnumber:=get_intconst;
  508. end;
  509. procedure pd_interrupt(const procnames:Tstringcontainer);
  510. begin
  511. {$ifndef i386}
  512. Message(parser_w_proc_interrupt_ignored);
  513. {$else i386}
  514. if lexlevel<>normal_function_level then
  515. Message(parser_e_dont_nest_interrupt);
  516. {$endif i386}
  517. end;
  518. procedure pd_system(const procnames:Tstringcontainer);
  519. begin
  520. aktprocsym^.definition^.setmangledname(realname);
  521. end;
  522. procedure pd_abstract(const procnames:Tstringcontainer);
  523. begin
  524. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  525. {$ifdef INCLUDEOK}
  526. include(aktprocsym^.definition^.procoptions,po_abstractmethod)
  527. {$else}
  528. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_abstractmethod]
  529. {$endif}
  530. else
  531. Message(parser_e_only_virtual_methods_abstract);
  532. { the method is defined }
  533. aktprocsym^.definition^.forwarddef:=false;
  534. end;
  535. procedure pd_virtual(const procnames:Tstringcontainer);
  536. {$ifdef WITHDMT}
  537. var
  538. pt : ptree;
  539. {$endif WITHDMT}
  540. begin
  541. if (aktprocsym^.definition^.proctypeoption=potype_constructor) and
  542. not(aktprocsym^.definition^._class^.is_class) then
  543. Message(parser_e_constructor_cannot_be_not_virtual);
  544. {$ifdef WITHDMT}
  545. if not(aktprocsym^.definition^._class^.is_class) and
  546. (token<>_SEMICOLON) then
  547. begin
  548. { any type of parameter is allowed here! }
  549. pt:=comp_expr(true);
  550. do_firstpass(pt);
  551. if is_constintnode(pt) then
  552. begin
  553. include(aktprocsym^.definition^.procoptions,po_msgint);
  554. aktprocsym^.definition^.messageinf.i:=pt^.value;
  555. end
  556. else
  557. Message(parser_e_ill_msg_expr);
  558. disposetree(pt);
  559. end;
  560. {$endif WITHDMT}
  561. end;
  562. procedure pd_static(const procnames:Tstringcontainer);
  563. begin
  564. if (cs_static_keyword in aktmoduleswitches) then
  565. begin
  566. {$ifdef INCLUDEOK}
  567. include(aktprocsym^.symoptions,sp_static);
  568. include(aktprocsym^.definition^.procoptions,po_staticmethod);
  569. {$else}
  570. aktprocsym^.symoptions:=aktprocsym^.symoptions+[sp_static];
  571. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_staticmethod];
  572. {$endif}
  573. end;
  574. end;
  575. procedure pd_override(const procnames:Tstringcontainer);
  576. begin
  577. if not(aktprocsym^.definition^._class^.is_class) then
  578. Message(parser_e_no_object_override);
  579. end;
  580. procedure pd_overload(const procnames:Tstringcontainer);
  581. begin
  582. end;
  583. procedure pd_message(const procnames:Tstringcontainer);
  584. var
  585. pt : ptree;
  586. begin
  587. { check parameter type }
  588. if not(po_containsself in aktprocsym^.definition^.procoptions) and
  589. ((aktprocsym^.definition^.para^.count<>1) or
  590. (pparaitem(aktprocsym^.definition^.para^.first)^.paratyp<>vs_var)) then
  591. Message(parser_e_ill_msg_param);
  592. pt:=comp_expr(true);
  593. do_firstpass(pt);
  594. if pt^.treetype=stringconstn then
  595. begin
  596. {$ifdef INCLUDEOK}
  597. include(aktprocsym^.definition^.procoptions,po_msgstr);
  598. {$else}
  599. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_msgstr];
  600. {$endif}
  601. aktprocsym^.definition^.messageinf.str:=strnew(pt^.value_str);
  602. end
  603. else
  604. if is_constintnode(pt) then
  605. begin
  606. {$ifdef INCLUDEOK}
  607. include(aktprocsym^.definition^.procoptions,po_msgint);
  608. {$else}
  609. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_msgint];
  610. {$endif}
  611. aktprocsym^.definition^.messageinf.i:=pt^.value;
  612. end
  613. else
  614. Message(parser_e_ill_msg_expr);
  615. disposetree(pt);
  616. end;
  617. procedure resetvaluepara(p:pnamedindexobject);{$ifndef FPC}far;{$endif}
  618. begin
  619. if psym(p)^.typ=varsym then
  620. with pvarsym(p)^ do
  621. if copy(name,1,3)='val' then
  622. aktprocsym^.definition^.parast^.symsearch^.rename(name,copy(name,4,length(name)));
  623. end;
  624. procedure pd_cdecl(const procnames:Tstringcontainer);
  625. begin
  626. if aktprocsym^.definition^.deftype<>procvardef then
  627. aktprocsym^.definition^.setmangledname(target_os.Cprefix+realname);
  628. { do not copy on local !! }
  629. if (aktprocsym^.definition^.deftype=procdef) and
  630. assigned(aktprocsym^.definition^.parast) then
  631. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}resetvaluepara);
  632. end;
  633. procedure pd_pascal(const procnames:Tstringcontainer);
  634. var st,parast : psymtable;
  635. lastps,ps : psym;
  636. begin
  637. new(st,init(parasymtable));
  638. parast:=aktprocsym^.definition^.parast;
  639. lastps:=nil;
  640. while assigned(parast^.symindex^.first) and (lastps<>psym(parast^.symindex^.first)) do
  641. begin
  642. ps:=psym(parast^.symindex^.first);
  643. while assigned(ps^.next) and (psym(ps^.next)<>lastps) do
  644. ps:=psym(ps^.next);
  645. ps^.owner:=st;
  646. { recalculate the corrected offset }
  647. { the really_insert_in_data procedure
  648. for parasymtable should only calculateoffset PM }
  649. ps^.insert_in_data;
  650. { reset the owner correctly }
  651. ps^.owner:=parast;
  652. lastps:=ps;
  653. end;
  654. end;
  655. procedure pd_register(const procnames:Tstringcontainer);
  656. begin
  657. Message1(parser_w_proc_directive_ignored,'REGISTER');
  658. end;
  659. procedure pd_reintroduce(const procnames:Tstringcontainer);
  660. begin
  661. Message1(parser_w_proc_directive_ignored,'REINTRODUCE');
  662. end;
  663. procedure pd_syscall(const procnames:Tstringcontainer);
  664. begin
  665. aktprocsym^.definition^.forwarddef:=false;
  666. aktprocsym^.definition^.extnumber:=get_intconst;
  667. end;
  668. procedure pd_external(const procnames:Tstringcontainer);
  669. {
  670. If import_dll=nil the procedure is assumed to be in another
  671. object file. In that object file it should have the name to
  672. which import_name is pointing to. Otherwise, the procedure is
  673. assumed to be in the DLL to which import_dll is pointing to. In
  674. that case either import_nr<>0 or import_name<>nil is true, so
  675. the procedure is either imported by number or by name. (DM)
  676. }
  677. var
  678. import_dll,
  679. import_name : string;
  680. import_nr : word;
  681. begin
  682. aktprocsym^.definition^.forwarddef:=false;
  683. { If the procedure should be imported from a DLL, a constant string follows.
  684. This isn't really correct, an contant string expression follows
  685. so we check if an semicolon follows, else a string constant have to
  686. follow (FK) }
  687. import_nr:=0;
  688. import_name:='';
  689. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  690. begin
  691. import_dll:=get_stringconst;
  692. if (idtoken=_NAME) then
  693. begin
  694. consume(_NAME);
  695. import_name:=get_stringconst;
  696. end;
  697. if (idtoken=_INDEX) then
  698. begin
  699. {After the word index follows the index number in the DLL.}
  700. consume(_INDEX);
  701. import_nr:=get_intconst;
  702. end;
  703. if (import_nr=0) and (import_name='') then
  704. {if (aktprocsym^.definition^.options and pocdecl)<>0 then
  705. import_name:=aktprocsym^.definition^.mangledname
  706. else
  707. Message(parser_w_empty_import_name);}
  708. { this should work both for win32 and Linux !! PM }
  709. import_name:=realname;
  710. if not(current_module^.uses_imports) then
  711. begin
  712. current_module^.uses_imports:=true;
  713. importlib^.preparelib(current_module^.modulename^);
  714. end;
  715. if not(m_repeat_forward in aktmodeswitches) then
  716. begin
  717. { we can only have one overloaded here ! }
  718. if assigned(aktprocsym^.definition^.nextoverloaded) then
  719. importlib^.importprocedure(aktprocsym^.definition^.nextoverloaded^.mangledname,
  720. import_dll,import_nr,import_name)
  721. else
  722. importlib^.importprocedure(aktprocsym^.mangledname,import_dll,import_nr,import_name);
  723. end
  724. else
  725. importlib^.importprocedure(aktprocsym^.mangledname,import_dll,import_nr,import_name);
  726. end
  727. else
  728. begin
  729. if (idtoken=_NAME) then
  730. begin
  731. consume(_NAME);
  732. import_name:=get_stringconst;
  733. aktprocsym^.definition^.setmangledname(import_name);
  734. end
  735. else
  736. begin
  737. { external shouldn't override the cdecl/system name }
  738. if not (pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
  739. aktprocsym^.definition^.setmangledname(aktprocsym^.name);
  740. end;
  741. end;
  742. end;
  743. {$ifdef TP}
  744. {$F-}
  745. {$endif}
  746. {$ifdef Delphi}
  747. {$define TP}
  748. {$endif Delphi}
  749. {const
  750. namelength=15;}
  751. type
  752. pd_handler=procedure(const procnames:Tstringcontainer);
  753. proc_dir_rec=record
  754. idtok : ttoken;
  755. pd_flags : longint;
  756. handler : pd_handler;
  757. pocall : tproccalloptions;
  758. pooption : tprocoptions;
  759. mutexclpocall : tproccalloptions;
  760. mutexclpotype : tproctypeoptions;
  761. mutexclpo : tprocoptions;
  762. end;
  763. const
  764. {Should contain the number of procedure directives we support.}
  765. num_proc_directives=31;
  766. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  767. (
  768. (
  769. idtok:_ABSTRACT;
  770. pd_flags : pd_interface+pd_object;
  771. handler : {$ifndef TP}@{$endif}pd_abstract;
  772. pocall : [];
  773. pooption : [po_abstractmethod];
  774. mutexclpocall : [pocall_internproc,pocall_inline];
  775. mutexclpotype : [potype_constructor,potype_destructor];
  776. mutexclpo : [po_exports,po_interrupt,po_external]
  777. ),(
  778. idtok:_ALIAS;
  779. pd_flags : pd_implemen+pd_body;
  780. handler : {$ifndef TP}@{$endif}pd_alias;
  781. pocall : [];
  782. pooption : [];
  783. mutexclpocall : [pocall_inline];
  784. mutexclpotype : [];
  785. mutexclpo : [po_external]
  786. ),(
  787. idtok:_ASMNAME;
  788. pd_flags : pd_interface+pd_implemen;
  789. handler : {$ifndef TP}@{$endif}pd_asmname;
  790. pocall : [pocall_cdecl,pocall_clearstack];
  791. pooption : [po_external];
  792. mutexclpocall : [pocall_internproc];
  793. mutexclpotype : [];
  794. mutexclpo : [po_external]
  795. ),(
  796. idtok:_ASSEMBLER;
  797. pd_flags : pd_implemen+pd_body;
  798. handler : nil;
  799. pocall : [];
  800. pooption : [po_assembler];
  801. mutexclpocall : [];
  802. mutexclpotype : [];
  803. mutexclpo : [po_external]
  804. ),(
  805. idtok:_CDECL;
  806. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  807. handler : {$ifndef TP}@{$endif}pd_cdecl;
  808. pocall : [pocall_cdecl,pocall_clearstack];
  809. pooption : [po_savestdregs];
  810. mutexclpocall : [pocall_internproc,pocall_leftright,pocall_inline];
  811. mutexclpotype : [];
  812. mutexclpo : [po_assembler,po_external]
  813. ),(
  814. idtok:_DYNAMIC;
  815. pd_flags : pd_interface+pd_object;
  816. handler : {$ifndef TP}@{$endif}pd_virtual;
  817. pocall : [];
  818. pooption : [po_virtualmethod];
  819. mutexclpocall : [pocall_internproc,pocall_inline];
  820. mutexclpotype : [];
  821. mutexclpo : [po_exports,po_interrupt,po_external]
  822. ),(
  823. idtok:_EXPORT;
  824. pd_flags : pd_body+pd_global+pd_interface+pd_implemen{??};
  825. handler : {$ifndef TP}@{$endif}pd_export;
  826. pocall : [];
  827. pooption : [po_exports];
  828. mutexclpocall : [pocall_internproc,pocall_inline];
  829. mutexclpotype : [];
  830. mutexclpo : [po_external,po_interrupt]
  831. ),(
  832. idtok:_EXTERNAL;
  833. pd_flags : pd_implemen+pd_interface;
  834. handler : {$ifndef TP}@{$endif}pd_external;
  835. pocall : [];
  836. pooption : [po_external];
  837. mutexclpocall : [pocall_internproc,pocall_inline,pocall_palmossyscall];
  838. mutexclpotype : [];
  839. mutexclpo : [po_exports,po_interrupt,po_assembler]
  840. ),(
  841. idtok:_FAR;
  842. pd_flags : pd_implemen+pd_body+pd_interface+pd_procvar;
  843. handler : {$ifndef TP}@{$endif}pd_far;
  844. pocall : [];
  845. pooption : [];
  846. mutexclpocall : [pocall_internproc,pocall_inline];
  847. mutexclpotype : [];
  848. mutexclpo : []
  849. ),(
  850. idtok:_FORWARD;
  851. pd_flags : pd_implemen;
  852. handler : {$ifndef TP}@{$endif}pd_forward;
  853. pocall : [];
  854. pooption : [];
  855. mutexclpocall : [pocall_internproc,pocall_inline];
  856. mutexclpotype : [];
  857. mutexclpo : [po_external]
  858. ),(
  859. idtok:_INLINE;
  860. pd_flags : pd_implemen+pd_body;
  861. handler : {$ifndef TP}@{$endif}pd_inline;
  862. pocall : [pocall_inline];
  863. pooption : [];
  864. mutexclpocall : [pocall_internproc];
  865. mutexclpotype : [potype_constructor,potype_destructor];
  866. mutexclpo : [po_exports,po_external,po_interrupt]
  867. ),(
  868. idtok:_INTERNCONST;
  869. pd_flags : pd_implemen+pd_body;
  870. handler : {$ifndef TP}@{$endif}pd_intern;
  871. pocall : [pocall_internconst];
  872. pooption : [];
  873. mutexclpocall : [];
  874. mutexclpotype : [potype_operator];
  875. mutexclpo : []
  876. ),(
  877. idtok:_INTERNPROC;
  878. pd_flags : pd_implemen;
  879. handler : {$ifndef TP}@{$endif}pd_intern;
  880. pocall : [pocall_internproc];
  881. pooption : [];
  882. mutexclpocall : [pocall_inline,pocall_clearstack,pocall_leftright,pocall_cdecl];
  883. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  884. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck]
  885. ),(
  886. idtok:_INTERRUPT;
  887. pd_flags : pd_implemen+pd_body;
  888. handler : {$ifndef TP}@{$endif}pd_interrupt;
  889. pocall : [];
  890. pooption : [po_interrupt];
  891. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_clearstack,pocall_leftright,pocall_inline];
  892. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  893. mutexclpo : [po_external]
  894. ),(
  895. idtok:_IOCHECK;
  896. pd_flags : pd_implemen+pd_body;
  897. handler : nil;
  898. pocall : [];
  899. pooption : [po_iocheck];
  900. mutexclpocall : [pocall_internproc];
  901. mutexclpotype : [];
  902. mutexclpo : [po_external]
  903. ),(
  904. idtok:_MESSAGE;
  905. pd_flags : pd_interface+pd_object;
  906. handler : {$ifndef TP}@{$endif}pd_message;
  907. pocall : [];
  908. pooption : []; { can be po_msgstr or po_msgint }
  909. mutexclpocall : [pocall_inline,pocall_internproc];
  910. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  911. mutexclpo : [po_interrupt,po_external]
  912. ),(
  913. idtok:_NEAR;
  914. pd_flags : pd_implemen+pd_body+pd_procvar;
  915. handler : {$ifndef TP}@{$endif}pd_near;
  916. pocall : [];
  917. pooption : [];
  918. mutexclpocall : [pocall_internproc];
  919. mutexclpotype : [];
  920. mutexclpo : []
  921. ),(
  922. idtok:_OVERLOAD;
  923. pd_flags : pd_implemen+pd_interface+pd_body;
  924. handler : {$ifndef TP}@{$endif}pd_overload;
  925. pocall : [];
  926. pooption : [po_overload];
  927. mutexclpocall : [pocall_internproc];
  928. mutexclpotype : [];
  929. mutexclpo : []
  930. ),(
  931. idtok:_OVERRIDE;
  932. pd_flags : pd_interface+pd_object;
  933. handler : {$ifndef TP}@{$endif}pd_override;
  934. pocall : [];
  935. pooption : [po_overridingmethod,po_virtualmethod];
  936. mutexclpocall : [pocall_inline,pocall_internproc];
  937. mutexclpotype : [];
  938. mutexclpo : [po_exports,po_external,po_interrupt]
  939. ),(
  940. idtok:_PASCAL;
  941. pd_flags : pd_implemen+pd_body+pd_procvar;
  942. handler : {$ifndef TP}@{$endif}pd_pascal;
  943. pocall : [pocall_leftright];
  944. pooption : [];
  945. mutexclpocall : [pocall_internproc];
  946. mutexclpotype : [];
  947. mutexclpo : [po_external]
  948. ),(
  949. idtok:_POPSTACK;
  950. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  951. handler : nil;
  952. pocall : [pocall_clearstack];
  953. pooption : [];
  954. mutexclpocall : [pocall_inline,pocall_internproc];
  955. mutexclpotype : [];
  956. mutexclpo : [po_assembler,po_external]
  957. ),(
  958. idtok:_PUBLIC;
  959. pd_flags : pd_implemen+pd_body+pd_global+pd_notobject;
  960. handler : nil;
  961. pocall : [];
  962. pooption : [];
  963. mutexclpocall : [pocall_internproc,pocall_inline];
  964. mutexclpotype : [];
  965. mutexclpo : [po_external]
  966. ),(
  967. idtok:_REGISTER;
  968. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  969. handler : {$ifndef TP}@{$endif}pd_register;
  970. pocall : [pocall_register];
  971. pooption : [];
  972. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_internproc];
  973. mutexclpotype : [];
  974. mutexclpo : [po_external]
  975. ),(
  976. idtok:_REINTRODUCE;
  977. pd_flags : pd_interface+pd_object;
  978. handler : {$ifndef TP}@{$endif}pd_reintroduce;
  979. pocall : [];
  980. pooption : [];
  981. mutexclpocall : [];
  982. mutexclpotype : [];
  983. mutexclpo : []
  984. ),(
  985. idtok:_SAFECALL;
  986. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  987. handler : {$ifndef TP}@{$endif}pd_safecall;
  988. pocall : [pocall_safecall];
  989. pooption : [po_savestdregs];
  990. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_internproc,pocall_inline];
  991. mutexclpotype : [];
  992. mutexclpo : [po_external]
  993. ),(
  994. idtok:_SAVEREGISTERS;
  995. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  996. handler : nil;
  997. pocall : [];
  998. pooption : [po_saveregisters];
  999. mutexclpocall : [pocall_internproc];
  1000. mutexclpotype : [];
  1001. mutexclpo : [po_external]
  1002. ),(
  1003. idtok:_STATIC;
  1004. pd_flags : pd_interface+pd_object;
  1005. handler : {$ifndef TP}@{$endif}pd_static;
  1006. pocall : [];
  1007. pooption : [po_staticmethod];
  1008. mutexclpocall : [pocall_inline,pocall_internproc];
  1009. mutexclpotype : [potype_constructor,potype_destructor];
  1010. mutexclpo : [po_external,po_interrupt,po_exports]
  1011. ),(
  1012. idtok:_STDCALL;
  1013. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  1014. handler : {$ifndef TP}@{$endif}pd_stdcall;
  1015. pocall : [pocall_stdcall];
  1016. pooption : [po_savestdregs];
  1017. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_inline,pocall_internproc];
  1018. mutexclpotype : [];
  1019. mutexclpo : [po_external]
  1020. ),(
  1021. idtok:_SYSCALL;
  1022. pd_flags : pd_interface;
  1023. handler : {$ifndef TP}@{$endif}pd_syscall;
  1024. pocall : [pocall_palmossyscall];
  1025. pooption : [];
  1026. mutexclpocall : [pocall_cdecl,pocall_inline,pocall_internproc];
  1027. mutexclpotype : [];
  1028. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  1029. ),(
  1030. idtok:_SYSTEM;
  1031. pd_flags : pd_implemen;
  1032. handler : {$ifndef TP}@{$endif}pd_system;
  1033. pocall : [pocall_clearstack];
  1034. pooption : [];
  1035. mutexclpocall : [pocall_leftright,pocall_inline,pocall_internproc];
  1036. mutexclpotype : [];
  1037. mutexclpo : [po_external,po_assembler,po_interrupt]
  1038. ),(
  1039. idtok:_VIRTUAL;
  1040. pd_flags : pd_interface+pd_object;
  1041. handler : {$ifndef TP}@{$endif}pd_virtual;
  1042. pocall : [];
  1043. pooption : [po_virtualmethod];
  1044. mutexclpocall : [pocall_inline,pocall_internproc];
  1045. mutexclpotype : [];
  1046. mutexclpo : [po_external,po_interrupt,po_exports]
  1047. )
  1048. );
  1049. function is_proc_directive(tok:ttoken):boolean;
  1050. var
  1051. i : longint;
  1052. begin
  1053. is_proc_directive:=false;
  1054. for i:=1 to num_proc_directives do
  1055. if proc_direcdata[i].idtok=idtoken then
  1056. begin
  1057. is_proc_directive:=true;
  1058. exit;
  1059. end;
  1060. end;
  1061. function parse_proc_direc(const proc_names:Tstringcontainer;var pdflags:word):boolean;
  1062. {
  1063. Parse the procedure directive, returns true if a correct directive is found
  1064. }
  1065. var
  1066. p : longint;
  1067. found : boolean;
  1068. name : string;
  1069. begin
  1070. parse_proc_direc:=false;
  1071. name:=pattern;
  1072. found:=false;
  1073. for p:=1 to num_proc_directives do
  1074. if proc_direcdata[p].idtok=idtoken then
  1075. begin
  1076. found:=true;
  1077. break;
  1078. end;
  1079. { Check if the procedure directive is known }
  1080. if not found then
  1081. begin
  1082. { parsing a procvar type the name can be any
  1083. next variable !! }
  1084. if (pdflags and (pd_procvar or pd_object))=0 then
  1085. Message1(parser_w_unknown_proc_directive_ignored,name);
  1086. exit;
  1087. end;
  1088. { static needs a special treatment }
  1089. if (idtoken=_STATIC) and not (cs_static_keyword in aktmoduleswitches) then
  1090. exit;
  1091. { Conflicts between directives ? }
  1092. if (aktprocsym^.definition^.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1093. ((aktprocsym^.definition^.proccalloptions*proc_direcdata[p].mutexclpocall)<>[]) or
  1094. ((aktprocsym^.definition^.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1095. begin
  1096. Message1(parser_e_proc_dir_conflict,name);
  1097. exit;
  1098. end;
  1099. { Check if the directive is only for objects }
  1100. if ((proc_direcdata[p].pd_flags and pd_object)<>0) and
  1101. not assigned(aktprocsym^.definition^._class) then
  1102. begin
  1103. exit;
  1104. end;
  1105. { check if method and directive not for object public }
  1106. if ((proc_direcdata[p].pd_flags and pd_notobject)<>0) and
  1107. assigned(aktprocsym^.definition^._class) then
  1108. begin
  1109. exit;
  1110. end;
  1111. { consume directive, and turn flag on }
  1112. consume(token);
  1113. parse_proc_direc:=true;
  1114. { Check the pd_flags if the directive should be allowed }
  1115. if ((pdflags and pd_interface)<>0) and
  1116. ((proc_direcdata[p].pd_flags and pd_interface)=0) then
  1117. begin
  1118. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1119. exit;
  1120. end;
  1121. if ((pdflags and pd_implemen)<>0) and
  1122. ((proc_direcdata[p].pd_flags and pd_implemen)=0) then
  1123. begin
  1124. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1125. exit;
  1126. end;
  1127. if ((pdflags and pd_procvar)<>0) and
  1128. ((proc_direcdata[p].pd_flags and pd_procvar)=0) then
  1129. begin
  1130. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1131. exit;
  1132. end;
  1133. { Return the new pd_flags }
  1134. if (proc_direcdata[p].pd_flags and pd_body)=0 then
  1135. pdflags:=pdflags and (not pd_body);
  1136. if (proc_direcdata[p].pd_flags and pd_global)<>0 then
  1137. pdflags:=pdflags or pd_global;
  1138. { Add the correct flag }
  1139. aktprocsym^.definition^.proccalloptions:=aktprocsym^.definition^.proccalloptions+proc_direcdata[p].pocall;
  1140. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+proc_direcdata[p].pooption;
  1141. { Adjust positions of args for cdecl or stdcall }
  1142. if (aktprocsym^.definition^.deftype=procdef) and
  1143. (([pocall_cdecl,pocall_stdcall]*aktprocsym^.definition^.proccalloptions)<>[]) then
  1144. aktprocsym^.definition^.parast^.set_alignment(target_os.size_of_longint);
  1145. { Call the handler }
  1146. if pointer({$ifndef FPC}@{$endif}proc_direcdata[p].handler)<>nil then
  1147. proc_direcdata[p].handler(proc_names);
  1148. end;
  1149. {***************************************************************************}
  1150. function check_identical(var p : pprocdef) : boolean;
  1151. {
  1152. Search for idendical definitions,
  1153. if there is a forward, then kill this.
  1154. Returns the result of the forward check.
  1155. Removed from unter_dec to keep the source readable
  1156. }
  1157. var
  1158. hd,pd : Pprocdef;
  1159. storeparast : psymtable;
  1160. ad,fd : psym;
  1161. s : string;
  1162. begin
  1163. check_identical:=false;
  1164. p:=nil;
  1165. pd:=aktprocsym^.definition;
  1166. if assigned(pd) then
  1167. begin
  1168. { Is there an overload/forward ? }
  1169. if assigned(pd^.nextoverloaded) then
  1170. begin
  1171. { walk the procdef list }
  1172. while (assigned(pd)) and (assigned(pd^.nextoverloaded)) do
  1173. begin
  1174. hd:=pd^.nextoverloaded;
  1175. { check for allowing overloading }
  1176. if not(m_fpc in aktmodeswitches) then
  1177. begin
  1178. { if one of the two has overload directive then
  1179. we should issue an other error }
  1180. if (po_overload in pd^.procoptions) or
  1181. (po_overload in hd^.procoptions) then
  1182. begin
  1183. if not((po_overload in pd^.procoptions) and
  1184. (po_overload in hd^.procoptions)) then
  1185. Message1(parser_e_no_overload_for_all_procs,aktprocsym^.name);
  1186. end
  1187. else
  1188. begin
  1189. if not(hd^.forwarddef) then
  1190. Message(parser_e_procedure_overloading_is_off);
  1191. end;
  1192. end;
  1193. { check the parameters }
  1194. if (not(m_repeat_forward in aktmodeswitches) and
  1195. (aktprocsym^.definition^.para^.count=0)) or
  1196. (equal_paras(aktprocsym^.definition^.para,hd^.para,cp_none) and
  1197. { for operators equal_paras is not enough !! }
  1198. ((aktprocsym^.definition^.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  1199. is_equal(pd^.nextoverloaded^.rettype.def,aktprocsym^.definition^.rettype.def))) then
  1200. begin
  1201. if not equal_paras(aktprocsym^.definition^.para,hd^.para,cp_all) then
  1202. begin
  1203. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName);
  1204. exit;
  1205. end;
  1206. if hd^.forwarddef then
  1207. { remove the forward definition but don't delete it, }
  1208. { the symtable is the owner !! }
  1209. begin
  1210. { Check if the procedure type and return type are correct }
  1211. if (hd^.proctypeoption<>aktprocsym^.definition^.proctypeoption) or
  1212. (not(is_equal(hd^.rettype.def,aktprocsym^.definition^.rettype.def)) and
  1213. (m_repeat_forward in aktmodeswitches)) then
  1214. begin
  1215. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName);
  1216. exit;
  1217. end;
  1218. { Check calling convention, no check for internconst,internproc which
  1219. are only defined in interface or implementation }
  1220. if (hd^.proccalloptions-[pocall_internconst,pocall_internproc]<>
  1221. aktprocsym^.definition^.proccalloptions-[pocall_internconst,pocall_internproc]) then
  1222. begin
  1223. { only trigger an error, becuase it doesn't hurt }
  1224. Message(parser_e_call_convention_dont_match_forward);
  1225. { set the mangledname to the interface name so it doesn't trigger
  1226. the Note about different manglednames (PFV) }
  1227. aktprocsym^.definition^.setmangledname(hd^.mangledname);
  1228. end;
  1229. { manglednames are equal? }
  1230. hd^.count:=false;
  1231. if (m_repeat_forward in aktmodeswitches) or
  1232. aktprocsym^.definition^.haspara then
  1233. begin
  1234. if (hd^.mangledname<>aktprocsym^.definition^.mangledname) then
  1235. begin
  1236. { When overloading is not possible then we issue an error }
  1237. { This is not true, tp7/delphi don't give an error when a renamed
  1238. type is used in the other declaration (PFV)
  1239. if not(m_repeat_forward in aktmodeswitches) then
  1240. begin
  1241. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName);
  1242. exit;
  1243. end; }
  1244. if not(po_external in aktprocsym^.definition^.procoptions) then
  1245. Message2(parser_n_interface_name_diff_implementation_name,hd^.mangledname,
  1246. aktprocsym^.definition^.mangledname);
  1247. { reset the mangledname of the interface part to be sure }
  1248. { this is wrong because the mangled name might have been used already !! }
  1249. if hd^.is_used then
  1250. renameasmsymbol(hd^.mangledname,aktprocsym^.definition^.mangledname);
  1251. hd^.setmangledname(aktprocsym^.definition^.mangledname);
  1252. { so we need to keep the name of interface !!
  1253. No!!!! The procedure directives can change the mangledname.
  1254. I fixed this by first calling check_identical and then doing
  1255. the proc directives, but this is not a good solution.(DM)}
  1256. { this is also wrong (PM)
  1257. aktprocsym^.definition^.setmangledname(hd^.mangledname);}
  1258. end
  1259. else
  1260. begin
  1261. { If mangled names are equal, therefore }
  1262. { they have the same number of parameters }
  1263. { Therefore we can check the name of these }
  1264. { parameters... }
  1265. if hd^.forwarddef and aktprocsym^.definition^.forwarddef then
  1266. begin
  1267. Message1(parser_e_function_already_declared_public_forward,aktprocsym^.demangledName);
  1268. Check_identical:=true;
  1269. { Remove other forward from the list to reduce errors }
  1270. pd^.nextoverloaded:=pd^.nextoverloaded^.nextoverloaded;
  1271. exit;
  1272. end;
  1273. ad:=psym(hd^.parast^.symindex^.first);
  1274. fd:=psym(aktprocsym^.definition^.parast^.symindex^.first);
  1275. if assigned(ad) and assigned(fd) then
  1276. begin
  1277. while assigned(ad) and assigned(fd) do
  1278. begin
  1279. s:=ad^.name;
  1280. if s<>fd^.name then
  1281. begin
  1282. Message3(parser_e_header_different_var_names,
  1283. aktprocsym^.name,s,fd^.name);
  1284. break;
  1285. end;
  1286. { it is impossible to have a nil pointer }
  1287. { for only one parameter - since they }
  1288. { have the same number of parameters. }
  1289. { Left = next parameter. }
  1290. ad:=psym(ad^.left);
  1291. fd:=psym(fd^.left);
  1292. end;
  1293. end;
  1294. end;
  1295. end;
  1296. { also the para_offset }
  1297. hd^.parast^.address_fixup:=aktprocsym^.definition^.parast^.address_fixup;
  1298. hd^.count:=true;
  1299. { remove pd^.nextoverloaded from the list }
  1300. { and add aktprocsym^.definition }
  1301. pd^.nextoverloaded:=pd^.nextoverloaded^.nextoverloaded;
  1302. hd^.nextoverloaded:=aktprocsym^.definition^.nextoverloaded;
  1303. { Alert! All fields of aktprocsym^.definition that are modified
  1304. by the procdir handlers must be copied here!.}
  1305. hd^.forwarddef:=false;
  1306. hd^.proccalloptions:=hd^.proccalloptions + aktprocsym^.definition^.proccalloptions;
  1307. hd^.procoptions:=hd^.procoptions + aktprocsym^.definition^.procoptions;
  1308. if aktprocsym^.definition^.extnumber=-1 then
  1309. aktprocsym^.definition^.extnumber:=hd^.extnumber
  1310. else
  1311. if hd^.extnumber=-1 then
  1312. hd^.extnumber:=aktprocsym^.definition^.extnumber;
  1313. { switch parast for warning in implementation PM }
  1314. if (m_repeat_forward in aktmodeswitches) or
  1315. aktprocsym^.definition^.haspara then
  1316. begin
  1317. storeparast:=hd^.parast;
  1318. hd^.parast:=aktprocsym^.definition^.parast;
  1319. aktprocsym^.definition^.parast:=storeparast;
  1320. end;
  1321. if pd=aktprocsym^.definition then
  1322. p:=nil
  1323. else
  1324. p:=pd;
  1325. aktprocsym^.definition:=hd;
  1326. check_identical:=true;
  1327. end
  1328. else
  1329. { abstract methods aren't forward defined, but this }
  1330. { needs another error message }
  1331. if not(po_abstractmethod in pd^.nextoverloaded^.procoptions) then
  1332. Message(parser_e_overloaded_have_same_parameters)
  1333. else
  1334. Message(parser_e_abstract_no_definition);
  1335. break;
  1336. end;
  1337. pd:=pd^.nextoverloaded;
  1338. end;
  1339. end
  1340. else
  1341. begin
  1342. { there is no overloaded, so its always identical with itself }
  1343. check_identical:=true;
  1344. end;
  1345. end;
  1346. { insert opsym only in the right symtable }
  1347. if ((procinfo^.flags and pi_operator)<>0) and assigned(opsym)
  1348. and not parse_only then
  1349. begin
  1350. if ret_in_param(aktprocsym^.definition^.rettype.def) then
  1351. begin
  1352. pprocdef(aktprocsym^.definition)^.parast^.insert(opsym);
  1353. { this increases the data size }
  1354. { correct this to get the right ret $value }
  1355. dec(pprocdef(aktprocsym^.definition)^.parast^.datasize,opsym^.getpushsize);
  1356. { this allows to read the funcretoffset }
  1357. opsym^.address:=-4;
  1358. opsym^.varspez:=vs_var;
  1359. end
  1360. else
  1361. pprocdef(aktprocsym^.definition)^.localst^.insert(opsym);
  1362. end;
  1363. end;
  1364. procedure compile_proc_body(const proc_names:Tstringcontainer;
  1365. make_global,parent_has_class:boolean);
  1366. {
  1367. Compile the body of a procedure
  1368. }
  1369. var
  1370. oldexitlabel,oldexit2label : pasmlabel;
  1371. oldfaillabel,oldquickexitlabel:Pasmlabel;
  1372. _class,hp:Pobjectdef;
  1373. { switches can change inside the procedure }
  1374. entryswitches, exitswitches : tlocalswitches;
  1375. oldaktmaxfpuregisters,localmaxfpuregisters : longint;
  1376. { code for the subroutine as tree }
  1377. {$ifdef newcg}
  1378. code:ptree;
  1379. {$else newcg}
  1380. code:ptree;
  1381. {$endif newcg}
  1382. { size of the local strackframe }
  1383. stackframe:longint;
  1384. { true when no stackframe is required }
  1385. nostackframe:boolean;
  1386. { number of bytes which have to be cleared by RET }
  1387. parasize:longint;
  1388. { filepositions }
  1389. entrypos,
  1390. savepos,
  1391. exitpos : tfileposinfo;
  1392. begin
  1393. { calculate the lexical level }
  1394. inc(lexlevel);
  1395. if lexlevel>32 then
  1396. Message(parser_e_too_much_lexlevel);
  1397. { static is also important for local procedures !! }
  1398. if (po_staticmethod in aktprocsym^.definition^.procoptions) then
  1399. allow_only_static:=true
  1400. else if (lexlevel=normal_function_level) then
  1401. allow_only_static:=false;
  1402. { save old labels }
  1403. oldexitlabel:=aktexitlabel;
  1404. oldexit2label:=aktexit2label;
  1405. oldquickexitlabel:=quickexitlabel;
  1406. oldfaillabel:=faillabel;
  1407. { get new labels }
  1408. getlabel(aktexitlabel);
  1409. getlabel(aktexit2label);
  1410. { exit for fail in constructors }
  1411. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1412. begin
  1413. getlabel(faillabel);
  1414. getlabel(quickexitlabel);
  1415. end;
  1416. { reset break and continue labels }
  1417. block_type:=bt_general;
  1418. aktbreaklabel:=nil;
  1419. aktcontinuelabel:=nil;
  1420. { insert symtables for the class, by only if it is no nested function }
  1421. if assigned(procinfo^._class) and not(parent_has_class) then
  1422. begin
  1423. { insert them in the reverse order ! }
  1424. hp:=nil;
  1425. repeat
  1426. _class:=procinfo^._class;
  1427. while _class^.childof<>hp do
  1428. _class:=_class^.childof;
  1429. hp:=_class;
  1430. _class^.symtable^.next:=symtablestack;
  1431. symtablestack:=_class^.symtable;
  1432. until hp=procinfo^._class;
  1433. end;
  1434. { insert parasymtable in symtablestack}
  1435. { only if lexlevel > 1 !!! global symtable should be right after staticsymtazble
  1436. for checking of same names used in interface and implementation !! }
  1437. if lexlevel>=normal_function_level then
  1438. begin
  1439. aktprocsym^.definition^.parast^.next:=symtablestack;
  1440. symtablestack:=aktprocsym^.definition^.parast;
  1441. symtablestack^.symtablelevel:=lexlevel;
  1442. end;
  1443. { insert localsymtable in symtablestack}
  1444. aktprocsym^.definition^.localst^.next:=symtablestack;
  1445. symtablestack:=aktprocsym^.definition^.localst;
  1446. symtablestack^.symtablelevel:=lexlevel;
  1447. { constant symbols are inserted in this symboltable }
  1448. constsymtable:=symtablestack;
  1449. { reset the temporary memory }
  1450. cleartempgen;
  1451. {$ifdef newcg}
  1452. tg.usedinproc:=[];
  1453. {$else newcg}
  1454. { no registers are used }
  1455. usedinproc:=0;
  1456. {$endif newcg}
  1457. { save entry info }
  1458. entrypos:=aktfilepos;
  1459. entryswitches:=aktlocalswitches;
  1460. localmaxfpuregisters:=aktmaxfpuregisters;
  1461. {$ifdef newcg}
  1462. {$ifdef dummy}
  1463. { parse the code ... }
  1464. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1465. code:=convtree2node(assembler_block)
  1466. else
  1467. code:=convtree2node(block(current_module^.islibrary));
  1468. {$endif dummy}
  1469. { parse the code ... }
  1470. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1471. code:=assembler_block
  1472. else
  1473. code:=block(current_module^.islibrary);
  1474. {$else newcg}
  1475. { parse the code ... }
  1476. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1477. code:=assembler_block
  1478. else
  1479. code:=block(current_module^.islibrary);
  1480. {$endif newcg}
  1481. { get a better entry point }
  1482. if assigned(code) then
  1483. entrypos:=code^.fileinfo;
  1484. { save exit info }
  1485. exitswitches:=aktlocalswitches;
  1486. exitpos:=last_endtoken_filepos;
  1487. { save current filepos }
  1488. savepos:=aktfilepos;
  1489. {When we are called to compile the body of a unit, aktprocsym should
  1490. point to the unit initialization. If the unit has no initialization,
  1491. aktprocsym=nil. But in that case code=nil. hus we should check for
  1492. code=nil, when we use aktprocsym.}
  1493. { set the framepointer to esp for assembler functions }
  1494. { but only if the are no local variables }
  1495. { already done in assembler_block }
  1496. {$ifdef newcg}
  1497. tg.setfirsttemp(procinfo^.firsttemp_offset);
  1498. {$else newcg}
  1499. setfirsttemp(procinfo^.firsttemp_offset);
  1500. {$endif newcg}
  1501. { ... and generate assembler }
  1502. { but set the right switches for entry !! }
  1503. aktlocalswitches:=entryswitches;
  1504. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  1505. aktmaxfpuregisters:=localmaxfpuregisters;
  1506. {$ifndef NOPASS2}
  1507. {$ifdef newcg}
  1508. if assigned(code) then
  1509. generatecode(code);
  1510. {$else newcg}
  1511. if assigned(code) then
  1512. generatecode(code);
  1513. {$endif newcg}
  1514. { set switches to status at end of procedure }
  1515. aktlocalswitches:=exitswitches;
  1516. if assigned(code) then
  1517. begin
  1518. aktprocsym^.definition^.code:=code;
  1519. { the procedure is now defined }
  1520. aktprocsym^.definition^.forwarddef:=false;
  1521. {$ifdef newcg}
  1522. aktprocsym^.definition^.usedregisters:=tg.usedinproc;
  1523. {$else newcg}
  1524. aktprocsym^.definition^.usedregisters:=usedinproc;
  1525. {$endif newcg}
  1526. end;
  1527. {$ifdef newcg}
  1528. stackframe:=tg.gettempsize;
  1529. {$else newcg}
  1530. stackframe:=gettempsize;
  1531. {$endif newcg}
  1532. { first generate entry code with the correct position and switches }
  1533. aktfilepos:=entrypos;
  1534. aktlocalswitches:=entryswitches;
  1535. {$ifdef newcg}
  1536. if assigned(code) then
  1537. cg^.g_entrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  1538. {$else newcg}
  1539. if assigned(code) then
  1540. genentrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  1541. {$endif newcg}
  1542. { now generate exit code with the correct position and switches }
  1543. aktfilepos:=exitpos;
  1544. aktlocalswitches:=exitswitches;
  1545. if assigned(code) then
  1546. begin
  1547. {$ifdef newcg}
  1548. cg^.g_exitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  1549. {$else newcg}
  1550. genexitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  1551. {$endif newcg}
  1552. procinfo^.aktproccode^.insertlist(procinfo^.aktentrycode);
  1553. procinfo^.aktproccode^.concatlist(procinfo^.aktexitcode);
  1554. {$ifdef i386}
  1555. {$ifndef NoOpt}
  1556. if (cs_optimize in aktglobalswitches) and
  1557. { do not optimize pure assembler procedures }
  1558. ((procinfo^.flags and pi_is_assembler)=0) then
  1559. Optimize(procinfo^.aktproccode);
  1560. {$endif NoOpt}
  1561. {$endif}
  1562. { save local data (casetable) also in the same file }
  1563. if assigned(procinfo^.aktlocaldata) and
  1564. (not procinfo^.aktlocaldata^.empty) then
  1565. begin
  1566. procinfo^.aktproccode^.concat(new(pai_section,init(sec_data)));
  1567. procinfo^.aktproccode^.concatlist(procinfo^.aktlocaldata);
  1568. procinfo^.aktproccode^.concat(new(pai_section,init(sec_code)));
  1569. end;
  1570. { now we can insert a cut }
  1571. if (cs_create_smart in aktmoduleswitches) then
  1572. codesegment^.concat(new(pai_cut,init));
  1573. { add the procedure to the codesegment }
  1574. codesegment^.concatlist(procinfo^.aktproccode);
  1575. end;
  1576. {$else}
  1577. if assigned(code) then
  1578. firstpass(code);
  1579. {$endif NOPASS2}
  1580. { ... remove symbol tables, for the browser leave the static table }
  1581. { if (cs_browser in aktmoduleswitches) and (symtablestack^.symtabletype=staticsymtable) then
  1582. symtablestack^.next:=symtablestack^.next^.next
  1583. else }
  1584. if lexlevel>=normal_function_level then
  1585. symtablestack:=symtablestack^.next^.next
  1586. else
  1587. symtablestack:=symtablestack^.next;
  1588. { ... check for unused symbols }
  1589. { but only if there is no asm block }
  1590. if assigned(code) then
  1591. begin
  1592. if (Errorcount=0) then
  1593. begin
  1594. aktprocsym^.definition^.localst^.check_forwards;
  1595. aktprocsym^.definition^.localst^.checklabels;
  1596. end;
  1597. if (procinfo^.flags and pi_uses_asm)=0 then
  1598. begin
  1599. { not for unit init, becuase the var can be used in finalize,
  1600. it will be done in proc_unit }
  1601. if not(aktprocsym^.definition^.proctypeoption
  1602. in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  1603. aktprocsym^.definition^.localst^.allsymbolsused;
  1604. aktprocsym^.definition^.parast^.allsymbolsused;
  1605. end;
  1606. end;
  1607. { the local symtables can be deleted, but the parast }
  1608. { doesn't, (checking definitons when calling a }
  1609. { function }
  1610. { not for a inline procedure !! (PM) }
  1611. { at lexlevel = 1 localst is the staticsymtable itself }
  1612. { so no dispose here !! }
  1613. if assigned(code) and
  1614. not(cs_browser in aktmoduleswitches) and
  1615. not(pocall_inline in aktprocsym^.definition^.proccalloptions) then
  1616. begin
  1617. if lexlevel>=normal_function_level then
  1618. dispose(aktprocsym^.definition^.localst,done);
  1619. aktprocsym^.definition^.localst:=nil;
  1620. end;
  1621. {$ifdef newcg}
  1622. { all registers can be used again }
  1623. tg.resetusableregisters;
  1624. { only now we can remove the temps }
  1625. tg.resettempgen;
  1626. {$else newcg}
  1627. { all registers can be used again }
  1628. resetusableregisters;
  1629. { only now we can remove the temps }
  1630. resettempgen;
  1631. {$endif newcg}
  1632. { remove code tree, if not inline procedure }
  1633. if assigned(code) and not(pocall_inline in aktprocsym^.definition^.proccalloptions) then
  1634. {$ifdef newcg}
  1635. {!!!!!!! dispose(code,done); }
  1636. disposetree(code);
  1637. {$else newcg}
  1638. disposetree(code);
  1639. {$endif newcg}
  1640. { remove class member symbol tables }
  1641. while symtablestack^.symtabletype=objectsymtable do
  1642. symtablestack:=symtablestack^.next;
  1643. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  1644. { restore filepos, the switches are already set }
  1645. aktfilepos:=savepos;
  1646. { restore labels }
  1647. aktexitlabel:=oldexitlabel;
  1648. aktexit2label:=oldexit2label;
  1649. quickexitlabel:=oldquickexitlabel;
  1650. faillabel:=oldfaillabel;
  1651. { reset to normal non static function }
  1652. if (lexlevel=normal_function_level) then
  1653. allow_only_static:=false;
  1654. { previous lexlevel }
  1655. dec(lexlevel);
  1656. end;
  1657. procedure parse_proc_directives(Anames:Pstringcontainer;var pdflags:word);
  1658. {
  1659. Parse the procedure directives. It does not matter if procedure directives
  1660. are written using ;procdir; or ['procdir'] syntax.
  1661. }
  1662. var
  1663. res : boolean;
  1664. begin
  1665. while token in [_ID,_LECKKLAMMER] do
  1666. begin
  1667. if try_to_consume(_LECKKLAMMER) then
  1668. begin
  1669. repeat
  1670. parse_proc_direc(Anames^,pdflags);
  1671. until not try_to_consume(_COMMA);
  1672. consume(_RECKKLAMMER);
  1673. { we always expect at least '[];' }
  1674. res:=true;
  1675. end
  1676. else
  1677. res:=parse_proc_direc(Anames^,pdflags);
  1678. { A procedure directive normally followed by a semicolon, but in
  1679. a const section we should stop when _EQUAL is found }
  1680. if res then
  1681. begin
  1682. if (block_type=bt_const) and
  1683. (token=_EQUAL) then
  1684. break;
  1685. consume(_SEMICOLON);
  1686. end
  1687. else
  1688. break;
  1689. end;
  1690. end;
  1691. procedure parse_var_proc_directives(var sym : psym);
  1692. var
  1693. anames : pstringcontainer;
  1694. pdflags : word;
  1695. oldsym : pprocsym;
  1696. pd : pabstractprocdef;
  1697. begin
  1698. oldsym:=aktprocsym;
  1699. anames:=new(pstringcontainer,init);
  1700. pdflags:=pd_procvar;
  1701. { we create a temporary aktprocsym to read the directives }
  1702. aktprocsym:=new(pprocsym,init(sym^.name));
  1703. case sym^.typ of
  1704. varsym :
  1705. pd:=pabstractprocdef(pvarsym(sym)^.vartype.def);
  1706. typedconstsym :
  1707. pd:=pabstractprocdef(ptypedconstsym(sym)^.typedconsttype.def);
  1708. typesym :
  1709. pd:=pabstractprocdef(ptypesym(sym)^.restype.def);
  1710. else
  1711. internalerror(994932432);
  1712. end;
  1713. if pd^.deftype<>procvardef then
  1714. internalerror(994932433);
  1715. pabstractprocdef(aktprocsym^.definition):=pd;
  1716. { names should never be used anyway }
  1717. inc(lexlevel);
  1718. parse_proc_directives(anames,pdflags);
  1719. dec(lexlevel);
  1720. aktprocsym^.definition:=nil;
  1721. dispose(aktprocsym,done);
  1722. dispose(anames,done);
  1723. aktprocsym:=oldsym;
  1724. end;
  1725. procedure parse_object_proc_directives(var sym : pprocsym);
  1726. var
  1727. anames : pstringcontainer;
  1728. pdflags : word;
  1729. begin
  1730. pdflags:=pd_object;
  1731. anames:=new(pstringcontainer,init);
  1732. inc(lexlevel);
  1733. parse_proc_directives(anames,pdflags);
  1734. dec(lexlevel);
  1735. dispose(anames,done);
  1736. if (po_containsself in aktprocsym^.definition^.procoptions) and
  1737. (([po_msgstr,po_msgint]*aktprocsym^.definition^.procoptions)=[]) then
  1738. Message(parser_e_self_in_non_message_handler);
  1739. end;
  1740. procedure checkvaluepara(p:pnamedindexobject);{$ifndef FPC}far;{$endif}
  1741. var
  1742. vs : pvarsym;
  1743. s : string;
  1744. begin
  1745. with pvarsym(p)^ do
  1746. begin
  1747. if copy(name,1,3)='val' then
  1748. begin
  1749. s:=Copy(name,4,255);
  1750. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  1751. begin
  1752. vs:=new(Pvarsym,initdef(s,vartype.def));
  1753. vs^.fileinfo:=fileinfo;
  1754. vs^.varspez:=varspez;
  1755. aktprocsym^.definition^.localst^.insert(vs);
  1756. {$ifdef INCLUDEOK}
  1757. include(vs^.varoptions,vo_is_local_copy);
  1758. {$else}
  1759. vs^.varoptions:=vs^.varoptions+[vo_is_local_copy];
  1760. {$endif}
  1761. vs^.varstate:=vs_assigned;
  1762. localvarsym:=vs;
  1763. inc(refs); { the para was used to set the local copy ! }
  1764. { warnings only on local copy ! }
  1765. varstate:=vs_used;
  1766. end
  1767. else
  1768. begin
  1769. aktprocsym^.definition^.parast^.rename(name,s);
  1770. end;
  1771. end;
  1772. end;
  1773. end;
  1774. procedure read_proc;
  1775. {
  1776. Parses the procedure directives, then parses the procedure body, then
  1777. generates the code for it
  1778. }
  1779. var
  1780. oldprefix : string;
  1781. oldprocsym : Pprocsym;
  1782. oldprocinfo : pprocinfo;
  1783. oldconstsymtable : Psymtable;
  1784. oldfilepos : tfileposinfo;
  1785. names : Pstringcontainer;
  1786. pdflags : word;
  1787. prevdef,stdef : pprocdef;
  1788. begin
  1789. { save old state }
  1790. oldprocsym:=aktprocsym;
  1791. oldprefix:=procprefix;
  1792. oldconstsymtable:=constsymtable;
  1793. oldprocinfo:=procinfo;
  1794. { create a new procedure }
  1795. new(names,init);
  1796. {$ifdef fixLeaksOnError}
  1797. strContStack.push(names);
  1798. {$endif fixLeaksOnError}
  1799. codegen_newprocedure;
  1800. with procinfo^ do
  1801. begin
  1802. parent:=oldprocinfo;
  1803. { clear flags }
  1804. flags:=0;
  1805. { standard frame pointer }
  1806. framepointer:=frame_pointer;
  1807. { funcret_is_valid:=false; }
  1808. funcret_state:=vs_declared;
  1809. { is this a nested function of a method ? }
  1810. if assigned(oldprocinfo) then
  1811. _class:=oldprocinfo^._class;
  1812. end;
  1813. parse_proc_dec;
  1814. procinfo^.sym:=aktprocsym;
  1815. procinfo^.def:=aktprocsym^.definition;
  1816. { set the default function options }
  1817. if parse_only then
  1818. begin
  1819. aktprocsym^.definition^.forwarddef:=true;
  1820. { set also the interface flag, for better error message when the
  1821. implementation doesn't much this header }
  1822. aktprocsym^.definition^.interfacedef:=true;
  1823. pdflags:=pd_interface;
  1824. end
  1825. else
  1826. begin
  1827. pdflags:=pd_body;
  1828. if current_module^.in_implementation then
  1829. pdflags:=pdflags or pd_implemen;
  1830. if (not current_module^.is_unit) or (cs_create_smart in aktmoduleswitches) then
  1831. pdflags:=pdflags or pd_global;
  1832. procinfo^.exported:=false;
  1833. aktprocsym^.definition^.forwarddef:=false;
  1834. end;
  1835. { parse the directives that may follow }
  1836. inc(lexlevel);
  1837. parse_proc_directives(names,pdflags);
  1838. dec(lexlevel);
  1839. { set aktfilepos to the beginning of the function declaration }
  1840. oldfilepos:=aktfilepos;
  1841. aktfilepos:=aktprocsym^.definition^.fileinfo;
  1842. { search for forward declarations }
  1843. if not check_identical(prevdef) then
  1844. begin
  1845. { A method must be forward defined (in the object declaration) }
  1846. if assigned(procinfo^._class) and (not assigned(oldprocinfo^._class)) then
  1847. Message(parser_e_header_dont_match_any_member);
  1848. { Give a better error if there is a forward def in the interface and only
  1849. a single implementation }
  1850. if (not aktprocsym^.definition^.forwarddef) and
  1851. assigned(aktprocsym^.definition^.nextoverloaded) and
  1852. aktprocsym^.definition^.nextoverloaded^.forwarddef and
  1853. aktprocsym^.definition^.nextoverloaded^.interfacedef and
  1854. not(assigned(aktprocsym^.definition^.nextoverloaded^.nextoverloaded)) then
  1855. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName)
  1856. else
  1857. begin
  1858. { check the global flag }
  1859. if (procinfo^.flags and pi_is_global)<>0 then
  1860. Message(parser_e_overloaded_must_be_all_global);
  1861. end
  1862. end;
  1863. { set return type here, becuase the aktprocsym^.definition can be
  1864. changed by check_identical (PFV) }
  1865. procinfo^.returntype.def:=aktprocsym^.definition^.rettype.def;
  1866. {$ifdef i386}
  1867. if (po_interrupt in aktprocsym^.definition^.procoptions) then
  1868. begin
  1869. { we push Flags and CS as long
  1870. to cope with the IRETD
  1871. and we save 6 register + 4 selectors }
  1872. inc(procinfo^.para_offset,8+6*4+4*2);
  1873. end;
  1874. {$endif i386}
  1875. { pointer to the return value ? }
  1876. if ret_in_param(procinfo^.returntype.def) then
  1877. begin
  1878. procinfo^.return_offset:=procinfo^.para_offset;
  1879. inc(procinfo^.para_offset,target_os.size_of_pointer);
  1880. end;
  1881. { allows to access the parameters of main functions in nested functions }
  1882. aktprocsym^.definition^.parast^.address_fixup:=procinfo^.para_offset;
  1883. { when it is a value para and it needs a local copy then rename
  1884. the parameter and insert a copy in the localst. This is not done
  1885. for assembler procedures }
  1886. if (not parse_only) and (not aktprocsym^.definition^.forwarddef) then
  1887. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}checkvaluepara);
  1888. { restore file pos }
  1889. aktfilepos:=oldfilepos;
  1890. { compile procedure when a body is needed }
  1891. if (pdflags and pd_body)<>0 then
  1892. begin
  1893. Message1(parser_p_procedure_start,aktprocsym^.demangledname);
  1894. names^.insert(aktprocsym^.definition^.mangledname);
  1895. { set _FAIL as keyword if constructor }
  1896. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1897. tokeninfo^[_FAIL].keyword:=m_all;
  1898. if assigned(aktprocsym^.definition^._class) then
  1899. tokeninfo^[_SELF].keyword:=m_all;
  1900. compile_proc_body(names^,((pdflags and pd_global)<>0),assigned(oldprocinfo^._class));
  1901. { reset _FAIL as normal }
  1902. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1903. tokeninfo^[_FAIL].keyword:=m_none;
  1904. if assigned(aktprocsym^.definition^._class) and (lexlevel=main_program_level) then
  1905. tokeninfo^[_SELF].keyword:=m_none;
  1906. consume(_SEMICOLON);
  1907. end;
  1908. { close }
  1909. {$ifdef fixLeaksOnError}
  1910. if names <> strContStack.pop then
  1911. writeln('problem with strContStack in psub!');
  1912. {$endif fixLeaksOnError}
  1913. dispose(names,done);
  1914. codegen_doneprocedure;
  1915. { Restore old state }
  1916. constsymtable:=oldconstsymtable;
  1917. { from now on all refernece to mangledname means
  1918. that the function is already used }
  1919. aktprocsym^.definition^.count:=true;
  1920. { restore the interface order to maintain CRC values PM }
  1921. if assigned(prevdef) and assigned(aktprocsym^.definition^.nextoverloaded) then
  1922. begin
  1923. stdef:=aktprocsym^.definition;
  1924. aktprocsym^.definition:=stdef^.nextoverloaded;
  1925. stdef^.nextoverloaded:=prevdef^.nextoverloaded;
  1926. prevdef^.nextoverloaded:=stdef;
  1927. end;
  1928. aktprocsym:=oldprocsym;
  1929. procprefix:=oldprefix;
  1930. procinfo:=oldprocinfo;
  1931. opsym:=nil;
  1932. end;
  1933. end.
  1934. {
  1935. $Log$
  1936. Revision 1.64 2000-06-20 12:47:52 pierre
  1937. * equal_paras and convertable_paras changed by transforming third parameter
  1938. into an enum with three possible values:
  1939. cp_none, cp_value_equal_const and cp_all.
  1940. Revision 1.63 2000/06/18 18:12:40 peter
  1941. * support overload keyword
  1942. Revision 1.62 2000/06/02 21:24:48 pierre
  1943. * operator overloading now uses isbinaryoperatoracceptable
  1944. and is unaryoperatoracceptable
  1945. Revision 1.61 2000/05/10 19:22:51 pierre
  1946. * Delphi defines TP so that code compiles
  1947. sent by Kovacs Attila Zoltan
  1948. Revision 1.60 2000/05/09 14:19:08 pierre
  1949. * calculate para_offset for interrupt procedures
  1950. Revision 1.59 2000/04/26 08:54:19 pierre
  1951. * More changes for operator bug
  1952. Order_overloaded method removed because it conflicted with
  1953. new implementation where the defs are ordered
  1954. according to the unit loading order !
  1955. Revision 1.58 2000/04/25 23:55:29 pierre
  1956. + Hint about unused unit
  1957. * Testop bug fixed !!
  1958. Now the operators are only applied if the unit is explicitly loaded
  1959. Revision 1.57 2000/04/24 12:48:37 peter
  1960. * removed unused vars
  1961. Revision 1.56 2000/03/31 22:56:47 pierre
  1962. * fix the handling of value parameters in cdecl function
  1963. Revision 1.55 2000/03/27 11:57:22 pierre
  1964. * fix for bug 890
  1965. Revision 1.54 2000/03/23 22:17:51 pierre
  1966. * fix tf000008 bug
  1967. Revision 1.53 2000/03/16 16:41:13 pierre
  1968. * fix for bug 807
  1969. Revision 1.52 2000/03/15 23:10:00 pierre
  1970. * fix for bug 848 (that still genrated wrong code)
  1971. + better testing for variables used in assembler
  1972. (gives an error if variable is not directly reachable !)
  1973. Revision 1.51 2000/02/27 14:44:39 peter
  1974. * if calling convention doesn't match don't print note about
  1975. different manglednames
  1976. Revision 1.50 2000/02/20 20:49:45 florian
  1977. * newcg is compiling
  1978. * fixed the dup id problem reported by Paul Y.
  1979. Revision 1.49 2000/02/17 14:53:42 florian
  1980. * some updates for the newcg
  1981. Revision 1.48 2000/02/09 13:23:00 peter
  1982. * log truncated
  1983. Revision 1.47 2000/02/08 13:55:13 peter
  1984. * reset section back to code after localdata
  1985. Revision 1.46 2000/02/04 20:00:22 florian
  1986. * an exception in a construcor calls now the destructor (this applies only
  1987. to classes)
  1988. Revision 1.45 2000/02/04 14:54:17 jonas
  1989. * moved call to resetusableregs to compile_proc_body (put it right before the
  1990. reset of the temp generator) so the optimizer can know which registers are
  1991. regvars
  1992. Revision 1.44 2000/01/28 23:17:53 florian
  1993. * virtual XXXX; support for objects, only if -dWITHDMT is defined
  1994. Revision 1.43 2000/01/21 22:06:16 florian
  1995. * fixed for the fix of bug 793
  1996. * fpu variables modified by nested subroutines aren't regable anymore
  1997. * $maxfpuregisters doesn't modify anymore the behavior of a procedure before
  1998. Revision 1.42 2000/01/16 22:17:12 peter
  1999. * renamed call_offset to para_offset
  2000. Revision 1.41 2000/01/11 17:16:06 jonas
  2001. * removed a lot of memory leaks when an error is encountered (caused by
  2002. procinfo and pstringcontainers). There are still plenty left though :)
  2003. Revision 1.40 2000/01/07 01:14:31 peter
  2004. * updated copyright to 2000
  2005. Revision 1.39 1999/12/22 01:01:52 peter
  2006. - removed freelabel()
  2007. * added undefined label detection in internal assembler, this prevents
  2008. a lot of ld crashes and wrong .o files
  2009. * .o files aren't written anymore if errors have occured
  2010. * inlining of assembler labels is now correct
  2011. Revision 1.38 1999/12/06 18:17:09 peter
  2012. * newcg compiler compiles again
  2013. Revision 1.37 1999/11/30 10:40:48 peter
  2014. + ttype, tsymlist
  2015. Revision 1.36 1999/11/22 00:23:09 pierre
  2016. * also complain about unused functions in program
  2017. Revision 1.35 1999/11/17 17:05:02 pierre
  2018. * Notes/hints changes
  2019. Revision 1.34 1999/11/10 00:24:02 pierre
  2020. * more browser details
  2021. Revision 1.33 1999/11/09 23:43:08 pierre
  2022. * better browser info
  2023. Revision 1.32 1999/11/09 23:06:45 peter
  2024. * esi_offset -> selfpointer_offset to be newcg compatible
  2025. * hcogegen -> cgbase fixes for newcg
  2026. Revision 1.31 1999/11/06 14:34:23 peter
  2027. * truncated log to 20 revs
  2028. Revision 1.30 1999/10/26 12:30:44 peter
  2029. * const parameter is now checked
  2030. * better and generic check if a node can be used for assigning
  2031. * export fixes
  2032. * procvar equal works now (it never had worked at least from 0.99.8)
  2033. * defcoll changed to linkedlist with pparaitem so it can easily be
  2034. walked both directions
  2035. Revision 1.29 1999/10/22 10:39:35 peter
  2036. * split type reading from pdecl to ptype unit
  2037. * parameter_dec routine is now used for procedure and procvars
  2038. Revision 1.28 1999/10/13 10:37:36 peter
  2039. * moved mangledname creation of normal proc so it also handles a wrong
  2040. method proc
  2041. }