psub.pas 70 KB

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