psub.pas 69 KB

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