psub.pas 70 KB

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