psub.pas 72 KB

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