psub.pas 70 KB

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