psub.pas 72 KB

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