File indexing completed on 2024-06-16 08:27:29

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #ifndef LANGUAGE_H
0012 #define LANGUAGE_H
0013 
0014 #include <QObject>
0015 #include <QStringList>
0016 
0017 class FlowCodeDocument;
0018 class KTechlab;
0019 class LogView;
0020 class MessageInfo;
0021 class MicroSettings;
0022 class OutputMethodInfo;
0023 class ProcessChain;
0024 class ProcessOptions;
0025 class TextDocument;
0026 class QProcess;
0027 
0028 typedef QList<ProcessOptions> ProcessOptionsList;
0029 
0030 class ProcessOptionsSpecial
0031 {
0032 public:
0033     ProcessOptionsSpecial();
0034 
0035     bool b_addToProject;
0036     bool b_forceList;
0037     QString m_picID;
0038     FlowCodeDocument *p_flowCodeDocument;
0039 
0040     // Linking
0041     QString m_hexFormat;
0042     bool m_bOutputMapFile;
0043     QString m_libraryDir;
0044     QString m_linkerScript;
0045     QStringList m_linkLibraries;
0046     QString m_linkOther;
0047 
0048     // Programming
0049     QString m_port;
0050     QString m_program;
0051 };
0052 
0053 class ProcessOptionsHelper : public QObject
0054 {
0055     Q_OBJECT
0056 #define protected public
0057 signals:
0058 #undef protected
0059     void textOutputtedTo(TextDocument *outputtedTo);
0060 };
0061 
0062 class ProcessOptions : public ProcessOptionsSpecial
0063 {
0064 public:
0065     ProcessOptions();
0066     ProcessOptions(OutputMethodInfo info);
0067 
0068     class ProcessPath
0069     {
0070     public:
0071         enum MediaType {
0072             AssemblyAbsolute,
0073             AssemblyRelocatable,
0074             C,
0075             Disassembly,
0076             FlowCode,
0077             Library,
0078             Microbe,
0079             Object,
0080             Pic,
0081             Program,
0082 
0083             Unknown // Used for guessing the media type
0084         };
0085 
0086         enum Path                   // From_To              // processor that will be invoked first
0087         { AssemblyAbsolute_PIC,     // gpasm (indirect)
0088           AssemblyAbsolute_Program, // gpasm (direct)
0089 
0090           AssemblyRelocatable_Library, // gpasm (indirect)
0091           AssemblyRelocatable_Object,  // gpasm (direct)
0092           AssemblyRelocatable_PIC,     // gpasm (indirect)
0093           AssemblyRelocatable_Program, // gpasm (indirect)
0094 
0095           C_AssemblyRelocatable, // sdcc (direct)
0096           C_Library,             // sdcc (indirect)
0097           C_Object,              // sdcc (indirect)
0098           C_PIC,                 // sdcc (indirect)
0099           C_Program,             // sdcc (indirect)
0100 
0101           FlowCode_AssemblyAbsolute, // flowcode (indirect)
0102           FlowCode_Microbe,          // flowcode (direct)
0103           FlowCode_PIC,              // flowcode (indirect)
0104           FlowCode_Program,          // flowcode (indirect)
0105 
0106           Microbe_AssemblyAbsolute, // microbe (direct)
0107           Microbe_PIC,              // microbe (indirect)
0108           Microbe_Program,          // microbe (indirect)
0109 
0110           Object_Disassembly, // gpdasm (direct)
0111           Object_Library,     // gplib (direct)
0112           Object_PIC,         // gplink (indirect)
0113           Object_Program,     // gplink (direct)
0114 
0115           PIC_AssemblyAbsolute, // download from pic (direct)
0116 
0117           Program_Disassembly, // gpdasm (direct)
0118           Program_PIC,         // upload to pic (direct)
0119 
0120           Invalid, // From and to types are incompatible
0121           None     // From and to types are the same
0122         };
0123 
0124         static Path path(MediaType from, MediaType to);
0125         static MediaType from(Path path);
0126         static MediaType to(Path path);
0127     };
0128 
0129     class Method
0130     {
0131     public:
0132         enum type {
0133             Forget,    // Don't do anything after processing successfully
0134             LoadAsNew, // Load the output as a new file
0135             Load       // Load the output file
0136         };
0137     };
0138 
0139     /**
0140      * Tries to guess the media type from the url (and possible the contents
0141      * of the file as well).
0142      * @param url a path to a file in the local filesystem
0143      */
0144     static ProcessPath::MediaType guessMediaType(const QString &url);
0145     /**
0146      * The *final* target file (not any intermediatary ones)
0147      */
0148     QString targetFile() const
0149     {
0150         return m_targetFile;
0151     }
0152     /**
0153      * This sets the final target file, as well as the initial intermediatary one
0154      */
0155     void setTargetFile(const QString &file);
0156 
0157     void setIntermediaryOutput(const QString &file)
0158     {
0159         m_intermediaryFile = file;
0160     }
0161     QString intermediaryOutput() const
0162     {
0163         return m_intermediaryFile;
0164     }
0165 
0166     /**
0167      * As paths in local filesystem
0168      */
0169     void setInputFiles(const QStringList &files)
0170     {
0171         m_inputFiles = files;
0172     }
0173     QStringList inputFiles() const
0174     {
0175         return m_inputFiles;
0176     }
0177 
0178     void setMethod(Method::type method)
0179     {
0180         m_method = method;
0181     }
0182     Method::type method() const
0183     {
0184         return m_method;
0185     }
0186 
0187     void setProcessPath(ProcessPath::Path path)
0188     {
0189         m_processPath = path;
0190     }
0191     ProcessPath::Path processPath() const
0192     {
0193         return m_processPath;
0194     }
0195 
0196     /**
0197      * If the output is text; If the user has selected (in config options)
0198      * ReuseSameViewForOutput, then the given TextDocument will have its
0199      * text set to the output if the TextDocument is not modified and has
0200      * an empty url. Otherwise a new TextDocument will be created. Either
0201      * way, once the processing has finished, a signal will be emitted
0202      * to the given receiver passing a TextDocument * as an argument. This
0203      * is not to be confused with setTextOutputtedTo, which is called once
0204      * the processing has finished, and will call-back to the slot given.
0205      */
0206     void setTextOutputTarget(TextDocument *target, QObject *receiver, const char *slot);
0207     /**
0208      * @see setTextOutputTarget
0209      */
0210     TextDocument *textOutputTarget() const
0211     {
0212         return m_pTextOutputTarget;
0213     }
0214     /**
0215      * @see setTextOuputTarget
0216      */
0217     void setTextOutputtedTo(TextDocument *outputtedTo);
0218 
0219 protected:
0220     TextDocument *m_pTextOutputTarget;
0221     ProcessOptionsHelper *m_pHelper;
0222     bool b_targetFileSet;
0223     QStringList m_inputFiles;
0224     QString m_targetFile;
0225     QString m_intermediaryFile;
0226     Method::type m_method;
0227     ProcessPath::Path m_processPath;
0228 };
0229 
0230 /**
0231 @author Daniel Clarke
0232 @author David Saxton
0233 */
0234 class Language : public QObject
0235 {
0236     Q_OBJECT
0237 public:
0238     Language(ProcessChain *processChain, const QString &name);
0239     ~Language() override;
0240 
0241     /**
0242      * Compile / assemble / dissassembly / whatever the given input.
0243      */
0244     virtual void processInput(ProcessOptions options) = 0;
0245     /**
0246      * Return the ProcessOptions object current state
0247      */
0248     ProcessOptions processOptions() const
0249     {
0250         return m_processOptions;
0251     }
0252     /**
0253      * Return the output path from the given input path. Will return None
0254      * if we've done processing.
0255      */
0256     virtual ProcessOptions::ProcessPath::Path outputPath(ProcessOptions::ProcessPath::Path inputPath) const = 0;
0257 
0258 signals:
0259     /**
0260      * Emitted when the processing was successful.
0261      * @param language Pointer to this class
0262      */
0263     void processSucceeded(Language *language);
0264     /**
0265      * Emitted when the processing failed.
0266      * @param language Pointer to this class
0267      */
0268     void processFailed(Language *language);
0269 
0270 protected:
0271     /**
0272      * Examines the string for the line number if applicable, and creates a new
0273      * MessageInfo for it.
0274      */
0275     virtual MessageInfo extractMessageInfo(const QString &text);
0276 
0277     /**
0278      * Reset the error count
0279      */
0280     void reset();
0281     void outputMessage(const QString &message);
0282     void outputWarning(const QString &message);
0283     void outputError(const QString &error);
0284     void finish(bool successful);
0285 
0286     int m_errorCount;
0287     ProcessOptions m_processOptions;
0288     ProcessChain *p_processChain;
0289 
0290     /**
0291      * A message appropriate to the language's success after compilation or similar.
0292      */
0293     QString m_successfulMessage;
0294     /**
0295      * A message appropriate to the language's failure after compilation or similar.
0296      */
0297     QString m_failedMessage;
0298 };
0299 
0300 #endif