psub.pas 71 KB

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