psub.pas 71 KB

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